-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_id.go
90 lines (74 loc) · 2.4 KB
/
entity_id.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
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2021-2022, the SS project owners. All rights reserved.
// Please see the OWNERS and LICENSE files for details.
package ss
import (
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/google/uuid"
)
////////////////////////////////////////////////////////////////////////////////
// EntityIDSize is the size of EntityID in bytes.
const EntityIDSize = 16
// EntityID is an abstract project entity ID.
type EntityID uuid.UUID
// NewEntityID generates new entity ID.
func NewEntityID() EntityID { return EntityID(uuid.New()) }
// ParseEntityID parses ID in string.
func ParseEntityID(source string) (EntityID, error) {
bin, err := base64.RawStdEncoding.DecodeString(source)
if err != nil {
return EntityID{}, fmt.Errorf(`failed to decode entity ID: "%w"`, err)
}
result := EntityID{}
if err := result.UnmarshalBinary(bin); err != nil {
return EntityID{}, fmt.Errorf(`failed to unmarshal entity ID: "%w"`, err)
}
return result, nil
}
// String returns ID as string.
func (id EntityID) String() string {
return base64.RawStdEncoding.EncodeToString(id[:])
}
// MarshalText implements encoding.TextMarshaler.
func (id EntityID) MarshalText() ([]byte, error) {
return []byte(id.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (id *EntityID) UnmarshalText(data []byte) error {
var err error
*id, err = ParseEntityID(string(data))
return err
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (id EntityID) MarshalBinary() ([]byte, error) {
return uuid.UUID(id).MarshalBinary()
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (id *EntityID) UnmarshalBinary(data []byte) error {
uuid := uuid.UUID{}
if err := uuid.UnmarshalBinary(data); err != nil {
return err
}
*id = EntityID(uuid)
return nil
}
// MarshalDynamoDBAttributeValue implements serialization for Dynamodb.
func (id EntityID) MarshalDynamoDBAttributeValue(
result *dynamodb.AttributeValue,
) error {
var err error
result.B, err = id.MarshalBinary()
return err
}
// UnmarshalDynamoDBAttributeValue implements reading from Dynamodb.
func (id *EntityID) UnmarshalDynamoDBAttributeValue(
source *dynamodb.AttributeValue,
) error {
if source.S != nil {
// Entity ID could be represent as text.
return id.UnmarshalText([]byte(*source.S))
}
return id.UnmarshalBinary(source.B)
}
////////////////////////////////////////////////////////////////////////////////