Last active
January 3, 2022 05:36
-
-
Save yiplee/5c69cde11f80c9de1afe7402ad5f4b30 to your computer and use it in GitHub Desktop.
install stringer: go get -u -a golang.org/x/tools/cmd/stringer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core | |
import ( | |
"testing" | |
) | |
func TestPermissionFromName(t *testing.T) { | |
for i := 0; i < len(_Permission_index)-1; i++ { | |
left, right := _Permission_index[i], _Permission_index[i+1] | |
name := _Permission_name[left:right] | |
t.Run(name, func(t *testing.T) { | |
p, ok := PermissionFromName(name) | |
if !ok { | |
t.Fatalf("permision name %q should be valid", name) | |
} | |
if expect, got := Permission(i), p; expect != got { | |
t.Fatalf("name %s expect %v but got %v", name, expect, got) | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package core | |
//go:generate stringer --type Permission | |
type Permission int | |
const ( | |
A Permission = iota | |
B | |
C | |
D | |
E | |
F | |
) | |
func PermissionFromName(p string) (Permission, bool) { | |
for i := 0; i < len(_Permission_index)-1; i++ { | |
l, r := _Permission_index[i], _Permission_index[i+1] | |
if p == _Permission_name[l:r] { | |
return Permission(i), true | |
} | |
} | |
return 0, false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by "stringer --type Permission"; DO NOT EDIT. | |
package core | |
import "strconv" | |
func _() { | |
// An "invalid array index" compiler error signifies that the constant values have changed. | |
// Re-run the stringer command to generate them again. | |
var x [1]struct{} | |
_ = x[A-0] | |
_ = x[B-1] | |
_ = x[C-2] | |
_ = x[D-3] | |
_ = x[E-4] | |
_ = x[F-5] | |
} | |
const _Permission_name = "ABCDEF" | |
var _Permission_index = [...]uint8{0, 1, 2, 3, 4, 5, 6} | |
func (i Permission) String() string { | |
if i < 0 || i >= Permission(len(_Permission_index)-1) { | |
return "Permission(" + strconv.FormatInt(int64(i), 10) + ")" | |
} | |
return _Permission_name[_Permission_index[i]:_Permission_index[i+1]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment