pquerna / ffjson

faster JSON serialization for Go
Apache License 2.0
2.97k stars 234 forks source link

Incorrect generation of type in a slice in a map in a struct #225

Open posener opened 7 years ago

posener commented 7 years ago

For the following go file:

package bug

type Struct struct {
    Field map[string][]Obj
}

type Obj string

running ffjson results in a go file that can't be compiled:

handle_Field:

    /* handler: j.Field type=map[string][]bug.Obj kind=map quoted=false*/

    {

        {
            if tok != fflib.FFTok_left_bracket && tok != fflib.FFTok_null {
                return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for ", tok))
            }
        }

        if tok == fflib.FFTok_null {
            j.Field = nil
        } else {

            j.Field = make(map[string][]bug.Obj, 0)

            wantVal := true

            for {

                var k string
...

Notice the bug.Obj which should be just Obj. Thanks

brycewilson-wf commented 6 years ago

Just wanted to chime in that I'm also running into this issue.

drew-richardson commented 3 years ago

Here's another example

package foo

//go:generate ffjson foo.go

type props struct {
    Series map[int][]*series `json:"series"`
}

type series struct {
    Name string `json:"name"`
}

which after running go generate ./... creates this snippet of code which does not compile

        if tok == fflib.FFTok_null {
            j.Series = nil
        } else {

            j.Series = make(map[int][]*foo.series, 0)

            wantVal := true

            for {

                var k int

                var tmpJSeries []*foo.series

                tok = fs.Scan()
                if tok == fflib.FFTok_error {
                    goto tokerror
$ go build ./...
# github.com/drew-richardson/foo
foo/foo_ffjson.go:182:31: undefined: foo
foo/foo_ffjson.go:190:23: undefined: foo
arichard@gentoo:~/dist/go> 

I believe the correct code may be j.Series = make(map[int][]*series, 0) and j.Series = make(map[int][]*series, 0)