aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlgen/graphql/bool.go
blob: 7053bbcaa3280d37547ccd82f1df3cd4bfbde6c3 (plain) (blame)
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
package graphql

import (
	"fmt"
	"io"
	"strings"
)

func MarshalBoolean(b bool) Marshaler {
	return WriterFunc(func(w io.Writer) {
		if b {
			w.Write(trueLit)
		} else {
			w.Write(falseLit)
		}
	})
}

func UnmarshalBoolean(v interface{}) (bool, error) {
	switch v := v.(type) {
	case string:
		return "true" == strings.ToLower(v), nil
	case int:
		return v != 0, nil
	case bool:
		return v, nil
	default:
		return false, fmt.Errorf("%T is not a bool", v)
	}
}