github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/api/graphql/model/model.go (about)

     1  package model
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  const (
     8  	ISO8601LocalTime = "2006-01-02T15:04:05"
     9  	DateFormat       = "2006-01-02"
    10  )
    11  
    12  type ErrorResponse struct {
    13  	Error   string `json:"error,omitempty"`
    14  	Message string `json:"message,omitempty"`
    15  }
    16  
    17  type Time struct {
    18  	time.Time
    19  }
    20  
    21  func (t *Time) UnmarshalJSON(data []byte) error {
    22  	// Ignore null, like in the main JSON package.
    23  	if string(data) == "null" {
    24  		return nil
    25  	}
    26  	var err error
    27  	t.Time, err = time.Parse(`"`+ISO8601LocalTime+`"`, string(data))
    28  	return err
    29  }
    30  
    31  func (t *Time) MarshalJSON() ([]byte, error) {
    32  	d := make([]byte, 0, len(ISO8601LocalTime)+2)
    33  	d = append(d, '"')
    34  	d = t.Time.AppendFormat(d, ISO8601LocalTime)
    35  	d = append(d, '"')
    36  	return d, nil
    37  }
    38  
    39  type Date struct {
    40  	time.Time
    41  }
    42  
    43  func (t *Date) UnmarshalJSON(data []byte) error {
    44  	// Ignore null, like in the main JSON package.
    45  	if string(data) == "null" {
    46  		return nil
    47  	}
    48  	var err error
    49  	t.Time, err = time.Parse(`"`+DateFormat+`"`, string(data))
    50  	return err
    51  }
    52  
    53  func (t *Date) MarshalJSON() ([]byte, error) {
    54  	d := make([]byte, 0, len(DateFormat)+2)
    55  	d = append(d, '"')
    56  	d = t.Time.AppendFormat(d, DateFormat)
    57  	d = append(d, '"')
    58  	return d, nil
    59  }