This commit is contained in:
xing 2023-01-11 13:56:56 +08:00
parent 458d5b70ba
commit c9fe47da2f
3 changed files with 84 additions and 37 deletions

View File

@ -8,7 +8,6 @@ import (
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/models/wp" "github/fthvgb1/wp-go/models/wp"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -21,29 +20,6 @@ func GetPostMetaByPostId(ctx context.Context, id uint64) (r map[string]any, err
return 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) { func ToPostThumbnail(c context.Context, postId uint64) (r wp.PostThumbnail) {
meta, err := GetPostMetaByPostId(c, postId) meta, err := GetPostMetaByPostId(c, postId)
if err == nil { if err == nil {
@ -60,23 +36,20 @@ func ToPostThumbnail(c context.Context, postId uint64) (r wp.PostThumbnail) {
r.Path = ff r.Path = ff
} }
} }
tt := getMetaVal("_wp_attachment_metadata.sizes.post-thumbnail", mm) tt, ok := helper.GetStrMapAnyVal[map[string]any]("_wp_attachment_metadata.sizes.post-thumbnail", mm)
if tt != nil { if ok && tt != nil {
sss, ok := tt.(map[string]any) width, ok := tt["width"]
if ok { if ok {
width, ok := sss["width"] w, ok := width.(int)
if ok { if ok {
w, ok := width.(int) r.Width = w
if ok {
r.Width = w
}
} }
height, ok := sss["height"] }
height, ok := tt["height"]
if ok {
h, ok := height.(int)
if ok { if ok {
h, ok := height.(int) r.Height = h
if ok {
r.Height = h
}
} }
} }
} }

26
helper/function.go Normal file
View File

@ -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
}

48
helper/function_test.go Normal file
View File

@ -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)
}
})
}
}