-
Notifications
You must be signed in to change notification settings - Fork 2
/
uid.go
79 lines (60 loc) · 1.48 KB
/
uid.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
package actoruid
import (
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/google/uuid"
)
const prefix = "urn:opg:poas:makeregister:users:"
// Service is used when the application does an action.
var Service = UID{value: [16]byte{0, 0, 0, 0, 0, 0, 64}}
type UID struct{ value uuid.UUID }
func New() UID {
return UID{value: uuid.New()}
}
func Parse(uid string) (UID, error) {
u, err := uuid.Parse(uid)
if err != nil {
return UID{}, err
}
return UID{value: u}, nil
}
func FromRequest(r interface{ FormValue(string) string }) UID {
uid, _ := Parse(r.FormValue("id"))
return uid
}
func (u UID) IsZero() bool {
return u == UID{}
}
func (u UID) String() string {
return u.value.String()
}
func (u UID) PrefixedString() string {
return prefix + u.String()
}
func (u UID) MarshalJSON() ([]byte, error) {
if u.IsZero() {
return []byte("null"), nil
}
return []byte(`"` + u.String() + `"`), nil
}
func (u *UID) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
uid, err := Parse(string(text))
if err != nil {
return err
}
*u = uid
return nil
}
func (u UID) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
return attributevalue.Marshal(u.value.String())
}
func (u *UID) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error {
var s string
if err := attributevalue.Unmarshal(av, &s); err != nil {
return err
}
return u.UnmarshalText([]byte(s))
}