From 5f14bd13dd51f869b05e40ec358e1faa7485784c Mon Sep 17 00:00:00 2001 From: zhaomingqiang Date: Thu, 2 Dec 2021 18:28:26 +0800 Subject: [PATCH] structToMap Anonymous field bug --- sample/api/api.go | 31 ++++++++++++++++++++-------- sample/api/message_model_card.go | 35 ++++++++++++++++++++++++++++++++ v2/message_model.go | 4 ++-- v2/utils.go | 31 ++++++++++++++++++---------- 4 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 sample/api/message_model_card.go diff --git a/sample/api/api.go b/sample/api/api.go index 41f5de98..7ec02d4c 100644 --- a/sample/api/api.go +++ b/sample/api/api.go @@ -15,17 +15,32 @@ func main() { lark.WithLogger(lark.NewDefaultLogger(), lark.LogLevelDebug)) ctx := context.Background() - sendMessage(ctx, larkApp) - uploadImage(ctx, larkApp) - downloadImage(ctx, larkApp) + //sendMessage(ctx, larkApp) + //uploadImage(ctx, larkApp) + //downloadImage(ctx, larkApp) + getJsTicket(ctx, larkApp) } func sendMessage(ctx context.Context, larkApp *lark.App) { - resp, err := larkApp.SendRequest(ctx, http.MethodGet, "/open-apis/message/v4/send", lark.AccessTokenTypeTenant, map[string]interface{}{ - "user_id": "77bbc392", - "msg_type": "text", - "content": &lark.MessageText{Text: "test"}, - }) + resp, err := larkApp.SendRequest(ctx, http.MethodGet, "/open-apis/message/v4/send", + lark.AccessTokenTypeTenant, map[string]interface{}{ + "user_id": "77bbc392", + "msg_type": "text", + "content": &lark.MessageText{Text: "test"}, + }) + if err != nil { + fmt.Println(err) + return + } + fmt.Printf("request id: %s \n", resp.RequestId()) + fmt.Println(resp) + fmt.Println() + fmt.Println() +} + +func getJsTicket(ctx context.Context, larkApp *lark.App) { + resp, err := larkApp.SendRequest(ctx, http.MethodGet, "/open-apis/jssdk/ticket/get", + lark.AccessTokenTypeApp, nil) if err != nil { fmt.Println(err) return diff --git a/sample/api/message_model_card.go b/sample/api/message_model_card.go new file mode 100644 index 00000000..57dc8f9f --- /dev/null +++ b/sample/api/message_model_card.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "github.com/larksuite/oapi-sdk-go/v2" +) + +func main() { + printMessageCard1() +} + +func printMessageCard1() { + card := lark.MessageCard{ + Header: &lark.MessageCardHeader{ + Title: &lark.MessageCardPlainText{Content: "k8s ticket"}, + Template: lark.StringPtr("indigo"), + }, + } + element := &lark.MessageCardAction{} + cluster := &lark.MessageCardEmbedSelectMenuStatic{ + MessageCardEmbedSelectMenuBase: &lark.MessageCardEmbedSelectMenuBase{ + Placeholder: &lark.MessageCardPlainText{ + Content: "choose cluster", + }, + InitialOption: "dev", + }, + } + element.Actions = append(element.Actions, cluster) + card.Elements = append(card.Elements, element) + content, err := card.JSON() + if err != nil { + fmt.Println(err) + } + fmt.Printf("card:%s\n", content) +} diff --git a/v2/message_model.go b/v2/message_model.go index 407e09a8..74301b87 100644 --- a/v2/message_model.go +++ b/v2/message_model.go @@ -3,7 +3,7 @@ package lark import "encoding/json" type MessageText struct { - Text string `json:"text"` + Text string `json:"text,omitempty"` } func (m *MessageText) JSON() (string, error) { @@ -520,7 +520,7 @@ type MessageCardEmbedSelectMenuBase struct { InitialOption string `json:"initial_option,omitempty"` Options []*MessageCardEmbedSelectOption `json:"options,omitempty"` Value map[string]interface { - } `json:"value"` + } `json:"value,omitempty"` Confirm *MessageCardActionConfirm `json:"confirm,omitempty"` } diff --git a/v2/utils.go b/v2/utils.go index 6bd59be5..1a2a6d8c 100644 --- a/v2/utils.go +++ b/v2/utils.go @@ -308,7 +308,19 @@ func structToMap(val interface{}) (map[string]interface{}, error) { s := reflect.Indirect(reflect.ValueOf(val)) st := s.Type() for i := 0; i < s.NumField(); i++ { - jsonTag := st.Field(i).Tag.Get("json") + fieldDesc := st.Field(i) + fieldVal := s.Field(i) + if fieldDesc.Anonymous { + embeddedMap, err := structToMap(fieldVal.Interface()) + if err != nil { + return nil, err + } + for k, v := range embeddedMap { + m[k] = v + } + continue + } + jsonTag := fieldDesc.Tag.Get("json") if jsonTag == "" { continue } @@ -319,23 +331,20 @@ func structToMap(val interface{}) (map[string]interface{}, error) { if tag.ignore { continue } - v := s.Field(i) - f := st.Field(i) - - if f.Type.Kind() == reflect.Ptr && v.IsNil() { + if fieldDesc.Type.Kind() == reflect.Ptr && fieldVal.IsNil() { continue } // nil maps are treated as empty maps. - if f.Type.Kind() == reflect.Map && v.IsNil() { + if fieldDesc.Type.Kind() == reflect.Map && fieldVal.IsNil() { continue } - if f.Type.Kind() == reflect.Slice && v.IsNil() { + if fieldDesc.Type.Kind() == reflect.Slice && fieldVal.IsNil() { continue } if tag.stringFormat { - m[tag.apiName] = formatAsString(v, f.Type.Kind()) + m[tag.name] = formatAsString(fieldVal, fieldDesc.Type.Kind()) } else { - m[tag.apiName] = v.Interface() + m[tag.name] = fieldVal.Interface() } } return m, nil @@ -349,7 +358,7 @@ func formatAsString(v reflect.Value, kind reflect.Kind) string { } type jsonTag struct { - apiName string + name string stringFormat bool ignore bool } @@ -364,7 +373,7 @@ func parseJSONTag(val string) (jsonTag, error) { return tag, fmt.Errorf("malformed json tag: %s", val) } tag = jsonTag{ - apiName: val[:i], + name: val[:i], } switch val[i+1:] { case "omitempty":