From 06ab5a4b62072ad7af49cf178f79d83aaf3e9d92 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Wed, 8 Jan 2020 10:55:18 +0100 Subject: [PATCH] Add documentation about unmarshaling into embedded structs --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index 327308b..2bb54fa 100644 --- a/README.md +++ b/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