fix(ios): mapstructure for sound field. (#385)

This commit is contained in:
Bo-Yi Wu 2018-11-20 17:01:35 +08:00 committed by GitHub
parent 375eec1d90
commit a7a451e363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 558 additions and 105 deletions

2
go.mod
View File

@ -30,7 +30,7 @@ require (
github.com/magiconair/properties v0.0.0-20170902060319-8d7837e64d3c // indirect github.com/magiconair/properties v0.0.0-20170902060319-8d7837e64d3c // indirect
github.com/mattn/go-isatty v0.0.0-20170216235908-dda3de49cbfc github.com/mattn/go-isatty v0.0.0-20170216235908-dda3de49cbfc
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e // indirect github.com/mitchellh/mapstructure v1.1.2
github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2 // indirect github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2 // indirect
github.com/pkg/errors v0.0.0-20171216070316-e881fd58d78e // indirect github.com/pkg/errors v0.0.0-20171216070316-e881fd58d78e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect

2
go.sum
View File

@ -58,6 +58,8 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e h1:PtGHLB3CX3TFPcksODQMxncoeQKWwCgTg0bJ40VLJP4= github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e h1:PtGHLB3CX3TFPcksODQMxncoeQKWwCgTg0bJ40VLJP4=
github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v0.0.0-20171017171808-06020f85339e/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2 h1:RTx6HBVnEPvm6X4nj0Tw4ebgCq6XgwnKCvsqbKGD4XQ= github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2 h1:RTx6HBVnEPvm6X4nj0Tw4ebgCq6XgwnKCvsqbKGD4XQ=
github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v0.0.0-20171022022338-8c31c2ec65b2/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.0.0-20171216070316-e881fd58d78e h1:+RHxT/gm0O3UF7nLJbdNzAmULvCFt4XfXHWzh3XI/zs= github.com/pkg/errors v0.0.0-20171216070316-e881fd58d78e h1:+RHxT/gm0O3UF7nLJbdNzAmULvCFt4XfXHWzh3XI/zs=

View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/mitchellh/mapstructure"
"github.com/sideshow/apns2" "github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate" "github.com/sideshow/apns2/certificate"
"github.com/sideshow/apns2/payload" "github.com/sideshow/apns2/payload"
@ -197,7 +198,13 @@ func GetIOSNotification(req PushNotification) *apns2.Notification {
payload.MutableContent() payload.MutableContent()
} }
if _, ok := req.Sound.(Sound); ok { switch req.Sound.(type) {
// from http request binding
case map[string]interface{}:
result := &Sound{}
_ = mapstructure.Decode(req.Sound, &result)
payload.Sound(result)
case Sound:
payload.Sound(&req.Sound) payload.Sound(&req.Sound)
} }

View File

@ -126,12 +126,10 @@ func TestIOSSoundAndVolume(t *testing.T) {
Priority: "normal", Priority: "normal",
Message: message, Message: message,
Sound: Sound{ Sound: Sound{
Critical: 2, Critical: 3,
Name: test, Name: test,
Volume: 1.0, Volume: 4.5,
}, },
SoundName: "foo",
SoundVolume: 2.0,
} }
notification := GetIOSNotification(req) notification := GetIOSNotification(req)
@ -152,9 +150,53 @@ func TestIOSSoundAndVolume(t *testing.T) {
assert.Equal(t, test, notification.Topic) assert.Equal(t, test, notification.Topic)
assert.Equal(t, ApnsPriorityLow, notification.Priority) assert.Equal(t, ApnsPriorityLow, notification.Priority)
assert.Equal(t, message, alert) assert.Equal(t, message, alert)
assert.Equal(t, "foo", soundName) assert.Equal(t, test, soundName)
assert.Equal(t, 2.0, soundVolume) assert.Equal(t, 4.5, soundVolume)
assert.Equal(t, int64(3), soundCritical)
req.SoundName = "foobar"
req.SoundVolume = 5.5
notification = GetIOSNotification(req)
dump, _ = json.Marshal(notification.Payload)
data = []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
panic(err)
}
soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
assert.Equal(t, 5.5, soundVolume)
assert.Equal(t, int64(1), soundCritical) assert.Equal(t, int64(1), soundCritical)
assert.Equal(t, "foobar", soundName)
req = PushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
Message: message,
Sound: map[string]interface{}{
"critical": 3,
"name": "test",
"volume": 4.5,
},
}
notification = GetIOSNotification(req)
dump, _ = json.Marshal(notification.Payload)
data = []byte(string(dump))
if err := json.Unmarshal(data, &dat); err != nil {
panic(err)
}
soundName, _ = jsonparser.GetString(data, "aps", "sound", "name")
soundVolume, _ = jsonparser.GetFloat(data, "aps", "sound", "volume")
soundCritical, _ = jsonparser.GetInt(data, "aps", "sound", "critical")
assert.Equal(t, 4.5, soundVolume)
assert.Equal(t, int64(3), soundCritical)
assert.Equal(t, "test", soundName)
} }
func TestIOSSummaryArg(t *testing.T) { func TestIOSSummaryArg(t *testing.T) {

21
vendor/github.com/mitchellh/mapstructure/CHANGELOG.md generated vendored Normal file
View File

@ -0,0 +1,21 @@
## 1.1.2
* Fix error when decode hook decodes interface implementation into interface
type. [GH-140]
## 1.1.1
* Fix panic that can happen in `decodePtr`
## 1.1.0
* Added `StringToIPHookFunc` to convert `string` to `net.IP` and `net.IPNet` [GH-133]
* Support struct to struct decoding [GH-137]
* If source map value is nil, then destination map value is nil (instead of empty)
* If source slice value is nil, then destination slice value is nil (instead of empty)
* If source pointer is nil, then destination pointer is set to nil (instead of
allocated zero value of type)
## 1.0.0
* Initial tagged stable release.

View File

@ -1,4 +1,4 @@
# mapstructure # mapstructure [![Godoc](https://godoc.org/github.com/mitchellh/mapstructure?status.svg)](https://godoc.org/github.com/mitchellh/mapstructure)
mapstructure is a Go library for decoding generic map values to structures mapstructure is a Go library for decoding generic map values to structures
and vice versa, while providing helpful error handling. and vice versa, while providing helpful error handling.

View File

@ -2,6 +2,8 @@ package mapstructure
import ( import (
"errors" "errors"
"fmt"
"net"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@ -115,6 +117,69 @@ func StringToTimeDurationHookFunc() DecodeHookFunc {
} }
} }
// StringToIPHookFunc returns a DecodeHookFunc that converts
// strings to net.IP
func StringToIPHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(net.IP{}) {
return data, nil
}
// Convert it by parsing
ip := net.ParseIP(data.(string))
if ip == nil {
return net.IP{}, fmt.Errorf("failed parsing ip %v", data)
}
return ip, nil
}
}
// StringToIPNetHookFunc returns a DecodeHookFunc that converts
// strings to net.IPNet
func StringToIPNetHookFunc() DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(net.IPNet{}) {
return data, nil
}
// Convert it by parsing
_, net, err := net.ParseCIDR(data.(string))
return net, err
}
}
// StringToTimeHookFunc returns a DecodeHookFunc that converts
// strings to time.Time.
func StringToTimeHookFunc(layout string) DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{}) (interface{}, error) {
if f.Kind() != reflect.String {
return data, nil
}
if t != reflect.TypeOf(time.Time{}) {
return data, nil
}
// Convert it by parsing
return time.Parse(layout, data.(string))
}
}
// WeaklyTypedHook is a DecodeHookFunc which adds support for weak typing to // WeaklyTypedHook is a DecodeHookFunc which adds support for weak typing to
// the decoder. // the decoder.
// //

