Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5698938

Browse files
committed
use fmt over str builder for easier to read
1 parent 2804b92 commit 5698938

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

coderd/authz/permission.go

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package authz
22

33
import (
4+
"fmt"
45
"golang.org/x/xerrors"
56
"strings"
67
)
@@ -31,28 +32,18 @@ type Permission struct {
3132
}
3233

3334
// String returns the <level>.<resource_type>.<id>.<action> string formatted permission.
34-
// A string builder is used to be the most efficient.
3535
func (p Permission) String() string {
36-
var s strings.Builder
37-
// This could be 1 more than the actual capacity. But being 1 byte over for capacity is ok.
38-
s.Grow(1 + 4 + len(p.Level) + len(p.LevelID) + len(p.ResourceType) + len(p.ResourceID) + len(p.Action))
36+
sign := "-"
3937
if p.Sign {
40-
s.WriteRune('+')
41-
} else {
42-
s.WriteRune('-')
38+
sign = "+"
4339
}
44-
s.WriteString(string(p.Level))
40+
levelID := ""
4541
if p.LevelID != "" {
46-
s.WriteRune(':')
47-
s.WriteString(p.LevelID)
42+
levelID = ":" + p.LevelID
4843
}
49-
s.WriteRune('.')
50-
s.WriteString(string(p.ResourceType))
51-
s.WriteRune('.')
52-
s.WriteString(p.ResourceID)
53-
s.WriteRune('.')
54-
s.WriteString(string(p.Action))
55-
return s.String()
44+
45+
return fmt.Sprintf("%s%s%s.%s.%s.%s",
46+
sign, p.Level, levelID, p.ResourceType, p.ResourceID, p.Action)
5647
}
5748

5849
func ParsePermissions(perms string) ([]Permission, error) {

0 commit comments

Comments
 (0)