reflection_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package reflection
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. type Fakeb struct {
  7. In int `json:"in"`
  8. BS map[string]string `json:"bs"`
  9. }
  10. type Ab struct {
  11. Name string `json:"name"`
  12. }
  13. type fake struct {
  14. Name string `json:"name"`
  15. Age int `json:"age"`
  16. Usage []Fakeb `json:"usage"`
  17. XX Fakeb `json:"xx"`
  18. AX *Fakeb `json:"ax"`
  19. SS []string `json:"ss"`
  20. DS []int `json:"ds"`
  21. Ms map[string]int `json:"ms"`
  22. AB map[string]Ab `json:"ab"`
  23. }
  24. func TestSetter(t *testing.T) {
  25. dst := &fake{}
  26. vs := map[string]any{
  27. "name": "xxx",
  28. }
  29. vvs := []map[string]any{
  30. {
  31. "in": 15,
  32. "bs": map[string]any{
  33. "aa": "vv",
  34. },
  35. },
  36. }
  37. ms := map[string]any{
  38. "name": "aa",
  39. "age": "5",
  40. "usage": vvs,
  41. "xx": map[string]any{"in": 45},
  42. "ax": map[string]any{"in": 55},
  43. "ss": []string{"11", "ss"},
  44. "ds": []int{55, 55, 34},
  45. "ms": map[string]any{"aa": "23"},
  46. "ab": map[string]any{
  47. "xx": vs,
  48. },
  49. }
  50. err := Setter(dst, ms)
  51. if err != nil {
  52. t.Error(err)
  53. return
  54. }
  55. if dst.Age != 5 {
  56. t.Errorf("setter failed")
  57. } else {
  58. fmt.Printf("%+v", dst)
  59. }
  60. }