Skip to content

Commit 0a2f711

Browse files
committed
chore
1 parent 8d4888b commit 0a2f711

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

trim/fetch.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4747
type 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
5858
type 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

trim/fetch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func TestFetchAnyWithDisallowNotFound(t *testing.T) {
367367
},
368368
}
369369

370-
_, err := FetchAny(desc, obj, FetchOptions{DisallowNotFound: true})
370+
_, err := FetchAny(desc, obj, WithDisallowNotFound(true))
371371
if err == nil {
372372
t.Fatalf("expected ErrNotFound, got nil")
373373
}
@@ -410,7 +410,7 @@ func TestFetchAnyWithDisallowNotFound(t *testing.T) {
410410
},
411411
}
412412

413-
_, err := FetchAny(desc, obj, FetchOptions{DisallowNotFound: true})
413+
_, err := FetchAny(desc, obj, WithDisallowNotFound(true))
414414
if err == nil {
415415
t.Fatalf("expected ErrNotFound, got nil")
416416
}
@@ -457,7 +457,7 @@ func TestFetchAnyWithDisallowNotFound(t *testing.T) {
457457
},
458458
}
459459

460-
_, err := FetchAny(desc, obj, FetchOptions{DisallowNotFound: true})
460+
_, err := FetchAny(desc, obj, WithDisallowNotFound(true))
461461
if err == nil {
462462
t.Fatalf("expected ErrNotFound, got nil")
463463
}
@@ -485,7 +485,7 @@ func TestFetchAnyWithDisallowNotFound(t *testing.T) {
485485
},
486486
}
487487

488-
ret, err := FetchAny(desc, obj, FetchOptions{DisallowNotFound: true})
488+
ret, err := FetchAny(desc, obj, WithDisallowNotFound(true))
489489
if err != nil {
490490
t.Fatalf("unexpected error: %v", err)
491491
}

0 commit comments

Comments
 (0)