Skip to content

Commit fa4b7ae

Browse files
benluddyk8s-publishing-bot
authored andcommitted
KEP-4222: Adopt text and JSON transcoding support for CBOR.
Kubernetes-commit: 0b60c12194766e1473e65790369d5a8bcee537c6
1 parent b025858 commit fa4b7ae

File tree

13 files changed

+526
-753
lines changed

13 files changed

+526
-753
lines changed

pkg/runtime/serializer/cbor/cbor.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,6 @@ func (s *serializer) encode(mode modes.EncMode, obj runtime.Object, w io.Writer)
152152
v = u.UnstructuredContent()
153153
}
154154

155-
if err := modes.RejectCustomMarshalers(v); err != nil {
156-
return err
157-
}
158-
159155
if _, err := w.Write(selfDescribedCBOR); err != nil {
160156
return err
161157
}
@@ -270,8 +266,6 @@ func (s *serializer) unmarshal(data []byte, into interface{}) (strict, lax error
270266
}
271267
}()
272268
into = &content
273-
} else if err := modes.RejectCustomMarshalers(into); err != nil {
274-
return nil, err
275269
}
276270

277271
if !s.options.strict {

pkg/runtime/serializer/cbor/cbor_test.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -209,38 +209,38 @@ func TestEncode(t *testing.T) {
209209
},
210210
},
211211
{
212-
name: "unsupported marshaler",
212+
name: "text marshaler",
213213
in: &textMarshalerObject{},
214214
assertOnWriter: func() (io.Writer, func(*testing.T)) {
215215
var b bytes.Buffer
216216
return &b, func(t *testing.T) {
217-
if b.Len() != 0 {
218-
t.Errorf("expected no bytes to be written, got %d", b.Len())
217+
if diff := cmp.Diff(b.Bytes(), []byte{0xd9, 0xd9, 0xf7, 0x64, 't', 'e', 's', 't'}); diff != "" {
218+
t.Errorf("unexpected diff:\n%s", diff)
219219
}
220220
}
221221
},
222222
assertOnError: func(t *testing.T, err error) {
223-
if want := "unable to serialize *cbor.textMarshalerObject: *cbor.textMarshalerObject implements encoding.TextMarshaler without corresponding cbor interface"; err == nil || err.Error() != want {
224-
t.Errorf("expected error %q, got: %v", want, err)
223+
if err != nil {
224+
t.Errorf("expected nil error, got: %v", err)
225225
}
226226
},
227227
},
228228
{
229-
name: "unsupported marshaler within unstructured content",
229+
name: "text marshaler within unstructured content",
230230
in: &unstructured.Unstructured{
231231
Object: map[string]interface{}{"": textMarshalerObject{}},
232232
},
233233
assertOnWriter: func() (io.Writer, func(*testing.T)) {
234234
var b bytes.Buffer
235235
return &b, func(t *testing.T) {
236-
if b.Len() != 0 {
237-
t.Errorf("expected no bytes to be written, got %d", b.Len())
236+
if diff := cmp.Diff(b.Bytes(), []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x40, 0x64, 't', 'e', 's', 't'}); diff != "" {
237+
t.Errorf("unexpected diff:\n%s", diff)
238238
}
239239
}
240240
},
241241
assertOnError: func(t *testing.T, err error) {
242-
if want := "unable to serialize map[string]interface {}: cbor.textMarshalerObject implements encoding.TextMarshaler without corresponding cbor interface"; err == nil || err.Error() != want {
243-
t.Errorf("expected error %q, got: %v", want, err)
242+
if err != nil {
243+
t.Errorf("expected nil error, got: %v", err)
244244
}
245245
},
246246
},
@@ -689,19 +689,6 @@ func TestDecode(t *testing.T) {
689689
}
690690
},
691691
},
692-
{
693-
name: "into unsupported marshaler",
694-
data: []byte("\xa0"),
695-
into: &textMarshalerObject{},
696-
metaFactory: stubMetaFactory{gvk: &schema.GroupVersionKind{}},
697-
typer: stubTyper{gvks: []schema.GroupVersionKind{{Version: "v", Kind: "k"}}},
698-
expectedGVK: &schema.GroupVersionKind{Version: "v", Kind: "k"},
699-
assertOnError: func(t *testing.T, err error) {
700-
if want := "unable to serialize *cbor.textMarshalerObject: *cbor.textMarshalerObject implements encoding.TextMarshaler without corresponding cbor interface"; err == nil || err.Error() != want {
701-
t.Errorf("expected error %q, got: %v", want, err)
702-
}
703-
},
704-
},
705692
} {
706693
t.Run(tc.name, func(t *testing.T) {
707694
s := newSerializer(tc.metaFactory, tc.creater, tc.typer, tc.options...)
@@ -731,7 +718,7 @@ func (textMarshalerObject) DeepCopyObject() runtime.Object {
731718
}
732719

733720
func (textMarshalerObject) MarshalText() ([]byte, error) {
734-
return nil, nil
721+
return []byte("test"), nil
735722
}
736723

737724
func TestMetaFactoryInterpret(t *testing.T) {

pkg/runtime/serializer/cbor/direct/direct.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,13 @@ import (
2525

2626
// Marshal serializes a value to CBOR. If there is more than one way to encode the value, it will
2727
// make the same choice as the CBOR implementation of runtime.Serializer.
28-
//
29-
// Note: Support for CBOR is at an alpha stage. If the value (or, for composite types, any of its
30-
// nested values) implement any of the interfaces encoding.TextMarshaler, encoding.TextUnmarshaler,
31-
// encoding/json.Marshaler, or encoding/json.Unmarshaler, a non-nil error will be returned unless
32-
// the value also implements the corresponding CBOR interfaces. This limitation will ultimately be
33-
// removed in favor of automatic transcoding to CBOR.
34-
func Marshal(src interface{}) ([]byte, error) {
35-
if err := modes.RejectCustomMarshalers(src); err != nil {
36-
return nil, err
37-
}
28+
func Marshal(src any) ([]byte, error) {
3829
return modes.Encode.Marshal(src)
3930
}
4031

4132
// Unmarshal deserializes from CBOR into an addressable value. If there is more than one way to
4233
// unmarshal a value, it will make the same choice as the CBOR implementation of runtime.Serializer.
43-
//
44-
// Note: Support for CBOR is at an alpha stage. If the value (or, for composite types, any of its
45-
// nested values) implement any of the interfaces encoding.TextMarshaler, encoding.TextUnmarshaler,
46-
// encoding/json.Marshaler, or encoding/json.Unmarshaler, a non-nil error will be returned unless
47-
// the value also implements the corresponding CBOR interfaces. This limitation will ultimately be
48-
// removed in favor of automatic transcoding to CBOR.
49-
func Unmarshal(src []byte, dst interface{}) error {
50-
if err := modes.RejectCustomMarshalers(dst); err != nil {
51-
return err
52-
}
34+
func Unmarshal(src []byte, dst any) error {
5335
return modes.Decode.Unmarshal(src, dst)
5436
}
5537

pkg/runtime/serializer/cbor/direct/direct_test.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)