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 for decoding struct types #11

Open
mewmew opened this issue Aug 19, 2021 · 0 comments
Open

support for decoding struct types #11

mewmew opened this issue Aug 19, 2021 · 0 comments

Comments

@mewmew
Copy link

mewmew commented Aug 19, 2021

It seems the current version of pkg/json does not yet support decoding into struct tyoes. As pkg/josn is still in development, the issue is probably known. This issue is meant to track the progress of implementing support for decoding into struct types.

$ go test
--- FAIL: TestDecodeIntoStruct (0.00s)
    foo_test.go:29: unable to decode into struct; decodeValue: unhandled type: struct
FAIL
exit status 1
FAIL	github.com/pkg/json	0.276s

The following test case passes when using encoding/json but fails with unable to decode into struct; decodeValue: unhandled type: struct when using github.com/pkg/json.

package json

import (
	//"encoding/json"
	"reflect"
	"strings"
	"testing"
)

func TestDecodeIntoStruct(t *testing.T) {
	type Foo struct {
		X int
		Y int
	}
	golden := []struct {
		want Foo
		in   string
	}{
		{
			want: Foo{X: 1, Y: 2},
			in:   `{"X": 1, "Y": 2}`,
		},
	}
	for _, g := range golden {
		r := strings.NewReader(g.in)
		dec := NewDecoder(r)
		got := Foo{}
		if err := dec.Decode(&got); err != nil {
			t.Errorf("unable to decode into struct; %v", err)
			continue
		}
		if g.want != got {
			t.Errorf("decoded value mismatch; expected %#v, got %#v", g.want, got)
		}
	}
}

func TestDecodeIntoMap(t *testing.T) {
	type Foo map[string]int
	golden := []struct {
		want Foo
		in   string
	}{
		{
			want: Foo{"X": 1, "Y": 2},
			in:   `{"X": 1, "Y": 2}`,
		},
	}
	for _, g := range golden {
		r := strings.NewReader(g.in)
		dec := NewDecoder(r)
		got := Foo{}
		if err := dec.Decode(&got); err != nil {
			t.Errorf("unable to decode into struct; %v", err)
			continue
		}
		if !reflect.DeepEqual(g.want, got) {
			t.Errorf("decoded value mismatch; expected %#v, got %#v", g.want, got)
		}
	}
}

Cheers,
Robin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant