예제 #1
0
// Get a value from a DynamoDB table containing entries like:
// {"id": "my primary key", "value": "valuable value"}
func (ig *ItemGetter) Get(id string) (value string) {
	var input = &dynamodb.GetItemInput{
		Key: map[string]*dynamodb.AttributeValue{
			"id": {
				S: aws.String(id),
			},
		},
		TableName: aws.String("my_table"),
		AttributesToGet: []*string{
			aws.String("value"),
		},
	}
	if output, err := ig.DynamoDB.GetItem(input); err == nil {
		if _, ok := output.Item["value"]; ok {
			dynamodbattribute.Unmarshal(output.Item["value"], &value)
		}
	}
	return
}
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
}