-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
encoding_aws.go
50 lines (40 loc) · 1.24 KB
/
encoding_aws.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
package dynamo
import (
"fmt"
"reflect"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type Coder interface {
Marshaler
Unmarshaler
}
type awsEncoder struct {
iface interface{}
}
func (w awsEncoder) MarshalDynamo() (types.AttributeValue, error) {
return attributevalue.Marshal(w.iface)
}
func (w awsEncoder) UnmarshalDynamo(av types.AttributeValue) error {
return attributevalue.Unmarshal(av, w.iface)
}
// AWSEncoding wraps an object, forcing it to use AWS's official dynamodbattribute package
// for encoding and decoding. This allows you to use the "dynamodbav" struct tags.
// When decoding, v must be a pointer.
func AWSEncoding(v interface{}) Coder {
return awsEncoder{v}
}
func unmarshalAppendAWS(item Item, out interface{}) error {
rv := reflect.ValueOf(out)
if rv.Kind() != reflect.Ptr || rv.Elem().Kind() != reflect.Slice {
return fmt.Errorf("dynamo: unmarshal append AWS: result argument must be a slice pointer")
}
slicev := rv.Elem()
innerRV := reflect.New(slicev.Type().Elem())
if err := attributevalue.UnmarshalMap(item, innerRV.Interface()); err != nil {
return err
}
slicev = reflect.Append(slicev, innerRV.Elem())
rv.Elem().Set(slicev)
return nil
}