Get returns the raw, uncast any value of a given key path in the config map. If the key path does not exist, nil is returned.
(path string)
| 327 | // Get returns the raw, uncast any value of a given key path |
| 328 | // in the config map. If the key path does not exist, nil is returned. |
| 329 | func (ko *Koanf) Get(path string) any { |
| 330 | // No path. Return the whole conf map. |
| 331 | if path == "" { |
| 332 | return ko.Raw() |
| 333 | } |
| 334 | |
| 335 | // Does the path exist? |
| 336 | ko.mu.RLock() |
| 337 | defer ko.mu.RUnlock() |
| 338 | |
| 339 | p, ok := ko.keyMap[path] |
| 340 | if !ok { |
| 341 | return nil |
| 342 | } |
| 343 | res := maps.Search(ko.confMap, p) |
| 344 | |
| 345 | // Non-reference types are okay to return directly. |
| 346 | // Other types are "copied" with maps.Copy or json.Marshal |
| 347 | // that change the numeric types to float64. |
| 348 | |
| 349 | switch v := res.(type) { |
| 350 | case int, int8, int16, int32, int64, float32, float64, string, bool: |
| 351 | return v |
| 352 | case map[string]any: |
| 353 | return maps.Copy(v) |
| 354 | case nil: |
| 355 | return nil |
| 356 | } |
| 357 | |
| 358 | // Skip nil pointers before copying. |
| 359 | if rv := reflect.ValueOf(res); rv.Kind() == reflect.Ptr && rv.IsNil() { |
| 360 | return res |
| 361 | } |
| 362 | |
| 363 | out, _ := copystructure.Copy(&res) |
| 364 | if ptrOut, ok := out.(*any); ok { |
| 365 | return *ptrOut |
| 366 | } |
| 367 | return out |
| 368 | } |
| 369 | |
| 370 | // Slices returns a list of Koanf instances constructed out of a |
| 371 | // []map[string]any interface at the given path. |