🎉 maps

This commit is contained in:
coward 2021-12-03 15:26:41 +08:00
parent 413948f322
commit 1c7a9de7c7
10 changed files with 314 additions and 2 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

9
.idea/maps.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

4
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GOROOT" url="file://E:/golang/golang" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/maps.iml" filepath="$PROJECT_DIR$/.idea/maps.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

8
.idea/watcherTasks.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<enabled-global>
<option value="go fmt" />
</enabled-global>
</component>
</project>

View File

@ -1,4 +1,35 @@
# maps # maps
golang maps ###maps
可对map根据key进行排序 可根据map的key按照有序排序的map包
###使用:
- go get -u github.com/cowardmrx/maps
```go
// Set
maps := NewMaps()
maps.Set("key1", "123123")
// Exist
maps.Exist("key1")
// Get
maps.Get("key")
// Delete
maps.Delete("key1","key2")
// GetMapsKeys return a []string
maps.Keys()
// GetMapsValues return a []interface
maps.Values()
// GetMapsOriginMap get this maps origin data return a map[string]interface
maps.OriginMap()
// SortKey sort this maps by keys
maps.SortKey()
// other example ./maps_test.go
```

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/cowardmrx/maps
go 1.17

171
maps.go Normal file
View File

@ -0,0 +1,171 @@
package maps
import (
"encoding/json"
"sync"
)
const (
// SortAsc sort by asc
SortAsc = "asc"
// SortDesc sort by desc
SortDesc = "desc"
)
type maps struct {
rw sync.RWMutex
Key []string
Value []interface{}
Map map[string]interface{}
}
func NewMaps() Maps {
return &maps{
Key: []string{},
Value: []interface{}{},
Map: map[string]interface{}{},
}
}
type Maps interface {
// Set 设置键值对
Set(key string, value interface{}) Maps
// Exist 指定的key是否存在
Exist(Key string) bool
// Get 获取指定Key的值
Get(key string) interface{}
// Delete 删除指定的某一些key 返回受影响的数目
Delete(keys ...string) int
// Keys 获取这个map的全部key
Keys() []string
// Values 获取这个map的全部值
Values() []interface{}
// OriginMap 获取这个map的原始map
OriginMap() map[string]interface{}
// SortKey 根据Key排序
SortKey(sortFunc func(keys []string))
// String 数据转为String
String() string
}
// @method Set
// @description: set
// @receiver m
// @param key string
// @param value interface{}
// @return Maps
func (m *maps) Set(key string, value interface{}) Maps {
defer m.rw.Unlock()
m.rw.Lock()
m.Key = append(m.Key, key)
m.Value = append(m.Value, value)
m.Map[key] = value
return m
}
// @method Exist
// @description: 判断某个key是否存在
// @receiver m
// @param Key string
// @return bool
func (m *maps) Exist(Key string) bool {
defer m.rw.RUnlock()
m.rw.RLock()
for _, v := range m.Key {
if Key == v {
return true
}
}
return false
}
// @method Get
// @description: 获取指定key的数据
// @receiver m
// @param key string
// @return interface{}
func (m *maps) Get(key string) interface{} {
defer m.rw.RUnlock()
m.rw.RLock()
if !m.Exist(key) {
return nil
}
return m.Map[key]
}
// @method Delete
// @description: 删除指定key
// @receiver m
// @param keys ...string
// @return int 删除成功的数量
func (m *maps) Delete(keys ...string) int {
defer m.rw.Unlock()
m.rw.Lock()
if len(keys) <= 0 {
return 0
}
mCount := len(m.Value)
for _, v := range keys {
delete(m.Map, v)
}
return mCount - len(m.Value)
}
// @method Keys
// @description: 获取全部的key
// @receiver m
// @return []string
func (m *maps) Keys() []string {
return m.Key
}
// @method Values
// @description: 获取全部的value
// @receiver m
// @return []interface{}
func (m *maps) Values() []interface{} {
return m.Value
}
// @method OriginMap
// @description: 获取原始map
// @receiver m
// @return map[string]interface{}
func (m *maps) OriginMap() map[string]interface{} {
return m.Map
}
// @method SortKey
// @description: 根据Key排序
// @receiver m
// @param sortFunc func(keys []string)
func (m *maps) SortKey(sortFunc func(keys []string)) {
sortFunc(m.Key)
}
// @method String
// @description: 数据string
// @receiver m
// @return string
func (m *maps) String() string {
result, err := json.Marshal(m)
if err != nil {
return ""
}
return string(result)
}

64
maps_test.go Normal file
View File

@ -0,0 +1,64 @@
package maps
import (
"fmt"
"sort"
"testing"
)
func TestSetMaps(t *testing.T) {
maps := NewMaps()
a := maps.Set("key1", "123123")
fmt.Println(a)
}
func TestExistMaps(t *testing.T) {
maps := NewMaps()
a := maps.Set("key2", "this is value 2")
fmt.Println(a.Exist("key3"))
}
func TestGetMaps(t *testing.T) {
maps := NewMaps()
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
fmt.Println(a.Get("key4"))
}
func TestDeleteMaps(t *testing.T) {
maps := NewMaps()
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
fmt.Println(a.Delete("key1", "key2", "key3", "key4"))
}
func TestKeysValues(t *testing.T) {
maps := NewMaps()
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
fmt.Println(a.Keys())
fmt.Println(a.Values())
}
func TestOriginMap(t *testing.T) {
maps := NewMaps()
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
fmt.Println(a.OriginMap())
}
func TestSortKey(t *testing.T) {
maps := NewMaps()
a := maps.Set("key3", "value1").Set("key4", "value 2").Set("key1", "value 3")
a.SortKey(sort.Strings)
fmt.Println(a)
}