From ea8de1879bcf3d4f88b1cc9b5e7c75495133a276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Fri, 1 Mar 2024 09:15:07 +0800 Subject: [PATCH] refactor(maps): rename Maps to M (#122) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(maps): rename Maps to M Signed-off-by: Flc゛ * refactor(maps): rename Maps to M Signed-off-by: Flc゛ --------- Signed-off-by: Flc゛ --- maps/README.md | 2 +- maps/maps.go | 38 +++++++++++++++++++------------------- maps/maps_test.go | 16 ++++++++-------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/maps/README.md b/maps/README.md index 766f8ae4..d22b183d 100644 --- a/maps/README.md +++ b/maps/README.md @@ -12,7 +12,7 @@ import ( ) func main() { - m := maps.Maps{} + m := maps.M{} m.Merge(map[string]interface{}{ "name": "Flc", diff --git a/maps/maps.go b/maps/maps.go index 67f5260f..1f20636e 100644 --- a/maps/maps.go +++ b/maps/maps.go @@ -4,42 +4,42 @@ import ( "fmt" ) -type Maps map[string]interface{} +type M map[string]interface{} -func (m Maps) Maps() map[string]interface{} { +func (m M) Maps() map[string]interface{} { return m } -func (m Maps) All() map[string]interface{} { +func (m M) All() map[string]interface{} { return m.Maps() } -func (m Maps) Merge(n Maps) Maps { +func (m M) Merge(n M) M { for k, v := range n { m[k] = v } return m } -func (m Maps) Clone() Maps { - n := make(Maps, len(m)) +func (m M) Clone() M { + n := make(M, len(m)) for k, v := range m { n[k] = v } return n } -func (m Maps) Has(k string) bool { +func (m M) Has(k string) bool { _, ok := m[k] return ok } -func (m Maps) Get(k string) (interface{}, bool) { +func (m M) Get(k string) (interface{}, bool) { v, ok := m[k] return v, ok } -func (m Maps) GetX(k string) interface{} { +func (m M) GetX(k string) interface{} { if v, ok := m.Get(k); ok { return v } @@ -47,17 +47,17 @@ func (m Maps) GetX(k string) interface{} { panic(fmt.Sprintf("maps: key %s not exists", k)) } -func (m Maps) Set(k string, v interface{}) Maps { +func (m M) Set(k string, v interface{}) M { m[k] = v return m } -func (m Maps) Delete(k string) Maps { +func (m M) Delete(k string) M { delete(m, k) return m } -func (m Maps) Keys() []string { +func (m M) Keys() []string { keys := make([]string, 0, len(m)) for k := range m { keys = append(keys, k) @@ -65,7 +65,7 @@ func (m Maps) Keys() []string { return keys } -func (m Maps) Values() []interface{} { +func (m M) Values() []interface{} { values := make([]interface{}, 0, len(m)) for _, v := range m { values = append(values, v) @@ -73,26 +73,26 @@ func (m Maps) Values() []interface{} { return values } -func (m Maps) Len() int { +func (m M) Len() int { return len(m) } -func (m Maps) When(guard bool, fn func(maps Maps) Maps) Maps { +func (m M) When(guard bool, fn func(maps M) M) M { if guard { return fn(m) } return m } -func (m Maps) Unless(guard bool, fn func(maps Maps) Maps) Maps { +func (m M) Unless(guard bool, fn func(maps M) M) M { if !guard { return fn(m) } return m } -func (m Maps) Map(fn func(k string, v interface{}) (string, interface{})) Maps { - n := make(Maps, len(m)) +func (m M) Map(fn func(k string, v interface{}) (string, interface{})) M { + n := make(M, len(m)) for k, v := range m { k, v := fn(k, v) n[k] = v @@ -100,7 +100,7 @@ func (m Maps) Map(fn func(k string, v interface{}) (string, interface{})) Maps { return n } -func (m Maps) Each(fn func(k string, v interface{})) { +func (m M) Each(fn func(k string, v interface{})) { for k, v := range m { fn(k, v) } diff --git a/maps/maps_test.go b/maps/maps_test.go index a1a61c83..f551f4f4 100644 --- a/maps/maps_test.go +++ b/maps/maps_test.go @@ -6,10 +6,10 @@ import ( "github.com/stretchr/testify/assert" ) -func TestMaps(t *testing.T) { - maps := Maps{}.Set("name", "Flc").When(true, func(maps Maps) Maps { +func TestM(t *testing.T) { + maps := M{}.Set("name", "Flc").When(true, func(maps M) M { return maps.Set("age", 18) - }).When(false, func(maps Maps) Maps { + }).When(false, func(maps M) M { return maps.Set("age", 20) }).Map(func(key string, value interface{}) (string, interface{}) { if key == "age" { //nolint:goconst @@ -65,19 +65,19 @@ func TestMaps(t *testing.T) { maps.Delete("first_name") assert.Nil(t, maps["first_name"]) - maps.Merge(Maps{"year": "123"}) + maps.Merge(M{"year": "123"}) assert.Equal(t, "123", maps["year"]) - maps.Unless(true, func(maps Maps) Maps { + maps.Unless(true, func(maps M) M { return maps.Set("sex", "woman") - }).Unless(false, func(maps Maps) Maps { + }).Unless(false, func(maps M) M { return maps.Set("sex", "man") }) assert.Equal(t, "man", maps["sex"]) } -func TestMaps_Get(t *testing.T) { - m := Maps{ +func TestM_Get(t *testing.T) { + m := M{ "name": "Flc", } name, ok := m.Get("name")