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/model/role.go

47 lines
694 B
Go
Raw Normal View History

2021-07-14 04:58:10 +00:00
package model
2021-02-21 09:17:54 +00:00
import (
"fmt"
"github.com/pkg/errors"
2021-02-21 09:17:54 +00:00
"io"
"strconv"
"strings"
)
type Role string
const (
RoleAdmin Role = "admin"
2021-03-06 10:54:55 +00:00
RoleUser Role = "user"
2021-02-21 09:17:54 +00:00
)
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 errors.New("enums must be strings")
2021-02-21 09:17:54 +00:00
}
*role = Role(strings.ToLower(str))
if !role.IsValid() {
return errors.Errorf("%s is not a valid Role", str)
2021-02-21 09:17:54 +00:00
}
return nil
}
func (role Role) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(role.String()))
}