mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-04-16 08:42:29 +00:00
testing/protocmp: switch Enum.Number to be a method
Accessing Number as a method, rather than a field paves the way to have Enum potentially implement protoreflect.Enum in the future. Change-Id: Iebe9c0ec12e067decf2121d12fe2fb1549477b32 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/207077 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
613285cf7a
commit
fab1c8d9a3
@ -20,8 +20,8 @@ import (
|
|||||||
// Enum is a dynamic representation of a protocol buffer enum that is
|
// Enum is a dynamic representation of a protocol buffer enum that is
|
||||||
// suitable for cmp.Equal and cmp.Diff to compare upon.
|
// suitable for cmp.Equal and cmp.Diff to compare upon.
|
||||||
type Enum struct {
|
type Enum struct {
|
||||||
Number protoreflect.EnumNumber
|
num protoreflect.EnumNumber
|
||||||
ed protoreflect.EnumDescriptor
|
ed protoreflect.EnumDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
// Descriptor returns the enum descriptor.
|
// Descriptor returns the enum descriptor.
|
||||||
@ -29,21 +29,26 @@ func (e Enum) Descriptor() protoreflect.EnumDescriptor {
|
|||||||
return e.ed
|
return e.ed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Number returns the enum value as an integer.
|
||||||
|
func (e Enum) Number() protoreflect.EnumNumber {
|
||||||
|
return e.num
|
||||||
|
}
|
||||||
|
|
||||||
// Equal reports whether e1 and e2 represent the same enum value.
|
// Equal reports whether e1 and e2 represent the same enum value.
|
||||||
func (e1 Enum) Equal(e2 Enum) bool {
|
func (e1 Enum) Equal(e2 Enum) bool {
|
||||||
if e1.ed.FullName() != e2.ed.FullName() {
|
if e1.ed.FullName() != e2.ed.FullName() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return e1.Number == e2.Number
|
return e1.num == e2.num
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the name of the enum value if known (e.g., "ENUM_VALUE"),
|
// String returns the name of the enum value if known (e.g., "ENUM_VALUE"),
|
||||||
// otherwise it returns the formatted decimal enum number (e.g., "14").
|
// otherwise it returns the formatted decimal enum number (e.g., "14").
|
||||||
func (e Enum) String() string {
|
func (e Enum) String() string {
|
||||||
if ev := e.ed.Values().ByNumber(e.Number); ev != nil {
|
if ev := e.ed.Values().ByNumber(e.num); ev != nil {
|
||||||
return string(ev.Name())
|
return string(ev.Name())
|
||||||
}
|
}
|
||||||
return strconv.Itoa(int(e.Number))
|
return strconv.Itoa(int(e.num))
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageTypeKey = "@type"
|
const messageTypeKey = "@type"
|
||||||
@ -187,7 +192,7 @@ func transformMap(fd protoreflect.FieldDescriptor, mv protoreflect.Map) interfac
|
|||||||
func transformSingular(fd protoreflect.FieldDescriptor, v protoreflect.Value) interface{} {
|
func transformSingular(fd protoreflect.FieldDescriptor, v protoreflect.Value) interface{} {
|
||||||
switch fd.Kind() {
|
switch fd.Kind() {
|
||||||
case protoreflect.EnumKind:
|
case protoreflect.EnumKind:
|
||||||
return Enum{Number: v.Enum(), ed: fd.Enum()}
|
return Enum{num: v.Enum(), ed: fd.Enum()}
|
||||||
case protoreflect.MessageKind, protoreflect.GroupKind:
|
case protoreflect.MessageKind, protoreflect.GroupKind:
|
||||||
return transformMessage(v.Message())
|
return transformMessage(v.Message())
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user