mirror of https://github.com/helm/helm
This package mainly exists to workaround an issue in Go where the serializer doesn't omit an empty value for time: https://github.com/golang/go/issues/11939. This replaces all release and hook object time references with the new time package so things actually marshal correctly Signed-off-by: Taylor Thomas <taylor.thomas@microsoft.com>pull/6679/head
parent
3edad39e08
commit
aa429e150a
@ -1 +1 @@
|
||||
{"name":"flummoxed-chickadee","info":{"first_deployed":"0001-01-01T00:00:00Z","last_deployed":"2016-01-16T00:00:00Z","deleted":"0001-01-01T00:00:00Z","status":"deployed","notes":"release notes"},"namespace":"default"}
|
||||
{"name":"flummoxed-chickadee","info":{"first_deployed":"","last_deployed":"2016-01-16T00:00:00Z","deleted":"","status":"deployed","notes":"release notes"},"namespace":"default"}
|
||||
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package time contains a wrapper for time.Time in the standard library and
|
||||
// associated methods. This package mainly exists to workaround an issue in Go
|
||||
// where the serializer doesn't omit an empty value for time:
|
||||
// https://github.com/golang/go/issues/11939. As such, this can be removed if a
|
||||
// proposal is ever accepted for Go
|
||||
package time
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"time"
|
||||
)
|
||||
|
||||
// emptyString contains an empty JSON string value to be used as output
|
||||
var emptyString = `""`
|
||||
|
||||
// Time is a convenience wrapper around stdlib time, but with different
|
||||
// marshalling and unmarshaling for zero values
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// Now returns the current time. It is a convenience wrapper around time.Now()
|
||||
func Now() Time {
|
||||
return Time{time.Now()}
|
||||
}
|
||||
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
if t.Time.IsZero() {
|
||||
return []byte(emptyString), nil
|
||||
}
|
||||
|
||||
return t.Time.MarshalJSON()
|
||||
}
|
||||
|
||||
func (t *Time) UnmarshalJSON(b []byte) error {
|
||||
if bytes.Equal(b, []byte("null")) {
|
||||
return nil
|
||||
}
|
||||
// If it is empty, we don't have to set anything since time.Time is not a
|
||||
// pointer and will be set to the zero value
|
||||
if bytes.Equal([]byte(emptyString), b) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return t.Time.UnmarshalJSON(b)
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package time
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
testingTime, _ = time.Parse(time.RFC3339, "1977-09-02T22:04:05Z")
|
||||
testingTimeString = `"1977-09-02T22:04:05Z"`
|
||||
)
|
||||
|
||||
func TestNonZeroValueMarshal(t *testing.T) {
|
||||
myTime := Time{testingTime}
|
||||
res, err := json.Marshal(myTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if testingTimeString != string(res) {
|
||||
t.Errorf("expected a marshaled value of %s, got %s", testingTimeString, res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZeroValueMarshal(t *testing.T) {
|
||||
res, err := json.Marshal(Time{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(res) != emptyString {
|
||||
t.Errorf("expected zero value to marshal to empty string, got %s", res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonZeroValueUnmarshal(t *testing.T) {
|
||||
var myTime Time
|
||||
err := json.Unmarshal([]byte(testingTimeString), &myTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !myTime.Equal(testingTime) {
|
||||
t.Errorf("expected time to be equal to %v, got %v", testingTime, myTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyStringUnmarshal(t *testing.T) {
|
||||
var myTime Time
|
||||
err := json.Unmarshal([]byte(emptyString), &myTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !myTime.IsZero() {
|
||||
t.Errorf("expected time to be equal to zero value, got %v", myTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestZeroValueUnmarshal(t *testing.T) {
|
||||
// This test ensures that we can unmarshal any time value that was output
|
||||
// with the current go default value of "0001-01-01T00:00:00Z"
|
||||
var myTime Time
|
||||
err := json.Unmarshal([]byte(`"0001-01-01T00:00:00Z"`), &myTime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !myTime.IsZero() {
|
||||
t.Errorf("expected time to be equal to zero value, got %v", myTime)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue