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

support pointer to object slice #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func makeBuilder(t reflect.Type) (builder, error) {
case reflect.Struct:
return makeStructBuilder(t)
case reflect.Ptr:
if t.Elem().Kind() != reflect.Struct {
if t.Elem().Kind() != reflect.Struct &&
t.Elem().Kind() != reflect.Slice {
return makePrimitiveBuilder(t)
}
return makePointerBuilder(t)
Expand Down
15 changes: 14 additions & 1 deletion format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ func TestFormat(t *testing.T) {
format: "foo",
output: `[{"foo":1}]`,
},
{
src: &[]struct {
Foo int `json:"foo"`
Bar int `json:"bar"`
}{
{
Foo: 1,
Bar: 2,
},
},
format: "foo",
output: `[{"foo":1}]`,
},
{
src: struct {
Foo []int `json:"foo"`
Expand Down Expand Up @@ -400,7 +413,7 @@ func TestFormat(t *testing.T) {
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("test #%d", i), func(t *testing.T) {
t.Run(fmt.Sprintf("test #%d %s %s", i, tt.format, tt.output), func(t *testing.T) {
f := NewFormatter()
var fields []string
if tt.format != "" {
Expand Down
14 changes: 12 additions & 2 deletions pointer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dynjson

import (
"errors"
"reflect"
)

Expand All @@ -25,7 +26,7 @@ func (f *pointerFormatter) format(src reflect.Value) (reflect.Value, error) {

type pointerBuilder struct {
t reflect.Type
elem *structBuilder
elem builder
}

func (b *pointerBuilder) build(fields []string, prefix string) (formatter, error) {
Expand All @@ -37,7 +38,16 @@ func (b *pointerBuilder) build(fields []string, prefix string) (formatter, error
}

func makePointerBuilder(t reflect.Type) (*pointerBuilder, error) {
eb, err := makeStructBuilder(t.Elem())
var eb builder
var err error
if t.Elem().Kind() == reflect.Struct {
eb, err = makeStructBuilder(t.Elem())
} else if t.Elem().Kind() == reflect.Slice {
eb, err = makeSliceBuilder(t.Elem())
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on builder.go, this will never occur...

return nil, errors.New("unable to handle this type")
}

if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func (f *sliceFormatter) format(src reflect.Value) (reflect.Value, error) {
}
dst.Index(i).Set(dv)
}
return dst, nil
dstAddr := reflect.New(f.t).Elem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this part necessary ?

dstAddr.Set(dst)
return dstAddr, nil
}

type sliceBuilder struct {
Expand Down