int.go

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