Skip to content

Commit

Permalink
refactor(maps): rename Maps to M (#122)
Browse files Browse the repository at this point in the history
* refactor(maps): rename Maps to M

Signed-off-by: Flc゛ <[email protected]>

* refactor(maps): rename Maps to M

Signed-off-by: Flc゛ <[email protected]>

---------

Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Mar 1, 2024
1 parent 69df7cb commit ea8de18
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func main() {
m := maps.Maps{}
m := maps.M{}

m.Merge(map[string]interface{}{
"name": "Flc",
Expand Down
38 changes: 19 additions & 19 deletions maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,103 @@ 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
}

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)
}
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)
}
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
}
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)
}
Expand Down
16 changes: 8 additions & 8 deletions maps/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit ea8de18

Please sign in to comment.