Skip to content

Commit 84a90f3

Browse files
Emyrkjohnstcn
authored andcommitted
fix: use return size over size
1 parent e1d5893 commit 84a90f3

File tree

8 files changed

+51
-16
lines changed

8 files changed

+51
-16
lines changed

coderd/authz/authztest/set.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ func NewRole(sets ...iterable) *Role {
3939
}
4040
}
4141

42+
func (s *Role) Reset() {
43+
for _, i := range s.PermissionSets {
44+
i.Reset()
45+
}
46+
}
47+
4248
// Next will gr
4349
func (s *Role) Next() bool {
4450
if !s.first {
@@ -63,7 +69,7 @@ func (s *Role) Permissions() Set {
6369
for _, set := range s.PermissionSets {
6470
i += copy(s.buffer[i:], set.Permissions())
6571
}
66-
all := make(Set, 0, s.Size)
72+
all := make(Set, 0, s.ReturnSize)
6773
for _, set := range s.PermissionSets {
6874
all = append(all, set.Permissions()...)
6975
}

coderd/authz/authztest/table.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

coderd/authz/permission.go

Whitespace-only changes.

coderd/authz/permission_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package authz
2+
3+
import (
4+
crand "github.com/coder/coder/cryptorand"
5+
"testing"
6+
)
7+
8+
func BenchmarkPermissionString(b *testing.B) {
9+
total := 10000
10+
if b.N < total {
11+
total = b.N
12+
}
13+
perms := make([]Permission, b.N)
14+
for n := 0; n < total; n++ {
15+
perms[n] = RandomPermission()
16+
}
17+
18+
b.ResetTimer()
19+
for n := 0; n < b.N; n++ {
20+
var _ = perms[n%total].String()
21+
}
22+
}
23+
24+
var resourceTypes = []string{
25+
"project", "config", "user", "user_role",
26+
"workspace", "dev-url", "metric", "*",
27+
}
28+
29+
var actions = []string{
30+
"read", "create", "delete", "modify", "*",
31+
}
32+
33+
func RandomPermission() Permission {
34+
n, _ := crand.Intn(len(PermissionLevels))
35+
m, _ := crand.Intn(len(resourceTypes))
36+
a, _ := crand.Intn(len(actions))
37+
return Permission{
38+
Sign: n%2 == 0,
39+
Level: PermissionLevels[n],
40+
ResourceType: resourceTypes[m],
41+
ResourceID: "*",
42+
Action: actions[a],
43+
}
44+
}

coderd/authz/testdata/group.go

Whitespace-only changes.

coderd/authz/testdata/permissions.go

Whitespace-only changes.

coderd/authz/testdata/role.go

Whitespace-only changes.

coderd/authz/testdata/set.go

Whitespace-only changes.

0 commit comments

Comments
 (0)