-
Notifications
You must be signed in to change notification settings - Fork 46
/
models.go
346 lines (292 loc) · 8.61 KB
/
models.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package types
import (
"fmt"
"strconv"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// ParseSubspaceID parses the given value as a subspace id, returning an error if it's invalid
func ParseSubspaceID(value string) (uint64, error) {
if value == "" {
return 0, nil
}
subspaceID, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid subspace id: %s", err)
}
return subspaceID, nil
}
// ParseSubspacesIDs parses the given value as a list of subspace ids separated by a comma
func ParseSubspacesIDs(value string) (ids []uint64, err error) {
for _, value := range strings.Split(value, ",") {
id, err := ParseSubspaceID(value)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}
// NewSubspace is a constructor for the Subspace type
func NewSubspace(subspaceID uint64, name, description, treasury, owner, creator string, creationTime time.Time) Subspace {
return Subspace{
ID: subspaceID,
Name: name,
Description: description,
Treasury: treasury,
Owner: owner,
Creator: creator,
CreationTime: creationTime,
}
}
// Validate implements fmt.Validator
func (sub Subspace) Validate() error {
if sub.ID == 0 {
return fmt.Errorf("invalid subspace id: %d", sub.ID)
}
if strings.TrimSpace(sub.Name) == "" {
return fmt.Errorf("subspace name cannot be empty or blank")
}
if sub.Treasury != "" {
_, err := sdk.AccAddressFromBech32(sub.Treasury)
if err != nil {
return fmt.Errorf("invalid treasury address: %s", sub.Treasury)
}
}
_, err := sdk.AccAddressFromBech32(sub.Owner)
if err != nil {
return fmt.Errorf("invalid owner address: %s", sub.Owner)
}
_, err = sdk.AccAddressFromBech32(sub.Creator)
if err != nil {
return fmt.Errorf("invalid creator address: %s", sub.Creator)
}
if sub.CreationTime.IsZero() {
return fmt.Errorf("invalid subspace creation time: %s", sub.CreationTime)
}
return nil
}
// Update updates the fields of a given subspace without validating it.
// Before storing the updated subspace, a validation with Validate() should
// be performed.
func (sub Subspace) Update(update SubspaceUpdate) Subspace {
if update.Name == DoNotModify {
update.Name = sub.Name
}
if update.Description == DoNotModify {
update.Description = sub.Description
}
if update.Treasury == DoNotModify {
update.Treasury = sub.Treasury
}
if update.Owner == DoNotModify {
update.Owner = sub.Owner
}
return NewSubspace(
sub.ID,
update.Name,
update.Description,
update.Treasury,
update.Owner,
sub.Creator,
sub.CreationTime,
)
}
// SubspaceUpdate contains all the data that can be updated about a subspace.
// When performing an update, if a field should not be edited then it must be set to types.DoNotModify
type SubspaceUpdate struct {
Name string
Description string
Treasury string
Owner string
}
// NewSubspaceUpdate builds a new SubspaceUpdate instance containing the given data
func NewSubspaceUpdate(name, description, treasury, owner string) SubspaceUpdate {
return SubspaceUpdate{
Name: name,
Description: description,
Treasury: treasury,
Owner: owner,
}
}
// --------------------------------------------------------------------------------------------------------------------
const (
// RootSectionID represents the id of the root section of each subspace
RootSectionID = 0
)
// ParseSectionID parses the given value as a section id, returning an error if it's invalid
func ParseSectionID(value string) (uint32, error) {
if value == "" {
return 0, nil
}
sectionID, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid group id: %s", err)
}
return uint32(sectionID), nil
}
// NewSection returns a new Section instance
func NewSection(subspaceID uint64, id uint32, parentID uint32, name string, description string) Section {
return Section{
SubspaceID: subspaceID,
ID: id,
ParentID: parentID,
Name: name,
Description: description,
}
}
// DefaultSection returns the default section for the given subspace
func DefaultSection(subspaceID uint64) Section {
return NewSection(
subspaceID,
RootSectionID,
RootSectionID,
"Default section",
"This is the default subspace section",
)
}
// Validate implements fmt.Validator
func (s Section) Validate() error {
if s.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", s.SubspaceID)
}
if s.ID != RootSectionID && s.ParentID == s.ID {
return fmt.Errorf("invalid parent id: %d", s.ParentID)
}
if strings.TrimSpace(s.Name) == "" {
return fmt.Errorf("invalid section name: %s", s.Name)
}
return nil
}
// Update updates the fields of a given section without validating it.
// Before storing the updated section, a validation with Validate() should
// be performed.
func (s Section) Update(update SectionUpdate) Section {
if update.Name == DoNotModify {
update.Name = s.Name
}
if update.Description == DoNotModify {
update.Description = s.Description
}
return NewSection(
s.SubspaceID,
s.ID,
s.ParentID,
update.Name,
update.Description,
)
}
// SectionUpdate contains all the data that can be updated about a section.
// When performing an update, if a field should not be edited then it must be set to types.DoNotModify
type SectionUpdate struct {
Name string
Description string
}
// NewSectionUpdate returns a new SectionUpdate instance
func NewSectionUpdate(name string, description string) SectionUpdate {
return SectionUpdate{
Name: name,
Description: description,
}
}
// --------------------------------------------------------------------------------------------------------------------
// ParseGroupID parses the given value as a group id, returning an error if it's invalid
func ParseGroupID(value string) (uint32, error) {
if value == "" {
return 0, nil
}
groupID, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid group id: %s", err)
}
return uint32(groupID), nil
}
// NewUserGroup returns a new UserGroup instance
func NewUserGroup(subspaceID uint64, sectionID uint32, id uint32, name, description string, permissions Permissions) UserGroup {
return UserGroup{
SubspaceID: subspaceID,
SectionID: sectionID,
ID: id,
Name: name,
Description: description,
Permissions: permissions,
}
}
// DefaultUserGroup returns the default user group for the given subspace
func DefaultUserGroup(subspaceID uint64) UserGroup {
return NewUserGroup(
subspaceID,
0,
0,
"Default",
"This is a default user group which all users are automatically part of",
nil,
)
}
// Validate implements fmt.Validator
func (group UserGroup) Validate() error {
if group.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", group.SubspaceID)
}
if strings.TrimSpace(group.Name) == "" {
return fmt.Errorf("invalid group name: %s", group.Name)
}
return nil
}
// Update updates the fields of a given group without validating it.
// Before storing the updated group, a validation with Validate() should
// be performed.
func (group UserGroup) Update(update GroupUpdate) UserGroup {
if update.Name == DoNotModify {
update.Name = group.Name
}
if update.Description == DoNotModify {
update.Description = group.Description
}
return NewUserGroup(
group.SubspaceID,
group.SectionID,
group.ID,
update.Name,
update.Description,
group.Permissions,
)
}
// GroupUpdate contains all the data that can be updated about a group.
// When performing an update, if a field should not be edited then it must be set to types.DoNotModify
type GroupUpdate struct {
Name string
Description string
}
// NewGroupUpdate builds a new SubspaceUpdate instance containing the given data
func NewGroupUpdate(name, description string) GroupUpdate {
return GroupUpdate{
Name: name,
Description: description,
}
}
// -------------------------------------------------------------------------------------------------------------------
// NewUserPermission returns a new UserPermission instance
func NewUserPermission(subspaceID uint64, sectionID uint32, user string, permissions Permissions) UserPermission {
return UserPermission{
SubspaceID: subspaceID,
SectionID: sectionID,
User: user,
Permissions: permissions,
}
}
// Validate implements fmt.Validator
func (p UserPermission) Validate() error {
if p.SubspaceID == 0 {
return fmt.Errorf("invalid subspace id: %d", p.SubspaceID)
}
if !ArePermissionsValid(p.Permissions) {
return fmt.Errorf("invalid permissions: %s", p.Permissions)
}
_, err := sdk.AccAddressFromBech32(p.User)
if err != nil {
return fmt.Errorf("invalid user address: %s", err)
}
return nil
}