more fields testing
This commit is contained in:
parent
a7b031f3b4
commit
78886422e8
27
field.go
27
field.go
@ -59,12 +59,6 @@ func fieldFromType(name string, t reflect.Type) Field {
|
|||||||
|
|
||||||
func prependFieldName(name string, f []Field) {
|
func prependFieldName(name string, f []Field) {
|
||||||
for i := range f {
|
for i := range f {
|
||||||
if f[i].name == "" {
|
|
||||||
f[i].name = strcase.ToKebab(name)
|
|
||||||
f[i].path = []string{name}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
f[i].name = fmt.Sprintf("%s-%s", strcase.ToKebab(name), f[i].name)
|
f[i].name = fmt.Sprintf("%s-%s", strcase.ToKebab(name), f[i].name)
|
||||||
f[i].path = append([]string{name}, f[i].path...)
|
f[i].path = append([]string{name}, f[i].path...)
|
||||||
}
|
}
|
||||||
@ -160,7 +154,16 @@ func structFields(v reflect.Value) []Field {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ffi := fieldValues(vi)
|
vvi := vi
|
||||||
|
if unpackType(fi.Type, pointer).Kind() == reflect.Slice {
|
||||||
|
var ok bool
|
||||||
|
vvi, ok = allocate(fi.Type, 1)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ffi := fieldValues(vvi)
|
||||||
if !fi.Anonymous {
|
if !fi.Anonymous {
|
||||||
prependFieldName(fi.Name, ffi)
|
prependFieldName(fi.Name, ffi)
|
||||||
}
|
}
|
||||||
@ -278,11 +281,7 @@ func bindMapField(receiver reflect.Value, values []Field) bool {
|
|||||||
t := receiver.Type()
|
t := receiver.Type()
|
||||||
kt := t.Key()
|
kt := t.Key()
|
||||||
vt := t.Elem()
|
vt := t.Elem()
|
||||||
kv, ok := bindScalarCreate(kt, []any{key})
|
kv, _ := bindScalarCreate(kt, []any{key})
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
vv, ok := bindScalarCreate(vt, v)
|
vv, ok := bindScalarCreate(vt, v)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
@ -370,10 +369,6 @@ func bindStructField(receiver reflect.Value, values []Field) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func bindField(receiver reflect.Value, values []Field) bool {
|
func bindField(receiver reflect.Value, values []Field) bool {
|
||||||
if !receiver.IsValid() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if values[0].name == "" && len(values[0].path) == 0 {
|
if values[0].name == "" && len(values[0].path) == 0 {
|
||||||
ret := bindScalarField(receiver, values)
|
ret := bindScalarField(receiver, values)
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
@ -128,6 +128,14 @@ func TestField(t *testing.T) {
|
|||||||
t.Fatal(notation.Sprint(f))
|
t.Fatal(notation.Sprint(f))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("list of struct", func(t *testing.T) {
|
||||||
|
type s struct{Foo []struct{Bar int}}
|
||||||
|
f := bind.Fields[s]()
|
||||||
|
if len(f) != 1 || f[0].Name() != "foo-bar" || !f[0].List() {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("field values", func(t *testing.T) {
|
t.Run("field values", func(t *testing.T) {
|
||||||
@ -358,6 +366,22 @@ func TestField(t *testing.T) {
|
|||||||
t.Fatal(notation.Sprint(f))
|
t.Fatal(notation.Sprint(f))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("list of struct", func(t *testing.T) {
|
||||||
|
type s struct{Foo []struct{Bar int}}
|
||||||
|
var v s
|
||||||
|
f := bind.FieldValues(v)
|
||||||
|
if len(f) != 1 || f[0].Name() != "foo-bar" || !f[0].List() {
|
||||||
|
t.Fatal(notation.Sprint(f))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nil", func(t *testing.T) {
|
||||||
|
f := bind.FieldValues(nil)
|
||||||
|
if len(f) != 0 {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("bind fields", func(t *testing.T) {
|
t.Run("bind fields", func(t *testing.T) {
|
||||||
@ -711,6 +735,19 @@ func TestField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("pointer field with invalid value", func(t *testing.T) {
|
||||||
|
type s struct {Foo *struct{ Bar int }}
|
||||||
|
var v s
|
||||||
|
u := bind.BindFields(
|
||||||
|
&v,
|
||||||
|
bind.NamedValue("foo-bar", "baz"),
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(u) != 1 {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("unsupported pointer fields", func(t *testing.T) {
|
t.Run("unsupported pointer fields", func(t *testing.T) {
|
||||||
type s struct {
|
type s struct {
|
||||||
Foo *int
|
Foo *int
|
||||||
@ -823,6 +860,14 @@ func TestField(t *testing.T) {
|
|||||||
t.Fatal(notation.Sprint(u), notation.Sprint(v))
|
t.Fatal(notation.Sprint(u), notation.Sprint(v))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("scalar map with invalid field type", func(t *testing.T) {
|
||||||
|
v := make(map[string]int)
|
||||||
|
u := bind.BindFields(v, bind.NamedValue("foo", "bar"))
|
||||||
|
if len(u) != 1 {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("bind fields create", func(t *testing.T) {
|
t.Run("bind fields create", func(t *testing.T) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user