From fc215989319aac0a2d5a0f3b7247db9fcb366ff7 Mon Sep 17 00:00:00 2001 From: Michael Fraenkel Date: Tue, 17 Apr 2018 18:38:06 -0400 Subject: [PATCH] encoding/json: simplify dominantField Fixes #18037 Change-Id: I20e27bcc013b00b726eb348daf5ca86b138ddcc2 Reviewed-on: https://go-review.googlesource.com/107598 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/encoding/json/encode.go | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index 99407e0f7a..28ca5fe9e0 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -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 -- 2.50.0