forked from awsdocs/aws-doc-sdk-examples
/
GetItem.go
116 lines (101 loc) · 3.42 KB
/
GetItem.go
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
113
114
115
116
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[dynamodb.go.get_item]
package main
// snippet-start:[dynamodb.go.get_item.imports]
import (
"errors"
"flag"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// snippet-end:[dynamodb.go.get_item.imports]
// Item defines the item for the table
// snippet-start:[dynamodb.go.get_item.struct]
type Item struct {
Year int
Title string
Plot string
Rating float64
}
// snippet-end:[dynamodb.go.get_item.struct]
// GetTableItem retrieves the item with the year and title from the table
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// table is the name of the table
// title is the movie title
// year is when the movie was released
// Output:
// If success, the information about the table item and nil
// Otherwise, nil and an error from the call to GetItem or UnmarshalMap
func GetTableItem(svc dynamodbiface.DynamoDBAPI, table, title *string, year *int) (*Item, error) {
// snippet-start:[dynamodb.go.get_item.call]
result, err := svc.GetItem(&dynamodb.GetItemInput{
TableName: table,
Key: map[string]*dynamodb.AttributeValue{
"year": {
N: aws.String(strconv.Itoa(*year)),
},
"title": {
S: title,
},
},
})
// snippet-end:[dynamodb.go.get_item.call]
if err != nil {
return nil, err
}
// snippet-start:[dynamodb.go.get_item.unmarshall]
if result.Item == nil {
msg := "Could not find '" + *title + "'"
return nil, errors.New(msg)
}
item := Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
// snippet-end:[dynamodb.go.get_item.unmarshall]
if err != nil {
return nil, err
}
return &item, nil
}
func main() {
// snippet-start:[dynamodb.go.get_item.args]
table := flag.String("t", "", "The table to retrieve item from")
title := flag.String("n", "", "The name of the movie")
year := flag.Int("y", -1, "The year the movie was released")
flag.Parse()
if *table == "" || *title == "" || *year == -1 {
fmt.Println("You must supply a table name, movie title, and valid year")
fmt.Println("(-t TABLE -n NAME -y YEAR")
return
}
// snippet-end:[dynamodb.go.get_item.args]
// snippet-start:[dynamodb.go.get_item.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB client
svc := dynamodb.New(sess)
// snippet-end:[dynamodb.go.get_item.session]
item, err := GetTableItem(svc, table, title, year)
if err != nil {
fmt.Println("Got an error retrieving the item:")
fmt.Println(err)
return
}
if item == nil {
fmt.Println("Could not find the table entry")
return
}
fmt.Println("Found item:")
fmt.Println("Year: ", item.Year)
fmt.Println("Title: ", item.Title)
fmt.Println("Plot: ", item.Plot)
fmt.Println("Rating:", item.Rating)
}
// snippet-end:[dynamodb.go.get_item]