-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
CreateTableItem.go
101 lines (88 loc) · 3.26 KB
/
CreateTableItem.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[dynamodb.go.create_new_item]
package main
// snippet-start:[dynamodb.go.create_new_item.imports]
import (
"flag"
"fmt"
"strconv"
"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.create_new_item.imports]
// Item holds info about new item
// snippet-start:[dynamodb.go.create_new_item.struct]
type Item struct {
Year int
Title string
Plot string
Rating float64
}
// snippet-end:[dynamodb.go.create_new_item.struct]
// AddTableItem adds an item to an Amazon DynamoDB table
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// year is the year when the movie was released
// table is the name of the table
// title is the movie title
// plot is a summary of the plot of the movie
// rating is the movie rating, from 0.0 to 10.0
// Output:
// If success, nil
// Otherwise, an error from the call to PutItem
func AddTableItem(svc dynamodbiface.DynamoDBAPI, year *int, table, title, plot *string, rating *float64) error {
// snippet-start:[dynamodb.go.create_new_item.assign_struct]
item := Item{
Year: *year,
Title: *title,
Plot: *plot,
Rating: *rating,
}
av, err := dynamodbattribute.MarshalMap(item)
// snippet-end:[dynamodb.go.create_new_item.assign_struct]
if err != nil {
return err
}
// snippet-start:[dynamodb.go.create_new_item.call]
_, err = svc.PutItem(&dynamodb.PutItemInput{
Item: av,
TableName: table,
})
// snippet-end:[dynamodb.go.create_new_item.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[dynamodb.go.create_new_item.args]
table := flag.String("d", "", "The name of the database table")
year := flag.Int("y", 0, "The year the movie debuted")
title := flag.String("t", "", "The title of the movie")
plot := flag.String("p", "", "The plot of the movie")
rating := flag.Float64("r", -1.0, "The movie rating, from 0.0 to 10.0")
flag.Parse()
if *table == "" || *year == 0 || *title == "" || *plot == "" || *rating == -1.0 {
fmt.Println("You must supply a database table name, year, title, plot and rating")
fmt.Println("-d TABLE -y YEAR -t TITLE -r RATING")
return
}
// snippet-end:[dynamodb.go.create_new_item.args]
// snippet-start:[dynamodb.go.create_new_item.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
// snippet-end:[dynamodb.go.create_new_item.session]
err := AddTableItem(svc, year, table, title, plot, rating)
if err != nil {
fmt.Println("Got an error adding item to table:")
fmt.Println(err)
return
}
fmt.Println("Successfully added '"+*title+"' ("+strconv.Itoa(*year)+") to table "+*table+" with rating", *rating)
}
// snippet-end:[dynamodb.go.create_new_item]