-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3397709
commit 9a81510
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ | |
+ **xtesting** | ||
+ xregexp | ||
+ xstatus | ||
+ xmap | ||
|
||
### Dependency | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}{" ": " "}) | ||
} |