-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
78 lines (66 loc) · 1.71 KB
/
examples_test.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
package gonetable_test
import (
"context"
"encoding/json"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/juranki/gonetable"
)
const TABLENAME = "SchemaExample"
type ExampleDocument struct {
ID string
Name string
}
func (ed *ExampleDocument) Gonetable_TypeID() string {
return "ed"
}
func (ed *ExampleDocument) Gonetable_Key() gonetable.CompositeKey {
return gonetable.CompositeKey{
HashSegments: []string{"ed", ed.ID},
RangeSegments: []string{"ed"},
}
}
func ExampleSchema() {
cfg := MustLoadLocalDDBConfig()
client := dynamodb.NewFromConfig(cfg)
DeleteTableIfExists(context.Background(), client, TABLENAME)
schema, err := gonetable.NewSchema([]gonetable.Document{
&ExampleDocument{},
})
if err != nil {
panic(err)
}
_, err = client.CreateTable(
context.Background(),
&dynamodb.CreateTableInput{
TableName: aws.String(TABLENAME),
BillingMode: types.BillingModePayPerRequest,
AttributeDefinitions: schema.AttributeDefinitions(),
KeySchema: schema.KeySchema(),
GlobalSecondaryIndexes: schema.GlobalSecondaryIndexes(),
},
)
if err != nil {
panic(err)
}
ed := &ExampleDocument{
ID: "123456",
Name: "Example",
}
marshaled, err := schema.Marshal(ed)
if err != nil {
panic(err)
}
_, err = client.PutItem(context.Background(), &dynamodb.PutItemInput{
TableName: aws.String(TABLENAME),
Item: marshaled,
})
if err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(marshaled)
// Output:
// {"ID":{"Value":"123456"},"Name":{"Value":"Example"},"PK":{"Value":"ed#123456"},"SK":{"Value":"ed"},"_Type":{"Value":"ed"}}
}