-
Notifications
You must be signed in to change notification settings - Fork 228
Fix meta.ObjectMeta.Created
to always be present and update itself automatically
#174
Conversation
meta.ObjectMeta.Created
to always be present and update itself automaticallymeta.ObjectMeta.Created
to always be present and update itself automatically
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I think we can reuse most stuff from k8s apimachinery here and avoid the embedded pointer.
I'm fine with embedding still, but without the pointer
pkg/apis/meta/v1alpha1/time.go
Outdated
@@ -9,21 +10,67 @@ import ( | |||
) | |||
|
|||
type Time struct { | |||
metav1.Time | |||
*metav1.Time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we actually need this?
There is a metav1.Time.IsZero() method we could use instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about metav1.Time.IsZero()
, will definitely use that instead of a pointer 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5b9bb3f.
pkg/apis/meta/v1alpha1/time.go
Outdated
ti = Timestamp() | ||
} | ||
|
||
b, err := ti.MarshalText() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this marshals the value without "
on the sides. I don't think that's a good idea with YAML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead, re-use metav1.Time's MarshalJSON func here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, fixed in 5b9bb3f.
pkg/apis/meta/v1alpha1/time.go
Outdated
return json.Marshal(string(b)) | ||
} | ||
|
||
func (t *Time) UnmarshalJSON(b []byte) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this function, only call the underlying metav1.Time Unmarshal func. If the input is no content, give metav1.Time.Unmarshal a byte array of null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the input is no content, the byte array is already empty (nil
). We don't even need the custom unmarshaler to overload the underlying metav1.Time.UnmarshalJSON
. Fixed in 5b9bb3f.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks 👍 !
This changes
meta.ObjectMeta.Created
from being a pointer withomitempty
to a requiredTime
struct instance.Time
now embeds a pointer tometav1.Time
to tell if it's set and will set itself automatically upon marshaling if unset.If we don't need any
metav1.Time
specific functionality directly, we should probably move from embedding to have a private field, so anymetav1.Time
methods cannot be accidentally called with a nil receiver. What do you think @luxas ?