int.go

 1package graphql
 2
 3import (
 4	"fmt"
 5	"io"
 6	"strconv"
 7)
 8
 9func MarshalInt(i int) Marshaler {
10	return WriterFunc(func(w io.Writer) {
11		io.WriteString(w, strconv.Itoa(i))
12	})
13}
14
15func UnmarshalInt(v interface{}) (int, error) {
16	switch v := v.(type) {
17	case string:
18		return strconv.Atoi(v)
19	case int:
20		return v, nil
21	case float64:
22		return int(v), nil
23	default:
24		return 0, fmt.Errorf("%T is not an int", v)
25	}
26}