Skip to content

Commit

Permalink
Add ForEachPresentOption method to optionExtensionDescriptor (#2758)
Browse files Browse the repository at this point in the history
Pulling changes to `protosource` from
#2752 into its own PR.
  • Loading branch information
gilwong00 authored Feb 9, 2024
1 parent c088255 commit b585408
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit b585408

Please sign in to comment.