Depenancy upgrades and movign to dep. (#8630)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
bf24f51c4e
Коммит
6e2cb00008
36
vendor/github.com/gorilla/schema/cache.go
сгенерированный
поставляемый
36
vendor/github.com/gorilla/schema/cache.go
сгенерированный
поставляемый
@@ -63,7 +63,7 @@ func (c *cache) parsePath(p string, t reflect.Type) ([]pathPart, error) {
|
||||
}
|
||||
// Valid field. Append index.
|
||||
path = append(path, field.name)
|
||||
if field.ss {
|
||||
if field.isSliceOfStructs && (!field.unmarshalerInfo.IsValid || (field.unmarshalerInfo.IsValid && field.unmarshalerInfo.IsSliceElement)) {
|
||||
// Parse a special case: slices of structs.
|
||||
// i+1 must be the slice index.
|
||||
//
|
||||
@@ -142,7 +142,7 @@ func (c *cache) create(t reflect.Type, info *structInfo) *structInfo {
|
||||
c.create(ft, info)
|
||||
for _, fi := range info.fields[bef:len(info.fields)] {
|
||||
// exclude required check because duplicated to embedded field
|
||||
fi.required = false
|
||||
fi.isRequired = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,7 @@ func (c *cache) createField(field reflect.StructField, info *structInfo) {
|
||||
// First let's get the basic type.
|
||||
isSlice, isStruct := false, false
|
||||
ft := field.Type
|
||||
m := isTextUnmarshaler(reflect.Zero(ft))
|
||||
if ft.Kind() == reflect.Ptr {
|
||||
ft = ft.Elem()
|
||||
}
|
||||
@@ -185,12 +186,13 @@ func (c *cache) createField(field reflect.StructField, info *structInfo) {
|
||||
}
|
||||
|
||||
info.fields = append(info.fields, &fieldInfo{
|
||||
typ: field.Type,
|
||||
name: field.Name,
|
||||
ss: isSlice && isStruct,
|
||||
alias: alias,
|
||||
anon: field.Anonymous,
|
||||
required: options.Contains("required"),
|
||||
typ: field.Type,
|
||||
name: field.Name,
|
||||
alias: alias,
|
||||
unmarshalerInfo: m,
|
||||
isSliceOfStructs: isSlice && isStruct,
|
||||
isAnonymous: field.Anonymous,
|
||||
isRequired: options.Contains("required"),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -215,12 +217,18 @@ func (i *structInfo) get(alias string) *fieldInfo {
|
||||
}
|
||||
|
||||
type fieldInfo struct {
|
||||
typ reflect.Type
|
||||
name string // field name in the struct.
|
||||
ss bool // true if this is a slice of structs.
|
||||
alias string
|
||||
anon bool // is an embedded field
|
||||
required bool // tag option
|
||||
typ reflect.Type
|
||||
// name is the field name in the struct.
|
||||
name string
|
||||
alias string
|
||||
// unmarshalerInfo contains information regarding the
|
||||
// encoding.TextUnmarshaler implementation of the field type.
|
||||
unmarshalerInfo unmarshaler
|
||||
// isSliceOfStructs indicates if the field type is a slice of structs.
|
||||
isSliceOfStructs bool
|
||||
// isAnonymous indicates whether the field is embedded in the struct.
|
||||
isAnonymous bool
|
||||
isRequired bool
|
||||
}
|
||||
|
||||
type pathPart struct {
|
||||
|
||||
89
vendor/github.com/gorilla/schema/decoder.go
сгенерированный
поставляемый
89
vendor/github.com/gorilla/schema/decoder.go
сгенерированный
поставляемый
@@ -106,7 +106,7 @@ func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string, prefix
|
||||
if f.typ.Kind() == reflect.Struct {
|
||||
err := d.checkRequired(f.typ, src, prefix+f.alias+".")
|
||||
if err != nil {
|
||||
if !f.anon {
|
||||
if !f.isAnonymous {
|
||||
return err
|
||||
}
|
||||
// check embedded parent field.
|
||||
@@ -116,7 +116,7 @@ func (d *Decoder) checkRequired(t reflect.Type, src map[string][]string, prefix
|
||||
}
|
||||
}
|
||||
}
|
||||
if f.required {
|
||||
if f.isRequired {
|
||||
key := f.alias
|
||||
if prefix != "" {
|
||||
key = prefix + key
|
||||
@@ -185,7 +185,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
// Get the converter early in case there is one for a slice type.
|
||||
conv := d.cache.converter(t)
|
||||
m := isTextUnmarshaler(v)
|
||||
if conv == nil && t.Kind() == reflect.Slice && m.IsSlice {
|
||||
if conv == nil && t.Kind() == reflect.Slice && m.IsSliceElement {
|
||||
var items []reflect.Value
|
||||
elemT := t.Elem()
|
||||
isPtrElem := elemT.Kind() == reflect.Ptr
|
||||
@@ -211,7 +211,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
}
|
||||
} else if m.IsValid {
|
||||
u := reflect.New(elemT)
|
||||
if m.IsPtr {
|
||||
if m.IsSliceElementPtr {
|
||||
u = reflect.New(reflect.PtrTo(elemT).Elem())
|
||||
}
|
||||
if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(value)); err != nil {
|
||||
@@ -222,7 +222,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
if m.IsPtr {
|
||||
if m.IsSliceElementPtr {
|
||||
items = append(items, u.Elem().Addr())
|
||||
} else if u.Kind() == reflect.Ptr {
|
||||
items = append(items, u.Elem())
|
||||
@@ -283,11 +283,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
val = values[len(values)-1]
|
||||
}
|
||||
|
||||
if val == "" {
|
||||
if d.zeroEmpty {
|
||||
v.Set(reflect.Zero(t))
|
||||
}
|
||||
} else if conv != nil {
|
||||
if conv != nil {
|
||||
if value := conv(val); value.IsValid() {
|
||||
v.Set(value.Convert(t))
|
||||
} else {
|
||||
@@ -298,15 +294,32 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
}
|
||||
}
|
||||
} else if m.IsValid {
|
||||
// If the value implements the encoding.TextUnmarshaler interface
|
||||
// apply UnmarshalText as the converter
|
||||
if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
|
||||
return ConversionError{
|
||||
Key: path,
|
||||
Type: t,
|
||||
Index: -1,
|
||||
Err: err,
|
||||
if m.IsPtr {
|
||||
u := reflect.New(v.Type())
|
||||
if err := u.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(val)); err != nil {
|
||||
return ConversionError{
|
||||
Key: path,
|
||||
Type: t,
|
||||
Index: -1,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
v.Set(reflect.Indirect(u))
|
||||
} else {
|
||||
// If the value implements the encoding.TextUnmarshaler interface
|
||||
// apply UnmarshalText as the converter
|
||||
if err := m.Unmarshaler.UnmarshalText([]byte(val)); err != nil {
|
||||
return ConversionError{
|
||||
Key: path,
|
||||
Type: t,
|
||||
Index: -1,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if val == "" {
|
||||
if d.zeroEmpty {
|
||||
v.Set(reflect.Zero(t))
|
||||
}
|
||||
} else if conv := builtinConverters[t.Kind()]; conv != nil {
|
||||
if value := conv(val); value.IsValid() {
|
||||
@@ -326,18 +339,18 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
|
||||
}
|
||||
|
||||
func isTextUnmarshaler(v reflect.Value) unmarshaler {
|
||||
|
||||
// Create a new unmarshaller instance
|
||||
m := unmarshaler{}
|
||||
|
||||
// As the UnmarshalText function should be applied
|
||||
// to the pointer of the type, we convert the value to pointer.
|
||||
if v.CanAddr() {
|
||||
v = v.Addr()
|
||||
}
|
||||
if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
|
||||
return m
|
||||
}
|
||||
// As the UnmarshalText function should be applied to the pointer of the
|
||||
// type, we check that type to see if it implements the necessary
|
||||
// method.
|
||||
if m.Unmarshaler, m.IsValid = reflect.New(v.Type()).Interface().(encoding.TextUnmarshaler); m.IsValid {
|
||||
m.IsPtr = true
|
||||
return m
|
||||
}
|
||||
|
||||
// if v is []T or *[]T create new T
|
||||
t := v.Type()
|
||||
@@ -345,12 +358,17 @@ func isTextUnmarshaler(v reflect.Value) unmarshaler {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() == reflect.Slice {
|
||||
// if t is a pointer slice, check if it implements encoding.TextUnmarshaler
|
||||
m.IsSlice = true
|
||||
// Check if the slice implements encoding.TextUnmarshaller
|
||||
if m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler); m.IsValid {
|
||||
return m
|
||||
}
|
||||
// If t is a pointer slice, check if its elements implement
|
||||
// encoding.TextUnmarshaler
|
||||
m.IsSliceElement = true
|
||||
if t = t.Elem(); t.Kind() == reflect.Ptr {
|
||||
t = reflect.PtrTo(t.Elem())
|
||||
v = reflect.Zero(t)
|
||||
m.IsPtr = true
|
||||
m.IsSliceElementPtr = true
|
||||
m.Unmarshaler, m.IsValid = v.Interface().(encoding.TextUnmarshaler)
|
||||
return m
|
||||
}
|
||||
@@ -365,9 +383,18 @@ func isTextUnmarshaler(v reflect.Value) unmarshaler {
|
||||
// unmarshaller contains information about a TextUnmarshaler type
|
||||
type unmarshaler struct {
|
||||
Unmarshaler encoding.TextUnmarshaler
|
||||
IsSlice bool
|
||||
IsPtr bool
|
||||
IsValid bool
|
||||
// IsValid indicates whether the resolved type indicated by the other
|
||||
// flags implements the encoding.TextUnmarshaler interface.
|
||||
IsValid bool
|
||||
// IsPtr indicates that the resolved type is the pointer of the original
|
||||
// type.
|
||||
IsPtr bool
|
||||
// IsSliceElement indicates that the resolved type is a slice element of
|
||||
// the original type.
|
||||
IsSliceElement bool
|
||||
// IsSliceElementPtr indicates that the resolved type is a pointer to a
|
||||
// slice element of the original type.
|
||||
IsSliceElementPtr bool
|
||||
}
|
||||
|
||||
// Errors ---------------------------------------------------------------------
|
||||
|
||||
1693
vendor/github.com/gorilla/schema/decoder_test.go
сгенерированный
поставляемый
1693
vendor/github.com/gorilla/schema/decoder_test.go
сгенерированный
поставляемый
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
420
vendor/github.com/gorilla/schema/encoder_test.go
сгенерированный
поставляемый
420
vendor/github.com/gorilla/schema/encoder_test.go
сгенерированный
поставляемый
@@ -1,420 +0,0 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type E1 struct {
|
||||
F01 int `schema:"f01"`
|
||||
F02 int `schema:"-"`
|
||||
F03 string `schema:"f03"`
|
||||
F04 string `schema:"f04,omitempty"`
|
||||
F05 bool `schema:"f05"`
|
||||
F06 bool `schema:"f06"`
|
||||
F07 *string `schema:"f07"`
|
||||
F08 *int8 `schema:"f08"`
|
||||
F09 float64 `schema:"f09"`
|
||||
F10 func() `schema:"f10"`
|
||||
F11 inner
|
||||
}
|
||||
type inner struct {
|
||||
F12 int
|
||||
}
|
||||
|
||||
func TestFilled(t *testing.T) {
|
||||
f07 := "seven"
|
||||
var f08 int8 = 8
|
||||
s := &E1{
|
||||
F01: 1,
|
||||
F02: 2,
|
||||
F03: "three",
|
||||
F04: "four",
|
||||
F05: true,
|
||||
F06: false,
|
||||
F07: &f07,
|
||||
F08: &f08,
|
||||
F09: 1.618,
|
||||
F10: func() {},
|
||||
F11: inner{12},
|
||||
}
|
||||
|
||||
vals := make(map[string][]string)
|
||||
errs := NewEncoder().Encode(s, vals)
|
||||
|
||||
valExists(t, "f01", "1", vals)
|
||||
valNotExists(t, "f02", vals)
|
||||
valExists(t, "f03", "three", vals)
|
||||
valExists(t, "f05", "true", vals)
|
||||
valExists(t, "f06", "false", vals)
|
||||
valExists(t, "f07", "seven", vals)
|
||||
valExists(t, "f08", "8", vals)
|
||||
valExists(t, "f09", "1.618000", vals)
|
||||
valExists(t, "F12", "12", vals)
|
||||
|
||||
emptyErr := MultiError{}
|
||||
if errs.Error() == emptyErr.Error() {
|
||||
t.Errorf("Expected error got %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
type Aa int
|
||||
|
||||
type E3 struct {
|
||||
F01 bool `schema:"f01"`
|
||||
F02 float32 `schema:"f02"`
|
||||
F03 float64 `schema:"f03"`
|
||||
F04 int `schema:"f04"`
|
||||
F05 int8 `schema:"f05"`
|
||||
F06 int16 `schema:"f06"`
|
||||
F07 int32 `schema:"f07"`
|
||||
F08 int64 `schema:"f08"`
|
||||
F09 string `schema:"f09"`
|
||||
F10 uint `schema:"f10"`
|
||||
F11 uint8 `schema:"f11"`
|
||||
F12 uint16 `schema:"f12"`
|
||||
F13 uint32 `schema:"f13"`
|
||||
F14 uint64 `schema:"f14"`
|
||||
F15 Aa `schema:"f15"`
|
||||
}
|
||||
|
||||
// Test compatibility with default decoder types.
|
||||
func TestCompat(t *testing.T) {
|
||||
src := &E3{
|
||||
F01: true,
|
||||
F02: 4.2,
|
||||
F03: 4.3,
|
||||
F04: -42,
|
||||
F05: -43,
|
||||
F06: -44,
|
||||
F07: -45,
|
||||
F08: -46,
|
||||
F09: "foo",
|
||||
F10: 42,
|
||||
F11: 43,
|
||||
F12: 44,
|
||||
F13: 45,
|
||||
F14: 46,
|
||||
F15: 1,
|
||||
}
|
||||
dst := &E3{}
|
||||
|
||||
vals := make(map[string][]string)
|
||||
encoder := NewEncoder()
|
||||
decoder := NewDecoder()
|
||||
|
||||
encoder.RegisterEncoder(src.F15, func(reflect.Value) string { return "1" })
|
||||
decoder.RegisterConverter(src.F15, func(string) reflect.Value { return reflect.ValueOf(1) })
|
||||
|
||||
err := encoder.Encode(src, vals)
|
||||
if err != nil {
|
||||
t.Errorf("Encoder has non-nil error: %v", err)
|
||||
}
|
||||
err = decoder.Decode(dst, vals)
|
||||
if err != nil {
|
||||
t.Errorf("Decoder has non-nil error: %v", err)
|
||||
}
|
||||
|
||||
if *src != *dst {
|
||||
t.Errorf("Decoder-Encoder compatibility: expected %v, got %v\n", src, dst)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
s := &E1{
|
||||
F01: 1,
|
||||
F02: 2,
|
||||
F03: "three",
|
||||
}
|
||||
|
||||
estr := "schema: encoder not found for <nil>"
|
||||
vals := make(map[string][]string)
|
||||
err := NewEncoder().Encode(s, vals)
|
||||
if err.Error() != estr {
|
||||
t.Errorf("Expected: %s, got %v", estr, err)
|
||||
}
|
||||
|
||||
valExists(t, "f03", "three", vals)
|
||||
valNotExists(t, "f04", vals)
|
||||
}
|
||||
|
||||
func TestStruct(t *testing.T) {
|
||||
estr := "schema: interface must be a struct"
|
||||
vals := make(map[string][]string)
|
||||
err := NewEncoder().Encode("hello world", vals)
|
||||
|
||||
if err.Error() != estr {
|
||||
t.Errorf("Expected: %s, got %v", estr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlices(t *testing.T) {
|
||||
type oneAsWord int
|
||||
ones := []oneAsWord{1, 2}
|
||||
s1 := &struct {
|
||||
ones []oneAsWord `schema:"ones"`
|
||||
ints []int `schema:"ints"`
|
||||
nonempty []int `schema:"nonempty"`
|
||||
empty []int `schema:"empty,omitempty"`
|
||||
}{ones, []int{1, 1}, []int{}, []int{}}
|
||||
vals := make(map[string][]string)
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.RegisterEncoder(ones[0], func(v reflect.Value) string { return "one" })
|
||||
err := encoder.Encode(s1, vals)
|
||||
if err != nil {
|
||||
t.Errorf("Encoder has non-nil error: %v", err)
|
||||
}
|
||||
|
||||
valsExist(t, "ones", []string{"one", "one"}, vals)
|
||||
valsExist(t, "ints", []string{"1", "1"}, vals)
|
||||
valsExist(t, "nonempty", []string{}, vals)
|
||||
valNotExists(t, "empty", vals)
|
||||
}
|
||||
|
||||
func TestCompatSlices(t *testing.T) {
|
||||
type oneAsWord int
|
||||
type s1 struct {
|
||||
Ones []oneAsWord `schema:"ones"`
|
||||
Ints []int `schema:"ints"`
|
||||
}
|
||||
ones := []oneAsWord{1, 1}
|
||||
src := &s1{ones, []int{1, 1}}
|
||||
vals := make(map[string][]string)
|
||||
dst := &s1{}
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.RegisterEncoder(ones[0], func(v reflect.Value) string { return "one" })
|
||||
|
||||
decoder := NewDecoder()
|
||||
decoder.RegisterConverter(ones[0], func(s string) reflect.Value {
|
||||
if s == "one" {
|
||||
return reflect.ValueOf(1)
|
||||
}
|
||||
return reflect.ValueOf(2)
|
||||
})
|
||||
|
||||
err := encoder.Encode(src, vals)
|
||||
if err != nil {
|
||||
t.Errorf("Encoder has non-nil error: %v", err)
|
||||
}
|
||||
err = decoder.Decode(dst, vals)
|
||||
if err != nil {
|
||||
t.Errorf("Dncoder has non-nil error: %v", err)
|
||||
}
|
||||
|
||||
if len(src.Ints) != len(dst.Ints) || len(src.Ones) != len(src.Ones) {
|
||||
t.Fatalf("Expected %v, got %v", src, dst)
|
||||
}
|
||||
|
||||
for i, v := range src.Ones {
|
||||
if dst.Ones[i] != v {
|
||||
t.Fatalf("Expected %v, got %v", v, dst.Ones[i])
|
||||
}
|
||||
}
|
||||
|
||||
for i, v := range src.Ints {
|
||||
if dst.Ints[i] != v {
|
||||
t.Fatalf("Expected %v, got %v", v, dst.Ints[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterEncoder(t *testing.T) {
|
||||
type oneAsWord int
|
||||
type twoAsWord int
|
||||
type oneSliceAsWord []int
|
||||
|
||||
s1 := &struct {
|
||||
oneAsWord
|
||||
twoAsWord
|
||||
oneSliceAsWord
|
||||
}{1, 2, []int{1, 1}}
|
||||
v1 := make(map[string][]string)
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.RegisterEncoder(s1.oneAsWord, func(v reflect.Value) string { return "one" })
|
||||
encoder.RegisterEncoder(s1.twoAsWord, func(v reflect.Value) string { return "two" })
|
||||
encoder.RegisterEncoder(s1.oneSliceAsWord, func(v reflect.Value) string { return "one" })
|
||||
|
||||
err := encoder.Encode(s1, v1)
|
||||
if err != nil {
|
||||
t.Errorf("Encoder has non-nil error: %v", err)
|
||||
}
|
||||
|
||||
valExists(t, "oneAsWord", "one", v1)
|
||||
valExists(t, "twoAsWord", "two", v1)
|
||||
valExists(t, "oneSliceAsWord", "one", v1)
|
||||
}
|
||||
|
||||
func TestEncoderOrder(t *testing.T) {
|
||||
type builtinEncoderSimple int
|
||||
type builtinEncoderSimpleOverridden int
|
||||
type builtinEncoderSlice []int
|
||||
type builtinEncoderSliceOverridden []int
|
||||
type builtinEncoderStruct struct{ nr int }
|
||||
type builtinEncoderStructOverridden struct{ nr int }
|
||||
|
||||
s1 := &struct {
|
||||
builtinEncoderSimple `schema:"simple"`
|
||||
builtinEncoderSimpleOverridden `schema:"simple_overridden"`
|
||||
builtinEncoderSlice `schema:"slice"`
|
||||
builtinEncoderSliceOverridden `schema:"slice_overridden"`
|
||||
builtinEncoderStruct `schema:"struct"`
|
||||
builtinEncoderStructOverridden `schema:"struct_overridden"`
|
||||
}{
|
||||
1,
|
||||
1,
|
||||
[]int{2},
|
||||
[]int{2},
|
||||
builtinEncoderStruct{3},
|
||||
builtinEncoderStructOverridden{3},
|
||||
}
|
||||
v1 := make(map[string][]string)
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.RegisterEncoder(s1.builtinEncoderSimpleOverridden, func(v reflect.Value) string { return "one" })
|
||||
encoder.RegisterEncoder(s1.builtinEncoderSliceOverridden, func(v reflect.Value) string { return "two" })
|
||||
encoder.RegisterEncoder(s1.builtinEncoderStructOverridden, func(v reflect.Value) string { return "three" })
|
||||
|
||||
err := encoder.Encode(s1, v1)
|
||||
if err != nil {
|
||||
t.Errorf("Encoder has non-nil error: %v", err)
|
||||
}
|
||||
|
||||
valExists(t, "simple", "1", v1)
|
||||
valExists(t, "simple_overridden", "one", v1)
|
||||
valExists(t, "slice", "2", v1)
|
||||
valExists(t, "slice_overridden", "two", v1)
|
||||
valExists(t, "nr", "3", v1)
|
||||
valExists(t, "struct_overridden", "three", v1)
|
||||
}
|
||||
|
||||
func valExists(t *testing.T, key string, expect string, result map[string][]string) {
|
||||
valsExist(t, key, []string{expect}, result)
|
||||
}
|
||||
|
||||
func valsExist(t *testing.T, key string, expect []string, result map[string][]string) {
|
||||
vals, ok := result[key]
|
||||
if !ok {
|
||||
t.Fatalf("Key not found. Expected: %s", key)
|
||||
}
|
||||
|
||||
if len(expect) != len(vals) {
|
||||
t.Fatalf("Expected: %v, got: %v", expect, vals)
|
||||
}
|
||||
|
||||
for i, v := range expect {
|
||||
if vals[i] != v {
|
||||
t.Fatalf("Unexpected value. Expected: %v, got %v", v, vals[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func valNotExists(t *testing.T, key string, result map[string][]string) {
|
||||
if val, ok := result[key]; ok {
|
||||
t.Error("Key not ommited. Expected: empty; got: " + val[0] + ".")
|
||||
}
|
||||
}
|
||||
|
||||
type E4 struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func TestEncoderSetAliasTag(t *testing.T) {
|
||||
data := map[string][]string{}
|
||||
|
||||
s := E4{
|
||||
ID: "foo",
|
||||
}
|
||||
encoder := NewEncoder()
|
||||
encoder.SetAliasTag("json")
|
||||
encoder.Encode(&s, data)
|
||||
valExists(t, "id", "foo", data)
|
||||
}
|
||||
|
||||
type E5 struct {
|
||||
F01 int `schema:"f01,omitempty"`
|
||||
F02 string `schema:"f02,omitempty"`
|
||||
F03 *string `schema:"f03,omitempty"`
|
||||
F04 *int8 `schema:"f04,omitempty"`
|
||||
F05 float64 `schema:"f05,omitempty"`
|
||||
F06 E5F06 `schema:"f06,omitempty"`
|
||||
F07 E5F06 `schema:"f07,omitempty"`
|
||||
F08 []string `schema:"f08,omitempty"`
|
||||
F09 []string `schema:"f09,omitempty"`
|
||||
}
|
||||
|
||||
type E5F06 struct {
|
||||
F0601 string `schema:"f0601,omitempty"`
|
||||
}
|
||||
|
||||
func TestEncoderWithOmitempty(t *testing.T) {
|
||||
vals := map[string][]string{}
|
||||
|
||||
s := E5{
|
||||
F02: "test",
|
||||
F07: E5F06{
|
||||
F0601: "test",
|
||||
},
|
||||
F09: []string{"test"},
|
||||
}
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.Encode(&s, vals)
|
||||
|
||||
valNotExists(t, "f01", vals)
|
||||
valExists(t, "f02", "test", vals)
|
||||
valNotExists(t, "f03", vals)
|
||||
valNotExists(t, "f04", vals)
|
||||
valNotExists(t, "f05", vals)
|
||||
valNotExists(t, "f06", vals)
|
||||
valExists(t, "f0601", "test", vals)
|
||||
valNotExists(t, "f08", vals)
|
||||
valsExist(t, "f09", []string{"test"}, vals)
|
||||
}
|
||||
|
||||
type E6 struct {
|
||||
F01 *inner
|
||||
F02 *inner
|
||||
F03 *inner `schema:",omitempty"`
|
||||
}
|
||||
|
||||
func TestStructPointer(t *testing.T) {
|
||||
vals := map[string][]string{}
|
||||
s := E6{
|
||||
F01: &inner{2},
|
||||
}
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.Encode(&s, vals)
|
||||
valExists(t, "F12", "2", vals)
|
||||
valExists(t, "F02", "null", vals)
|
||||
valNotExists(t, "F03", vals)
|
||||
}
|
||||
|
||||
func TestRegisterEncoderCustomArrayType(t *testing.T) {
|
||||
type CustomInt []int
|
||||
type S1 struct {
|
||||
SomeInts CustomInt `schema:",omitempty"`
|
||||
}
|
||||
|
||||
ss := []S1{
|
||||
{},
|
||||
{CustomInt{}},
|
||||
{CustomInt{1, 2, 3}},
|
||||
}
|
||||
|
||||
for s := range ss {
|
||||
vals := map[string][]string{}
|
||||
|
||||
encoder := NewEncoder()
|
||||
encoder.RegisterEncoder(CustomInt{}, func(value reflect.Value) string {
|
||||
return fmt.Sprint(value.Interface())
|
||||
})
|
||||
|
||||
encoder.Encode(s, vals)
|
||||
t.Log(vals)
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user