diff --git a/actions/common/postmeta.go b/actions/common/postmeta.go index 17cce39..7ae4011 100644 --- a/actions/common/postmeta.go +++ b/actions/common/postmeta.go @@ -8,7 +8,6 @@ import ( "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models/wp" "strconv" - "strings" "time" ) @@ -21,29 +20,6 @@ func GetPostMetaByPostId(ctx context.Context, id uint64) (r map[string]any, err return } -func getMetaVal(key string, v any) (r any) { - vv, ok := v.(map[string]any) - if !ok { - return - } - k := strings.Split(key, ".") - if len(k) > 1 { - val, ok := vv[k[0]] - if ok { - vx, ok := val.(map[string]any) - if ok { - r = getMetaVal(strings.Join(k[1:], "."), vx) - } - } - } else { - x, ok := vv[k[0]] - if ok { - r = x - } - } - return -} - func ToPostThumbnail(c context.Context, postId uint64) (r wp.PostThumbnail) { meta, err := GetPostMetaByPostId(c, postId) if err == nil { @@ -60,23 +36,20 @@ func ToPostThumbnail(c context.Context, postId uint64) (r wp.PostThumbnail) { r.Path = ff } } - tt := getMetaVal("_wp_attachment_metadata.sizes.post-thumbnail", mm) - if tt != nil { - sss, ok := tt.(map[string]any) + tt, ok := helper.GetStrMapAnyVal[map[string]any]("_wp_attachment_metadata.sizes.post-thumbnail", mm) + if ok && tt != nil { + width, ok := tt["width"] if ok { - width, ok := sss["width"] + w, ok := width.(int) if ok { - w, ok := width.(int) - if ok { - r.Width = w - } + r.Width = w } - height, ok := sss["height"] + } + height, ok := tt["height"] + if ok { + h, ok := height.(int) if ok { - h, ok := height.(int) - if ok { - r.Height = h - } + r.Height = h } } } diff --git a/helper/function.go b/helper/function.go new file mode 100644 index 0000000..10d369b --- /dev/null +++ b/helper/function.go @@ -0,0 +1,26 @@ +package helper + +import "strings" + +func GetStrMapAnyVal[T any](key string, v map[string]any) (r T, o bool) { + k := strings.Split(key, ".") + if len(k) > 1 { + val, ok := v[k[0]] + if ok { + vx, ok := val.(map[string]any) + if ok { + r, o = GetStrMapAnyVal[T](strings.Join(k[1:], "."), vx) + } + } + } else { + x, ok := v[k[0]] + if ok { + vv, ok := x.(T) + if ok { + o = true + r = vv + } + } + } + return +} diff --git a/helper/function_test.go b/helper/function_test.go new file mode 100644 index 0000000..f938a72 --- /dev/null +++ b/helper/function_test.go @@ -0,0 +1,48 @@ +package helper + +import ( + "reflect" + "testing" +) + +func TestGetStrMapAnyVal(t *testing.T) { + type args struct { + key string + v map[string]any + } + type testCase[T any] struct { + name string + args args + wantR T + wantO bool + } + tests := []testCase[int]{ + {name: "t1", args: args{ + key: "k1", + v: map[string]any{ + "k1": 1, + "k2": 2, + }, + }, wantR: 1, wantO: true}, + {name: "t2", args: args{ + key: "k2.kk", + v: map[string]any{ + "k1": 1, + "k2": map[string]any{ + "kk": 10, + }, + }, + }, wantR: 10, wantO: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotR, gotO := GetStrMapAnyVal[int](tt.args.key, tt.args.v) + if !reflect.DeepEqual(gotR, tt.wantR) { + t.Errorf("GetStrMapAnyVal() gotR = %v, want %v", gotR, tt.wantR) + } + if gotO != tt.wantO { + t.Errorf("GetStrMapAnyVal() gotO = %v, want %v", gotO, tt.wantO) + } + }) + } +}