github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/timestamp.go (about)

     1  package graphql
     2  
     3  import (
     4  	"io"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/scalar"
    11  )
    12  
    13  // Timestamp missing godoc
    14  type Timestamp time.Time
    15  
    16  // UnmarshalGQL missing godoc
    17  func (y *Timestamp) UnmarshalGQL(v interface{}) error {
    18  	tmpStr, err := scalar.ConvertToString(v)
    19  	if err != nil {
    20  		return err
    21  	}
    22  
    23  	t, err := time.Parse(time.RFC3339, tmpStr)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	*y = Timestamp(t)
    29  
    30  	return nil
    31  }
    32  
    33  // MarshalGQL missing godoc
    34  func (y Timestamp) MarshalGQL(w io.Writer) {
    35  	_, err := w.Write([]byte(strconv.Quote(time.Time(y).Format(time.RFC3339))))
    36  	if err != nil {
    37  		log.D().Errorf("while writing %T: %s", y, err)
    38  	}
    39  }
    40  
    41  // MarshalJSON missing godoc
    42  func (y Timestamp) MarshalJSON() ([]byte, error) {
    43  	return time.Time(y).MarshalJSON()
    44  }
    45  
    46  // UnmarshalJSON missing godoc
    47  func (y *Timestamp) UnmarshalJSON(data []byte) error {
    48  	return (*time.Time)(y).UnmarshalJSON(data)
    49  }