fix: type matching supports string to int (#2038)

* fix: type matching supports string to int

* feat: type matching supports string to int

Co-authored-by: 程家福 <chengjiafu@uniontech.com>
This commit is contained in:
家福
2022-07-01 23:21:31 +08:00
committed by GitHub
parent 6a4885ba64
commit f3b8fef34f
5 changed files with 126 additions and 3 deletions

View File

@@ -15,7 +15,7 @@ type Foo struct {
StrWithTagAndOption string `key:"stringwithtag,string"`
}
func TestDeferInt(t *testing.T) {
func TestDerefInt(t *testing.T) {
i := 1
s := "hello"
number := struct {
@@ -60,6 +60,51 @@ func TestDeferInt(t *testing.T) {
}
}
func TestDerefValInt(t *testing.T) {
i := 1
s := "hello"
number := struct {
f float64
}{
f: 6.4,
}
cases := []struct {
t reflect.Value
expect reflect.Kind
}{
{
t: reflect.ValueOf(i),
expect: reflect.Int,
},
{
t: reflect.ValueOf(&i),
expect: reflect.Int,
},
{
t: reflect.ValueOf(s),
expect: reflect.String,
},
{
t: reflect.ValueOf(&s),
expect: reflect.String,
},
{
t: reflect.ValueOf(number.f),
expect: reflect.Float64,
},
{
t: reflect.ValueOf(&number.f),
expect: reflect.Float64,
},
}
for _, each := range cases {
t.Run(each.t.String(), func(t *testing.T) {
assert.Equal(t, each.expect, DerefVal(each.t).Kind())
})
}
}
func TestParseKeyAndOptionWithoutTag(t *testing.T) {
var foo Foo
rte := reflect.TypeOf(&foo).Elem()