66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
package reflection
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
type Fakeb struct {
|
|
In int `json:"in"`
|
|
BS map[string]string `json:"bs"`
|
|
}
|
|
|
|
type Ab struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type fake struct {
|
|
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"`
|
|
}
|
|
|
|
func TestSetter(t *testing.T) {
|
|
dst := &fake{}
|
|
vs := map[string]any{
|
|
"name": "xxx",
|
|
}
|
|
vvs := []map[string]any{
|
|
{
|
|
"in": 15,
|
|
"bs": map[string]any{
|
|
"aa": "vv",
|
|
},
|
|
},
|
|
}
|
|
ms := map[string]any{
|
|
"name": "aa",
|
|
"age": "5",
|
|
"usage": vvs,
|
|
"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": vs,
|
|
},
|
|
}
|
|
err := Setter(dst, ms)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if dst.Age != 5 {
|
|
t.Errorf("setter failed")
|
|
} else {
|
|
fmt.Printf("%+v", dst)
|
|
}
|
|
}
|