file
API
file
packageAPI reference for the file
package.
Imports
(3)
S
struct
Provider
Provider implements configuration.Provider for JSON files.
pkg/configuration/source/file/provider.go:15-18
type Provider struct
Example
p := file.New("config.json")
data, err := p.Load(ctx)
Methods
Load
Method
Load reads and parses a JSON configuration file, returning a flat key-value map. Nested JSON objects are flattened with colon-separated keys (e.g. "db:host").
Parameters
ctx
context.Context
Returns
map[string]any
error
func (*Provider) Load(ctx context.Context) (map[string]any, error)
{
data, err := os.ReadFile(p.Path)
if err != nil {
return nil, err
}
var parsed map[string]any
if err := json.Unmarshal(data, &parsed); err != nil {
return nil, err
}
flat := make(map[string]any)
flatten("", parsed, flat)
return flat, nil
}
Fields
| Name | Type | Description |
|---|---|---|
| Path | string |
F
function
New
New creates a new JSON file provider.
Parameters
path
string
Returns
pkg/configuration/source/file/provider.go:21-23
func New(path string) *Provider
{
return &Provider{Path: path}
}
F
function
flatten
Parameters
prefix
string
src
map[string]any
dst
map[string]any
pkg/configuration/source/file/provider.go:48-60
func flatten(prefix string, src map[string]any, dst map[string]any)
{
for k, v := range src {
key := k
if prefix != "" {
key = prefix + ":" + k
}
if nested, ok := v.(map[string]any); ok {
flatten(key, nested, dst)
} else {
dst[key] = v
}
}
}