2022-03-22 11:03:15 -04:00
|
|
|
package role
|
|
|
|
|
2022-04-11 04:31:10 -04:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
//go:generate stringer -type=Role
|
|
|
|
|
|
|
|
// Role is a peer's role.
|
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
Unknown Role = iota
|
|
|
|
Coordinator
|
|
|
|
Node
|
2022-04-13 06:39:55 -04:00
|
|
|
Admin
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
2022-04-11 04:31:10 -04:00
|
|
|
|
|
|
|
// MarshalJSON marshals the Role to JSON string.
|
|
|
|
func (r Role) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the Role from JSON string.
|
|
|
|
func (r *Role) UnmarshalJSON(b []byte) error {
|
|
|
|
var roleString string
|
|
|
|
if err := json.Unmarshal(b, &roleString); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch strings.ToLower(roleString) {
|
|
|
|
case "coordinator":
|
|
|
|
*r = Coordinator
|
|
|
|
case "node":
|
|
|
|
*r = Node
|
2022-04-13 06:39:55 -04:00
|
|
|
case "admin":
|
|
|
|
*r = Admin
|
2022-04-11 04:31:10 -04:00
|
|
|
default:
|
|
|
|
*r = Unknown
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|