forked from awsdocs/aws-doc-sdk-examples
/
UpdateItem.go
116 lines (101 loc) · 3.55 KB
/
UpdateItem.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.update_movie]
package main
// snippet-start:[dynamodb.go.update_movie.imports]
import (
"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/dynamodbiface"
)
// snippet-end:[dynamodb.go.update_movie.imports]
// UpdateMovie updates the year and rating of a movie in a table
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// tableName is the name of the table
// movieName is the name of the movie
// movieYear is the year the movie was released
// movieRating is the rating, from 0.0 to 1.0, of the movie
// Output:
// If success, nil
// Otherwise, an error from the call to UpdateItem
func UpdateMovie(svc dynamodbiface.DynamoDBAPI, table, movie, year, rating *string) error {
// snippet-start:[dynamodb.go.update_movie.call]
input := &dynamodb.UpdateItemInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":r": {
N: rating,
},
},
TableName: table,
Key: map[string]*dynamodb.AttributeValue{
"Year": {
N: year,
},
"Title": {
S: movie,
},
},
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set Rating = :r"),
}
_, err := svc.UpdateItem(input)
// snippet-end:[dynamodb.go.update_movie.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[dynamodb.go.update_movie.args]
table := flag.String("t", "", "The name of the table")
movie := flag.String("m", "", "The name of the movie")
year := flag.String("y", "", "The year the movie was made")
rating := flag.String("r", "", "The rating, from 0 (zero) to 1 (one)")
flag.Parse()
if *table == "" || *movie == "" || *year == "" || *rating == "" {
fmt.Println("You must supply a table name (-t TABLE), movie name (-m MOVIE), movie year (-y YEAR), and rating (-r RATING)")
return
}
// Make sure rating is in the range 0 to 1, and year is in the range 1900 - 2020
yearInt, err := strconv.Atoi(*year)
if err != nil {
fmt.Println("Year is not an integer value")
return
}
if yearInt < 1900 {
*year = "1900"
} else if yearInt > 2020 {
*year = "2020"
}
ratingFloat, err := strconv.ParseFloat(*rating, 64)
if err != nil {
fmt.Println("Rating is not a floating-point value")
return
}
if ratingFloat < 0.0 {
*rating = "0.0"
} else if ratingFloat > 1.0 {
*rating = "1.0"
}
// snippet-end:[dynamodb.go.update_movie.args]
// snippet-start:[dynamodb.go.update_movie.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
// snippet-end:[dynamodb.go.update_movie.session]
err = UpdateMovie(svc, table, movie, year, rating)
if err != nil {
fmt.Println(err)
return
}
// snippet-start:[dynamodb.go.update_movie.print]
fmt.Println("Successfully updated '" + *movie + "' (" + *year + ") rating to " + *rating)
// snippet-end:[dynamodb.go.update_movie.print]
}
// snippet-end:[dynamodb.go.update_movie]