reflection_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. ms := map[string]any{
  27. "name": "aa",
  28. "age": "5",
  29. "usage": []map[string]any{
  30. {
  31. "in": 15,
  32. "bs": map[string]any{
  33. "aa": "vv",
  34. },
  35. },
  36. },
  37. "xx": map[string]any{"in": 45},
  38. "ax": map[string]any{"in": 55},
  39. "ss": []string{"11", "ss"},
  40. "ds": []int{55, 55, 34},
  41. "ms": map[string]any{"aa": "23"},
  42. "ab": map[string]any{
  43. "xx": map[string]any{
  44. "name": "xxx",
  45. },
  46. },
  47. }
  48. err := Setter(dst, ms)
  49. if err != nil {
  50. t.Error(err)
  51. return
  52. }
  53. if dst.Age != 5 {
  54. t.Errorf("setter failed")
  55. } else {
  56. fmt.Printf("%+v", dst)
  57. }
  58. }