structure_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package structure
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. var (
  7. decoder = NewDecoder(Option{TagName: "test"})
  8. weakTypeDecoder = NewDecoder(Option{TagName: "test", WeaklyTypedInput: true})
  9. )
  10. type Baz struct {
  11. Foo int `test:"foo"`
  12. Bar string `test:"bar"`
  13. }
  14. type BazSlice struct {
  15. Foo int `test:"foo"`
  16. Bar []string `test:"bar"`
  17. }
  18. type BazOptional struct {
  19. Foo int `test:"foo,omitempty"`
  20. Bar string `test:"bar,omitempty"`
  21. }
  22. func TestStructure_Basic(t *testing.T) {
  23. rawMap := map[string]any{
  24. "foo": 1,
  25. "bar": "test",
  26. "extra": false,
  27. }
  28. goal := &Baz{
  29. Foo: 1,
  30. Bar: "test",
  31. }
  32. s := &Baz{}
  33. err := decoder.Decode(rawMap, s)
  34. assert.Nil(t, err)
  35. assert.Equal(t, goal, s)
  36. }
  37. func TestStructure_Slice(t *testing.T) {
  38. rawMap := map[string]any{
  39. "foo": 1,
  40. "bar": []string{"one", "two"},
  41. }
  42. goal := &BazSlice{
  43. Foo: 1,
  44. Bar: []string{"one", "two"},
  45. }
  46. s := &BazSlice{}
  47. err := decoder.Decode(rawMap, s)
  48. assert.Nil(t, err)
  49. assert.Equal(t, goal, s)
  50. }
  51. func TestStructure_Optional(t *testing.T) {
  52. rawMap := map[string]any{
  53. "foo": 1,
  54. }
  55. goal := &BazOptional{
  56. Foo: 1,
  57. }
  58. s := &BazOptional{}
  59. err := decoder.Decode(rawMap, s)
  60. assert.Nil(t, err)
  61. assert.Equal(t, goal, s)
  62. }
  63. func TestStructure_MissingKey(t *testing.T) {
  64. rawMap := map[string]any{
  65. "foo": 1,
  66. }
  67. s := &Baz{}
  68. err := decoder.Decode(rawMap, s)
  69. assert.NotNilf(t, err, "should throw error: %#v", s)
  70. }
  71. func TestStructure_ParamError(t *testing.T) {
  72. rawMap := map[string]any{}
  73. s := Baz{}
  74. err := decoder.Decode(rawMap, s)
  75. assert.NotNilf(t, err, "should throw error: %#v", s)
  76. }
  77. func TestStructure_SliceTypeError(t *testing.T) {
  78. rawMap := map[string]any{
  79. "foo": 1,
  80. "bar": []int{1, 2},
  81. }
  82. s := &BazSlice{}
  83. err := decoder.Decode(rawMap, s)
  84. assert.NotNilf(t, err, "should throw error: %#v", s)
  85. }
  86. func TestStructure_WeakType(t *testing.T) {
  87. rawMap := map[string]any{
  88. "foo": "1",
  89. "bar": []int{1},
  90. }
  91. goal := &BazSlice{
  92. Foo: 1,
  93. Bar: []string{"1"},
  94. }
  95. s := &BazSlice{}
  96. err := weakTypeDecoder.Decode(rawMap, s)
  97. assert.Nil(t, err)
  98. assert.Equal(t, goal, s)
  99. }
  100. func TestStructure_Nest(t *testing.T) {
  101. rawMap := map[string]any{
  102. "foo": 1,
  103. }
  104. goal := BazOptional{
  105. Foo: 1,
  106. }
  107. s := &struct {
  108. BazOptional
  109. }{}
  110. err := decoder.Decode(rawMap, s)
  111. assert.Nil(t, err)
  112. assert.Equal(t, s.BazOptional, goal)
  113. }
  114. func TestStructure_SliceNilValue(t *testing.T) {
  115. rawMap := map[string]any{
  116. "foo": 1,
  117. "bar": []any{"bar", nil},
  118. }
  119. goal := &BazSlice{
  120. Foo: 1,
  121. Bar: []string{"bar", ""},
  122. }
  123. s := &BazSlice{}
  124. err := weakTypeDecoder.Decode(rawMap, s)
  125. assert.Nil(t, err)
  126. assert.Equal(t, goal.Bar, s.Bar)
  127. s = &BazSlice{}
  128. err = decoder.Decode(rawMap, s)
  129. assert.NotNil(t, err)
  130. }
  131. func TestStructure_SliceNilValueComplex(t *testing.T) {
  132. rawMap := map[string]any{
  133. "bar": []any{map[string]any{"bar": "foo"}, nil},
  134. }
  135. s := &struct {
  136. Bar []map[string]any `test:"bar"`
  137. }{}
  138. err := decoder.Decode(rawMap, s)
  139. assert.Nil(t, err)
  140. assert.Nil(t, s.Bar[1])
  141. ss := &struct {
  142. Bar []Baz `test:"bar"`
  143. }{}
  144. err = decoder.Decode(rawMap, ss)
  145. assert.NotNil(t, err)
  146. }