Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ExtensionFieldNumber() function #91

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}