models.ServerStatus and models.LanguageTag should implement gql marshaler interface
This commit is contained in:
parent
0b3cb090ed
commit
9ecd704aec
|
@ -1,7 +1,51 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LanguageTag string
|
||||
|
||||
const (
|
||||
LanguageTagPL LanguageTag = "pl"
|
||||
LanguageTagInt LanguageTag = "en"
|
||||
LanguageTagDE LanguageTag = "de"
|
||||
)
|
||||
|
||||
func (lt LanguageTag) IsValid() bool {
|
||||
switch lt {
|
||||
case LanguageTagPL,
|
||||
LanguageTagInt,
|
||||
LanguageTagDE:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (lt LanguageTag) String() string {
|
||||
return string(lt)
|
||||
}
|
||||
|
||||
func (lt *LanguageTag) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
}
|
||||
|
||||
*lt = LanguageTag(strings.ToLower(str))
|
||||
if !lt.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid LanguageTag", str)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lt LanguageTag) MarshalGQL(w io.Writer) {
|
||||
fmt.Fprint(w, strconv.Quote(lt.String()))
|
||||
}
|
||||
|
||||
type LangVersion struct {
|
||||
Tag LanguageTag `pg:",pk" json:"tag" gqlgen:"tag"`
|
||||
Name string `json:"name" gqlgen:"name" pg:",unique"`
|
||||
|
|
|
@ -1,12 +1,49 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ServerStatus string
|
||||
|
||||
const (
|
||||
ServerStatusOpen = "open"
|
||||
ServerStatusClose = "close"
|
||||
ServerStatusOpen ServerStatus = "open"
|
||||
ServerStatusClose ServerStatus = "close"
|
||||
)
|
||||
|
||||
func (ss ServerStatus) IsValid() bool {
|
||||
switch ss {
|
||||
case ServerStatusOpen,
|
||||
ServerStatusClose:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (ss ServerStatus) String() string {
|
||||
return string(ss)
|
||||
}
|
||||
|
||||
func (ss *ServerStatus) UnmarshalGQL(v interface{}) error {
|
||||
str, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("enums must be strings")
|
||||
}
|
||||
|
||||
*ss = ServerStatus(strings.ToLower(str))
|
||||
if !ss.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid ServerStatus", str)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss ServerStatus) MarshalGQL(w io.Writer) {
|
||||
fmt.Fprint(w, strconv.Quote(ss.String()))
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
tableName struct{} `pg:"alias:server"`
|
||||
|
||||
|
|
Reference in New Issue