1package graphql
2
3import (
4 "fmt"
5 "io"
6 "strings"
7)
8
9func MarshalBoolean(b bool) Marshaler {
10 return WriterFunc(func(w io.Writer) {
11 if b {
12 w.Write(trueLit)
13 } else {
14 w.Write(falseLit)
15 }
16 })
17}
18
19func UnmarshalBoolean(v interface{}) (bool, error) {
20 switch v := v.(type) {
21 case string:
22 return strings.ToLower(v) == "true", nil
23 case int:
24 return v != 0, nil
25 case bool:
26 return v, nil
27 default:
28 return false, fmt.Errorf("%T is not a bool", v)
29 }
30}