@@ -27,23 +27,23 @@ import (
2727)
2828
2929// FetchAny fetches the value of the field described by desc from any based on go reflect.
30- func FetchAny (desc * Descriptor , any interface {}, opts ... FetchOptions ) (interface {}, error ) {
30+ func FetchAny (desc * Descriptor , any interface {}, opts ... FetchOption ) (interface {}, error ) {
3131 if any == nil || desc == nil {
3232 return nil , nil
3333 }
3434
3535 desc .Normalize ()
3636
3737 var opt FetchOptions
38- if len ( opts ) > 0 {
39- opt = opts [ 0 ]
38+ for _ , op := range opts {
39+ op ( & opt )
4040 }
4141
4242 v := reflect .ValueOf (any )
4343 return fetchValue (desc , v , & opt )
4444}
4545
46- // ErrNotFound is returned when a field/index/key is not found and DisallowNotFound is enabled
46+ // ErrNotFound is returned when a field/index/key is not found and disallowNotFound is enabled
4747type ErrNotFound struct {
4848 Parent * Descriptor
4949 Field Field // the field that is not found
@@ -57,7 +57,15 @@ func (e ErrNotFound) Error() string {
5757// FetchOptions contains options for FetchAny
5858type FetchOptions struct {
5959 // DisallowNotFound if true, returns ErrNotFound when a field/index/key is not found
60- DisallowNotFound bool
60+ disallowNotFound bool
61+ }
62+
63+ type FetchOption func (* FetchOptions )
64+
65+ func WithDisallowNotFound (b bool ) FetchOption {
66+ return func (opt * FetchOptions ) {
67+ opt .disallowNotFound = b
68+ }
6169}
6270
6371// structFieldInfo caches field mapping information for a struct type
@@ -173,7 +181,7 @@ func fetchStruct(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interfac
173181 if found {
174182 fieldValue := v .Field (fieldIdx )
175183 if fieldValue .Kind () == reflect .Ptr && fieldValue .IsNil () {
176- if opt .DisallowNotFound {
184+ if opt .disallowNotFound {
177185 return nil , ErrNotFound {Parent : desc , Field : * field , Msg : fmt .Sprintf ("field ID=%d is nil" , field .ID )}
178186 }
179187 continue
@@ -196,10 +204,10 @@ func fetchStruct(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interfac
196204 // Convert the value based on the field's Descriptor
197205 // (e.g., map[FieldID]interface{} -> map[string]interface{} for nested structs)
198206 result [field .Name ] = fetchUnknownValue (val , field .Desc )
199- } else if opt .DisallowNotFound {
207+ } else if opt .disallowNotFound {
200208 return nil , ErrNotFound {Parent : desc , Field : * field , Msg : fmt .Sprintf ("field ID=%d not found in struct or unknownFields" , field .ID )}
201209 }
202- } else if opt .DisallowNotFound {
210+ } else if opt .disallowNotFound {
203211 return nil , ErrNotFound {Parent : desc , Field : * field , Msg : fmt .Sprintf ("field ID=%d not found in struct" , field .ID )}
204212 }
205213 }
@@ -335,7 +343,7 @@ func fetchStrMap(desc *Descriptor, v reflect.Value, opt *FetchOptions) (interfac
335343 val := v .MapIndex (reflect .ValueOf (key ))
336344 // Check if specific keys are requested but not available in the map
337345 if ! val .IsValid () {
338- if opt .DisallowNotFound {
346+ if opt .disallowNotFound {
339347 return nil , ErrNotFound {Parent : desc , Field : keyDescMap [key ], Msg : fmt .Sprintf ("key '%s' not found in map" , key )}
340348 } else {
341349 continue
0 commit comments