github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/release/v1alpha1/deep_copy.go (about)

     1  package v1alpha1
     2  
     3  import (
     4  	"time"
     5  
     6  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     7  )
     8  
     9  // +kubebuilder:validation:Type=string
    10  // DeepCopyDate is a date type designed to be validated with json-schema date
    11  // type.
    12  type DeepCopyDate struct {
    13  	metav1.Time `json:",inline"`
    14  }
    15  
    16  // MarshalJSON implements the json.Marshaler interface. The time is
    17  // expected to be a quoted string in yyyy-mm-dd format.
    18  //
    19  // NOTE: This method has a value (not pointer) receiver. Otherwise marshalling
    20  // will stop working for values. When this is a value receiver it works for both.
    21  func (d DeepCopyDate) MarshalJSON() ([]byte, error) {
    22  	s := d.Format(`"2006-01-02"`)
    23  	return []byte(s), nil
    24  }
    25  
    26  // UnmarshalJSON implements the json.Unmarshaler interface. The time is
    27  // expected to be a quoted string in yyyy-mm-dd format.
    28  func (d *DeepCopyDate) UnmarshalJSON(data []byte) error {
    29  	// Ignore null, like in the main JSON package.
    30  	if string(data) == "null" {
    31  		return nil
    32  	}
    33  
    34  	// Error masking is skipped here as this will go trough generated
    35  	// unmarshaling code.
    36  	parsed, err := time.Parse(`"2006-01-02"`, string(data))
    37  	d.Time = metav1.Time{Time: parsed}
    38  	return err
    39  }