Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Apr 26, 2024
1 parent c9b0809 commit ce6ff91
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
23 changes: 23 additions & 0 deletions binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type TestMapIntKey struct {
Map2 map[TestTypeString]TestTypeInt
}

type TestMapIntKey2 struct {
Map map[int]*TestProfile
}

func TestMapToAnonymous(t *testing.T) {
e := New()
m := &TestAnonymous{}
Expand Down Expand Up @@ -369,6 +373,25 @@ func TestStructMapIntKey(t *testing.T) {
},
}, m)
}
func TestStructMapIntKey2(t *testing.T) {
e := New()
m := &TestMapIntKey2{}
err := FormToStruct(e, m, map[string][]string{
`map[1][address]`: {`a`},
`map[2][address]`: {`b`},
`map[3][address]`: {`c`},
`map[5][address]`: {`e`},
}, ``)
assert.NoError(t, err)
assert.Equal(t, &TestMapIntKey2{
Map: map[int]*TestProfile{
1: {Address: `a`},
2: {Address: `b`},
3: {Address: `c`},
5: {Address: `e`},
},
}, m)
}

type TestBinderWithConvertor struct {
Options map[string]string `form_decoder:"splitKVRows:=" form_encoder:"joinKVRows"`
Expand Down
29 changes: 18 additions & 11 deletions binder_tostruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (e *Echo) parseFormItem(keyNormalizer func(string) string, m interface{}, t
value = value.Elem()
}
itemT = itemT.Elem()
index := reflect.ValueOf(name)
index := convertMapKey(tc, name)
newV := value.MapIndex(index)
if !newV.IsValid() {
newV = reflect.New(itemT).Elem()
Expand Down Expand Up @@ -421,6 +421,22 @@ func (e *Echo) binderValueDecode(name string, typev reflect.Type, tv reflect.Val
return ErrNotImplemented
}

func convertMapKey(typev reflect.Type, key string) reflect.Value {
var index reflect.Value
keyT := typev.Key()
kind := keyT.Kind()
if kind != reflect.String {
mapKey := param.AsType(kind.String(), key)
index = reflect.ValueOf(mapKey)
} else {
index = reflect.ValueOf(key)
}
if index.Type().Name() != keyT.Name() && index.CanConvert(keyT) {
index = index.Convert(keyT)
}
return index
}

func (e *Echo) setMap(logger logger.Logger,
parentT reflect.Type, parentV reflect.Value,
name string, value reflect.Value, typev reflect.Type,
Expand All @@ -429,16 +445,7 @@ func (e *Echo) setMap(logger logger.Logger,
value.Set(reflect.MakeMap(value.Type()))
}
value = reflect.Indirect(value)
var index reflect.Value
if typev.Key().Kind() != reflect.String {
mapKey := param.AsType(typev.Key().Kind().String(), name)
index = reflect.ValueOf(mapKey)
} else {
index = reflect.ValueOf(name)
}
if index.Type().Name() != typev.Key().Name() && index.CanConvert(typev.Key()) {
index = index.Convert(typev.Key())
}
index := convertMapKey(typev, name)
oldVal := value.MapIndex(index)
if !oldVal.IsValid() {
oldType := value.Type().Elem()
Expand Down

0 comments on commit ce6ff91

Please sign in to comment.