From c9fe47da2f382146ab852dd358f5547326045d3c Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 11 Jan 2023 13:56:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/postmeta.go | 47 ++++++++----------------------------- helper/function.go | 26 +++++++++++++++++++++ helper/function_test.go | 48 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 37 deletions(-) create mode 100644 helper/function.go create mode 100644 helper/function_test.go 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) + } + }) + } +}