json¶
The json driver reads values from JSON files.
Keys¶
The json tag is a key in the file. Tags of nested structs are keys of nested objects:
type Config struct {
Name string `json:"name"`
HTTP struct {
Host string `json:"host"`
Port int `json:"port"`
} `json:"http"`
Hosts []string `json:"hosts"`
}
Options after the comma are ignored: json:"port,omitempty" is the port key. Set the json tag on all parents of the field: without the tag of the parent the field is read from the root of the file, and the manager logs a warning. Embedded structs without the tag are the exception, their fields are on the level of the embedding struct.
Options¶
The options are the same as in the yaml driver:
jsonDriver, err := json.New("config.json",
json.WithFiles("config.local.json"),
json.WithOptional(),
json.WithEnvExpansion(),
)
| Option | Description |
|---|---|
WithFiles(files...) |
additional files from the OS file system |
WithFS(fsys, files...) |
files from any fs.FS, embed.FS for example |
WithData(name, data) |
in-memory content, the name is used in error messages |
WithSource(source) |
content from a remote storage: consul.Document |
WithOptional() |
files can be absent |
WithEnvExpansion() |
replaces ${VAR} and ${VAR:-default} in the content with environment variables |
WithWatchInterval(interval) |
interval of polling the files for reloading, 5 seconds by default; changes are detected by the events of the file system without waiting for it |
Several files are merged in the order of addition, objects are merged by keys. Files are read once per Parse call.
Documentation¶
GenDoc("json") generates a JSON document with all fields and their current values:
JSON has no comments, so the doc tags are not used. Values of the fields with hidden:"true" are replaced with empty strings.