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.

serder_test.go 7.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package serder
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
  8. )
  9. type FromAnyString struct {
  10. Str string
  11. }
  12. func (a *FromAnyString) FromAny(val any) (bool, error) {
  13. if str, ok := val.(string); ok {
  14. a.Str = "@" + str
  15. return true, nil
  16. }
  17. return false, nil
  18. }
  19. type ToAnyString struct {
  20. Str string
  21. }
  22. func (a *ToAnyString) ToAny(typ reflect.Type) (val any, ok bool, err error) {
  23. if typ == myreflect.TypeOf[map[string]any]() {
  24. return map[string]any{
  25. "str": "@" + a.Str,
  26. }, true, nil
  27. }
  28. return nil, false, nil
  29. }
  30. type FromAnySt struct {
  31. Value string
  32. }
  33. func (a *FromAnySt) FromAny(val any) (bool, error) {
  34. if st, ok := val.(ToAnySt); ok {
  35. a.Value = "From:" + st.Value
  36. return true, nil
  37. }
  38. return false, nil
  39. }
  40. type ToAnySt struct {
  41. Value string
  42. }
  43. func (a *ToAnySt) ToAny(typ reflect.Type) (val any, ok bool, err error) {
  44. if typ == myreflect.TypeOf[FromAnySt]() {
  45. return FromAnySt{
  46. Value: "To:" + a.Value,
  47. }, true, nil
  48. }
  49. return nil, false, nil
  50. }
  51. type DirToAnySt struct {
  52. Value string
  53. }
  54. func (a DirToAnySt) ToAny(typ reflect.Type) (val any, ok bool, err error) {
  55. if typ == myreflect.TypeOf[FromAnySt]() {
  56. return FromAnySt{
  57. Value: "DirTo:" + a.Value,
  58. }, true, nil
  59. }
  60. return nil, false, nil
  61. }
  62. func Test_AnyToAny(t *testing.T) {
  63. Convey("包含用字符串保存的int数据", t, func() {
  64. type Struct struct {
  65. A string `json:"a"`
  66. B int `json:"b"`
  67. C int64 `json:"c,string"`
  68. }
  69. mp := map[string]any{
  70. "a": "a",
  71. "b": 1,
  72. "c": "1234",
  73. }
  74. var st Struct
  75. err := AnyToAny(mp, &st)
  76. So(err, ShouldBeNil)
  77. So(st.A, ShouldEqual, "a")
  78. So(st.B, ShouldEqual, 1)
  79. So(st.C, ShouldEqual, 1234)
  80. })
  81. Convey("只有FromAny", t, func() {
  82. type Struct struct {
  83. Special FromAnyString `json:"str"`
  84. }
  85. mp := map[string]any{
  86. "str": "test",
  87. }
  88. var ret Struct
  89. err := AnyToAny(mp, &ret)
  90. So(err, ShouldBeNil)
  91. So(ret.Special.Str, ShouldEqual, "@test")
  92. })
  93. Convey("字段类型直接实现了FromAny", t, func() {
  94. type Struct struct {
  95. Special *FromAnyString `json:"str"`
  96. }
  97. mp := map[string]any{
  98. "str": "test",
  99. }
  100. var ret Struct
  101. err := AnyToAny(mp, &ret)
  102. So(err, ShouldBeNil)
  103. So(ret.Special.Str, ShouldEqual, "@test")
  104. })
  105. Convey("只有ToAny", t, func() {
  106. st := struct {
  107. Special ToAnyString `json:"str"`
  108. }{
  109. Special: ToAnyString{
  110. Str: "test",
  111. },
  112. }
  113. ret := map[string]any{}
  114. err := AnyToAny(st, &ret)
  115. So(err, ShouldBeNil)
  116. So(ret["str"].(map[string]any)["str"], ShouldEqual, "@test")
  117. })
  118. Convey("优先使用ToAny", t, func() {
  119. st1 := ToAnySt{
  120. Value: "test",
  121. }
  122. st2 := FromAnySt{}
  123. err := AnyToAny(st1, &st2)
  124. So(err, ShouldBeNil)
  125. So(st2.Value, ShouldEqual, "To:test")
  126. })
  127. Convey("使用Convertor", t, func() {
  128. type Struct1 struct {
  129. Value string
  130. }
  131. type Struct2 struct {
  132. Value string
  133. }
  134. st1 := Struct1{
  135. Value: "test",
  136. }
  137. st2 := Struct2{}
  138. err := AnyToAny(st1, &st2, AnyToAnyOption{
  139. Converters: []Converter{func(srcType reflect.Type, dstType reflect.Type, data interface{}) (interface{}, error) {
  140. if srcType == myreflect.TypeOf[Struct1]() && dstType == myreflect.TypeOf[Struct2]() {
  141. s1 := data.(Struct1)
  142. return Struct2{
  143. Value: "@" + s1.Value,
  144. }, nil
  145. }
  146. return nil, fmt.Errorf("should not arrive here!")
  147. }},
  148. })
  149. So(err, ShouldBeNil)
  150. So(st2.Value, ShouldEqual, "@test")
  151. })
  152. }
  153. func Test_TypedMapToObject(t *testing.T) {
  154. type Struct struct {
  155. A string `json:"a"`
  156. B int `json:"b"`
  157. C int64 `json:"c,string"`
  158. }
  159. nameResovler := NewTypeNameResolver(true)
  160. nameResovler.Register(myreflect.TypeOf[Struct]())
  161. Convey("结构体", t, func() {
  162. st := Struct{
  163. A: "a",
  164. B: 1,
  165. C: 2,
  166. }
  167. mp, err := ObjectToTypedMap(st, TypedSerderOption{
  168. TypeResolver: &nameResovler,
  169. TypeFieldName: "@type",
  170. })
  171. So(err, ShouldBeNil)
  172. st2Ptr, err := TypedMapToObject(mp, TypedSerderOption{
  173. TypeResolver: &nameResovler,
  174. TypeFieldName: "@type",
  175. })
  176. So(err, ShouldBeNil)
  177. st2, ok := st2Ptr.(Struct)
  178. So(ok, ShouldBeTrue)
  179. So(st2, ShouldHaveSameTypeAs, st)
  180. So(st2, ShouldResemble, st)
  181. })
  182. }
  183. func Test_MapToObject(t *testing.T) {
  184. type Base struct {
  185. Int int
  186. Bool bool
  187. String string
  188. Float float32
  189. }
  190. type ArraryStruct struct {
  191. IntArr []int
  192. StArr []Base
  193. ArrArr [][]int
  194. Nil []Base
  195. }
  196. type MapStruct struct {
  197. StrMap map[string]string
  198. StMap map[string]Base
  199. MapMap map[string]map[string]string
  200. Nil map[string]Base
  201. }
  202. type Top struct {
  203. ArrSt ArraryStruct
  204. MapSt *MapStruct
  205. BaseIf any
  206. NilPtr *Base
  207. }
  208. Convey("结构体递归转换成map[string]any", t, func() {
  209. val := Top{
  210. ArrSt: ArraryStruct{
  211. IntArr: []int{1, 2, 3},
  212. StArr: []Base{
  213. {
  214. Int: 1,
  215. Bool: true,
  216. String: "test",
  217. Float: 1,
  218. },
  219. {
  220. Int: 2,
  221. Bool: false,
  222. String: "test2",
  223. Float: 2,
  224. },
  225. },
  226. ArrArr: [][]int{
  227. {1, 2, 3},
  228. {},
  229. nil,
  230. },
  231. Nil: nil,
  232. },
  233. MapSt: &MapStruct{
  234. StrMap: map[string]string{
  235. "a": "1",
  236. "b": "2",
  237. },
  238. StMap: map[string]Base{
  239. "a": {
  240. Int: 1,
  241. Bool: true,
  242. String: "test",
  243. Float: 1,
  244. },
  245. "b": {
  246. Int: 2,
  247. Bool: false,
  248. String: "test2",
  249. Float: 2,
  250. },
  251. },
  252. MapMap: map[string]map[string]string{
  253. "a": {
  254. "a": "1",
  255. "b": "2",
  256. },
  257. "b": nil,
  258. },
  259. Nil: nil,
  260. },
  261. BaseIf: Base{
  262. Int: 1,
  263. Bool: true,
  264. String: "test",
  265. Float: 1,
  266. },
  267. NilPtr: nil,
  268. }
  269. retMp, err := ObjectToMap(val)
  270. So(err, ShouldBeNil)
  271. exceptMap := map[string]any{
  272. "ArrSt": map[string]any{
  273. "IntArr": []any{1, 2, 3},
  274. "StArr": []any{
  275. map[string]any{
  276. "Int": 1,
  277. "Bool": true,
  278. "String": "test",
  279. "Float": 1,
  280. },
  281. map[string]any{
  282. "Int": 2,
  283. "Bool": false,
  284. "String": "test2",
  285. "Float": 2,
  286. },
  287. },
  288. "ArrArr": []any{
  289. []any{1, 2, 3},
  290. []any{},
  291. []int(nil),
  292. },
  293. "Nil": []Base(nil),
  294. },
  295. "MapSt": map[string]any{
  296. "StrMap": map[string]any{
  297. "a": "1",
  298. "b": "2",
  299. },
  300. "StMap": map[string]any{
  301. "a": map[string]any{
  302. "Int": 1,
  303. "Bool": true,
  304. "String": "test",
  305. "Float": 1,
  306. },
  307. "b": map[string]any{
  308. "Int": 2,
  309. "Bool": false,
  310. "String": "test2",
  311. "Float": 2,
  312. },
  313. },
  314. "MapMap": map[string]any{
  315. "a": map[string]any{
  316. "a": "1",
  317. "b": "2",
  318. },
  319. "b": map[string]string(nil),
  320. },
  321. "Nil": map[string]Base(nil),
  322. },
  323. "BaseIf": map[string]any{
  324. "Int": 1,
  325. "Bool": true,
  326. "String": "test",
  327. "Float": 1,
  328. },
  329. "NilPtr": (*Base)(nil),
  330. }
  331. mpRetJson, err := ObjectToJSON(retMp)
  332. So(err, ShouldBeNil)
  333. exceptMapJson, err := ObjectToJSON(exceptMap)
  334. So(err, ShouldBeNil)
  335. So(string(mpRetJson), ShouldEqualJSON, string(exceptMapJson))
  336. })
  337. }

公共库

Contributors (1)