64 lines
1.1 KiB
Go
64 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{}
|
|
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",
|
|
},
|
|
},
|
|
}
|
|
err := Setter(dst, ms)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if dst.Age != 5 {
|
|
t.Errorf("setter failed")
|
|
} else {
|
|
fmt.Printf("%+v", dst)
|
|
}
|
|
}
|