Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slice encoding #64

Merged
merged 1 commit into from
Jul 3, 2024
Merged
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 cursor/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ func (s *encoderSuite) TestZeroValuePtr() {
_, err := e.Encode(struct{ ID *string }{})
s.Nil(err)
}

func (s *encoderSuite) TestSliceValue() {
e := NewEncoder([]EncoderField{{Key: "Slice"}})
_, err := e.Encode(struct{ Slice []string }{Slice: []string{"value"}})
s.Nil(err)
}
22 changes: 22 additions & 0 deletions cursor/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ func (s *encodingSuite) TestStructPtr() {
s.Equal(sv, *(v.(*structValue)))
}

/* slice */

type sliceModel struct {
Value []byte
ValuePtr *[]byte
}

func (s *encodingSuite) TestSlice() {
c, _ := s.encodeValue(sliceModel{
Value: []byte("123"),
})
v, _ := s.decodeValue(sliceModel{}, c)
s.Equal([]byte("123"), v)
}

func (s *encodingSuite) TestSlicePtr() {
sv := []byte("123")
c, _ := s.encodeValuePtr(sliceModel{ValuePtr: &sv})
v, _ := s.decodeValuePtr(sliceModel{}, c)
s.Equal(sv, *(v.(*[]byte)))
}

/* multiple */

type multipleModel struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"reflect"
)

// ReflectValue returns reflect value underlying given value, unwrapping pointer and slice
// ReflectValue returns reflect value underlying given value, unwrapping pointer
func ReflectValue(v interface{}) reflect.Value {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
for rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Slice {
for rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
return rv
Expand Down
Loading