chore: add more tests

master
kevin 2 years ago committed by Kevin Wan
parent b449f2f39e
commit eaac0ba8de

@ -177,6 +177,8 @@ func buildFieldsInfo(tp reflect.Type) (*fieldInfo, error) {
return buildStructFieldsInfo(tp) return buildStructFieldsInfo(tp)
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:
return buildFieldsInfo(mapping.Deref(tp.Elem())) return buildFieldsInfo(mapping.Deref(tp.Elem()))
case reflect.Chan, reflect.Func:
return nil, fmt.Errorf("unsupported type: %s", tp.Kind())
default: default:
return &fieldInfo{ return &fieldInfo{
children: make(map[string]*fieldInfo), children: make(map[string]*fieldInfo),
@ -243,7 +245,7 @@ func buildStructFieldsInfo(tp reflect.Type) (*fieldInfo, error) {
} }
func mergeFields(prev *fieldInfo, key string, children map[string]*fieldInfo) error { func mergeFields(prev *fieldInfo, key string, children map[string]*fieldInfo) error {
if len(children) == 0 { if len(prev.children) == 0 || len(children) == 0 {
return newDupKeyError(key) return newDupKeyError(key)
} }

@ -1007,9 +1007,19 @@ func TestLoadNamedFieldOverwritten(t *testing.T) {
) )
var c Config var c Config
input := []byte(`{"Val": {"Elem": {"Key": {"Value": "Value"}}}}`) input := []byte(`{"Val": {"Elem": "Value"}}`)
assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr) assert.ErrorAs(t, LoadFromJsonBytes(input, &c), &dupErr)
}) })
t.Run("overwritten named struct", func(t *testing.T) {
type Config struct {
Val chan int
}
var c Config
input := []byte(`{"Val": 1}`)
assert.Error(t, LoadFromJsonBytes(input, &c))
})
} }
func createTempFile(ext, text string) (string, error) { func createTempFile(ext, text string) (string, error) {

Loading…
Cancel
Save