aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorMichael Fraenkel <michael.fraenkel@gmail.com>2018-04-17 18:38:06 -0400
committerIan Lance Taylor <iant@golang.org>2018-04-17 23:04:19 +0000
commitfc215989319aac0a2d5a0f3b7247db9fcb366ff7 (patch)
tree056aa3730239ea0e1abcf1c712c2e3eb9d287cec /src/encoding
parent4984d843d93a6b94122c98fadbaa71cbb473a15c (diff)
downloadgo-fc215989319aac0a2d5a0f3b7247db9fcb366ff7.tar.xz
encoding/json: simplify dominantField
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>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/json/encode.go30
1 files 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