Skip to content

Commit

Permalink
feat: add ExtensionFieldNumber() function (#91)
Browse files Browse the repository at this point in the history
adds a new `ExtensionFieldNumber(any)` API that returns the field tag associated with a proto2 extension field descriptor
  • Loading branch information
Dylan Bourque authored Dec 12, 2022
1 parent d7f8115 commit f97268d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit f97268d

Please sign in to comment.