/
schema.go
50 lines (41 loc) · 1.02 KB
/
schema.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
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/dynamo
//
package ddb
import (
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/fogfish/dynamo"
"github.com/fogfish/golem/pure/hseq"
)
/*
Schema is utility that decodes type into projection expression
*/
type Schema[T dynamo.Thing] struct {
ExpectedAttributeNames map[string]string
Projection *string
}
func NewSchema[T dynamo.Thing]() *Schema[T] {
seq := hseq.FMap(
hseq.Generic[T](),
func(t hseq.Type[T]) string {
name := t.StructField.Tag.Get("dynamodbav")
return strings.Split(name, ",")[0]
},
)
names := make(map[string]string, len(seq))
schema := make([]string, len(seq))
for i, x := range seq {
name := "#__" + x + "__"
names[name] = x
schema[i] = name
}
return &Schema[T]{
ExpectedAttributeNames: names,
Projection: aws.String(strings.Join(schema, ", ")),
}
}