-
Notifications
You must be signed in to change notification settings - Fork 0
/
records.go
52 lines (49 loc) · 1.23 KB
/
records.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
package migrate
import (
"context"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/goccha/logging/log"
"github.com/google/uuid"
"github.com/pkg/errors"
"strings"
"time"
)
func SaveRecord(ctx context.Context, api MigrationApi, tableName string, record map[string]interface{}) (err error) {
var item map[string]types.AttributeValue
if item, err = attributevalue.MarshalMap(record); err != nil {
return
} else {
if _, err = api.PutItem(ctx, &dynamodb.PutItemInput{
Item: item,
TableName: &tableName,
}); err != nil {
log.Error(ctx).Str("table", tableName).Err(err).Send()
return errors.WithStack(err)
}
}
return
}
func convertValue(record map[string]interface{}) map[string]interface{} {
m := map[string]interface{}{}
for k, v := range record {
switch val := v.(type) {
case string:
val = strings.Trim(val, " ")
if strings.HasPrefix(val, "{{") && strings.HasSuffix(val, "}}") {
switch val[2 : len(val)-2] {
case "uuid()":
m[k] = uuid.NewString()
case "now()":
m[k] = time.Now()
}
} else {
m[k] = v
}
default:
m[k] = v
}
}
return m
}