github.com/nathanstitt/genqlient@v0.3.1-0.20211028004951-a2bda3c41ab8/internal/testutil/types.go (about)

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