-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddbcalc.go
33 lines (26 loc) · 871 Bytes
/
ddbcalc.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
package ddbcalc
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/ryeguard/ddbcalc/internal/calc"
)
const SizeLimitInBytes = 400_000 // 400 KB
// MapSizeInBytes returns the size of a map of AttributeValue in bytes.
func MapSizeInBytes(m map[string]types.AttributeValue) (int, error) {
size := 0
for k, v := range m {
size += len([]byte(k))
size += calc.SizeInBytes(&v)
}
return size, nil
}
// StructSizeInBytes returns the size of a struct in bytes.
// It is a convenience function as an alternative to first calling attributevalue.MarshalMap and then MapSizeInBytes.
func StructSizeInBytes(s interface{}) (int, error) {
av, err := attributevalue.MarshalMap(s)
if err != nil {
return 0, fmt.Errorf("marshal map: %w", err)
}
return MapSizeInBytes(av)
}