1
vendor/github.com/mitchellh/mapstructure/go.mod generated vendored Normal file
View File

@ -0,0 +1 @@
module github.com/mitchellh/mapstructure

View File

@ -114,12 +114,12 @@ type Metadata struct {
Unused []string Unused []string
} }
// Decode takes a map and uses reflection to convert it into the // Decode takes an input structure and uses reflection to translate it to
// given Go native structure. val must be a pointer to a struct. // the output structure. output must be a pointer to a map or struct.
func Decode(m interface{}, rawVal interface{}) error { func Decode(input interface{}, output interface{}) error {
config := &DecoderConfig{ config := &DecoderConfig{
Metadata: nil, Metadata: nil,
Result: rawVal, Result: output,
} }
decoder, err := NewDecoder(config) decoder, err := NewDecoder(config)
@ -127,7 +127,7 @@ func Decode(m interface{}, rawVal interface{}) error {
return err return err
} }
return decoder.Decode(m) return decoder.Decode(input)
} }
// WeakDecode is the same as Decode but is shorthand to enable // WeakDecode is the same as Decode but is shorthand to enable
@ -147,6 +147,40 @@ func WeakDecode(input, output interface{}) error {
return decoder.Decode(input) return decoder.Decode(input)
} }
// DecodeMetadata is the same as Decode, but is shorthand to
// enable metadata collection. See DecoderConfig for more info.
func DecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
config := &DecoderConfig{
Metadata: metadata,
Result: output,
}
decoder, err := NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}
// WeakDecodeMetadata is the same as Decode, but is shorthand to
// enable both WeaklyTypedInput and metadata collection. See
// DecoderConfig for more info.
func WeakDecodeMetadata(input interface{}, output interface{}, metadata *Metadata) error {
config := &DecoderConfig{
Metadata: metadata,
Result: output,
WeaklyTypedInput: true,
}
decoder, err := NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(input)
}
// NewDecoder returns a new decoder for the given configuration. Once // NewDecoder returns a new decoder for the given configuration. Once
// a decoder has been returned, the same configuration must not be used // a decoder has been returned, the same configuration must not be used
// again. // again.
@ -184,68 +218,91 @@ func NewDecoder(config *DecoderConfig) (*Decoder, error) {
// Decode decodes the given raw interface to the target pointer specified // Decode decodes the given raw interface to the target pointer specified
// by the configuration. // by the configuration.
func (d *Decoder) Decode(raw interface{}) error { func (d *Decoder) Decode(input interface{}) error {
return d.decode("", raw, reflect.ValueOf(d.config.Result).Elem()) return d.decode("", input, reflect.ValueOf(d.config.Result).Elem())
} }
// Decodes an unknown data type into a specific reflection value. // Decodes an unknown data type into a specific reflection value.
func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decode(name string, input interface{}, outVal reflect.Value) error {
if data == nil { var inputVal reflect.Value
// If the data is nil, then we don't set anything. if input != nil {
inputVal = reflect.ValueOf(input)
// We need to check here if input is a typed nil. Typed nils won't
// match the "input == nil" below so we check that here.
if inputVal.Kind() == reflect.Ptr && inputVal.IsNil() {
input = nil
}
}
if input == nil {
// If the data is nil, then we don't set anything, unless ZeroFields is set
// to true.
if d.config.ZeroFields {
outVal.Set(reflect.Zero(outVal.Type()))
if d.config.Metadata != nil && name != "" {
d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
}
}
return nil return nil
} }
dataVal := reflect.ValueOf(data) if !inputVal.IsValid() {
if !dataVal.IsValid() { // If the input value is invalid, then we just set the value
// If the data value is invalid, then we just set the value
// to be the zero value. // to be the zero value.
val.Set(reflect.Zero(val.Type())) outVal.Set(reflect.Zero(outVal.Type()))
if d.config.Metadata != nil && name != "" {
d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
}
return nil return nil
} }
if d.config.DecodeHook != nil { if d.config.DecodeHook != nil {
// We have a DecodeHook, so let's pre-process the data. // We have a DecodeHook, so let's pre-process the input.
var err error var err error
data, err = DecodeHookExec( input, err = DecodeHookExec(
d.config.DecodeHook, d.config.DecodeHook,
dataVal.Type(), val.Type(), data) inputVal.Type(), outVal.Type(), input)
if err != nil { if err != nil {
return fmt.Errorf("error decoding '%s': %s", name, err) return fmt.Errorf("error decoding '%s': %s", name, err)
} }
} }
var err error var err error
dataKind := getKind(val) outputKind := getKind(outVal)
switch dataKind { switch outputKind {
case reflect.Bool: case reflect.Bool:
err = d.decodeBool(name, data, val) err = d.decodeBool(name, input, outVal)
case reflect.Interface: case reflect.Interface:
err = d.decodeBasic(name, data, val) err = d.decodeBasic(name, input, outVal)
case reflect.String: case reflect.String:
err = d.decodeString(name, data, val) err = d.decodeString(name, input, outVal)
case reflect.Int: case reflect.Int:
err = d.decodeInt(name, data, val) err = d.decodeInt(name, input, outVal)
case reflect.Uint: case reflect.Uint:
err = d.decodeUint(name, data, val) err = d.decodeUint(name, input, outVal)
case reflect.Float32: case reflect.Float32:
err = d.decodeFloat(name, data, val) err = d.decodeFloat(name, input, outVal)
case reflect.Struct: case reflect.Struct:
err = d.decodeStruct(name, data, val) err = d.decodeStruct(name, input, outVal)
case reflect.Map: case reflect.Map:
err = d.decodeMap(name, data, val) err = d.decodeMap(name, input, outVal)
case reflect.Ptr: case reflect.Ptr:
err = d.decodePtr(name, data, val) err = d.decodePtr(name, input, outVal)
case reflect.Slice: case reflect.Slice:
err = d.decodeSlice(name, data, val) err = d.decodeSlice(name, input, outVal)
case reflect.Array:
err = d.decodeArray(name, input, outVal)
case reflect.Func: case reflect.Func:
err = d.decodeFunc(name, data, val) err = d.decodeFunc(name, input, outVal)
default: default:
// If we reached this point then we weren't able to decode it // If we reached this point then we weren't able to decode it
return fmt.Errorf("%s: unsupported type: %s", name, dataKind) return fmt.Errorf("%s: unsupported type: %s", name, outputKind)
} }
// If we reached here, then we successfully decoded SOMETHING, so // If we reached here, then we successfully decoded SOMETHING, so
// mark the key as used if we're tracking metadata. // mark the key as used if we're tracking metainput.
if d.config.Metadata != nil && name != "" { if d.config.Metadata != nil && name != "" {
d.config.Metadata.Keys = append(d.config.Metadata.Keys, name) d.config.Metadata.Keys = append(d.config.Metadata.Keys, name)
} }
@ -256,7 +313,19 @@ func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error
// This decodes a basic type (bool, int, string, etc.) and sets the // This decodes a basic type (bool, int, string, etc.) and sets the
// value to "data" of that type. // value to "data" of that type.
func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value) error {
if val.IsValid() && val.Elem().IsValid() {
return d.decode(name, data, val.Elem())
}
dataVal := reflect.ValueOf(data) dataVal := reflect.ValueOf(data)
// If the input data is a pointer, and the assigned type is the dereference
// of that exact pointer, then indirect it so that we can assign it.
// Example: *string to string
if dataVal.Kind() == reflect.Ptr && dataVal.Type().Elem() == val.Type() {
dataVal = reflect.Indirect(dataVal)
}
if !dataVal.IsValid() { if !dataVal.IsValid() {
dataVal = reflect.Zero(val.Type()) dataVal = reflect.Zero(val.Type())
} }
@ -273,7 +342,7 @@ func (d *Decoder) decodeBasic(name string, data interface{}, val reflect.Value)
} }
func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.ValueOf(data) dataVal := reflect.Indirect(reflect.ValueOf(data))
dataKind := getKind(dataVal) dataKind := getKind(dataVal)
converted := true converted := true
@ -292,12 +361,22 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
val.SetString(strconv.FormatUint(dataVal.Uint(), 10)) val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
case dataKind == reflect.Float32 && d.config.WeaklyTypedInput: case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64)) val.SetString(strconv.FormatFloat(dataVal.Float(), 'f', -1, 64))
case dataKind == reflect.Slice && d.config.WeaklyTypedInput: case dataKind == reflect.Slice && d.config.WeaklyTypedInput,
dataKind == reflect.Array && d.config.WeaklyTypedInput:
dataType := dataVal.Type() dataType := dataVal.Type()
elemKind := dataType.Elem().Kind() elemKind := dataType.Elem().Kind()
switch { switch elemKind {
case elemKind == reflect.Uint8: case reflect.Uint8:
val.SetString(string(dataVal.Interface().([]uint8))) var uints []uint8
if dataKind == reflect.Array {
uints = make([]uint8, dataVal.Len(), dataVal.Len())
for i := range uints {
uints[i] = dataVal.Index(i).Interface().(uint8)
}
} else {
uints = dataVal.Interface().([]uint8)
}
val.SetString(string(uints))
default: default:
converted = false converted = false
} }
@ -315,7 +394,7 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
} }
func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.ValueOf(data) dataVal := reflect.Indirect(reflect.ValueOf(data))
dataKind := getKind(dataVal) dataKind := getKind(dataVal)
dataType := dataVal.Type() dataType := dataVal.Type()
@ -357,7 +436,7 @@ func (d *Decoder) decodeInt(name string, data interface{}, val reflect.Value) er
} }
func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.ValueOf(data) dataVal := reflect.Indirect(reflect.ValueOf(data))
dataKind := getKind(dataVal) dataKind := getKind(dataVal)
switch { switch {
@ -400,7 +479,7 @@ func (d *Decoder) decodeUint(name string, data interface{}, val reflect.Value) e
} }
func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.ValueOf(data) dataVal := reflect.Indirect(reflect.ValueOf(data))
dataKind := getKind(dataVal) dataKind := getKind(dataVal)
switch { switch {
@ -431,7 +510,7 @@ func (d *Decoder) decodeBool(name string, data interface{}, val reflect.Value) e
} }
func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeFloat(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.ValueOf(data) dataVal := reflect.Indirect(reflect.ValueOf(data))
dataKind := getKind(dataVal) dataKind := getKind(dataVal)
dataType := dataVal.Type() dataType := dataVal.Type()
@ -487,13 +566,28 @@ func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) er
valMap = reflect.MakeMap(mapType) valMap = reflect.MakeMap(mapType)
} }
// Check input type // Check input type and based on the input type jump to the proper func
dataVal := reflect.Indirect(reflect.ValueOf(data)) dataVal := reflect.Indirect(reflect.ValueOf(data))
if dataVal.Kind() != reflect.Map {
// In weak mode, we accept a slice of maps as an input...
if d.config.WeaklyTypedInput {
switch dataVal.Kind() { switch dataVal.Kind() {
case reflect.Map:
return d.decodeMapFromMap(name, dataVal, val, valMap)
case reflect.Struct:
return d.decodeMapFromStruct(name, dataVal, val, valMap)
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
if d.config.WeaklyTypedInput {
return d.decodeMapFromSlice(name, dataVal, val, valMap)
}
fallthrough
default:
return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
}
}
func (d *Decoder) decodeMapFromSlice(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
// Special case for BC reasons (covered by tests) // Special case for BC reasons (covered by tests)
if dataVal.Len() == 0 { if dataVal.Len() == 0 {
val.Set(valMap) val.Set(valMap)
@ -510,15 +604,30 @@ func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) er
} }
return nil return nil
} }
}
return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind()) func (d *Decoder) decodeMapFromMap(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
} valType := val.Type()
valKeyType := valType.Key()
valElemType := valType.Elem()
// Accumulate errors // Accumulate errors
errors := make([]string, 0) errors := make([]string, 0)
// If the input data is empty, then we just match what the input data is.
if dataVal.Len() == 0 {
if dataVal.IsNil() {
if !val.IsNil() {
val.Set(dataVal)
}
} else {
// Set to empty allocated value
val.Set(valMap)
}
return nil
}
for _, k := range dataVal.MapKeys() { for _, k := range dataVal.MapKeys() {
fieldName := fmt.Sprintf("%s[%s]", name, k) fieldName := fmt.Sprintf("%s[%s]", name, k)
@ -551,12 +660,113 @@ func (d *Decoder) decodeMap(name string, data interface{}, val reflect.Value) er
return nil return nil
} }
func (d *Decoder) decodeMapFromStruct(name string, dataVal reflect.Value, val reflect.Value, valMap reflect.Value) error {
typ := dataVal.Type()
for i := 0; i < typ.NumField(); i++ {
// Get the StructField first since this is a cheap operation. If the
// field is unexported, then ignore it.
f := typ.Field(i)
if f.PkgPath != "" {
continue
}
// Next get the actual value of this field and verify it is assignable
// to the map value.
v := dataVal.Field(i)
if !v.Type().AssignableTo(valMap.Type().Elem()) {
return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", v.Type(), valMap.Type().Elem())
}
tagValue := f.Tag.Get(d.config.TagName)
tagParts := strings.Split(tagValue, ",")
// Determine the name of the key in the map
keyName := f.Name
if tagParts[0] != "" {
if tagParts[0] == "-" {
continue
}
keyName = tagParts[0]
}
// If "squash" is specified in the tag, we squash the field down.
squash := false
for _, tag := range tagParts[1:] {
if tag == "squash" {
squash = true
break
}
}
if squash && v.Kind() != reflect.Struct {
return fmt.Errorf("cannot squash non-struct type '%s'", v.Type())
}
switch v.Kind() {
// this is an embedded struct, so handle it differently
case reflect.Struct:
x := reflect.New(v.Type())
x.Elem().Set(v)
vType := valMap.Type()
vKeyType := vType.Key()
vElemType := vType.Elem()
mType := reflect.MapOf(vKeyType, vElemType)
vMap := reflect.MakeMap(mType)
err := d.decode(keyName, x.Interface(), vMap)
if err != nil {
return err
}
if squash {
for _, k := range vMap.MapKeys() {
valMap.SetMapIndex(k, vMap.MapIndex(k))
}
} else {
valMap.SetMapIndex(reflect.ValueOf(keyName), vMap)
}
default:
valMap.SetMapIndex(reflect.ValueOf(keyName), v)
}
}
if val.CanAddr() {
val.Set(valMap)
}
return nil
}
func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) error {
// If the input data is nil, then we want to just set the output
// pointer to be nil as well.
isNil := data == nil
if !isNil {
switch v := reflect.Indirect(reflect.ValueOf(data)); v.Kind() {
case reflect.Chan,
reflect.Func,
reflect.Interface,
reflect.Map,
reflect.Ptr,
reflect.Slice:
isNil = v.IsNil()
}
}
if isNil {
if !val.IsNil() && val.CanSet() {
nilValue := reflect.New(val.Type()).Elem()
val.Set(nilValue)
}
return nil
}
// Create an element of the concrete (non pointer) type and decode // Create an element of the concrete (non pointer) type and decode
// into that. Then set the value of the pointer to this type. // into that. Then set the value of the pointer to this type.
valType := val.Type() valType := val.Type()
valElemType := valType.Elem() valElemType := valType.Elem()
if val.CanSet() {
realVal := val realVal := val
if realVal.IsNil() || d.config.ZeroFields { if realVal.IsNil() || d.config.ZeroFields {
realVal = reflect.New(valElemType) realVal = reflect.New(valElemType)
@ -567,6 +777,11 @@ func (d *Decoder) decodePtr(name string, data interface{}, val reflect.Value) er
} }
val.Set(realVal) val.Set(realVal)
} else {
if err := d.decode(name, data, reflect.Indirect(val)); err != nil {
return err
}
}
return nil return nil
} }
@ -592,16 +807,23 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
valSlice := val valSlice := val
if valSlice.IsNil() || d.config.ZeroFields { if valSlice.IsNil() || d.config.ZeroFields {
// Check input type
if dataValKind != reflect.Array && dataValKind != reflect.Slice {
if d.config.WeaklyTypedInput { if d.config.WeaklyTypedInput {
switch { switch {
// Slice and array we use the normal logic
case dataValKind == reflect.Slice, dataValKind == reflect.Array:
break
// Empty maps turn into empty slices // Empty maps turn into empty slices
case dataValKind == reflect.Map: case dataValKind == reflect.Map:
if dataVal.Len() == 0 { if dataVal.Len() == 0 {
val.Set(reflect.MakeSlice(sliceType, 0, 0)) val.Set(reflect.MakeSlice(sliceType, 0, 0))
return nil return nil
} }
// Create slice of maps of other sizes
return d.decodeSlice(name, []interface{}{data}, val)
case dataValKind == reflect.String && valElemType.Kind() == reflect.Uint8:
return d.decodeSlice(name, []byte(dataVal.String()), val)
// All other types we try to convert to the slice type // All other types we try to convert to the slice type
// and "lift" it into it. i.e. a string becomes a string slice. // and "lift" it into it. i.e. a string becomes a string slice.
@ -611,11 +833,18 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
} }
} }
// Check input type
if dataValKind != reflect.Array && dataValKind != reflect.Slice {
return fmt.Errorf( return fmt.Errorf(
"'%s': source data must be an array or slice, got %s", name, dataValKind) "'%s': source data must be an array or slice, got %s", name, dataValKind)
} }
// If the input value is empty, then don't allocate since non-nil != nil
if dataVal.Len() == 0 {
return nil
}
// Make a new slice to hold our result, same size as the original data. // Make a new slice to hold our result, same size as the original data.
valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len()) valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
} }
@ -647,6 +876,73 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
return nil return nil
} }
func (d *Decoder) decodeArray(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.Indirect(reflect.ValueOf(data))
dataValKind := dataVal.Kind()
valType := val.Type()
valElemType := valType.Elem()
arrayType := reflect.ArrayOf(valType.Len(), valElemType)
valArray := val
if valArray.Interface() == reflect.Zero(valArray.Type()).Interface() || d.config.ZeroFields {
// Check input type
if dataValKind != reflect.Array && dataValKind != reflect.Slice {
if d.config.WeaklyTypedInput {
switch {
// Empty maps turn into empty arrays
case dataValKind == reflect.Map:
if dataVal.Len() == 0 {
val.Set(reflect.Zero(arrayType))
return nil
}
// All other types we try to convert to the array type
// and "lift" it into it. i.e. a string becomes a string array.
default:
// Just re-try this function with data as a slice.
return d.decodeArray(name, []interface{}{data}, val)
}
}
return fmt.Errorf(
"'%s': source data must be an array or slice, got %s", name, dataValKind)
}
if dataVal.Len() > arrayType.Len() {
return fmt.Errorf(
"'%s': expected source data to have length less or equal to %d, got %d", name, arrayType.Len(), dataVal.Len())
}
// Make a new array to hold our result, same size as the original data.
valArray = reflect.New(arrayType).Elem()
}
// Accumulate any errors
errors := make([]string, 0)
for i := 0; i < dataVal.Len(); i++ {
currentData := dataVal.Index(i).Interface()
currentField := valArray.Index(i)
fieldName := fmt.Sprintf("%s[%d]", name, i)
if err := d.decode(fieldName, currentData, currentField); err != nil {
errors = appendErrors(errors, err)
}
}
// Finally, set the value to the array we built up
val.Set(valArray)
// If there were errors, we return those
if len(errors) > 0 {
return &Error{errors}
}
return nil
}
func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error { func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value) error {
dataVal := reflect.Indirect(reflect.ValueOf(data)) dataVal := reflect.Indirect(reflect.ValueOf(data))
@ -658,10 +954,29 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
} }
dataValKind := dataVal.Kind() dataValKind := dataVal.Kind()
if dataValKind != reflect.Map { switch dataValKind {
return fmt.Errorf("'%s' expected a map, got '%s'", name, dataValKind) case reflect.Map:
return d.decodeStructFromMap(name, dataVal, val)
case reflect.Struct:
// Not the most efficient way to do this but we can optimize later if
// we want to. To convert from struct to struct we go to map first
// as an intermediary.
m := make(map[string]interface{})
mval := reflect.Indirect(reflect.ValueOf(&m))
if err := d.decodeMapFromStruct(name, dataVal, mval, mval); err != nil {
return err
} }
result := d.decodeStructFromMap(name, mval, val)
return result
default:
return fmt.Errorf("'%s' expected a map, got '%s'", name, dataVal.Kind())
}
}
func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) error {
dataValType := dataVal.Type() dataValType := dataVal.Type()
if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface { if kind := dataValType.Key().Kind(); kind != reflect.String && kind != reflect.Interface {
return fmt.Errorf( return fmt.Errorf(
@ -716,7 +1031,7 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
errors = appendErrors(errors, errors = appendErrors(errors,
fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind)) fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
} else { } else {
structs = append(structs, val.FieldByName(fieldType.Name)) structs = append(structs, structVal.FieldByName(fieldType.Name))
} }
continue continue
} }

6
vendor/vendor.json vendored
View File

@ -375,10 +375,10 @@
"revisionTime": "2016-04-24T11:30:07Z" "revisionTime": "2016-04-24T11:30:07Z"
}, },
{ {
"checksumSHA1": "gILp4IL+xwXLH6tJtRLrnZ56F24=", "checksumSHA1": "J+g0oZePWp2zSIISD2dZZKTxmgg=",
"path": "github.com/mitchellh/mapstructure", "path": "github.com/mitchellh/mapstructure",
"revision": "06020f85339e21b2478f756a78e295255ffa4d6a", "revision": "3536a929edddb9a5b34bd6861dc4a9647cb459fe",
"revisionTime": "2017-10-17T17:18:08Z" "revisionTime": "2018-10-05T04:51:35Z"
}, },
{ {
"checksumSHA1": "Uq37ug5c6mLAfigwDMHkvMNT97s=", "checksumSHA1": "Uq37ug5c6mLAfigwDMHkvMNT97s=",