fix golint issues in core/mapping (#497)

master
Kevin Wan 4 years ago committed by GitHub
parent 226513ed60
commit 334ee4213f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,10 +10,12 @@ const jsonTagKey = "json"
var jsonUnmarshaler = NewUnmarshaler(jsonTagKey) var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
// UnmarshalJsonBytes unmarshals content into v.
func UnmarshalJsonBytes(content []byte, v interface{}) error { func UnmarshalJsonBytes(content []byte, v interface{}) error {
return unmarshalJsonBytes(content, v, jsonUnmarshaler) return unmarshalJsonBytes(content, v, jsonUnmarshaler)
} }
// UnmarshalJsonReader unmarshals content from reader into v.
func UnmarshalJsonReader(reader io.Reader, v interface{}) error { func UnmarshalJsonReader(reader io.Reader, v interface{}) error {
return unmarshalJsonReader(reader, v, jsonUnmarshaler) return unmarshalJsonReader(reader, v, jsonUnmarshaler)
} }

@ -485,41 +485,41 @@ func TestUnmarshalBytesMap(t *testing.T) {
func TestUnmarshalBytesMapStruct(t *testing.T) { func TestUnmarshalBytesMapStruct(t *testing.T) {
var c struct { var c struct {
Persons map[string]struct { Persons map[string]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": {"Id": 1, "name": "kevin"}}}`) content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
assert.Equal(t, "kevin", c.Persons["first"].Name) assert.Equal(t, "kevin", c.Persons["first"].Name)
} }
func TestUnmarshalBytesMapStructPtr(t *testing.T) { func TestUnmarshalBytesMapStructPtr(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": {"Id": 1, "name": "kevin"}}}`) content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
assert.Equal(t, "kevin", c.Persons["first"].Name) assert.Equal(t, "kevin", c.Persons["first"].Name)
} }
func TestUnmarshalBytesMapStructMissingPartial(t *testing.T) { func TestUnmarshalBytesMapStructMissingPartial(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string Name string
} }
} }
content := []byte(`{"Persons": {"first": {"Id": 1}}}`) content := []byte(`{"Persons": {"first": {"ID": 1}}}`)
assert.NotNil(t, UnmarshalJsonBytes(content, &c)) assert.NotNil(t, UnmarshalJsonBytes(content, &c))
} }
@ -527,21 +527,21 @@ func TestUnmarshalBytesMapStructMissingPartial(t *testing.T) {
func TestUnmarshalBytesMapStructOptional(t *testing.T) { func TestUnmarshalBytesMapStructOptional(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": {"Id": 1}}}`) content := []byte(`{"Persons": {"first": {"ID": 1}}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
} }
func TestUnmarshalBytesMapEmptyStructSlice(t *testing.T) { func TestUnmarshalBytesMapEmptyStructSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]struct { Persons map[string][]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
@ -555,22 +555,22 @@ func TestUnmarshalBytesMapEmptyStructSlice(t *testing.T) {
func TestUnmarshalBytesMapStructSlice(t *testing.T) { func TestUnmarshalBytesMapStructSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]struct { Persons map[string][]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": [{"Id": 1, "name": "kevin"}]}}`) content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
assert.Equal(t, "kevin", c.Persons["first"][0].Name) assert.Equal(t, "kevin", c.Persons["first"][0].Name)
} }
func TestUnmarshalBytesMapEmptyStructPtrSlice(t *testing.T) { func TestUnmarshalBytesMapEmptyStructPtrSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
@ -584,26 +584,26 @@ func TestUnmarshalBytesMapEmptyStructPtrSlice(t *testing.T) {
func TestUnmarshalBytesMapStructPtrSlice(t *testing.T) { func TestUnmarshalBytesMapStructPtrSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": [{"Id": 1, "name": "kevin"}]}}`) content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
assert.Equal(t, "kevin", c.Persons["first"][0].Name) assert.Equal(t, "kevin", c.Persons["first"][0].Name)
} }
func TestUnmarshalBytesMapStructPtrSliceMissingPartial(t *testing.T) { func TestUnmarshalBytesMapStructPtrSliceMissingPartial(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string Name string
} }
} }
content := []byte(`{"Persons": {"first": [{"Id": 1}]}}`) content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`)
assert.NotNil(t, UnmarshalJsonBytes(content, &c)) assert.NotNil(t, UnmarshalJsonBytes(content, &c))
} }
@ -611,15 +611,15 @@ func TestUnmarshalBytesMapStructPtrSliceMissingPartial(t *testing.T) {
func TestUnmarshalBytesMapStructPtrSliceOptional(t *testing.T) { func TestUnmarshalBytesMapStructPtrSliceOptional(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`{"Persons": {"first": [{"Id": 1}]}}`) content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`)
assert.Nil(t, UnmarshalJsonBytes(content, &c)) assert.Nil(t, UnmarshalJsonBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
} }
func TestUnmarshalStructOptional(t *testing.T) { func TestUnmarshalStructOptional(t *testing.T) {

@ -33,23 +33,27 @@ var (
) )
type ( type (
// A Unmarshaler is used to unmarshal with given tag key.
Unmarshaler struct { Unmarshaler struct {
key string key string
opts unmarshalOptions opts unmarshalOptions
} }
// UnmarshalOption defines the method to customize a Unmarshaler.
UnmarshalOption func(*unmarshalOptions)
unmarshalOptions struct { unmarshalOptions struct {
fromString bool fromString bool
} }
keyCache map[string][]string keyCache map[string][]string
UnmarshalOption func(*unmarshalOptions)
) )
func init() { func init() {
cacheKeys.Store(make(keyCache)) cacheKeys.Store(make(keyCache))
} }
// NewUnmarshaler returns a Unmarshaler.
func NewUnmarshaler(key string, opts ...UnmarshalOption) *Unmarshaler { func NewUnmarshaler(key string, opts ...UnmarshalOption) *Unmarshaler {
unmarshaler := Unmarshaler{ unmarshaler := Unmarshaler{
key: key, key: key,
@ -62,14 +66,17 @@ func NewUnmarshaler(key string, opts ...UnmarshalOption) *Unmarshaler {
return &unmarshaler return &unmarshaler
} }
// UnmarshalKey unmarshals m into v with tag key.
func UnmarshalKey(m map[string]interface{}, v interface{}) error { func UnmarshalKey(m map[string]interface{}, v interface{}) error {
return keyUnmarshaler.Unmarshal(m, v) return keyUnmarshaler.Unmarshal(m, v)
} }
// Unmarshal unmarshals m into v.
func (u *Unmarshaler) Unmarshal(m map[string]interface{}, v interface{}) error { func (u *Unmarshaler) Unmarshal(m map[string]interface{}, v interface{}) error {
return u.UnmarshalValuer(MapValuer(m), v) return u.UnmarshalValuer(MapValuer(m), v)
} }
// UnmarshalValuer unmarshals m into v.
func (u *Unmarshaler) UnmarshalValuer(m Valuer, v interface{}) error { func (u *Unmarshaler) UnmarshalValuer(m Valuer, v interface{}) error {
return u.unmarshalWithFullName(m, v, "") return u.unmarshalWithFullName(m, v, "")
} }
@ -590,6 +597,7 @@ func (u *Unmarshaler) parseOptionsWithContext(field reflect.StructField, m Value
return key, optsWithContext, nil return key, optsWithContext, nil
} }
// WithStringValues customizes a Unmarshaler with number values from strings.
func WithStringValues() UnmarshalOption { func WithStringValues() UnmarshalOption {
return func(opt *unmarshalOptions) { return func(opt *unmarshalOptions) {
opt.fromString = true opt.fromString = true

@ -754,13 +754,13 @@ func TestUnmarshalJsonNumberInt64(t *testing.T) {
strValue := strconv.FormatInt(intValue, 10) strValue := strconv.FormatInt(intValue, 10)
var number = json.Number(strValue) var number = json.Number(strValue)
m := map[string]interface{}{ m := map[string]interface{}{
"Id": number, "ID": number,
} }
var v struct { var v struct {
Id int64 ID int64
} }
assert.Nil(t, UnmarshalKey(m, &v)) assert.Nil(t, UnmarshalKey(m, &v))
assert.Equal(t, intValue, v.Id) assert.Equal(t, intValue, v.ID)
} }
} }
@ -770,13 +770,13 @@ func TestUnmarshalJsonNumberUint64(t *testing.T) {
strValue := strconv.FormatUint(intValue, 10) strValue := strconv.FormatUint(intValue, 10)
var number = json.Number(strValue) var number = json.Number(strValue)
m := map[string]interface{}{ m := map[string]interface{}{
"Id": number, "ID": number,
} }
var v struct { var v struct {
Id uint64 ID uint64
} }
assert.Nil(t, UnmarshalKey(m, &v)) assert.Nil(t, UnmarshalKey(m, &v))
assert.Equal(t, intValue, v.Id) assert.Equal(t, intValue, v.ID)
} }
} }
@ -786,15 +786,15 @@ func TestUnmarshalJsonNumberUint64Ptr(t *testing.T) {
strValue := strconv.FormatUint(intValue, 10) strValue := strconv.FormatUint(intValue, 10)
var number = json.Number(strValue) var number = json.Number(strValue)
m := map[string]interface{}{ m := map[string]interface{}{
"Id": number, "ID": number,
} }
var v struct { var v struct {
Id *uint64 ID *uint64
} }
ast := assert.New(t) ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v)) ast.Nil(UnmarshalKey(m, &v))
ast.NotNil(v.Id) ast.NotNil(v.ID)
ast.Equal(intValue, *v.Id) ast.Equal(intValue, *v.ID)
} }
} }
@ -1061,38 +1061,38 @@ func TestUnmarshalWithOptionsAndSet(t *testing.T) {
func TestUnmarshalNestedKey(t *testing.T) { func TestUnmarshalNestedKey(t *testing.T) {
var c struct { var c struct {
Id int `json:"Persons.first.Id"` ID int `json:"Persons.first.ID"`
} }
m := map[string]interface{}{ m := map[string]interface{}{
"Persons": map[string]interface{}{ "Persons": map[string]interface{}{
"first": map[string]interface{}{ "first": map[string]interface{}{
"Id": 1, "ID": 1,
}, },
}, },
} }
assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c)) assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
assert.Equal(t, 1, c.Id) assert.Equal(t, 1, c.ID)
} }
func TestUnmarhsalNestedKeyArray(t *testing.T) { func TestUnmarhsalNestedKeyArray(t *testing.T) {
var c struct { var c struct {
First []struct { First []struct {
Id int ID int
} `json:"Persons.first"` } `json:"Persons.first"`
} }
m := map[string]interface{}{ m := map[string]interface{}{
"Persons": map[string]interface{}{ "Persons": map[string]interface{}{
"first": []map[string]interface{}{ "first": []map[string]interface{}{
{"Id": 1}, {"ID": 1},
{"Id": 2}, {"ID": 2},
}, },
}, },
} }
assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c)) assert.Nil(t, NewUnmarshaler("json").Unmarshal(m, &c))
assert.Equal(t, 2, len(c.First)) assert.Equal(t, 2, len(c.First))
assert.Equal(t, 1, c.First[0].Id) assert.Equal(t, 1, c.First[0].ID)
} }
func TestUnmarshalAnonymousOptionalRequiredProvided(t *testing.T) { func TestUnmarshalAnonymousOptionalRequiredProvided(t *testing.T) {

@ -45,6 +45,7 @@ type (
} }
) )
// Deref dereferences a type, if pointer type, returns its element type.
func Deref(t reflect.Type) reflect.Type { func Deref(t reflect.Type) reflect.Type {
if t.Kind() == reflect.Ptr { if t.Kind() == reflect.Ptr {
t = t.Elem() t = t.Elem()
@ -53,6 +54,7 @@ func Deref(t reflect.Type) reflect.Type {
return t return t
} }
// Repr returns the string representation of v.
func Repr(v interface{}) string { func Repr(v interface{}) string {
if v == nil { if v == nil {
return "" return ""
@ -72,6 +74,7 @@ func Repr(v interface{}) string {
return reprOfValue(val) return reprOfValue(val)
} }
// ValidatePtr validates v if it's a valid pointer.
func ValidatePtr(v *reflect.Value) error { func ValidatePtr(v *reflect.Value) error {
// sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr, // sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr,
// panic otherwise // panic otherwise

@ -1,13 +1,17 @@
package mapping package mapping
type ( type (
// A Valuer interface defines the way to get values from the underlying object with keys.
Valuer interface { Valuer interface {
// Value gets the value associated with the given key.
Value(key string) (interface{}, bool) Value(key string) (interface{}, bool)
} }
// A MapValuer is a map that can use Value method to get values with given keys.
MapValuer map[string]interface{} MapValuer map[string]interface{}
) )
// Value gets the value associated with the given key from mv.
func (mv MapValuer) Value(key string) (interface{}, bool) { func (mv MapValuer) Value(key string) (interface{}, bool) {
v, ok := mv[key] v, ok := mv[key]
return v, ok return v, ok

@ -13,15 +13,18 @@ import (
const yamlTagKey = "json" const yamlTagKey = "json"
var ( var (
// ErrUnsupportedType is an error that indicates the config format is not supported.
ErrUnsupportedType = errors.New("only map-like configs are suported") ErrUnsupportedType = errors.New("only map-like configs are suported")
yamlUnmarshaler = NewUnmarshaler(yamlTagKey) yamlUnmarshaler = NewUnmarshaler(yamlTagKey)
) )
// UnmarshalYamlBytes unmarshals content into v.
func UnmarshalYamlBytes(content []byte, v interface{}) error { func UnmarshalYamlBytes(content []byte, v interface{}) error {
return unmarshalYamlBytes(content, v, yamlUnmarshaler) return unmarshalYamlBytes(content, v, yamlUnmarshaler)
} }
// UnmarshalYamlReader unmarshals content from reader into v.
func UnmarshalYamlReader(reader io.Reader, v interface{}) error { func UnmarshalYamlReader(reader io.Reader, v interface{}) error {
return unmarshalYamlReader(reader, v, yamlUnmarshaler) return unmarshalYamlReader(reader, v, yamlUnmarshaler)
} }

@ -502,49 +502,49 @@ func TestUnmarshalYamlBytesMap(t *testing.T) {
func TestUnmarshalYamlBytesMapStruct(t *testing.T) { func TestUnmarshalYamlBytesMapStruct(t *testing.T) {
var c struct { var c struct {
Persons map[string]struct { Persons map[string]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
Id: 1 ID: 1
name: kevin`) name: kevin`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
assert.Equal(t, "kevin", c.Persons["first"].Name) assert.Equal(t, "kevin", c.Persons["first"].Name)
} }
func TestUnmarshalYamlBytesMapStructPtr(t *testing.T) { func TestUnmarshalYamlBytesMapStructPtr(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
Id: 1 ID: 1
name: kevin`) name: kevin`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
assert.Equal(t, "kevin", c.Persons["first"].Name) assert.Equal(t, "kevin", c.Persons["first"].Name)
} }
func TestUnmarshalYamlBytesMapStructMissingPartial(t *testing.T) { func TestUnmarshalYamlBytesMapStructMissingPartial(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string Name string
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
Id: 1`) ID: 1`)
assert.NotNil(t, UnmarshalYamlBytes(content, &c)) assert.NotNil(t, UnmarshalYamlBytes(content, &c))
} }
@ -552,41 +552,41 @@ func TestUnmarshalYamlBytesMapStructMissingPartial(t *testing.T) {
func TestUnmarshalYamlBytesMapStructOptional(t *testing.T) { func TestUnmarshalYamlBytesMapStructOptional(t *testing.T) {
var c struct { var c struct {
Persons map[string]*struct { Persons map[string]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
Id: 1`) ID: 1`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"].Id) assert.Equal(t, 1, c.Persons["first"].ID)
} }
func TestUnmarshalYamlBytesMapStructSlice(t *testing.T) { func TestUnmarshalYamlBytesMapStructSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]struct { Persons map[string][]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
- Id: 1 - ID: 1
name: kevin`) name: kevin`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
assert.Equal(t, "kevin", c.Persons["first"][0].Name) assert.Equal(t, "kevin", c.Persons["first"][0].Name)
} }
func TestUnmarshalYamlBytesMapEmptyStructSlice(t *testing.T) { func TestUnmarshalYamlBytesMapEmptyStructSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]struct { Persons map[string][]struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
@ -601,25 +601,25 @@ func TestUnmarshalYamlBytesMapEmptyStructSlice(t *testing.T) {
func TestUnmarshalYamlBytesMapStructPtrSlice(t *testing.T) { func TestUnmarshalYamlBytesMapStructPtrSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
- Id: 1 - ID: 1
name: kevin`) name: kevin`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
assert.Equal(t, "kevin", c.Persons["first"][0].Name) assert.Equal(t, "kevin", c.Persons["first"][0].Name)
} }
func TestUnmarshalYamlBytesMapEmptyStructPtrSlice(t *testing.T) { func TestUnmarshalYamlBytesMapEmptyStructPtrSlice(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
@ -634,13 +634,13 @@ func TestUnmarshalYamlBytesMapEmptyStructPtrSlice(t *testing.T) {
func TestUnmarshalYamlBytesMapStructPtrSliceMissingPartial(t *testing.T) { func TestUnmarshalYamlBytesMapStructPtrSliceMissingPartial(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string Name string
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
- Id: 1`) - ID: 1`)
assert.NotNil(t, UnmarshalYamlBytes(content, &c)) assert.NotNil(t, UnmarshalYamlBytes(content, &c))
} }
@ -648,17 +648,17 @@ func TestUnmarshalYamlBytesMapStructPtrSliceMissingPartial(t *testing.T) {
func TestUnmarshalYamlBytesMapStructPtrSliceOptional(t *testing.T) { func TestUnmarshalYamlBytesMapStructPtrSliceOptional(t *testing.T) {
var c struct { var c struct {
Persons map[string][]*struct { Persons map[string][]*struct {
Id int ID int
Name string `json:"name,optional"` Name string `json:"name,optional"`
} }
} }
content := []byte(`Persons: content := []byte(`Persons:
first: first:
- Id: 1`) - ID: 1`)
assert.Nil(t, UnmarshalYamlBytes(content, &c)) assert.Nil(t, UnmarshalYamlBytes(content, &c))
assert.Equal(t, 1, len(c.Persons)) assert.Equal(t, 1, len(c.Persons))
assert.Equal(t, 1, c.Persons["first"][0].Id) assert.Equal(t, 1, c.Persons["first"][0].ID)
} }
func TestUnmarshalYamlStructOptional(t *testing.T) { func TestUnmarshalYamlStructOptional(t *testing.T) {

Loading…
Cancel
Save