You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

walk_test.go 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package serder
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "gitlink.org.cn/cloudream/common/utils/reflect2"
  8. )
  9. func Test_WalkValue(t *testing.T) {
  10. type Base struct {
  11. Int int
  12. Bool bool
  13. String string
  14. Float float32
  15. }
  16. type ArraryStruct struct {
  17. IntArr []int
  18. StArr []Base
  19. ArrArr [][]int
  20. Nil []Base
  21. }
  22. type MapStruct struct {
  23. StrMap map[string]string
  24. StMap map[string]Base
  25. MapMap map[string]map[string]string
  26. Nil map[string]Base
  27. }
  28. type Top struct {
  29. ArrSt ArraryStruct
  30. MapSt *MapStruct
  31. BaseIf any
  32. NilPtr *Base
  33. }
  34. isBaseDataType := func(val reflect.Value) bool {
  35. typ := val.Type()
  36. return typ == reflect2.TypeOf[int]() || typ == reflect2.TypeOf[bool]() ||
  37. typ == reflect2.TypeOf[string]() || typ == reflect2.TypeOf[float32]() || val.IsZero()
  38. }
  39. toString := func(val any) string {
  40. return fmt.Sprintf("%v", val)
  41. }
  42. Convey("遍历", t, func() {
  43. val := Top{
  44. ArrSt: ArraryStruct{
  45. IntArr: []int{1, 2, 3},
  46. StArr: []Base{
  47. {
  48. Int: 1,
  49. Bool: true,
  50. String: "test",
  51. Float: 1,
  52. },
  53. {
  54. Int: 2,
  55. Bool: false,
  56. String: "test2",
  57. Float: 2,
  58. },
  59. },
  60. ArrArr: [][]int{
  61. {1, 2, 3},
  62. {},
  63. nil,
  64. },
  65. Nil: nil,
  66. },
  67. MapSt: &MapStruct{
  68. StrMap: map[string]string{
  69. "a": "1",
  70. "b": "2",
  71. },
  72. StMap: map[string]Base{
  73. "a": {
  74. Int: 1,
  75. Bool: true,
  76. String: "test",
  77. Float: 1,
  78. },
  79. "b": {
  80. Int: 2,
  81. Bool: false,
  82. String: "test2",
  83. Float: 2,
  84. },
  85. },
  86. MapMap: map[string]map[string]string{
  87. "a": {
  88. "a": "1",
  89. "b": "2",
  90. },
  91. "b": nil,
  92. },
  93. Nil: nil,
  94. },
  95. BaseIf: Base{
  96. Int: 1,
  97. Bool: true,
  98. String: "test",
  99. Float: 1,
  100. },
  101. NilPtr: nil,
  102. }
  103. var trace []string
  104. WalkValue(val, func(ctx *WalkContext, event WalkEvent) WalkingOp {
  105. switch e := event.(type) {
  106. case StructBeginEvent:
  107. trace = append(trace, "StructBeginEvent")
  108. case StructArriveFieldEvent:
  109. trace = append(trace, "StructFieldEvent", e.Info.Name)
  110. if isBaseDataType(e.Value) {
  111. trace = append(trace, toString(e.Value.Interface()))
  112. }
  113. case StructEndEvent:
  114. trace = append(trace, "StructEndEvent")
  115. case MapBeginEvent:
  116. trace = append(trace, "MapBeginEvent")
  117. case MapArriveEntryEvent:
  118. trace = append(trace, "MapEntryEvent", e.Key.String())
  119. if isBaseDataType(e.Value) {
  120. trace = append(trace, toString(e.Value.Interface()))
  121. }
  122. case MapEndEvent:
  123. trace = append(trace, "MapEndEvent")
  124. case ArrayBeginEvent:
  125. trace = append(trace, "ArrayBeginEvent")
  126. case ArrayArriveElementEvent:
  127. trace = append(trace, "ArrayElementEvent", fmt.Sprintf("%d", e.Index))
  128. if isBaseDataType(e.Value) {
  129. trace = append(trace, toString(e.Value.Interface()))
  130. }
  131. case ArrayEndEvent:
  132. trace = append(trace, "ArrayEndEvent")
  133. }
  134. return Next
  135. })
  136. So(trace, ShouldResemble, []string{})
  137. })
  138. }