File: marshaler_examples_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (112 lines) | stat: -rw-r--r-- 2,522 bytes parent folder | download | duplicates (5)
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
105
106
107
108
109
110
111
112
package attributevalue_test

import (
	"fmt"
	"reflect"

	"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
	"github.com/aws/aws-sdk-go-v2/internal/awsutil"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

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 := attributevalue.Marshal(r)
	m := av.(*types.AttributeValueMemberM)
	fmt.Println("err", err)
	fmt.Println("Bytes", awsutil.Prettify(m.Value["Bytes"]))
	fmt.Println("MyField", awsutil.Prettify(m.Value["MyField"]))
	fmt.Println("Letters", awsutil.Prettify(m.Value["Letters"]))
	fmt.Println("Numbers", awsutil.Prettify(m.Value["Numbers"]))

	// output:
	// err <nil>
	// Bytes &{
	//   Value: <binary> len 2
	// }
	// MyField &{
	//   Value: "MyFieldValue"
	// }
	// Letters &{
	//   Value: [
	//     &{
	//       Value: "a"
	//     },
	//     &{
	//       Value: "b"
	//     },
	//     &{
	//       Value: "c"
	//     },
	//     &{
	//       Value: "d"
	//     }
	//   ]
	// }
	// Numbers &{
	//   Value: [
	//     &{
	//       Value: "1"
	//     },
	//     &{
	//       Value: "2"
	//     },
	//     &{
	//       Value: "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 := &types.AttributeValueMemberM{
		Value: map[string]types.AttributeValue{
			"Bytes":   &types.AttributeValueMemberB{Value: []byte{48, 49}},
			"MyField": &types.AttributeValueMemberS{Value: "MyFieldValue"},
			"Letters": &types.AttributeValueMemberL{Value: []types.AttributeValue{
				&types.AttributeValueMemberS{Value: "a"},
				&types.AttributeValueMemberS{Value: "b"},
				&types.AttributeValueMemberS{Value: "c"},
				&types.AttributeValueMemberS{Value: "d"},
			}},
			"A2Num": &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{
				"a": &types.AttributeValueMemberN{Value: "1"},
				"b": &types.AttributeValueMemberN{Value: "2"},
				"c": &types.AttributeValueMemberN{Value: "3"},
			}},
		},
	}

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

	// Output:
	// <nil> true
}