Skip to content

Commit

Permalink
proto/generic/cast.go: let Value struct func <Interface> support Enum… (
Browse files Browse the repository at this point in the history
#44)

proto/generic/cast.go: let Value struct func <Interface> support Enum type

Co-authored-by: shibaoshun <[email protected]>
  • Loading branch information
sbs-copilot and shibaoshun committed Apr 14, 2024
1 parent ad05023 commit b93142e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions proto/generic/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ func (self Node) int() (int, error) {
}
}

// Enum returns the int value contained by a Enum node
func (self Node) Enum() (int, error) {
if self.IsError() {
return 0, self
}
return self.enum()
}

func (self Node) enum() (int, error) {
buf := rt.BytesFrom(self.v, int(self.l), int(self.l))
switch self.t {
case proto.ENUM:
v, _ := protowire.BinaryDecoder{}.DecodeInt32(buf)
return int(v), nil
default:
return 0, errNode(meta.ErrUnsupportedType, "Node.enum: the Node type is not Enum", nil)
}
}

// Float64 returns the float64 value contained by a DOUBLE node
func (self Node) Float64() (float64, error) {
if self.IsError() {
Expand Down Expand Up @@ -336,6 +355,8 @@ func (self Value) Interface(opts *Options) (interface{}, error) {
return self.binary()
}
return self.string()
case proto.ENUM:
return self.enum()
case proto.LIST:
return self.List(opts)
case proto.MAP:
Expand Down

0 comments on commit b93142e

Please sign in to comment.