Skip to content

Instantly share code, notes, and snippets.

@holykol
Created October 2, 2020 13:16
Show Gist options
  • Save holykol/c8056f4bb80c0b17e3478a85cbd27be2 to your computer and use it in GitHub Desktop.
Save holykol/c8056f4bb80c0b17e3478a85cbd27be2 to your computer and use it in GitHub Desktop.
type Op int
const (
OpNull Op = 0
OpCreate Op = 1
OpModify Op = 2
OpDelete Op = 3
)
var OpName = map[int]string{
0: "NULL",
1: "CREATE",
2: "MODIFY",
3: "DELETE",
}
var OpValue = map[string]int{
"NULL": 0,
"CREATE": 1,
"MODIFY": 2,
"DELETE": 3,
}
func (e Op) IsValid() bool {
switch e {
case OpNull, OpCreate, OpModify, OpDelete:
return true
}
return false
}
func (e Op) String() string {
return OpName[int(e)]
}
func (e *Op) UnmarshalGQL(v interface{}) error {
value, ok := v.(int)
if !ok {
return fmt.Errorf("enums must be integers")
}
*e = Op(value)
if !e.IsValid() {
return fmt.Errorf("%v is not a valid Op", value)
}
return nil
}
func (e Op) MarshalGQL(w io.Writer) {
fmt.Fprint(w, e)
}
directive @intEnum on ENUM
enum Op @intEnum {
NULL
CREATE
MODIFY
DELETE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment