-
Notifications
You must be signed in to change notification settings - Fork 0
/
put.go
79 lines (72 loc) · 1.76 KB
/
put.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
package goddb
import (
"context"
"errors"
"fmt"
"os"
"reflect"
"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"
)
type PutRequest[T any] struct {
input *dynamodb.PutItemInput
item *T
condition *Condition[T]
}
func Put[T any](item *T) *PutRequest[T] {
return &PutRequest[T]{
item: item,
input: &dynamodb.PutItemInput{
TableName: aws.String(os.Getenv("GODDB_TABLE_NAME")),
},
}
}
type tagValuePair struct {
tag string
value reflect.Value
}
func (r *PutRequest[T]) If(condition *Condition[T]) *PutRequest[T] {
r.condition = condition
return r
}
func (r *PutRequest[T]) Exec() error {
wrap := func(err error) error {
return fmt.Errorf("goddb put: %w", err)
}
val, err := valueOf(r.item)
if err != nil {
return wrap(err)
}
ty := val.Type()
r.input.Item, err = makeItem(ty, val, func(attr string) bool { return true })
if err != nil {
return wrap(err)
}
if err := validateCompleteKey(ty, val); err != nil {
return wrap(err)
}
if r.condition != nil {
exp, names, values, err := r.condition.expression(len(r.input.ExpressionAttributeValues))
if err != nil {
return wrap(err)
}
r.input.ConditionExpression = &exp
r.input.ExpressionAttributeNames = merge(r.input.ExpressionAttributeNames, names)
r.input.ExpressionAttributeValues = merge(r.input.ExpressionAttributeValues, values)
fmt.Println(exp)
fmt.Println(r.input.ExpressionAttributeNames)
for k, v := range r.input.ExpressionAttributeValues {
fmt.Println(k, v)
}
}
_, err = client.PutItem(context.Background(), r.input)
if err != nil {
var ex *types.ConditionalCheckFailedException
if errors.As(err, &ex) {
return ErrConditionFailed
}
return wrap(err)
}
return nil
}