github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/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  	if b {
    11  		return WriterFunc(func(w io.Writer) { w.Write(trueLit) })
    12  	}
    13  	return WriterFunc(func(w io.Writer) { w.Write(falseLit) })
    14  }
    15  
    16  func UnmarshalBoolean(v interface{}) (bool, error) {
    17  	switch v := v.(type) {
    18  	case string:
    19  		return strings.ToLower(v) == "true", nil
    20  	case int:
    21  		return v != 0, nil
    22  	case bool:
    23  		return v, nil
    24  	default:
    25  		return false, fmt.Errorf("%T is not a bool", v)
    26  	}
    27  }