Skip to content

support for decoding struct types #11

@mewmew

Description

@mewmew

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions