forked from jamscloud/enumer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgql.go
More file actions
36 lines (29 loc) · 673 Bytes
/
gql.go
File metadata and controls
36 lines (29 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
// Arguments to format are:
// [1]: type name
const gqlMarshalMethod = `func (i %[1]s) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(i.String()))
}
`
const gqlUnMarshalMethod = `func (i *%[1]s) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}
val, err := %[1]sString(str)
if err != nil {
return err
}
*i = val
if !i.IsA%[1]s() {
return fmt.Errorf("%%s is not a valid %[1]s", str)
}
return nil
}
`
func (g *Generator) addGQLMethods(typeName string) {
g.Printf("\n")
g.Printf(gqlMarshalMethod, typeName)
g.Printf("\n\n")
g.Printf(gqlUnMarshalMethod, typeName)
}