Skip to content

Commit 768681b

Browse files
committed
🌱 More tests for encoding.TextMarshaler
Test that json.Marshaler has precedence over encoding.TextMarshaler. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent 5b0e340 commit 768681b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

pkg/crd/testdata/cronjob_types.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"encoding/json"
2828
"fmt"
2929
"net/url"
30+
"strconv"
3031
"time"
3132

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

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

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

621+
// +kubebuilder:validation:Type=integer
622+
// +kubebuilder:validation:Format=int64
623+
// Time2 is a newtype around [metav1.Time].
624+
// It implements both [encoding.TextMarshaler] and [json.Marshaler].
625+
// The latter is authoritative for the CRD generation.
626+
type Time2 time.Time
627+
628+
var _ interface {
629+
encoding.TextMarshaler
630+
json.Marshaler
631+
} = (*Time2)(nil)
632+
633+
// MarshalText implements [encoding.TextMarshaler].
634+
func (t *Time2) MarshalText() (text []byte, err error) {
635+
return []byte((*time.Time)(t).String()), nil
636+
}
637+
638+
// MarshalJSON implements [json.Marshaler].
639+
func (t *Time2) MarshalJSON() ([]byte, error) {
640+
return strconv.AppendInt(nil, (*time.Time)(t).UnixMilli(), 10), nil
641+
}
642+
620643
// Duration has a custom Marshaler but no markers.
621644
// We want the CRD generation to infer type information
622645
// from the go types and ignore the presense of the Marshaler.
@@ -667,6 +690,10 @@ type CronJobStatus struct {
667690
// +optional
668691
LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty"`
669692

693+
// Information when was the last time the job was successfully scheduled.
694+
// +optional
695+
LastScheduleTime2 Time2 `json:"lastScheduleTime2,omitempty"`
696+
670697
// Information about the last time the job was successfully scheduled,
671698
// with microsecond precision.
672699
// +optional

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9240,6 +9240,11 @@ spec:
92409240
scheduled.
92419241
format: date-time
92429242
type: string
9243+
lastScheduleTime2:
9244+
description: Information when was the last time the job was successfully
9245+
scheduled.
9246+
format: int64
9247+
type: integer
92439248
type: object
92449249
type: object
92459250
selectableFields:

0 commit comments

Comments
 (0)