github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/codegen/config/testdata/autobinding/scalars/model/model.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  )
     8  
     9  type Banned bool
    10  
    11  func (b Banned) MarshalGQL(w io.Writer) {
    12  	if b {
    13  		w.Write([]byte("true"))
    14  	} else {
    15  		w.Write([]byte("false"))
    16  	}
    17  }
    18  
    19  func (b *Banned) UnmarshalGQL(v interface{}) error {
    20  	switch v := v.(type) {
    21  	case string:
    22  		*b = strings.ToLower(v) == "true"
    23  		return nil
    24  	case bool:
    25  		*b = Banned(v)
    26  		return nil
    27  	default:
    28  		return fmt.Errorf("%T is not a bool", v)
    29  	}
    30  }