constellation/internal/role/role.go
Thomas Tendyck bd63aa3c6b add license headers
sed -i '1i/*\nCopyright (c) Edgeless Systems GmbH\n\nSPDX-License-Identifier: AGPL-3.0-only\n*/\n' `grep -rL --include='*.go' 'DO NOT EDIT'`
gofumpt -w .
2022-09-05 09:17:25 +02:00

49 lines
783 B
Go

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
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
}