Этот коммит содержится в:
Christopher Speller
2018-01-29 14:17:40 -08:00
коммит произвёл GitHub
родитель 8d66523ba7
Коммит 961c04cae9
1508 изменённых файлов: 182081 добавлений и 24688 удалений

11
vendor/github.com/mitchellh/mapstructure/.travis.yml сгенерированный поставляемый
Просмотреть файл

@@ -1,7 +1,8 @@
language: go
language: go
go:
- 1.9.x
- tip
go:
- 1.8.1
script:
- go test
- go test

2
vendor/github.com/mitchellh/mapstructure/README.md сгенерированный поставляемый
Просмотреть файл

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

89
vendor/github.com/mitchellh/mapstructure/mapstructure.go сгенерированный поставляемый
Просмотреть файл

@@ -237,6 +237,8 @@ func (d *Decoder) decode(name string, data interface{}, val reflect.Value) error
err = d.decodePtr(name, data, val)
case reflect.Slice:
err = d.decodeSlice(name, data, val)
case reflect.Array:
err = d.decodeArray(name, data, val)
case reflect.Func:
err = d.decodeFunc(name, data, val)
default:
@@ -292,12 +294,22 @@ func (d *Decoder) decodeString(name string, data interface{}, val reflect.Value)
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
case dataKind == reflect.Float32 && d.config.WeaklyTypedInput:
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()
elemKind := dataType.Elem().Kind()
switch {
case elemKind == reflect.Uint8:
val.SetString(string(dataVal.Interface().([]uint8)))
switch elemKind {
case reflect.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:
converted = false
}
@@ -647,6 +659,73 @@ func (d *Decoder) decodeSlice(name string, data interface{}, val reflect.Value)
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 {
dataVal := reflect.Indirect(reflect.ValueOf(data))
@@ -716,7 +795,7 @@ func (d *Decoder) decodeStruct(name string, data interface{}, val reflect.Value)
errors = appendErrors(errors,
fmt.Errorf("%s: unsupported type for squash: %s", fieldType.Name, fieldKind))
} else {
structs = append(structs, val.FieldByName(fieldType.Name))
structs = append(structs, structVal.FieldByName(fieldType.Name))
}
continue
}

18
vendor/github.com/mitchellh/mapstructure/mapstructure_bugs_test.go сгенерированный поставляемый
Просмотреть файл

@@ -258,3 +258,21 @@ func TestDecodeSliceToEmptySliceWOZeroing(t *testing.T) {
}
}
}
// #70
func TestNextSquashMapstructure(t *testing.T) {
data := &struct {
Level1 struct {
Level2 struct {
Foo string
} `mapstructure:",squash"`
} `mapstructure:",squash"`
}{}
err := Decode(map[interface{}]interface{}{"foo": "baz"}, &data)
if err != nil {
t.Fatalf("should not error: %s", err)
}
if data.Level1.Level2.Foo != "baz" {
t.Fatal("value should be baz")
}
}

175
vendor/github.com/mitchellh/mapstructure/mapstructure_test.go сгенерированный поставляемый
Просмотреть файл

