Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api_message_send_ephemeral.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions api_message_send_ephemeral_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package lark

import (
"encoding/json"
"errors"
)

func (r SendEphemeralMessageReq) MarshalJSON() ([]byte, error) {
if r.Card != nil && len(r.CardV2) > 0 {
return nil, errors.New("lark: SendEphemeralMessageReq Card and CardV2 cannot both be set")
}

var card any
if len(r.CardV2) > 0 {
card = r.CardV2
} else if r.Card != nil {
card = r.Card
}

type request SendEphemeralMessageReq
return json.Marshal(struct {
request
Card any `json:"card,omitempty"`
}{
request: request(r),
Card: card,
})
}
80 changes: 80 additions & 0 deletions api_message_send_ephemeral_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package lark

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestSendEphemeralMessageReqMarshalsV2Card(t *testing.T) {
card := json.RawMessage(`{"schema":"2.0","body":{"elements":[]}}`)
req := &SendEphemeralMessageReq{
ChatID: "oc_test",
OpenID: "ou_test",
MsgType: MsgTypeInteractive,
CardV2: card,
}

body, err := json.Marshal(req)
require.NoError(t, err)
require.JSONEq(t, `{
"chat_id":"oc_test",
"open_id":"ou_test",
"msg_type":"interactive",
"card":{"schema":"2.0","body":{"elements":[]}}
}`, string(body))
}

func TestSendEphemeralMessageReqKeepsV1CardCompatibility(t *testing.T) {
card := &MessageContentCard{
Modules: []MessageContentCardModule{
MessageContentCardModuleDIV{
Text: &MessageContentCardObjectText{Tag: "lark_md", Content: "hello"},
},
},
}
req := &SendEphemeralMessageReq{
ChatID: "oc_test",
OpenID: "ou_test",
MsgType: MsgTypeInteractive,
Card: card,
}
var typedCard *MessageContentCard = req.Card
require.Same(t, card, typedCard)

body, err := json.Marshal(req)
require.NoError(t, err)
require.JSONEq(t, `{
"chat_id":"oc_test",
"open_id":"ou_test",
"msg_type":"interactive",
"card":{"elements":[{"tag":"div","text":{"tag":"lark_md","content":"hello"}}]}
}`, string(body))
}

func TestSendEphemeralMessageReqRejectsBothCardVersions(t *testing.T) {
req := &SendEphemeralMessageReq{
Card: &MessageContentCard{},
CardV2: json.RawMessage(`{"schema":"2.0","body":{"elements":[]}}`),
}

_, err := json.Marshal(req)
require.ErrorContains(t, err, "Card and CardV2 cannot both be set")
}

func TestSendEphemeralMessageReqOmitsEmptyCard(t *testing.T) {
req := &SendEphemeralMessageReq{
ChatID: "oc_test",
OpenID: "ou_test",
MsgType: MsgTypeInteractive,
}

body, err := json.Marshal(req)
require.NoError(t, err)
require.JSONEq(t, `{
"chat_id":"oc_test",
"open_id":"ou_test",
"msg_type":"interactive"
}`, string(body))
}
39 changes: 23 additions & 16 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ func Test_Request(t *testing.T) {
}

resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com/:id",
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com/:id",
Body: req{
ID: string("1234"),
},
Expand All @@ -60,8 +61,9 @@ func Test_Request(t *testing.T) {
ID int `path:"id"`
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com/:id",
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com/:id",
Body: req{
ID: 1234,
},
Expand All @@ -79,8 +81,9 @@ func Test_Request(t *testing.T) {
ID int `path:"id"`
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com/:type/:id",
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com/:type/:id",
Body: req{
Type: "x",
ID: 1234,
Expand All @@ -99,8 +102,9 @@ func Test_Request(t *testing.T) {
ID int `path:"id"`
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com/:id",
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com/:id",
Body: req{
Type: "x",
ID: 1234,
Expand All @@ -120,8 +124,9 @@ func Test_Request(t *testing.T) {
Name string `json:"name"`
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com/:id",
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com/:id",
Body: req{
Type: "x",
ID: 1234,
Expand All @@ -144,9 +149,10 @@ func Test_Request(t *testing.T) {
Image io.Reader `json:"image,omitempty"` // 图片内容,**示例值**:二进流
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com",
IsFile: true,
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com",
IsFile: true,
Body: req{
ImageType: ImageTypeMessage,
Image: bytes.NewReader([]byte("hi")),
Expand All @@ -167,9 +173,10 @@ func Test_Request(t *testing.T) {
Types []string `query:"types"`
}
resp, err := ins.parseRawHttpRequest(ctx, &RawRequestReq{
Method: "get",
URL: "http://x.com",
IsFile: true,
MethodOption: newMethodOption(nil),
Method: "get",
URL: "http://x.com",
IsFile: true,
Body: req{
Types: []string{"a", "b"},
},
Expand Down
1 change: 0 additions & 1 deletion test/drive_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func Test_DriveFile(t *testing.T) {
{
resp, _, err := AppAllPermission.Ins().Drive.DownloadDriveFile(ctx, &lark.DownloadDriveFileReq{
FileToken: fileToken,
Range: [2]int64{},
})
as.Nil(err)
as.NotNil(resp)
Expand Down