searchIndexableWithPathPrefixes recursively searches for a value for path in source map/slice. While searchMap() considers each path element as a single map key or slice index, this function searches for, and prioritizes, merged path elements. e.g., if in the source, "foo" is defined with a sub-key
(source any, path []string)
| 515 | // |
| 516 | // Note: This assumes that the path entries and map keys are lower cased. |
| 517 | func (v *Viper) searchIndexableWithPathPrefixes(source any, path []string) any { |
| 518 | if len(path) == 0 { |
| 519 | return source |
| 520 | } |
| 521 | |
| 522 | // search for path prefixes, starting from the longest one |
| 523 | for i := len(path); i > 0; i-- { |
| 524 | prefixKey := strings.ToLower(strings.Join(path[0:i], v.keyDelim)) |
| 525 | |
| 526 | var val any |
| 527 | switch sourceIndexable := source.(type) { |
| 528 | case []any: |
| 529 | val = v.searchSliceWithPathPrefixes(sourceIndexable, prefixKey, i, path) |
| 530 | case map[string]any: |
| 531 | val = v.searchMapWithPathPrefixes(sourceIndexable, prefixKey, i, path) |
| 532 | } |
| 533 | if val != nil { |
| 534 | return val |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // not found |
| 539 | return nil |
| 540 | } |
| 541 | |
| 542 | // searchSliceWithPathPrefixes searches for a value for path in sourceSlice |
| 543 | // |
no test coverage detected