fix crash for fields of built-in type

master
Felix Lange 8 years ago
parent 8b5010f571
commit bab2d3046d
  1. 13
      internal/tests/ftypes/input.go
  2. 42
      internal/tests/ftypes/output.go
  3. 1
      main_test.go
  4. 9
      typeutil.go

@ -0,0 +1,13 @@
// Copyright 2017 Felix Lange <fjl@twurst.com>.
// Use of this source code is governed by the MIT license,
// which can be found in the LICENSE file.
//go:generate gencodec -type X -formats json -out output.go
package ftypes
type X struct {
Int int
Err error
If interface{}
}

@ -0,0 +1,42 @@
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package ftypes
import (
"encoding/json"
)
func (x X) MarshalJSON() ([]byte, error) {
type X struct {
Int int
Err error
If interface{}
}
var enc X
enc.Int = x.Int
enc.Err = x.Err
enc.If = x.If
return json.Marshal(&enc)
}
func (x *X) UnmarshalJSON(input []byte) error {
type X struct {
Int *int
Err *error
If *interface{}
}
var dec X
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Int != nil {
x.Int = *dec.Int
}
if dec.Err != nil {
x.Err = *dec.Err
}
if dec.If != nil {
x.If = *dec.If
}
return nil
}

@ -24,6 +24,7 @@ func TestGolden(t *testing.T) {
Config{Dir: "nameclash", Type: "Y", FieldOverride: "Yo", Formats: AllFormats}, Config{Dir: "nameclash", Type: "Y", FieldOverride: "Yo", Formats: AllFormats},
Config{Dir: "omitempty", Type: "X", FieldOverride: "Xo", Formats: AllFormats}, Config{Dir: "omitempty", Type: "X", FieldOverride: "Xo", Formats: AllFormats},
Config{Dir: "reqfield", Type: "X", Formats: []string{"json"}}, Config{Dir: "reqfield", Type: "X", Formats: []string{"json"}},
Config{Dir: "ftypes", Type: "X", Formats: []string{"json"}},
Config{Dir: "funcoverride", Type: "Z", FieldOverride: "Zo", Formats: AllFormats}, Config{Dir: "funcoverride", Type: "Z", FieldOverride: "Zo", Formats: AllFormats},
} }
for _, test := range tests { for _, test := range tests {

@ -32,6 +32,10 @@ func walkNamedTypes(typ types.Type, callback func(*types.Named)) {
for i := 0; i < typ.NumFields(); i++ { for i := 0; i < typ.NumFields(); i++ {
walkNamedTypes(typ.Field(i).Type(), callback) walkNamedTypes(typ.Field(i).Type(), callback)
} }
case *types.Interface:
if typ.NumMethods() > 0 {
panic("BUG: can't walk non-empty interface")
}
default: default:
panic(fmt.Errorf("BUG: can't walk %T", typ)) panic(fmt.Errorf("BUG: can't walk %T", typ))
} }
@ -176,9 +180,10 @@ func (s *fileScope) addImport(path string) {
// addReferences marks all names referenced by typ as used. // addReferences marks all names referenced by typ as used.
func (s *fileScope) addReferences(typ types.Type) { func (s *fileScope) addReferences(typ types.Type) {
walkNamedTypes(typ, func(nt *types.Named) { walkNamedTypes(typ, func(nt *types.Named) {
if nt.Obj().Pkg() == s.pkg { pkg := nt.Obj().Pkg()
if pkg == s.pkg {
s.otherNames[nt.Obj().Name()] = true s.otherNames[nt.Obj().Name()] = true
} else { } else if pkg != nil {
s.insertImport(nt.Obj().Pkg()) s.insertImport(nt.Obj().Pkg())
} }
}) })

Loading…
Cancel
Save