Skip to content

Commit b93142e

Browse files
sbs-copilotshibaoshun
andauthored
proto/generic/cast.go: let Value struct func <Interface> support Enum… (#44)
proto/generic/cast.go: let Value struct func <Interface> support Enum type Co-authored-by: shibaoshun <[email protected]>
1 parent ad05023 commit b93142e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

proto/generic/cast.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ func (self Node) int() (int, error) {
128128
}
129129
}
130130

131+
// Enum returns the int value contained by a Enum node
132+
func (self Node) Enum() (int, error) {
133+
if self.IsError() {
134+
return 0, self
135+
}
136+
return self.enum()
137+
}
138+
139+
func (self Node) enum() (int, error) {
140+
buf := rt.BytesFrom(self.v, int(self.l), int(self.l))
141+
switch self.t {
142+
case proto.ENUM:
143+
v, _ := protowire.BinaryDecoder{}.DecodeInt32(buf)
144+
return int(v), nil
145+
default:
146+
return 0, errNode(meta.ErrUnsupportedType, "Node.enum: the Node type is not Enum", nil)
147+
}
148+
}
149+
131150
// Float64 returns the float64 value contained by a DOUBLE node
132151
func (self Node) Float64() (float64, error) {
133152
if self.IsError() {
@@ -336,6 +355,8 @@ func (self Value) Interface(opts *Options) (interface{}, error) {
336355
return self.binary()
337356
}
338357
return self.string()
358+
case proto.ENUM:
359+
return self.enum()
339360
case proto.LIST:
340361
return self.List(opts)
341362
case proto.MAP:

0 commit comments

Comments
 (0)