Load takes a Provider that either provides a parsed config map[string]any in which case pa (Parser) can be nil, or raw bytes to be parsed, where a Parser can be provided to parse. Additionally, options can be passed which modify the load behavior, such as passing a custom merge function.
(p Provider, pa Parser, opts ...Option)
| 91 | // can be provided to parse. Additionally, options can be passed which modify the |
| 92 | // load behavior, such as passing a custom merge function. |
| 93 | func (ko *Koanf) Load(p Provider, pa Parser, opts ...Option) error { |
| 94 | var ( |
| 95 | mp map[string]any |
| 96 | err error |
| 97 | ) |
| 98 | |
| 99 | if p == nil { |
| 100 | return fmt.Errorf("load received a nil provider") |
| 101 | } |
| 102 | |
| 103 | // No Parser is given. Call the Provider's Read() method to get |
| 104 | // the config map. |
| 105 | if pa == nil { |
| 106 | mp, err = p.Read() |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | } else { |
| 111 | // There's a Parser. Get raw bytes from the Provider to parse. |
| 112 | b, err := p.ReadBytes() |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | mp, err = pa.Unmarshal(b) |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return ko.merge(mp, newOptions(opts)) |
| 123 | } |
| 124 | |
| 125 | // Keys returns the slice of all flattened keys in the loaded configuration |
| 126 | // sorted alphabetically. |