From 240a41f1eb80b452c80ebf3c0493599f3164b820 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 6 Mar 2023 20:53:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=90=8E=E5=8F=B0=E8=A7=84?= =?UTF-8?q?=E6=A1=A3=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/maps/function.go | 51 ++++++++++ helper/maps/function_test.go | 93 +++++++++++++++++++ internal/phphelper/unserialize.go | 10 +- .../theme/twentyfifteen/layout/sidebar.gohtml | 18 +--- .../twentyseventeen/layout/sidebar.gohtml | 13 +-- internal/theme/wp/index.go | 4 +- internal/theme/wp/template.gohtml | 39 ++++++++ internal/theme/wp/widgetareadata.go | 5 + internal/wpconfig/options.go | 21 +++++ internal/wpconfig/thememods.go | 2 +- 10 files changed, 228 insertions(+), 28 deletions(-) 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 @@ -