internal/mapsort: use explicit kind for strings

Rather than handle the string kind in the default case,
explicitly check for the string kind.
Instead, panic with a more useful message for unknown kinds.

Change-Id: I7e205a19658edd62a03f47f5b50a556dd942bf52
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/191579
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Joe Tsai 2019-08-23 11:39:24 -07:00
parent 6c30280ad6
commit 592e52e148

View File

@ -23,20 +23,16 @@ func Range(mapv protoreflect.Map, keyKind protoreflect.Kind, f func(protoreflect
switch keyKind {
case protoreflect.BoolKind:
return !keys[i].Bool() && keys[j].Bool()
case protoreflect.Int32Kind,
protoreflect.Sint32Kind,
protoreflect.Sfixed32Kind,
protoreflect.Int64Kind,
protoreflect.Sint64Kind,
protoreflect.Sfixed64Kind:
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind,
protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
return keys[i].Int() < keys[j].Int()
case protoreflect.Uint32Kind,
protoreflect.Fixed32Kind,
protoreflect.Uint64Kind,
protoreflect.Fixed64Kind:
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind,
protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
return keys[i].Uint() < keys[j].Uint()
default:
case protoreflect.StringKind:
return keys[i].String() < keys[j].String()
default:
panic("invalid kind: " + keyKind.String())
}
})
for _, key := range keys {