This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
backend/internal/models/role.go
2021-03-06 11:54:55 +01:00

46 lines
667 B
Go

package models
import (
"fmt"
"io"
"strconv"
"strings"
)
type Role string
const (
RoleAdmin Role = "admin"
RoleUser Role = "user"
)
func (role Role) IsValid() bool {
switch role {
case RoleAdmin,
RoleUser:
return true
}
return false
}
func (role Role) String() string {
return string(role)
}
func (role *Role) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
*role = Role(strings.ToLower(str))
if !role.IsValid() {
return fmt.Errorf("%s is not a valid Role", str)
}
return nil
}
func (role Role) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(role.String()))
}