Skip to content

Commit

Permalink
Merge pull request #227 from dnephin/document-decode-with-squash
Browse files Browse the repository at this point in the history
Update godoc about DecodeHooks and squash
  • Loading branch information
mitchellh committed Jan 4, 2021
2 parents 95075d6 + 35a4b15 commit 2fb8a82
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions mapstructure.go
Expand Up @@ -72,6 +72,17 @@
// "name": "alice",
// }
//
// When decoding from a struct to a map, the squash tag squashes the struct
// fields into a single map. Using the example structs from above:
//
// Friend{Person: Person{Name: "alice"}}
//
// Will be decoded into a map:
//
// map[string]interface{}{
// "name": "alice",
// }
//
// DecoderConfig has a field that changes the behavior of mapstructure
// to always squash embedded structs.
//
Expand Down Expand Up @@ -161,10 +172,11 @@ import (
// data transformations. See "DecodeHook" in the DecoderConfig
// struct.
//
// The type should be DecodeHookFuncType or DecodeHookFuncKind.
// Either is accepted. Types are a superset of Kinds (Types can return
// Kinds) and are generally a richer thing to use, but Kinds are simpler
// if you only need those.
// The type must be one of DecodeHookFuncType, DecodeHookFuncKind, or
// DecodeHookFuncValue.
// Values are a superset of Types (Values can return types), and Types are a
// superset of Kinds (Types can return Kinds) and are generally a richer thing
// to use, but Kinds are simpler if you only need those.
//
// The reason DecodeHookFunc is multi-typed is for backwards compatibility:
// we started with Kinds and then realized Types were the better solution,
Expand All @@ -189,10 +201,13 @@ type DecodeHookFuncValue func(from reflect.Value, to reflect.Value) (interface{}
type DecoderConfig struct {
// DecodeHook, if set, will be called before any decoding and any
// type conversion (if WeaklyTypedInput is on). This lets you modify
// the values before they're set down onto the resulting struct.
// the values before they're set down onto the resulting struct. The
// DecodeHook is called for every map and value in the input. This means
// that if a struct has embedded fields with squash tags the decode hook
// is called only once with all of the input data, not once for each
// embedded struct.
//
// If an error is returned, the entire decode will fail with that
// error.
// If an error is returned, the entire decode will fail with that error.
DecodeHook DecodeHookFunc

// If ErrorUnused is true, then it is an error for there to exist
Expand Down

0 comments on commit 2fb8a82

Please sign in to comment.