From f97268d0effc934641e6c73af485fdd500da595e Mon Sep 17 00:00:00 2001 From: Dylan Bourque Date: Mon, 12 Dec 2022 13:00:48 -0600 Subject: [PATCH] feat: add ExtensionFieldNumber() function (#91) adds a new `ExtensionFieldNumber(any)` API that returns the field tag associated with a proto2 extension field descriptor --- extensions.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/extensions.go b/extensions.go index 2e45099..84de7c2 100644 --- a/extensions.go +++ b/extensions.go @@ -191,3 +191,18 @@ func ClearAllExtensions(msg interface{}) { // no-op } } + +// ExtensionFieldNumber returns the integer field tag associated with the specified proto2 extension +// descriptor. If ext is not one of the three supported types, this function returns 0 and an error. +func ExtensionFieldNumber(ext any) (int, error) { + switch tv := ext.(type) { + case *gogo.ExtensionDesc: + return int(tv.Field), nil + case *google.ExtensionDesc: + return int(tv.Field), nil + case protoreflect.ExtensionType: + return int(tv.TypeDescriptor().Number()), nil + default: + return 0, fmt.Errorf("unsupported proto2 extension descriptor type %T", ext) + } +}