From b93142e6e2abb97fc15b58f55837567dbc2080da Mon Sep 17 00:00:00 2001 From: sbs-copilot <142784835+sbs-copilot@users.noreply.github.com> Date: Sun, 14 Apr 2024 22:48:53 +0800 Subject: [PATCH] =?UTF-8?q?proto/generic/cast.go:=20let=20Value=20struct?= =?UTF-8?q?=20func=20=20support=20Enum=E2=80=A6=20(#44)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit proto/generic/cast.go: let Value struct func support Enum type Co-authored-by: shibaoshun --- proto/generic/cast.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/proto/generic/cast.go b/proto/generic/cast.go index c73a8cba..a5aac79a 100644 --- a/proto/generic/cast.go +++ b/proto/generic/cast.go @@ -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() { @@ -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: