Skip to content

Commit

Permalink
feat(xmap): add SliceToMap in xmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Aoi-hosizora committed Nov 5, 2020
1 parent 3397709 commit 9a81510
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions Hierarchy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ $1 -- the common library, base on {xtesting, xreflect, xcolor}
xcondition (xtesting)
xdi (xtesting, xreflect, xcolor)
xlinkedhashmap (xtesting, xreflect)
xmap (xtesting)
xnumber (xtesting)
xpointer (xtesting)
xregexp (xtesting)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
+ **xtesting**
+ xregexp
+ xstatus
+ xmap

### Dependency

Expand Down
10 changes: 10 additions & 0 deletions xmap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# xmap

### References

+ xtesting*

### Functions

+ `SliceToInterfaceMap(args []interface{}) map[interface{}]interface{}`
+ `SliceToStringMap(args []interface{}) map[string]interface{}`
51 changes: 51 additions & 0 deletions xmap/xmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xmap

import (
"fmt"
)

// SliceToInterfaceMap returns a map[interface{}]interface{} from an interface slice.
func SliceToInterfaceMap(args []interface{}) map[interface{}]interface{} {
out := make(map[interface{}]interface{})
l := len(args)
for i := 0; i < l; i += 2 {
keyIdx := i
valueIdx := i + 1
if i+1 >= l {
break
}
key := args[keyIdx]
value := args[valueIdx]
out[key] = value
}

return out
}

// SliceToInterfaceMap returns a map[string]interface{} from an interface slice.
func SliceToStringMap(args []interface{}) map[string]interface{} {
out := make(map[string]interface{})
l := len(args)
for i := 0; i < l; i += 2 {
keyIdx := i
valueIdx := i + 1
if i+1 >= l {
break
}

keyItf := args[keyIdx]
value := args[valueIdx]
key := ""
if keyItf == nil {
continue
}
if k, ok := keyItf.(string); ok {
key = k
} else {
key = fmt.Sprintf("%v", keyItf)
}
out[key] = value
}

return out
}
20 changes: 20 additions & 0 deletions xmap/xmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xmap

import (
"github.com/Aoi-hosizora/ahlib/xtesting"
"testing"
)

func TestSliceToMap(t *testing.T) {
xtesting.Equal(t, SliceToInterfaceMap(nil), map[interface{}]interface{}{})
xtesting.Equal(t, SliceToInterfaceMap([]interface{}{false, "b", "c"}), map[interface{}]interface{}{false: "b"})
xtesting.Equal(t, SliceToInterfaceMap([]interface{}{"a", "b", "c", "d"}), map[interface{}]interface{}{"a": "b", "c": "d"})
xtesting.Equal(t, SliceToInterfaceMap([]interface{}{1, 2}), map[interface{}]interface{}{1: 2})
xtesting.Equal(t, SliceToInterfaceMap([]interface{}{nil, " ", nil, " "}), map[interface{}]interface{}{nil: " "})

xtesting.Equal(t, SliceToStringMap(nil), map[string]interface{}{})
xtesting.Equal(t, SliceToStringMap([]interface{}{"a", "b", "c"}), map[string]interface{}{"a": "b"})
xtesting.Equal(t, SliceToStringMap([]interface{}{"a", "b", "c", "d"}), map[string]interface{}{"a": "b", "c": "d"})
xtesting.Equal(t, SliceToStringMap([]interface{}{1, 2}), map[string]interface{}{"1": 2})
xtesting.Equal(t, SliceToStringMap([]interface{}{nil, " ", " ", " "}), map[string]interface{}{" ": " "})
}

0 comments on commit 9a81510

Please sign in to comment.