forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#856 from pmorie/workaround-timestamp
Set CreationTimestamp in each storage implementation
- Loading branch information
Showing
10 changed files
with
262 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
// Time is a wrapper around time.Time which supports correct | ||
// marshaling to YAML and JSON. Wrappers are provided for many | ||
// of the factory methods that the time package offers. | ||
type Time struct { | ||
time.Time | ||
} | ||
|
||
// Date returns the Time corresponding to the supplied parameters | ||
// by wrapping time.Date. | ||
func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { | ||
return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} | ||
} | ||
|
||
// Now returns the current local time. | ||
func Now() Time { | ||
return Time{time.Now()} | ||
} | ||
|
||
// Unix returns the local time corresponding to the given Unix time | ||
// by wrapping time.Unix. | ||
func Unix(sec int64, nsec int64) Time { | ||
return Time{time.Unix(sec, nsec)} | ||
} | ||
|
||
// Rfc3339Copy returns a copy of the Time at second-level precision. | ||
func (t Time) Rfc3339Copy() Time { | ||
copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339)) | ||
return Time{copied} | ||
} | ||
|
||
// UnmarshalJSON implements the json.Unmarshaller interface. | ||
func (t *Time) UnmarshalJSON(b []byte) error { | ||
if len(b) == 4 && string(b) == "null" { | ||
t.Time = time.Time{} | ||
return nil | ||
} | ||
|
||
var str string | ||
json.Unmarshal(b, &str) | ||
|
||
pt, err := time.Parse(time.RFC3339, str) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.Time = pt | ||
return nil | ||
} | ||
|
||
// MarshalJSON implements the json.Marshaler interface. | ||
func (t Time) MarshalJSON() ([]byte, error) { | ||
if t.IsZero() { | ||
// Encode unset/nil objects as JSON's "null". | ||
return []byte("null"), nil | ||
} | ||
|
||
return json.Marshal(t.Format(time.RFC3339)) | ||
} | ||
|
||
// SetYAML implements the yaml.Setter interface. | ||
func (t *Time) SetYAML(tag string, value interface{}) bool { | ||
if value == nil { | ||
t.Time = time.Time{} | ||
return true | ||
} | ||
|
||
str, ok := value.(string) | ||
if !ok { | ||
return false | ||
} | ||
|
||
pt, err := time.Parse(time.RFC3339, str) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
t.Time = pt | ||
return true | ||
} | ||
|
||
// GetYAML implements the yaml.Getter interface. | ||
func (t Time) GetYAML() (tag string, value interface{}) { | ||
if t.IsZero() { | ||
value = "null" | ||
return | ||
} | ||
|
||
value = t.Format(time.RFC3339) | ||
return tag, value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"gopkg.in/v1/yaml" | ||
) | ||
|
||
type TimeHolder struct { | ||
T Time `json:"t" yaml:"t"` | ||
} | ||
|
||
func TestTimeMarshalYAML(t *testing.T) { | ||
cases := []struct { | ||
input Time | ||
result string | ||
}{ | ||
{Time{}, "t: \"null\"\n"}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "t: 1998-05-05T05:05:05Z\n"}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: 1998-05-05T05:05:05Z\n"}, | ||
} | ||
|
||
for _, c := range cases { | ||
input := TimeHolder{c.input} | ||
result, err := yaml.Marshal(&input) | ||
if err != nil { | ||
t.Errorf("Failed to marshal input: '%v': %v", input, err) | ||
} | ||
if string(result) != c.result { | ||
t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result)) | ||
} | ||
} | ||
} | ||
|
||
func TestTimeUnmarshalYAML(t *testing.T) { | ||
cases := []struct { | ||
input string | ||
result Time | ||
}{ | ||
{"t: \"null\"\n", Time{}}, | ||
{"t: 1998-05-05T05:05:05Z\n", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)}, | ||
} | ||
|
||
for _, c := range cases { | ||
var result TimeHolder | ||
if err := yaml.Unmarshal([]byte(c.input), &result); err != nil { | ||
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err) | ||
} | ||
if result.T != c.result { | ||
t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result) | ||
} | ||
} | ||
} | ||
|
||
func TestTimeMarshalJSON(t *testing.T) { | ||
cases := []struct { | ||
input Time | ||
result string | ||
}{ | ||
{Time{}, "{\"t\":null}"}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"}, | ||
} | ||
|
||
for _, c := range cases { | ||
input := TimeHolder{c.input} | ||
result, err := json.Marshal(&input) | ||
if err != nil { | ||
t.Errorf("Failed to marshal input: '%v': %v", input, err) | ||
} | ||
if string(result) != c.result { | ||
t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result)) | ||
} | ||
} | ||
} | ||
|
||
func TestTimeUnmarshalJSON(t *testing.T) { | ||
cases := []struct { | ||
input string | ||
result Time | ||
}{ | ||
{"{\"t\":null}", Time{}}, | ||
{"{\"t\":\"1998-05-05T05:05:05Z\"}", Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC)}, | ||
} | ||
|
||
for _, c := range cases { | ||
var result TimeHolder | ||
if err := json.Unmarshal([]byte(c.input), &result); err != nil { | ||
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err) | ||
} | ||
if result.T != c.result { | ||
t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result) | ||
} | ||
} | ||
} | ||
|
||
func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) { | ||
cases := []struct { | ||
input Time | ||
}{ | ||
{Time{}}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC).Rfc3339Copy()}, | ||
{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Rfc3339Copy()}, | ||
} | ||
|
||
for _, c := range cases { | ||
input := TimeHolder{c.input} | ||
jsonMarshalled, err := json.Marshal(&input) | ||
if err != nil { | ||
t.Errorf("1: Failed to marshal input: '%v': %v", input, err) | ||
} | ||
|
||
var result TimeHolder | ||
err = yaml.Unmarshal(jsonMarshalled, &result) | ||
if err != nil { | ||
t.Errorf("2: Failed to unmarshall '%+v': %v", string(jsonMarshalled), err) | ||
} | ||
|
||
if !reflect.DeepEqual(input, result) { | ||
t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters