-
Notifications
You must be signed in to change notification settings - Fork 1
/
realms.gno
119 lines (99 loc) · 2.47 KB
/
realms.gno
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
// base implementation
package zentasktic
import (
"gno.land/p/demo/avl"
)
// structs
type Realm struct {
Id string `json:"realmId"`
Name string `json:"realmName"`
}
type ZRealmManager struct {
Realms *avl.Tree
}
func NewZRealmManager() *ZRealmManager {
zrm := &ZRealmManager{
Realms: avl.NewTree(),
}
zrm.initializeHardcodedRealms()
return zrm
}
func (zrm *ZRealmManager) initializeHardcodedRealms() {
hardcodedRealms := []Realm{
{Id: "1", Name: "Assess"},
{Id: "2", Name: "Decide"},
{Id: "3", Name: "Do"},
{Id: "4", Name: "Collections"},
}
for _, realm := range hardcodedRealms {
zrm.Realms.Set(realm.Id, realm)
}
}
func (zrm *ZRealmManager) AddRealm(r Realm) (err error){
// implementation
if zrm.Realms.Size() != 0 {
_, exist := zrm.Realms.Get(r.Id)
if exist {
return ErrRealmIdAlreadyExists
}
}
// check for hardcoded values
if r.Id == "1" || r.Id == "2" || r.Id == "3" || r.Id == "4" {
return ErrRealmIdNotAllowed
}
zrm.Realms.Set(r.Id, r)
return nil
}
func (zrm *ZRealmManager) RemoveRealm(r Realm) (err error){
// implementation
if zrm.Realms.Size() != 0 {
_, exist := zrm.Realms.Get(r.Id)
if !exist {
return ErrRealmIdNotFound
} else {
// check for hardcoded values, not removable
if r.Id == "1" || r.Id == "2" || r.Id == "3" || r.Id == "4" {
return ErrRealmIdNotAllowed
}
}
}
_, removed := zrm.Realms.Remove(r.Id)
if !removed {
return ErrRealmNotRemoved
}
return nil
}
// getters
func (zrm *ZRealmManager) GetRealmById(realmId string) (r Realm, err error) {
// implementation
if zrm.Realms.Size() != 0 {
rInterface, exist := zrm.Realms.Get(realmId)
if exist {
return rInterface.(Realm), nil
} else {
return Realm{}, ErrRealmIdNotFound
}
}
return Realm{}, ErrRealmIdNotFound
}
func (zrm *ZRealmManager) GetRealms() (realms string, err error) {
// implementation
var allRealms []Realm
// Iterate over the Realms AVL tree to collect all Context objects.
zrm.Realms.Iterate("", "", func(key string, value interface{}) bool {
if realm, ok := value.(Realm); ok {
allRealms = append(allRealms, realm)
}
return false // Continue iteration until all nodes have been visited.
})
// Create a RealmsObject with all collected contexts.
realmsObject := &RealmsObject{
Realms: allRealms,
}
// Use the custom MarshalJSON method to marshal the realms into JSON.
marshalledRealms, rerr := realmsObject.MarshalJSON()
if rerr != nil {
return "", rerr
}
return string(marshalledRealms), nil
}