| 1 |
// Package confmap implements a koanf.Provider that takes nested |
| 2 |
// and flat map[string]any config maps and provides them |
| 3 |
// to koanf. |
| 4 |
package confmap |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
|
| 9 |
"github.com/knadh/koanf/maps" |
| 10 |
) |
| 11 |
|
| 12 |
// Confmap implements a raw map[string]any provider. |
| 13 |
type Confmap struct { |
| 14 |
mp map[string]any |
| 15 |
} |
| 16 |
|
| 17 |
// Provider returns a confmap Provider that takes a flat or nested |
| 18 |
// map[string]any. If a delim is provided, it indicates that the |
| 19 |
// keys are flat and the map needs to be unflattened by delim. |
| 20 |
func Provider(mp map[string]any, delim string) *Confmap { |
| 21 |
cp := maps.Copy(mp) |
| 22 |
maps.IntfaceKeysToStrings(cp) |
| 23 |
if delim != "" { |
| 24 |
cp = maps.Unflatten(cp, delim) |
| 25 |
} |
| 26 |
return &Confmap{mp: cp} |
| 27 |
} |
| 28 |
|
| 29 |
// ReadBytes is not supported by the confmap provider. |
| 30 |
func (e *Confmap) ReadBytes() ([]byte, error) { |
| 31 |
return nil, errors.New("confmap provider does not support this method") |
| 32 |
} |
| 33 |
|
| 34 |
// Read returns the loaded map[string]any. |
| 35 |
func (e *Confmap) Read() (map[string]any, error) { |
| 36 |
return e.mp, nil |
| 37 |
} |