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