-
Notifications
You must be signed in to change notification settings - Fork 0
/
usd.go
55 lines (49 loc) · 1.37 KB
/
usd.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
package main
// snippet-start:[dynamodb.go.read_item.imports]
import (
"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-lambda-go/lambda"
"fmt"
//"context"
)
type Item struct {
Currency string
Rating string
}
func Handle(item Item)(string,error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB client
svc := dynamodb.New(sess)
tableName := "Currency"
curr := "USD"
result, err := svc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
"Currency": {
S: aws.String(curr),
},
},
})
if err != nil {
return fmt.Sprintf(err.Error()),nil
}
if result.Item == nil {
msg := "Could not find "
return fmt.Sprintf(msg),nil
}
item = Item{}
err = dynamodbattribute.UnmarshalMap(result.Item, &item)
if err != nil {
panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
}
return fmt.Sprintf(item.Rating),nil
// snippet-end:[dynamodb.go.read_item.unmarshall]
}
func main(){
lambda.Start(Handle)
}