github.com/zikaeroh/gqlgen@v0.7.2/graphql/bool.go (about)

     1  package graphql
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  func 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  
    19  func UnmarshalBoolean(v interface{}) (bool, error) {
    20  	switch v := v.(type) {
    21  	case string:
    22  		return "true" == strings.ToLower(v), 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  }