Skip to content

Commit

Permalink
chore: improve util
Browse files Browse the repository at this point in the history
Signed-off-by: Norman <[email protected]>
  • Loading branch information
n0izn0iz committed Jan 7, 2025
1 parent 0e80e94 commit ee5c99c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions gno/p/daocond/daocond_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,23 @@ func newMockDAO(emitter func(evt daocond.Event)) *mockDAO {
}

func (m *mockDAO) assignRole(userId string, role string) {
if _, ok := m.members[userId]; !ok {
_, ok := m.members[userId]
if !ok {
panic(errors.New("unknown member"))
}
m.members[userId] = strsadd(m.members[userId], role)
if !m.noEvents {
m.members[userId], ok = strsadd(m.members[userId], role)
if ok && !m.noEvents {
m.emitter(&daocond.EventRoleAssigned{UserID: userId, Role: role})
}
}

func (m *mockDAO) unassignRole(userId string, role string) {
if _, ok := m.members[userId]; !ok {
_, ok := m.members[userId]
if !ok {
panic(errors.New("unknown member"))
}
m.members[userId] = strsrm(m.members[userId], role)
if !m.noEvents {
m.members[userId], ok = strsrm(m.members[userId], role)
if ok && !m.noEvents {
m.emitter(&daocond.EventRoleUnassigned{UserID: userId, Role: role})
}
}
Expand All @@ -268,22 +270,24 @@ func (m *mockDAO) hasRole(memberId string, role string) bool {
return false
}

func strsrm(strs []string, val string) []string {
func strsrm(strs []string, val string) ([]string, bool) {
removed := false
res := []string{}
for _, str := range strs {
if str == val {
removed = true
continue
}
res = append(res, str)
}
return res
return res, removed
}

func strsadd(strs []string, val string) []string {
func strsadd(strs []string, val string) ([]string, bool) {
for _, str := range strs {
if str == val {
return strs
return strs, false
}
}
return append(strs, val)
return append(strs, val), true
}

0 comments on commit ee5c99c

Please sign in to comment.