FAQ
In case I want to encode a struct containing an anonymous
field based on a predeclared type, the encoder will panic
in typeFields():
panic: reflect: NumField of non-struct type [recovered]

If the condition in line 621 of file encode.go,

... || !sf.Anonymous,

would be relaxed to

t := sf.Type
... || !(sf.Anonymous && (t.Kind() == reflect.Struct || t.Kind() ==
reflect.Ptr && t.Elem().Kind() == reflect.Struct)),

anonymous fields that have an underlying predeclared type could be
encoded and decoded as well (and the above-mentioned panic would
be avoided).

Michael

For an example program see: http://play.golang.org/p/XVWosi6Zhj



https://codereview.appspot.com/6460044/diff/13002/src/pkg/encoding/json/encode.go
File src/pkg/encoding/json/encode.go (right):

https://codereview.appspot.com/6460044/diff/13002/src/pkg/encoding/json/encode.go#newcode621
src/pkg/encoding/json/encode.go:621: if name != "" || !sf.Anonymous {
The THEN-block will be skipped not only for anonymous structs, but also
in case sf is an anonymous field based on a predeclared type like
uint16. This will result in a panic later, when NumField() is called on
such an (anonymous) non-struct field.

https://codereview.appspot.com/6460044/

Search Discussions

  • Russ Cox at Dec 11, 2012 at 5:39 pm

    On Mon, Nov 26, 2012 at 4:17 PM, wrote:
    In case I want to encode a struct containing an anonymous
    field based on a predeclared type, the encoder will panic
    in typeFields():
    panic: reflect: NumField of non-struct type [recovered]
    Thanks. Looks like this is issue 4474.

    Russ

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedNov 26, '12 at 9:33p
activeDec 11, '12 at 5:39p
posts2
users2
websitegolang.org

2 users in discussion

Russ Cox: 1 post Mt4swm: 1 post

People

Translate

site design / logo © 2022 Grokbase