@@ -49,6 +49,13 @@ type EmbeddedSlice struct {
Vunique string
}
type ArrayAlias [2]string
type EmbeddedArray struct {
ArrayAlias `mapstructure:"array_alias"`
Vunique string
}
type SquashOnNonStructType struct {
InvalidSquashType int `mapstructure:",squash"`
}
@@ -85,6 +92,15 @@ type SliceOfStruct struct {
Value []Basic
}
type Array struct {
Vfoo string
Vbar [2]string
}
type ArrayOfStruct struct {
Value [2]Basic
}
type Func struct {
Foo func() string
}
@@ -112,14 +128,19 @@ type TypeConversionResult struct {
FloatToBool bool
FloatToString string
SliceUint8ToString string
ArrayUint8ToString string
StringToInt int
StringToUint uint
StringToBool bool
StringToFloat float32
StringToStrSlice []string
StringToIntSlice []int
StringToStrArray [1]string
StringToIntArray [1]int
SliceToMap map[string]interface{}
MapToSlice []interface{}
ArrayToMap map[string]interface{}
MapToArray [1]interface{}
}
func TestBasicTypes(t *testing.T) {
@@ -322,6 +343,29 @@ func TestDecode_EmbeddedSlice(t *testing.T) {
}
}
func TestDecode_EmbeddedArray(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"array_alias": [2]string{"foo", "bar"},
"vunique": "bar",
}
var result EmbeddedArray
err := Decode(input, &result)
if err != nil {
t.Fatalf("got an err: %s", err.Error())
}
if !reflect.DeepEqual(result.ArrayAlias, ArrayAlias([2]string{"foo", "bar"})) {
t.Errorf("array value: %#v", result.ArrayAlias)
}
if result.Vunique != "bar" {
t.Errorf("vunique value should be 'bar': %#v", result.Vunique)
}
}
func TestDecode_EmbeddedSquash(t *testing.T) {
t.Parallel()
@@ -582,14 +626,19 @@ func TestDecode_TypeConversion(t *testing.T) {
"FloatToBool": 42.42,
"FloatToString": 42.42,
"SliceUint8ToString": []uint8("foo"),
"ArrayUint8ToString": [3]uint8{'f', 'o', 'o'},
"StringToInt": "42",
"StringToUint": "42",
"StringToBool": "1",
"StringToFloat": "42.42",
"StringToStrSlice": "A",
"StringToIntSlice": "42",
"StringToStrArray": "A",
"StringToIntArray": "42",
"SliceToMap": []interface{}{},
"MapToSlice": map[string]interface{}{},
"ArrayToMap": []interface{}{},
"MapToArray": map[string]interface{}{},
}
expectedResultStrict := TypeConversionResult{
@@ -622,14 +671,19 @@ func TestDecode_TypeConversion(t *testing.T) {
FloatToBool: true,
FloatToString: "42.42",
SliceUint8ToString: "foo",
ArrayUint8ToString: "foo",
StringToInt: 42,
StringToUint: 42,
StringToBool: true,
StringToFloat: 42.42,
StringToStrSlice: []string{"A"},
StringToIntSlice: []int{42},
StringToStrArray: [1]string{"A"},
StringToIntArray: [1]int{42},
SliceToMap: map[string]interface{}{},
MapToSlice: []interface{}{},
ArrayToMap: map[string]interface{}{},
MapToArray: [1]interface{}{},
}
// Test strict type conversion
@@ -965,6 +1019,99 @@ func TestSliceToMap(t *testing.T) {
}
}
func TestArray(t *testing.T) {
t.Parallel()
inputStringArray := map[string]interface{}{
"vfoo": "foo",
"vbar": [2]string{"foo", "bar"},
}
inputStringArrayPointer := map[string]interface{}{
"vfoo": "foo",
"vbar": &[2]string{"foo", "bar"},
}
outputStringArray := &Array{
"foo",
[2]string{"foo", "bar"},
}
testArrayInput(t, inputStringArray, outputStringArray)
testArrayInput(t, inputStringArrayPointer, outputStringArray)
}
func TestInvalidArray(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"vfoo": "foo",
"vbar": 42,
}
result := Array{}
err := Decode(input, &result)
if err == nil {
t.Errorf("expected failure")
}
}
func TestArrayOfStruct(t *testing.T) {
t.Parallel()
input := map[string]interface{}{
"value": []map[string]interface{}{
{"vstring": "one"},
{"vstring": "two"},
},
}
var result ArrayOfStruct
err := Decode(input, &result)
if err != nil {
t.Fatalf("got unexpected error: %s", err)
}
if len(result.Value) != 2 {
t.Fatalf("expected two values, got %d", len(result.Value))
}
if result.Value[0].Vstring != "one" {
t.Errorf("first value should be 'one', got: %s", result.Value[0].Vstring)
}
if result.Value[1].Vstring != "two" {
t.Errorf("second value should be 'two', got: %s", result.Value[1].Vstring)
}
}
func TestArrayToMap(t *testing.T) {
t.Parallel()
input := []map[string]interface{}{
{
"foo": "bar",
},
{
"bar": "baz",
},
}
var result map[string]interface{}
err := WeakDecode(input, &result)
if err != nil {
t.Fatalf("got an error: %s", err)
}
expected := map[string]interface{}{
"foo": "bar",
"bar": "baz",
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("bad: %#v", result)
}
}
func TestInvalidType(t *testing.T) {
t.Parallel()
@@ -1191,3 +1338,31 @@ func testSliceInput(t *testing.T, input map[string]interface{}, expected *Slice)
}
}
}
func testArrayInput(t *testing.T, input map[string]interface{}, expected *Array) {
var result Array
err := Decode(input, &result)
if err != nil {
t.Fatalf("got error: %s", err)
}
if result.Vfoo != expected.Vfoo {
t.Errorf("Vfoo expected '%s', got '%s'", expected.Vfoo, result.Vfoo)
}
if result.Vbar == [2]string{} {
t.Fatalf("Vbar a slice, got '%#v'", result.Vbar)
}
if len(result.Vbar) != len(expected.Vbar) {
t.Errorf("Vbar length should be %d, got %d", len(expected.Vbar), len(result.Vbar))
}
for i, v := range result.Vbar {
if v != expected.Vbar[i] {
t.Errorf(
"Vbar[%d] should be '%#v', got '%#v'",
i, expected.Vbar[i], v)
}
}
}