id.go

 1package graphql
 2
 3import (
 4	"fmt"
 5	"io"
 6	"strconv"
 7)
 8
 9func MarshalID(s string) Marshaler {
10	return WriterFunc(func(w io.Writer) {
11		io.WriteString(w, strconv.Quote(s))
12	})
13}
14func UnmarshalID(v interface{}) (string, error) {
15	switch v := v.(type) {
16	case string:
17		return v, nil
18	case int:
19		return strconv.Itoa(v), nil
20	case float64:
21		return fmt.Sprintf("%f", v), nil
22	case bool:
23		if v {
24			return "true", nil
25		} else {
26			return "false", nil
27		}
28	case nil:
29		return "null", nil
30	default:
31		return "", fmt.Errorf("%T is not a string", v)
32	}
33}