/
workout-populate.go
61 lines (49 loc) · 1.27 KB
/
workout-populate.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"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"
)
type Workout struct {
ID int `json:"id"`
Equipment string `json:"equipment"`
User string `json:"user"`
Name string `json:"name"`
Description string `json:"description"`
// Gym string `json:"gym"`
}
func main() {
svc := dynamodb.New(session.New(), &aws.Config{Endpoint: aws.String("http://localhost:8000"), Region: aws.String("us-east-1")})
file, e := ioutil.ReadFile("./mockdata/MOCK_DATA-workout.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
var u []Workout
json.Unmarshal(file, &u)
for i := 0; i < len(u); i++ {
fmt.Printf("Name: %v\n", u[i].Name)
item, err := dynamodbattribute.ConvertToMap(u[i])
if err != nil {
fmt.Println("Failed to convert", err)
return
}
fmt.Printf("Item %v\n", item)
params := &dynamodb.PutItemInput{
Item: item,
TableName: aws.String("workout"),
}
resp, err := svc.PutItem(params)
if err != nil {
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}
}