2023-08-16 09:57:42 +08:00
|
|
|
package reflection
|
|
|
|
|
2023-09-12 14:14:09 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
2023-08-16 09:57:42 +08:00
|
|
|
|
2023-09-12 14:14:09 +08:00
|
|
|
type Fakeb struct {
|
2023-09-12 15:42:28 +08:00
|
|
|
In int `json:"in"`
|
|
|
|
BS map[string]string `json:"bs"`
|
2023-09-12 14:14:09 +08:00
|
|
|
}
|
2023-09-12 15:42:28 +08:00
|
|
|
|
|
|
|
type Ab struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2023-08-16 09:57:42 +08:00
|
|
|
type fake struct {
|
2023-09-12 15:42:28 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
Usage []Fakeb `json:"usage"`
|
|
|
|
XX Fakeb `json:"xx"`
|
|
|
|
AX *Fakeb `json:"ax"`
|
|
|
|
SS []string `json:"ss"`
|
|
|
|
DS []int `json:"ds"`
|
|
|
|
Ms map[string]int `json:"ms"`
|
|
|
|
AB map[string]Ab `json:"ab"`
|
2023-08-16 09:57:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetter(t *testing.T) {
|
|
|
|
dst := &fake{}
|
2023-09-12 15:42:28 +08:00
|
|
|
ms := map[string]any{
|
|
|
|
"name": "aa",
|
|
|
|
"age": "5",
|
|
|
|
"usage": []map[string]any{
|
|
|
|
{
|
|
|
|
"in": 15,
|
|
|
|
"bs": map[string]any{
|
|
|
|
"aa": "vv",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"xx": map[string]any{"in": 45},
|
|
|
|
"ax": map[string]any{"in": 55},
|
|
|
|
"ss": []string{"11", "ss"},
|
|
|
|
"ds": []int{55, 55, 34},
|
|
|
|
"ms": map[string]any{"aa": "23"},
|
|
|
|
"ab": map[string]any{
|
|
|
|
"xx": map[string]any{
|
|
|
|
"name": "xxx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-09-12 14:14:09 +08:00
|
|
|
err := Setter(dst, ms)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-08-16 09:57:42 +08:00
|
|
|
if dst.Age != 5 {
|
|
|
|
t.Errorf("setter failed")
|
2023-09-12 14:14:09 +08:00
|
|
|
} else {
|
|
|
|
fmt.Printf("%+v", dst)
|
2023-08-16 09:57:42 +08:00
|
|
|
}
|
|
|
|
}
|