diff --git a/helper/maps/function.go b/helper/maps/function.go index 8c78f3d..3589550 100644 --- a/helper/maps/function.go +++ b/helper/maps/function.go @@ -55,3 +55,54 @@ func GetStrMapAnyValWithAny(key string, v map[string]any) (r any, o bool) { } return } + +func GetAnyAnyMapVal[T any](m map[any]any, k ...any) (r T, o bool) { + if len(k) > 1 { + val, ok := m[k[0]] + if ok { + vx, ok := val.(map[any]any) + if ok { + r, o = GetAnyAnyMapVal[T](vx, k[1:]...) + } + } + } else { + x, ok := m[k[0]] + if ok { + vv, ok := x.(T) + if ok { + o = true + r = vv + } + } + } + return +} + +func GetAnyAnyMapWithAny(v map[any]any, k ...any) (r any, o bool) { + if len(k) > 1 { + val, ok := v[k[0]] + if ok { + vx, ok := val.(map[any]any) + if ok { + r, o = GetAnyAnyMapWithAny(vx, k[1:]...) + } + } + } else { + x, ok := v[k[0]] + if ok { + o = true + r = x + } + } + return +} + +func GetAnyAnyValWithDefaults[T any](m map[any]any, defaults T, key ...any) (r T) { + r = defaults + v, ok := GetAnyAnyMapVal[T](m, key...) + if !ok { + return + } + r = v + return +} diff --git a/helper/maps/function_test.go b/helper/maps/function_test.go index 1045272..ea085b0 100644 --- a/helper/maps/function_test.go +++ b/helper/maps/function_test.go @@ -123,3 +123,96 @@ func TestGetStrMapAnyValWithAny(t *testing.T) { }) } } + +func TestGetAnyAnyMapVal(t *testing.T) { + m := map[any]any{ + 1: "1", + "1": 2, + "xxx": map[any]any{ + "sss": []int{1, 2, 3}, + 5: "5", + }, + } + + t.Run("t1", func(t *testing.T) { + wantR := []int{1, 2, 3} + gotR, gotO := GetAnyAnyMapVal[[]int](m, "xxx", "sss") + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + if gotO != true { + t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true) + } + }) + + t.Run("t2", func(t *testing.T) { + wantR := "5" + gotR, gotO := GetAnyAnyMapVal[string](m, "xxx", 5) + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + if gotO != true { + t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true) + } + }) +} + +func TestGetAnyAnyMapWithAny(t *testing.T) { + m := map[any]any{ + 1: "1", + "1": 2, + "xxx": map[any]any{ + "sss": []int{1, 2, 3}, + 5: "5", + }, + } + + t.Run("t1", func(t *testing.T) { + wantR := any([]int{1, 2, 3}) + gotR, gotO := GetAnyAnyMapWithAny(m, "xxx", "sss") + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + if gotO != true { + t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true) + } + }) + + t.Run("t2", func(t *testing.T) { + wantR := any("5") + gotR, gotO := GetAnyAnyMapWithAny(m, "xxx", 5) + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + if gotO != true { + t.Errorf("GetAnyAnyMapVal() gotO = %v, want %v", gotO, true) + } + }) +} + +func TestGetAnyAnyValWithDefaults(t *testing.T) { + m := map[any]any{ + 1: "1", + "1": 2, + "xxx": map[any]any{ + "sss": []int{1, 2, 3}, + 5: "5", + }, + } + + t.Run("t1", func(t *testing.T) { + wantR := []int{1, 2, 3} + gotR := GetAnyAnyValWithDefaults[[]int](m, nil, "xxx", "sss") + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + }) + + t.Run("t2", func(t *testing.T) { + wantR := "xxx" + gotR := GetAnyAnyValWithDefaults[string](m, "xxx", "xxx", 55) + if !reflect.DeepEqual(gotR, wantR) { + t.Errorf("GetAnyAnyMapVal() gotR = %v, want %v", gotR, wantR) + } + }) +} diff --git a/internal/phphelper/unserialize.go b/internal/phphelper/unserialize.go index 0fd0036..3ac5dfc 100644 --- a/internal/phphelper/unserialize.go +++ b/internal/phphelper/unserialize.go @@ -16,7 +16,7 @@ func UnPHPSerializeToStruct[T any](s string) (r T, err error) { return } -func UnPHPSerializeToAnyMap(s string) (map[string]any, error) { +func UnPHPSerializeToStrAnyMap(s string) (map[string]any, error) { m := map[string]any{} var r map[any]any err := phpserialize.Unmarshal([]byte(s), &r) @@ -27,3 +27,11 @@ func UnPHPSerializeToAnyMap(s string) (map[string]any, error) { m = maps.AnyAnyToStrAny(r) return m, err } +func UnPHPSerializeToAnyAnyMap(s string) (map[any]any, error) { + var r map[any]any + err := phpserialize.Unmarshal([]byte(s), &r) + if err != nil { + return nil, err + } + return r, err +} diff --git a/internal/theme/twentyfifteen/layout/sidebar.gohtml b/internal/theme/twentyfifteen/layout/sidebar.gohtml index e614563..dba5de8 100644 --- a/internal/theme/twentyfifteen/layout/sidebar.gohtml +++ b/internal/theme/twentyfifteen/layout/sidebar.gohtml @@ -22,7 +22,8 @@ -