constellation/internal/role/role.go
Fabian Kammel 22c912a56d move nodestate and role
Signed-off-by: Fabian Kammel <fk@edgeless.systems>
2022-08-29 16:07:55 +02:00

43 lines
700 B
Go

package role
import (
"encoding/json"
"strings"
)
//go:generate stringer -type=Role
// Role is a peer's role.
type Role uint
const (
Unknown Role = iota
ControlPlane
Worker
Admin
)
// 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 "controlplane":
*r = ControlPlane
case "worker":
*r = Worker
case "admin":
*r = Admin
default:
*r = Unknown
}
return nil
}