Skip to content

Commit

Permalink
🌱 More tests for encoding.TextMarshaler
Browse files Browse the repository at this point in the history
Test that json.Marshaler has precedence over encoding.TextMarshaler.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Nov 15, 2024
1 parent 5b0e340 commit 768681b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"

batchv1beta1 "k8s.io/api/batch/v1beta1"
Expand Down Expand Up @@ -605,9 +606,9 @@ func (u *URL3) MarshalText() (text []byte, err error) {
return u.MarshalBinary()
}

// URL4 is an alias of [net/url.URL]. It implements [encoding.TextMarshaler] so
// that it can be used in K8s CRDs such that the CRD resource will have the URL
// but operator code can can work with the URL struct.
// URL4 is newtype around [net/url.URL]. It implements [encoding.TextMarshaler]
// so that it can be used in K8s CRDs such that the CRD resource will have the
// URL but operator code can can work with the URL struct.
type URL4 url.URL

var _ encoding.TextMarshaler = (*URL4)(nil)
Expand All @@ -617,6 +618,28 @@ func (u *URL4) MarshalText() (text []byte, err error) {
return (*url.URL)(u).MarshalBinary()
}

// +kubebuilder:validation:Type=integer
// +kubebuilder:validation:Format=int64
// Time2 is a newtype around [metav1.Time].
// It implements both [encoding.TextMarshaler] and [json.Marshaler].
// The latter is authoritative for the CRD generation.
type Time2 time.Time

var _ interface {
encoding.TextMarshaler
json.Marshaler
} = (*Time2)(nil)

// MarshalText implements [encoding.TextMarshaler].
func (t *Time2) MarshalText() (text []byte, err error) {
return []byte((*time.Time)(t).String()), nil
}

// MarshalJSON implements [json.Marshaler].
func (t *Time2) MarshalJSON() ([]byte, error) {
return strconv.AppendInt(nil, (*time.Time)(t).UnixMilli(), 10), nil
}

// Duration has a custom Marshaler but no markers.
// We want the CRD generation to infer type information
// from the go types and ignore the presense of the Marshaler.
Expand Down Expand Up @@ -667,6 +690,10 @@ type CronJobStatus struct {
// +optional
LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty"`

// Information when was the last time the job was successfully scheduled.
// +optional
LastScheduleTime2 Time2 `json:"lastScheduleTime2,omitempty"`

// Information about the last time the job was successfully scheduled,
// with microsecond precision.
// +optional
Expand Down
5 changes: 5 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9240,6 +9240,11 @@ spec:
scheduled.
format: date-time
type: string
lastScheduleTime2:
description: Information when was the last time the job was successfully
scheduled.
format: int64
type: integer
type: object
type: object
selectableFields:
Expand Down

0 comments on commit 768681b

Please sign in to comment.