Skip to content
Open
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
6 changes: 6 additions & 0 deletions attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func encodeAttachmentValue(v reflect.Value, index *int) []io.Reader {
if !v.IsValid() {
return ret
}
if !v.CanInterface() {
return ret
}
switch v.Kind() {
case reflect.Struct:
if v.Type().Name() == "Attachment" {
Expand Down Expand Up @@ -96,6 +99,9 @@ func decodeAttachmentValue(v reflect.Value, binary [][]byte) error {
if !v.IsValid() {
return fmt.Errorf("invalid value")
}
if !v.CanInterface() {
return nil
}
switch v.Kind() {
case reflect.Struct:
if v.Type().Name() == "Attachment" {
Expand Down
37 changes: 37 additions & 0 deletions attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type HaveAttachment struct {
A *Attachment `json:"a"`
}

type HavePrivateAttachment struct {
A *Attachment `json:"a"`
hidden any
}

func TestEncodeAttachments(t *testing.T) {
var input any
var target []io.Reader
Expand Down Expand Up @@ -103,6 +108,21 @@ func TestEncodeAttachments(t *testing.T) {
So(string(b), ShouldEqual, `{"test":{"i":0,"a":{"_placeholder":true,"num":0}}}`)
})

Convey("Unexported fields are ignored", t, func() {
hidden := &Attachment{Data: buf2}
hidden.num = -1
input = HavePrivateAttachment{
A: attachment1,
hidden: hidden,
}
target = []io.Reader{buf1}

test()

So(attachment1.num, ShouldEqual, 0)
So(hidden.num, ShouldEqual, -1)
})

}

func TestDecodeAttachments(t *testing.T) {
Expand Down Expand Up @@ -182,4 +202,21 @@ func TestDecodeAttachments(t *testing.T) {
So(err, ShouldBeNil)
So(v.A.num, ShouldEqual, 2)
})

Convey("Unexported fields are ignored", t, func() {
input = [][]byte{[]byte("data1"), []byte("data2")}
hidden := &Attachment{Data: buf2}
hidden.num = 1
attachment1 = &Attachment{Data: buf1}
attachment2 = nil
attachment1.num = 0
v = HavePrivateAttachment{
A: attachment1,
hidden: hidden,
}

test()

So(buf2.String(), ShouldBeEmpty)
})
}