1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package maputils
- import "testing"
- func TestGetValueInMap(t *testing.T) {
- testMap := map[string]any{
- "foo1": map[string]any{
- "foo1_sub": []any{
- "value1", 2,
- },
- },
- }
- value, err := GetValueByPath(testMap, "foo1.foo1_sub.[0]")
- if err != nil {
- t.Fatal(err)
- }
- if value.(string) != "value1" {
- t.Fatal("值不一致")
- }
- value, err = GetValueByPath(testMap, "foo1.foo1_sub.[1]")
- if err != nil {
- t.Fatal(err)
- }
- if value.(int) != 2 {
- t.Fatal("值不一致")
- }
- }
- func TestGetValue(t *testing.T) {
- testMap := map[string]any{
- "foo1": map[string]any{
- "foo1_sub": []any{
- "value1", 2,
- },
- },
- }
- foo1Map, ok := GetValueByKey[string, map[string]any](testMap, "foo1")
- if !ok {
- t.Fatal("取foo1值失败")
- }
- foo1Sub, ok := GetValueByKey[string, []any](foo1Map, "foo1_sub")
- if !ok {
- t.Fatal("取foo1Sub值失败")
- }
- if foo1Sub[0].(string) != "value1" {
- t.Fatal("值不一致")
- }
- if foo1Sub[1].(int) != 2 {
- t.Fatal("值不一致")
- }
- }
|