Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Dec 10, 2022
1 parent 944193c commit 76a92f1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
13 changes: 12 additions & 1 deletion core/mapping/unmarshaler.go
Expand Up @@ -746,7 +746,9 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter

targetValue.SetMapIndex(key, target.Elem())
default:
targetValue.SetMapIndex(key, keythValue)
if err := setMapIndex(targetValue, key, keythValue); err != nil {
return targetValue, err
}
}
}
}
Expand Down Expand Up @@ -939,6 +941,15 @@ func readKeys(key string) []string {
return keys
}

func setMapIndex(targetMap, key, value reflect.Value) error {
if targetMap.MapIndex(key).Kind() != value.Kind() {
return errTypeMismatch
}

targetMap.SetMapIndex(key, value)
return nil
}

func setSameKindValue(targetType reflect.Type, target reflect.Value, value interface{}) {
if reflect.ValueOf(value).Type().AssignableTo(targetType) {
target.Set(reflect.ValueOf(value))
Expand Down
8 changes: 8 additions & 0 deletions core/mapping/unmarshaler_test.go
Expand Up @@ -3563,6 +3563,14 @@ func TestGoogleUUID(t *testing.T) {
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c2", val.Uidp.String())
}

func TestUnmarshalJsonReaderWithTypeMismatch(t *testing.T) {
var req struct {
Params map[string]string `json:"params"`
}
body := `{"params":{"a":{"a":123}}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
}

func BenchmarkDefaultValue(b *testing.B) {
for i := 0; i < b.N; i++ {
var a struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -22,7 +22,7 @@ require (
github.com/prometheus/client_golang v1.13.0
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.1
go.etcd.io/etcd/api/v3 v3.5.5
go.etcd.io/etcd/api/v3 v3.5.6
go.etcd.io/etcd/client/v3 v3.5.5
go.mongodb.org/mongo-driver v1.11.0
go.opentelemetry.io/otel v1.10.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Expand Up @@ -829,8 +829,9 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64 h1:5mLPGnFdSsevFRFc9q3yYbBkB6tsm4aCwwQV/j1JQAQ=
github.com/yuin/gopher-lua v0.0.0-20220504180219-658193537a64/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.etcd.io/etcd/api/v3 v3.5.5 h1:BX4JIbQ7hl7+jL+g+2j5UAr0o1bctCm6/Ct+ArBGkf0=
go.etcd.io/etcd/api/v3 v3.5.5/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
go.etcd.io/etcd/api/v3 v3.5.6 h1:Cy2qx3npLcYqTKqGJzMypnMv2tiRyifZJ17BlWIWA7A=
go.etcd.io/etcd/api/v3 v3.5.6/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
go.etcd.io/etcd/client/pkg/v3 v3.5.5 h1:9S0JUVvmrVl7wCF39iTQthdaaNIiAaQbmK75ogO6GU8=
go.etcd.io/etcd/client/pkg/v3 v3.5.5/go.mod h1:ggrwbk069qxpKPq8/FKkQ3Xq9y39kbFR4LnKszpRXeQ=
go.etcd.io/etcd/client/v3 v3.5.5 h1:q++2WTJbUgpQu4B6hCuT7VkdwaTP7Qz6Daak3WzbrlI=
Expand Down

0 comments on commit 76a92f1

Please sign in to comment.