decode_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package dynamodbattribute
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. "testing"
  7. "time"
  8. "github.com/aws/aws-sdk-go/aws"
  9. "github.com/aws/aws-sdk-go/aws/awserr"
  10. "github.com/aws/aws-sdk-go/service/dynamodb"
  11. )
  12. func TestUnmarshalErrorTypes(t *testing.T) {
  13. var _ awserr.Error = (*UnmarshalTypeError)(nil)
  14. var _ awserr.Error = (*InvalidUnmarshalError)(nil)
  15. }
  16. func TestUnmarshalShared(t *testing.T) {
  17. for i, c := range sharedTestCases {
  18. err := Unmarshal(c.in, c.actual)
  19. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  20. }
  21. }
  22. func TestUnmarshal(t *testing.T) {
  23. cases := []struct {
  24. in *dynamodb.AttributeValue
  25. actual, expected interface{}
  26. err error
  27. }{
  28. //------------
  29. // Sets
  30. //------------
  31. {
  32. in: &dynamodb.AttributeValue{BS: [][]byte{
  33. {48, 49}, {50, 51},
  34. }},
  35. actual: &[][]byte{},
  36. expected: [][]byte{{48, 49}, {50, 51}},
  37. },
  38. {
  39. in: &dynamodb.AttributeValue{NS: []*string{
  40. aws.String("123"), aws.String("321"),
  41. }},
  42. actual: &[]int{},
  43. expected: []int{123, 321},
  44. },
  45. {
  46. in: &dynamodb.AttributeValue{NS: []*string{
  47. aws.String("123"), aws.String("321"),
  48. }},
  49. actual: &[]interface{}{},
  50. expected: []interface{}{123., 321.},
  51. },
  52. {
  53. in: &dynamodb.AttributeValue{SS: []*string{
  54. aws.String("abc"), aws.String("123"),
  55. }},
  56. actual: &[]string{},
  57. expected: &[]string{"abc", "123"},
  58. },
  59. {
  60. in: &dynamodb.AttributeValue{SS: []*string{
  61. aws.String("abc"), aws.String("123"),
  62. }},
  63. actual: &[]*string{},
  64. expected: &[]*string{aws.String("abc"), aws.String("123")},
  65. },
  66. //------------
  67. // Interfaces
  68. //------------
  69. {
  70. in: &dynamodb.AttributeValue{B: []byte{48, 49}},
  71. actual: func() interface{} {
  72. var v interface{}
  73. return &v
  74. }(),
  75. expected: []byte{48, 49},
  76. },
  77. {
  78. in: &dynamodb.AttributeValue{BS: [][]byte{
  79. {48, 49}, {50, 51},
  80. }},
  81. actual: func() interface{} {
  82. var v interface{}
  83. return &v
  84. }(),
  85. expected: [][]byte{{48, 49}, {50, 51}},
  86. },
  87. {
  88. in: &dynamodb.AttributeValue{BOOL: aws.Bool(true)},
  89. actual: func() interface{} {
  90. var v interface{}
  91. return &v
  92. }(),
  93. expected: bool(true),
  94. },
  95. {
  96. in: &dynamodb.AttributeValue{L: []*dynamodb.AttributeValue{
  97. {S: aws.String("abc")}, {S: aws.String("123")},
  98. }},
  99. actual: func() interface{} {
  100. var v interface{}
  101. return &v
  102. }(),
  103. expected: []interface{}{"abc", "123"},
  104. },
  105. {
  106. in: &dynamodb.AttributeValue{M: map[string]*dynamodb.AttributeValue{
  107. "123": {S: aws.String("abc")},
  108. "abc": {S: aws.String("123")},
  109. }},
  110. actual: func() interface{} {
  111. var v interface{}
  112. return &v
  113. }(),
  114. expected: map[string]interface{}{"123": "abc", "abc": "123"},
  115. },
  116. {
  117. in: &dynamodb.AttributeValue{N: aws.String("123")},
  118. actual: func() interface{} {
  119. var v interface{}
  120. return &v
  121. }(),
  122. expected: float64(123),
  123. },
  124. {
  125. in: &dynamodb.AttributeValue{NS: []*string{
  126. aws.String("123"), aws.String("321"),
  127. }},
  128. actual: func() interface{} {
  129. var v interface{}
  130. return &v
  131. }(),
  132. expected: []float64{123., 321.},
  133. },
  134. {
  135. in: &dynamodb.AttributeValue{S: aws.String("123")},
  136. actual: func() interface{} {
  137. var v interface{}
  138. return &v
  139. }(),
  140. expected: "123",
  141. },
  142. {
  143. in: &dynamodb.AttributeValue{SS: []*string{
  144. aws.String("123"), aws.String("321"),
  145. }},
  146. actual: func() interface{} {
  147. var v interface{}
  148. return &v
  149. }(),
  150. expected: []string{"123", "321"},
  151. },
  152. {
  153. in: &dynamodb.AttributeValue{M: map[string]*dynamodb.AttributeValue{
  154. "abc": {S: aws.String("123")},
  155. "Cba": {S: aws.String("321")},
  156. }},
  157. actual: &struct{ Abc, Cba string }{},
  158. expected: struct{ Abc, Cba string }{Abc: "123", Cba: "321"},
  159. },
  160. {
  161. in: &dynamodb.AttributeValue{N: aws.String("512")},
  162. actual: new(uint8),
  163. err: &UnmarshalTypeError{
  164. Value: fmt.Sprintf("number overflow, 512"),
  165. Type: reflect.TypeOf(uint8(0)),
  166. },
  167. },
  168. }
  169. for i, c := range cases {
  170. err := Unmarshal(c.in, c.actual)
  171. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  172. }
  173. }
  174. func TestInterfaceInput(t *testing.T) {
  175. var v interface{}
  176. expected := []interface{}{"abc", "123"}
  177. err := Unmarshal(&dynamodb.AttributeValue{L: []*dynamodb.AttributeValue{
  178. {S: aws.String("abc")}, {S: aws.String("123")},
  179. }}, &v)
  180. assertConvertTest(t, 0, v, expected, err, nil)
  181. }
  182. func TestUnmarshalError(t *testing.T) {
  183. cases := []struct {
  184. in *dynamodb.AttributeValue
  185. actual, expected interface{}
  186. err error
  187. }{
  188. {
  189. in: &dynamodb.AttributeValue{},
  190. actual: int(0),
  191. expected: nil,
  192. err: &InvalidUnmarshalError{Type: reflect.TypeOf(int(0))},
  193. },
  194. }
  195. for i, c := range cases {
  196. err := Unmarshal(c.in, c.actual)
  197. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  198. }
  199. }
  200. func TestUnmarshalListShared(t *testing.T) {
  201. for i, c := range sharedListTestCases {
  202. err := UnmarshalList(c.in, c.actual)
  203. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  204. }
  205. }
  206. func TestUnmarshalListError(t *testing.T) {
  207. cases := []struct {
  208. in []*dynamodb.AttributeValue
  209. actual, expected interface{}
  210. err error
  211. }{
  212. {
  213. in: []*dynamodb.AttributeValue{},
  214. actual: []interface{}{},
  215. expected: nil,
  216. err: &InvalidUnmarshalError{Type: reflect.TypeOf([]interface{}{})},
  217. },
  218. }
  219. for i, c := range cases {
  220. err := UnmarshalList(c.in, c.actual)
  221. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  222. }
  223. }
  224. func TestUnmarshalConvertToData(t *testing.T) {
  225. type T struct {
  226. Int int
  227. Str string
  228. ByteSlice []byte
  229. StrSlice []string
  230. }
  231. exp := T{
  232. Int: 42,
  233. Str: "foo",
  234. ByteSlice: []byte{42, 97, 83},
  235. StrSlice: []string{"cat", "dog"},
  236. }
  237. av, err := ConvertToMap(exp)
  238. if err != nil {
  239. t.Fatalf("expect no error, got %v", err)
  240. }
  241. var act T
  242. err = UnmarshalMap(av, &act)
  243. assertConvertTest(t, 0, act, exp, err, nil)
  244. }
  245. func TestUnmarshalMapShared(t *testing.T) {
  246. for i, c := range sharedMapTestCases {
  247. err := UnmarshalMap(c.in, c.actual)
  248. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  249. }
  250. }
  251. func TestUnmarshalMapError(t *testing.T) {
  252. cases := []struct {
  253. in map[string]*dynamodb.AttributeValue
  254. actual, expected interface{}
  255. err error
  256. }{
  257. {
  258. in: map[string]*dynamodb.AttributeValue{},
  259. actual: map[string]interface{}{},
  260. expected: nil,
  261. err: &InvalidUnmarshalError{Type: reflect.TypeOf(map[string]interface{}{})},
  262. },
  263. {
  264. in: map[string]*dynamodb.AttributeValue{
  265. "BOOL": {BOOL: aws.Bool(true)},
  266. },
  267. actual: &map[int]interface{}{},
  268. expected: nil,
  269. err: &UnmarshalTypeError{Value: "map string key", Type: reflect.TypeOf(int(0))},
  270. },
  271. }
  272. for i, c := range cases {
  273. err := UnmarshalMap(c.in, c.actual)
  274. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  275. }
  276. }
  277. func TestUnmarshalListOfMaps(t *testing.T) {
  278. type testItem struct {
  279. Value string
  280. Value2 int
  281. }
  282. cases := []struct {
  283. in []map[string]*dynamodb.AttributeValue
  284. actual, expected interface{}
  285. err error
  286. }{
  287. { // Simple map conversion.
  288. in: []map[string]*dynamodb.AttributeValue{
  289. {
  290. "Value": &dynamodb.AttributeValue{
  291. BOOL: aws.Bool(true),
  292. },
  293. },
  294. },
  295. actual: &[]map[string]interface{}{},
  296. expected: []map[string]interface{}{
  297. {
  298. "Value": true,
  299. },
  300. },
  301. },
  302. { // attribute to struct.
  303. in: []map[string]*dynamodb.AttributeValue{
  304. {
  305. "Value": &dynamodb.AttributeValue{
  306. S: aws.String("abc"),
  307. },
  308. "Value2": &dynamodb.AttributeValue{
  309. N: aws.String("123"),
  310. },
  311. },
  312. },
  313. actual: &[]testItem{},
  314. expected: []testItem{
  315. {
  316. Value: "abc",
  317. Value2: 123,
  318. },
  319. },
  320. },
  321. }
  322. for i, c := range cases {
  323. err := UnmarshalListOfMaps(c.in, c.actual)
  324. assertConvertTest(t, i, c.actual, c.expected, err, c.err)
  325. }
  326. }
  327. type unmarshalUnmarshaler struct {
  328. Value string
  329. Value2 int
  330. Value3 bool
  331. Value4 time.Time
  332. }
  333. func (u *unmarshalUnmarshaler) UnmarshalDynamoDBAttributeValue(av *dynamodb.AttributeValue) error {
  334. if av.M == nil {
  335. return fmt.Errorf("expected AttributeValue to be map")
  336. }
  337. if v, ok := av.M["abc"]; !ok {
  338. return fmt.Errorf("expected `abc` map key")
  339. } else if v.S == nil {
  340. return fmt.Errorf("expected `abc` map value string")
  341. } else {
  342. u.Value = *v.S
  343. }
  344. if v, ok := av.M["def"]; !ok {
  345. return fmt.Errorf("expected `def` map key")
  346. } else if v.N == nil {
  347. return fmt.Errorf("expected `def` map value number")
  348. } else {
  349. n, err := strconv.ParseInt(*v.N, 10, 64)
  350. if err != nil {
  351. return err
  352. }
  353. u.Value2 = int(n)
  354. }
  355. if v, ok := av.M["ghi"]; !ok {
  356. return fmt.Errorf("expected `ghi` map key")
  357. } else if v.BOOL == nil {
  358. return fmt.Errorf("expected `ghi` map value number")
  359. } else {
  360. u.Value3 = *v.BOOL
  361. }
  362. if v, ok := av.M["jkl"]; !ok {
  363. return fmt.Errorf("expected `jkl` map key")
  364. } else if v.S == nil {
  365. return fmt.Errorf("expected `jkl` map value string")
  366. } else {
  367. t, err := time.Parse(time.RFC3339, *v.S)
  368. if err != nil {
  369. return err
  370. }
  371. u.Value4 = t
  372. }
  373. return nil
  374. }
  375. func TestUnmarshalUnmashaler(t *testing.T) {
  376. u := &unmarshalUnmarshaler{}
  377. av := &dynamodb.AttributeValue{
  378. M: map[string]*dynamodb.AttributeValue{
  379. "abc": {S: aws.String("value")},
  380. "def": {N: aws.String("123")},
  381. "ghi": {BOOL: aws.Bool(true)},
  382. "jkl": {S: aws.String("2016-05-03T17:06:26.209072Z")},
  383. },
  384. }
  385. err := Unmarshal(av, u)
  386. if err != nil {
  387. t.Errorf("expect no error, got %v", err)
  388. }
  389. if e, a := "value", u.Value; e != a {
  390. t.Errorf("expect %v, got %v", e, a)
  391. }
  392. if e, a := 123, u.Value2; e != a {
  393. t.Errorf("expect %v, got %v", e, a)
  394. }
  395. if e, a := true, u.Value3; e != a {
  396. t.Errorf("expect %v, got %v", e, a)
  397. }
  398. if e, a := testDate, u.Value4; e != a {
  399. t.Errorf("expect %v, got %v", e, a)
  400. }
  401. }
  402. func TestDecodeUseNumber(t *testing.T) {
  403. u := map[string]interface{}{}
  404. av := &dynamodb.AttributeValue{
  405. M: map[string]*dynamodb.AttributeValue{
  406. "abc": {S: aws.String("value")},
  407. "def": {N: aws.String("123")},
  408. "ghi": {BOOL: aws.Bool(true)},
  409. },
  410. }
  411. decoder := NewDecoder(func(d *Decoder) {
  412. d.UseNumber = true
  413. })
  414. err := decoder.Decode(av, &u)
  415. if err != nil {
  416. t.Errorf("expect no error, got %v", err)
  417. }
  418. if e, a := "value", u["abc"]; e != a {
  419. t.Errorf("expect %v, got %v", e, a)
  420. }
  421. n := u["def"].(Number)
  422. if e, a := "123", n.String(); e != a {
  423. t.Errorf("expect %v, got %v", e, a)
  424. }
  425. if e, a := true, u["ghi"]; e != a {
  426. t.Errorf("expect %v, got %v", e, a)
  427. }
  428. }
  429. func TestDecodeUseNumberNumberSet(t *testing.T) {
  430. u := map[string]interface{}{}
  431. av := &dynamodb.AttributeValue{
  432. M: map[string]*dynamodb.AttributeValue{
  433. "ns": {
  434. NS: []*string{
  435. aws.String("123"), aws.String("321"),
  436. },
  437. },
  438. },
  439. }
  440. decoder := NewDecoder(func(d *Decoder) {
  441. d.UseNumber = true
  442. })
  443. err := decoder.Decode(av, &u)
  444. if err != nil {
  445. t.Errorf("expect no error, got %v", err)
  446. }
  447. ns := u["ns"].([]Number)
  448. if e, a := "123", ns[0].String(); e != a {
  449. t.Errorf("expect %v, got %v", e, a)
  450. }
  451. if e, a := "321", ns[1].String(); e != a {
  452. t.Errorf("expect %v, got %v", e, a)
  453. }
  454. }
  455. func TestDecodeEmbeddedPointerStruct(t *testing.T) {
  456. type B struct {
  457. Bint int
  458. }
  459. type C struct {
  460. Cint int
  461. }
  462. type A struct {
  463. Aint int
  464. *B
  465. *C
  466. }
  467. av := &dynamodb.AttributeValue{
  468. M: map[string]*dynamodb.AttributeValue{
  469. "Aint": {
  470. N: aws.String("321"),
  471. },
  472. "Bint": {
  473. N: aws.String("123"),
  474. },
  475. },
  476. }
  477. decoder := NewDecoder()
  478. a := A{}
  479. err := decoder.Decode(av, &a)
  480. if err != nil {
  481. t.Errorf("expect no error, got %v", err)
  482. }
  483. if e, a := 321, a.Aint; e != a {
  484. t.Errorf("expect %v, got %v", e, a)
  485. }
  486. // Embedded pointer struct can be created automatically.
  487. if e, a := 123, a.Bint; e != a {
  488. t.Errorf("expect %v, got %v", e, a)
  489. }
  490. // But not for absent fields.
  491. if a.C != nil {
  492. t.Errorf("expect nil, got %v", a.C)
  493. }
  494. }
  495. func TestDecodeBooleanOverlay(t *testing.T) {
  496. type BooleanOverlay bool
  497. av := &dynamodb.AttributeValue{
  498. BOOL: aws.Bool(true),
  499. }
  500. decoder := NewDecoder()
  501. var v BooleanOverlay
  502. err := decoder.Decode(av, &v)
  503. if err != nil {
  504. t.Errorf("expect no error, got %v", err)
  505. }
  506. if e, a := BooleanOverlay(true), v; e != a {
  507. t.Errorf("expect %v, got %v", e, a)
  508. }
  509. }
  510. func TestDecodeUnixTime(t *testing.T) {
  511. type A struct {
  512. Normal time.Time
  513. Tagged time.Time `dynamodbav:",unixtime"`
  514. Typed UnixTime
  515. }
  516. expect := A{
  517. Normal: time.Unix(123, 0).UTC(),
  518. Tagged: time.Unix(456, 0),
  519. Typed: UnixTime(time.Unix(789, 0)),
  520. }
  521. input := &dynamodb.AttributeValue{
  522. M: map[string]*dynamodb.AttributeValue{
  523. "Normal": {
  524. S: aws.String("1970-01-01T00:02:03Z"),
  525. },
  526. "Tagged": {
  527. N: aws.String("456"),
  528. },
  529. "Typed": {
  530. N: aws.String("789"),
  531. },
  532. },
  533. }
  534. actual := A{}
  535. err := Unmarshal(input, &actual)
  536. if err != nil {
  537. t.Errorf("expect no error, got %v", err)
  538. }
  539. if e, a := expect, actual; e != a {
  540. t.Errorf("expect %v, got %v", e, a)
  541. }
  542. }
  543. func TestDecodeAliasedUnixTime(t *testing.T) {
  544. type A struct {
  545. Normal AliasedTime
  546. Tagged AliasedTime `dynamodbav:",unixtime"`
  547. }
  548. expect := A{
  549. Normal: AliasedTime(time.Unix(123, 0).UTC()),
  550. Tagged: AliasedTime(time.Unix(456, 0)),
  551. }
  552. input := &dynamodb.AttributeValue{
  553. M: map[string]*dynamodb.AttributeValue{
  554. "Normal": {
  555. S: aws.String("1970-01-01T00:02:03Z"),
  556. },
  557. "Tagged": {
  558. N: aws.String("456"),
  559. },
  560. },
  561. }
  562. actual := A{}
  563. err := Unmarshal(input, &actual)
  564. if err != nil {
  565. t.Errorf("expect no error, got %v", err)
  566. }
  567. if expect != actual {
  568. t.Errorf("expect %v, got %v", expect, actual)
  569. }
  570. }