]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: simplify dominantField
authorMichael Fraenkel <michael.fraenkel@gmail.com>
Tue, 17 Apr 2018 22:38:06 +0000 (18:38 -0400)
committerIan Lance Taylor <iant@golang.org>
Tue, 17 Apr 2018 23:04:19 +0000 (23:04 +0000)
Fixes #18037

Change-Id: I20e27bcc013b00b726eb348daf5ca86b138ddcc2
Reviewed-on: https://go-review.googlesource.com/107598
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/encoding/json/encode.go

index 99407e0f7ab956fd3044a643ee50a95ac438bdc9..28ca5fe9e009f015ee160c2730b4b20c6cf69783 100644 (file)
@@ -1237,32 +1237,10 @@ func typeFields(t reflect.Type) []field {
 // will be false: This condition is an error in Go and we skip all
 // the fields.
 func dominantField(fields []field) (field, bool) {
-       // The fields are sorted in increasing index-length order. The winner
-       // must therefore be one with the shortest index length. Drop all
-       // longer entries, which is easy: just truncate the slice.
-       length := len(fields[0].index)
-       tagged := -1 // Index of first tagged field.
-       for i, f := range fields {
-               if len(f.index) > length {
-                       fields = fields[:i]
-                       break
-               }
-               if f.tag {
-                       if tagged >= 0 {
-                               // Multiple tagged fields at the same level: conflict.
-                               // Return no field.
-                               return field{}, false
-                       }
-                       tagged = i
-               }
-       }
-       if tagged >= 0 {
-               return fields[tagged], true
-       }
-       // All remaining fields have the same length. If there's more than one,
-       // we have a conflict (two fields named "X" at the same level) and we
-       // return no field.
-       if len(fields) > 1 {
+       // The fields are sorted in increasing index-length order, then by presence of tag.
+       // That means that the first field is the dominant one. We need only check
+       // for error cases: two fields at top level, either both tagged or neither tagged.
+       if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
                return field{}, false
        }
        return fields[0], true