]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: cleanup detection of unexported embedded fields
authorJoe Tsai <joetsai@digital-static.net>
Fri, 22 Sep 2017 22:44:09 +0000 (15:44 -0700)
committerJoe Tsai <thebrokentoaster@gmail.com>
Sat, 23 Sep 2017 00:33:34 +0000 (00:33 +0000)
CL 60410 fixes the compiler such that reflect.StructField.PkgPath
is non-empty if and only if the field is unexported.
Given that property, we can cleanup the logic in the json encoder
to avoid parsing the field name to detect export properties.

Updates #21122

Change-Id: Ic01b9c4ca76386774846b742b0c1b9b948f53e7c
Reviewed-on: https://go-review.googlesource.com/65550
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/encoding/json/encode.go

index 9a2f84133544f7e5522d57a5fc87e3ed761a50c8..d1dda9796af8b61f486da5dd75589f91b62e210b 100644 (file)
@@ -1091,21 +1091,19 @@ func typeFields(t reflect.Type) []field {
                        // Scan f.typ for fields to include.
                        for i := 0; i < f.typ.NumField(); i++ {
                                sf := f.typ.Field(i)
+                               isUnexported := sf.PkgPath != ""
                                if sf.Anonymous {
                                        t := sf.Type
                                        if t.Kind() == reflect.Ptr {
                                                t = t.Elem()
                                        }
-                                       // If embedded, StructField.PkgPath is not a reliable
-                                       // indicator of whether the field is exported.
-                                       // See https://golang.org/issue/21122
-                                       if !isExported(t.Name()) && t.Kind() != reflect.Struct {
+                                       if isUnexported && t.Kind() != reflect.Struct {
                                                // Ignore embedded fields of unexported non-struct types.
-                                               // Do not ignore embedded fields of unexported struct types
-                                               // since they may have exported fields.
                                                continue
                                        }
-                               } else if sf.PkgPath != "" {
+                                       // Do not ignore embedded fields of unexported struct types
+                                       // since they may have exported fields.
+                               } else if isUnexported {
                                        // Ignore unexported non-embedded fields.
                                        continue
                                }
@@ -1224,12 +1222,6 @@ func typeFields(t reflect.Type) []field {
        return fields
 }
 
-// isExported reports whether the identifier is exported.
-func isExported(id string) bool {
-       r, _ := utf8.DecodeRuneInString(id)
-       return unicode.IsUpper(r)
-}
-
 // dominantField looks through the fields, all of which are known to
 // have the same name, to find the single field that dominates the
 // others using Go's embedding rules, modified by the presence of