forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
51 lines (40 loc) · 1.23 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package testutil
import (
"context"
"time"
"github.com/Khan/genqlient/graphql"
)
type ID string
type Pokemon struct {
Species string `json:"species"`
Level int `json:"level"`
}
func (p Pokemon) Battle(q Pokemon) bool {
return p.Level > q.Level
}
type MyContext interface {
context.Context
MyMethod()
}
func GetClientFromNowhere() (graphql.Client, error) { return nil, nil }
func GetClientFromContext(ctx context.Context) (graphql.Client, error) { return nil, nil }
func GetClientFromMyContext(ctx MyContext) (graphql.Client, error) { return nil, nil }
const dateFormat = "2006-01-02"
func MarshalDate(t *time.Time) ([]byte, error) {
// nil should never happen but we might as well check. zero-time does
// happen because omitempty doesn't consider it zero; we'd prefer to write
// null than "0001-01-01".
//
// (I mean, we're tests. Who cares! But we may as well try to match what
// prod code would want.)
if t == nil || t.IsZero() {
return []byte("null"), nil
}
return []byte(`"` + t.Format(dateFormat) + `"`), nil
}
func UnmarshalDate(b []byte, t *time.Time) error {
// (modified from time.Time.UnmarshalJSON)
var err error
*t, err = time.Parse(`"`+dateFormat+`"`, string(b))
return err
}