searchMap recursively searches for a value for path in source map. Returns nil if not found. Note: This assumes that the path entries and map keys are lower cased.
(source map[string]any, path []string)
| 476 | // Returns nil if not found. |
| 477 | // Note: This assumes that the path entries and map keys are lower cased. |
| 478 | func (v *Viper) searchMap(source map[string]any, path []string) any { |
| 479 | if len(path) == 0 { |
| 480 | return source |
| 481 | } |
| 482 | |
| 483 | next, ok := source[path[0]] |
| 484 | if ok { |
| 485 | // Fast path |
| 486 | if len(path) == 1 { |
| 487 | return next |
| 488 | } |
| 489 | |
| 490 | // Nested case |
| 491 | switch next := next.(type) { |
| 492 | case map[any]any: |
| 493 | return v.searchMap(cast.ToStringMap(next), path[1:]) |
| 494 | case map[string]any: |
| 495 | // Type assertion is safe here since it is only reached |
| 496 | // if the type of `next` is the same as the type being asserted |
| 497 | return v.searchMap(next, path[1:]) |
| 498 | default: |
| 499 | // got a value but nested key expected, return "nil" for not found |
| 500 | return nil |
| 501 | } |
| 502 | } |
| 503 | return nil |
| 504 | } |
| 505 | |
| 506 | // searchIndexableWithPathPrefixes recursively searches for a value for path in source map/slice. |
| 507 | // |
no outgoing calls
no test coverage detected