From a246eb19e5f801f05eeffb2bbf73ad25cb0c1d1c Mon Sep 17 00:00:00 2001 From: Ben Luddy Date: Thu, 9 May 2024 15:09:01 -0400 Subject: [PATCH] Decode CBOR time tags to interface{} as RFC 3339 timestamps. --- .../serializer/cbor/internal/modes/appendixa_test.go | 7 ++----- .../runtime/serializer/cbor/internal/modes/decode.go | 3 +++ .../serializer/cbor/internal/modes/decode_test.go | 12 +----------- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/appendixa_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/appendixa_test.go index c80f48e3dd7..dad7fc862c0 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/appendixa_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/appendixa_test.go @@ -300,17 +300,15 @@ func TestAppendixA(t *testing.T) { reasonByteString, reasonTimeToInterface, }, - fixme: "decoding of tagged time into interface{} must produce RFC3339 timestamp compatible with JSON, not time.Time", }, { example: hex("c11a514b67b0"), - decoded: "2013-03-21T16:04:00Z", - encoded: hex("54323031332d30332d32315431363a30343a30305a"), + decoded: "2013-03-21T20:04:00Z", + encoded: hex("54323031332d30332d32315432303a30343a30305a"), reasons: []string{ reasonByteString, reasonTimeToInterface, }, - fixme: "decoding of tagged time into interface{} must produce RFC3339 timestamp compatible with JSON, not time.Time", }, { example: hex("c1fb41d452d9ec200000"), @@ -320,7 +318,6 @@ func TestAppendixA(t *testing.T) { reasonByteString, reasonTimeToInterface, }, - fixme: "decoding of tagged time into interface{} must produce RFC3339 timestamp compatible with JSON, not time.Time", }, { example: hex("d74401020304"), diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode.go index f1290989754..c5df5cbba8b 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode.go @@ -85,6 +85,9 @@ var Decode cbor.DecMode = func() cbor.DecMode { // instead of the default, a cbor.Tag representing a (number, content) pair. UnrecognizedTagToAny: cbor.UnrecognizedTagContentToAny, + // Decode time tags to interface{} as strings containing RFC 3339 timestamps. + TimeTagToAny: cbor.TimeTagToRFC3339Nano, + // For parity with JSON, strings can be decoded into time.Time if they are RFC 3339 // timestamps. ByteStringToTime: cbor.ByteStringToTimeAllowed, diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode_test.go index 57ddef9fed3..6c5b9c09226 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/decode_test.go @@ -647,7 +647,6 @@ func TestDecode(t *testing.T) { name: "tag 0 RFC3339 text string", in: hex("c074323030362d30312d30325431353a30343a30355a"), // 0("2006-01-02T15:04:05Z") want: "2006-01-02T15:04:05Z", - fixme: "decoding RFC3339 text string tagged with 0 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { @@ -668,63 +667,54 @@ func TestDecode(t *testing.T) { name: "tag 1 timestamp unsigned integer", in: hex("c11a43b940e5"), // 1(1136214245) want: "2006-01-02T15:04:05Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with float16 value", in: hex("c1f93c00"), // 1(1.0_1) want: "1970-01-01T00:00:01Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with float32 value", in: hex("c1fa3f800000"), // 1(1.0_2) want: "1970-01-01T00:00:01Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with float64 value", in: hex("c1fb3ff0000000000000"), // 1(1.0_3) want: "1970-01-01T00:00:01Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with a five digit year", in: hex("c11b0000003afff44181"), // 1(253402300801) want: "10000-01-01T00:00:01Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", - assertOnError: assertNilError, + assertOnError: assertErrorMessage("cbor: decoded time cannot be represented in RFC3339 format with sub-second precision: Time.MarshalText: year outside of range [0,9999]"), }, { name: "tag 1 with a negative integer value", in: hex("c120"), // 1(-1) want: "1969-12-31T23:59:59Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with a negative float16 value", in: hex("c1f9bc00"), // 1(-1.0_1) want: "1969-12-31T23:59:59Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with a negative float32 value", in: hex("c1fabf800000"), // 1(-1.0_2) want: "1969-12-31T23:59:59Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, { name: "tag 1 with a negative float64 value", in: hex("c1fbbff0000000000000"), // 1(-1.0_3) want: "1969-12-31T23:59:59Z", - fixme: "decoding cbor data tagged with 1 produces time.Time instead of RFC3339 timestamp string", assertOnError: assertNilError, }, {