diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -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/ diff --git a/.idea/maps.iml b/.idea/maps.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/maps.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..019aa83 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..12bb06d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..4aabca4 --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d4e7b38..0ab1ba2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,35 @@ # maps -golang maps -可对map根据key进行排序 \ No newline at end of file +###maps +可根据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 +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1a1333c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/cowardmrx/maps + +go 1.17 diff --git a/maps.go b/maps.go new file mode 100644 index 0000000..169f0a3 --- /dev/null +++ b/maps.go @@ -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) +} diff --git a/maps_test.go b/maps_test.go new file mode 100644 index 0000000..1e07534 --- /dev/null +++ b/maps_test.go @@ -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) +}