generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity.go
202 lines (170 loc) · 4.54 KB
/
identity.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package configkit
import (
"bytes"
"errors"
"fmt"
"reflect"
"unicode"
"github.com/dogmatiq/configkit/internal/validation"
"github.com/dogmatiq/enginekit/protobuf/uuidpb"
)
// Identity is the application-defined identity of a Dogma entity.
type Identity struct {
// Name is the name component of the identity.
//
// For handlers, it is unique within an application at any given version,
// but may be changed over time.
//
// It is allowed, but not recommended to use the same name for an
// application as one of its constituent handlers.
Name string
// Key is the key component of the identity.
//
// It is not only unique within an application, but forever immutable. It is
// not permitted for an application and one of its constituent handlers to
// share the same key.
Key string
}
// NewIdentity returns a new identity.
//
// It returns a non-nil error if either of the name or key components is invalid.
func NewIdentity(n, k string) (Identity, error) {
i := Identity{n, k}
return i, i.Validate()
}
// MustNewIdentity returns a new identity.
//
// It panics if either of the name or key components is invalid.
func MustNewIdentity(n, k string) Identity {
i, err := NewIdentity(n, k)
if err != nil {
panic(err)
}
return i
}
// IsZero returns true if the identity is the zero-value.
func (i Identity) IsZero() bool {
return i.Name == "" && i.Key == ""
}
// ConflictsWith returns true if i has the same name or key as ident.
func (i Identity) ConflictsWith(ident Identity) bool {
if i.Name == ident.Name {
return true
}
if i.Key == ident.Key {
return true
}
return false
}
// Validate returns an error if i is not a valid identity.
func (i Identity) Validate() error {
if err := ValidateIdentityName(i.Name); err != nil {
return err
}
return ValidateIdentityKey(i.Key)
}
func (i Identity) String() string {
return fmt.Sprintf("%s/%s", i.Name, i.Key)
}
// MarshalText returns a UTF-8 representation of the identity.
func (i Identity) MarshalText() ([]byte, error) {
if err := i.Validate(); err != nil {
return nil, err
}
var buf bytes.Buffer
buf.WriteString(i.Name)
buf.WriteRune(' ')
buf.WriteString(i.Key)
return buf.Bytes(), nil
}
// UnmarshalText unmarshals an identity from its UTF-8 representation.
func (i *Identity) UnmarshalText(text []byte) error {
n := bytes.IndexRune(text, ' ')
if n == -1 {
return errors.New("could not decode identity, no name/key separator found")
}
i.Name = string(text[:n])
i.Key = string(text[n+1:])
return i.Validate()
}
// MarshalBinary returns a binary representation of the identity.
func (i Identity) MarshalBinary() ([]byte, error) {
return i.MarshalText()
}
// UnmarshalBinary unmarshals an identity from its binary representation.
func (i *Identity) UnmarshalBinary(data []byte) error {
return i.UnmarshalText(data)
}
// ValidateIdentityName returns nil if n is a valid application or handler name;
// otherwise, it returns an error.
func ValidateIdentityName(n string) error {
if !isValidIdentityName(n) {
return validation.Errorf(
"invalid name %#v, names must be non-empty, printable UTF-8 strings with no whitespace",
n,
)
}
return nil
}
// ValidateIdentityKey returns nil if n is a valid application or handler key;
// otherwise, it returns an error.
func ValidateIdentityKey(k string) error {
if _, err := uuidpb.Parse(k); err != nil {
return validation.Errorf(
"invalid key %#v, keys must be RFC 4122 UUIDs",
k,
)
}
return nil
}
// isValidIdentityName returns true if n is a valid application or handler
// name or key.
//
// A valid name/key is a non-empty string consisting of Unicode printable
// characters, except whitespace.
func isValidIdentityName(n string) bool {
if len(n) == 0 {
return false
}
for _, r := range n {
if unicode.IsSpace(r) || !unicode.IsPrint(r) {
return false
}
}
return true
}
func configureIdentity(
entityIdent *Identity,
name, key string,
entityType reflect.Type,
) {
if !entityIdent.IsZero() {
validation.Panicf(
"%s is configured with multiple identities (%s and %s/%s), Identity() must be called exactly once within Configure()",
entityType,
*entityIdent,
name,
key,
)
}
var err error
*entityIdent, err = NewIdentity(name, key)
if err != nil {
validation.Panicf(
"%s is configured with an invalid identity, %s",
entityType,
err,
)
}
}
func mustHaveValidIdentity(
entityIdent Identity,
entityType reflect.Type,
) {
if entityIdent.IsZero() {
validation.Panicf(
"%s is configured without an identity, Identity() must be called exactly once within Configure()",
entityType,
)
}
}