github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/mutation_with_custom_scalar.go (about)

     1  package followschema
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"regexp"
     8  )
     9  
    10  var re = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
    11  
    12  type Email string
    13  
    14  func (value *Email) UnmarshalGQL(v interface{}) error {
    15  	input, ok := v.(string)
    16  	if !ok {
    17  		return fmt.Errorf("email expects a string value")
    18  	}
    19  	if !re.MatchString(input) {
    20  		return fmt.Errorf("invalid email format")
    21  	}
    22  	*value = Email(input)
    23  	return nil
    24  }
    25  
    26  func (value Email) MarshalGQL(w io.Writer) {
    27  	output, _ := json.Marshal(string(value))
    28  	w.Write(output)
    29  }