mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Add documentation about unmarshaling into embedded structs
This commit is contained in:
parent
eabbc68a3e
commit
06ab5a4b62
1 changed files with 31 additions and 0 deletions
31
README.md
31
README.md
|
@ -692,6 +692,37 @@ var C config
|
|||
v.Unmarshal(&C)
|
||||
```
|
||||
|
||||
Viper also supports unmarshaling into embedded structs:
|
||||
|
||||
```go
|
||||
/*
|
||||
Example config:
|
||||
|
||||
module:
|
||||
enabled: true
|
||||
token: 89h3f98hbwf987h3f98wenf89ehf
|
||||
*/
|
||||
type config struct {
|
||||
Module struct {
|
||||
Enabled bool
|
||||
|
||||
moduleConfig `mapstructure:",squash"`
|
||||
}
|
||||
}
|
||||
|
||||
// moduleConfig could be in a module specific package
|
||||
type moduleConfig struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
var C config
|
||||
|
||||
err := viper.Unmarshal(&C)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to decode into struct, %v", err)
|
||||
}
|
||||
```
|
||||
|
||||
Viper uses [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure) under the hood for unmarshaling values which uses `mapstructure` tags by default.
|
||||
|
||||
### Marshalling to string
|
||||
|
|
Loading…
Reference in a new issue