File: marshaler_examples_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.44.133-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 245,296 kB
  • sloc: makefile: 120
file content (104 lines) | stat: -rw-r--r-- 2,029 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package dynamodbattribute_test

import (
	"fmt"
	"reflect"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)

func ExampleMarshal() {
	type Record struct {
		Bytes   []byte
		MyField string
		Letters []string
		Numbers []int
	}

	r := Record{
		Bytes:   []byte{48, 49},
		MyField: "MyFieldValue",
		Letters: []string{"a", "b", "c", "d"},
		Numbers: []int{1, 2, 3},
	}
	av, err := dynamodbattribute.Marshal(r)
	fmt.Println("err", err)
	fmt.Println("Bytes", av.M["Bytes"])
	fmt.Println("MyField", av.M["MyField"])
	fmt.Println("Letters", av.M["Letters"])
	fmt.Println("Numbers", av.M["Numbers"])

	// Output:
	// err <nil>
	// Bytes {
	//   B: <binary> len 2
	// }
	// MyField {
	//   S: "MyFieldValue"
	// }
	// Letters {
	//   L: [
	//     {
	//       S: "a"
	//     },
	//     {
	//       S: "b"
	//     },
	//     {
	//       S: "c"
	//     },
	//     {
	//       S: "d"
	//     }
	//   ]
	// }
	// Numbers {
	//   L: [{
	//       N: "1"
	//     },{
	//       N: "2"
	//     },{
	//       N: "3"
	//     }]
	// }
}

func ExampleUnmarshal() {
	type Record struct {
		Bytes   []byte
		MyField string
		Letters []string
		A2Num   map[string]int
	}

	expect := Record{
		Bytes:   []byte{48, 49},
		MyField: "MyFieldValue",
		Letters: []string{"a", "b", "c", "d"},
		A2Num:   map[string]int{"a": 1, "b": 2, "c": 3},
	}

	av := &dynamodb.AttributeValue{
		M: map[string]*dynamodb.AttributeValue{
			"Bytes":   {B: []byte{48, 49}},
			"MyField": {S: aws.String("MyFieldValue")},
			"Letters": {L: []*dynamodb.AttributeValue{
				{S: aws.String("a")}, {S: aws.String("b")}, {S: aws.String("c")}, {S: aws.String("d")},
			}},
			"A2Num": {M: map[string]*dynamodb.AttributeValue{
				"a": {N: aws.String("1")},
				"b": {N: aws.String("2")},
				"c": {N: aws.String("3")},
			}},
		},
	}

	actual := Record{}
	err := dynamodbattribute.Unmarshal(av, &actual)
	fmt.Println(err, reflect.DeepEqual(expect, actual))

	// Output:
	// <nil> true
}