native: introduce Stringer for noderoles types
And rename roles.go to role.go to match the role_string.go and the existing naming pattern for enums. Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
parent
04d778612e
commit
61a74ab331
4 changed files with 103 additions and 12 deletions
35
pkg/core/native/noderoles/role.go
Normal file
35
pkg/core/native/noderoles/role.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package noderoles
|
||||
|
||||
//go:generate stringer -type=Role
|
||||
|
||||
// Role represents the type of the participant.
|
||||
type Role byte
|
||||
|
||||
// Role enumeration.
|
||||
const (
|
||||
_ Role = 1 << iota
|
||||
_
|
||||
StateValidator
|
||||
Oracle
|
||||
NeoFSAlphabet
|
||||
P2PNotary
|
||||
// last denotes the end of roles enum. Consider adding new roles before the last.
|
||||
last
|
||||
)
|
||||
|
||||
// roles is a map of valid Role string representation to its type.
|
||||
var roles map[string]Role
|
||||
|
||||
func init() {
|
||||
roles = make(map[string]Role)
|
||||
for i := StateValidator; i < last; i = i << 1 {
|
||||
roles[i.String()] = i
|
||||
}
|
||||
}
|
||||
|
||||
// FromString returns a node role parsed from its string representation and a
|
||||
// boolean value denoting whether the conversion was OK and the role exists.
|
||||
func FromString(s string) (Role, bool) {
|
||||
r, ok := roles[s]
|
||||
return r, ok
|
||||
}
|
41
pkg/core/native/noderoles/role_string.go
Normal file
41
pkg/core/native/noderoles/role_string.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
// Code generated by "stringer -type=Role"; DO NOT EDIT.
|
||||
|
||||
package noderoles
|
||||
|
||||
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[StateValidator-4]
|
||||
_ = x[Oracle-8]
|
||||
_ = x[NeoFSAlphabet-16]
|
||||
_ = x[P2PNotary-32]
|
||||
_ = x[last-64]
|
||||
}
|
||||
|
||||
const (
|
||||
_Role_name_0 = "StateValidator"
|
||||
_Role_name_1 = "Oracle"
|
||||
_Role_name_2 = "NeoFSAlphabet"
|
||||
_Role_name_3 = "P2PNotary"
|
||||
_Role_name_4 = "last"
|
||||
)
|
||||
|
||||
func (i Role) String() string {
|
||||
switch {
|
||||
case i == 4:
|
||||
return _Role_name_0
|
||||
case i == 8:
|
||||
return _Role_name_1
|
||||
case i == 16:
|
||||
return _Role_name_2
|
||||
case i == 32:
|
||||
return _Role_name_3
|
||||
case i == 64:
|
||||
return _Role_name_4
|
||||
default:
|
||||
return "Role(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
}
|
27
pkg/core/native/noderoles/role_test.go
Normal file
27
pkg/core/native/noderoles/role_test.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package noderoles
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFromString(t *testing.T) {
|
||||
valid := map[string]Role{
|
||||
"StateValidator": StateValidator,
|
||||
"Oracle": Oracle,
|
||||
"NeoFSAlphabet": NeoFSAlphabet,
|
||||
"P2PNotary": P2PNotary,
|
||||
}
|
||||
for s, expected := range valid {
|
||||
actual, ok := FromString(s)
|
||||
require.True(t, ok)
|
||||
require.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
invalid := []string{"last", "InvalidRole"}
|
||||
for _, s := range invalid {
|
||||
_, ok := FromString(s)
|
||||
require.False(t, ok)
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package noderoles
|
||||
|
||||
// Role represents the type of the participant.
|
||||
type Role byte
|
||||
|
||||
// Role enumeration.
|
||||
const (
|
||||
StateValidator Role = 4
|
||||
Oracle Role = 8
|
||||
NeoFSAlphabet Role = 16
|
||||
P2PNotary Role = 32
|
||||
)
|
Loading…
Reference in a new issue