Skip to content

Commit

Permalink
structToMap Anonymous field bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaomingqiang committed Dec 2, 2021
1 parent 8e772c1 commit 5f14bd1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
31 changes: 23 additions & 8 deletions sample/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions sample/api/message_model_card.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions v2/message_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`
}

Expand Down
31 changes: 20 additions & 11 deletions v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -349,7 +358,7 @@ func formatAsString(v reflect.Value, kind reflect.Kind) string {
}

type jsonTag struct {
apiName string
name string
stringFormat bool
ignore bool
}
Expand All @@ -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":
Expand Down

0 comments on commit 5f14bd1

Please sign in to comment.