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

Add ForEachPresentOption method to optionExtensionDescriptor #2758

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions private/pkg/protosource/option_extension_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,10 @@ func isDescendantPath(descendant, ancestor []int32) bool {
}
return true
}

func (o *optionExtensionDescriptor) ForEachPresentOption(fn func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
// Note: This does not bother to handle unrecognized fields.
// Should not be a problem since descriptors models in the buf CLI codebase should have them
// all correctly parsed and known.
o.message.ProtoReflect().Range(fn)
}
11 changes: 9 additions & 2 deletions private/pkg/protosource/protosource.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,16 @@ type OptionExtensionDescriptor interface {
// If no relevant location is found in source code info, this returns nil.
OptionExtensionLocation(extensionType protoreflect.ExtensionType, extraPath ...int32) Location

// PresentExtensionNumbers returns field numbers for all options that
// have a set value on this descriptor.
// PresentExtensionNumbers returns field numbers for all extensions/custom options
// that have a set value on this descriptor.
PresentExtensionNumbers() []int32

// ForEachPresentOption iterates through all options that have a set value on this
// descriptor, invoking fn for each present option.
//
// If fn returns false, the iteration is terminated and ForEachPresentOption
// immediately returns.
ForEachPresentOption(fn func(protoreflect.FieldDescriptor, protoreflect.Value) bool)
}

// Location defines source code info location information.
Expand Down
Loading