Skip to content

Commit

Permalink
Add documentation about unmarshaling into embedded structs
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Jan 8, 2020
1 parent eabbc68 commit 06ab5a4
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down

0 comments on commit 06ab5a4

Please sign in to comment.