Skip to content

Commit 50e39b1

Browse files
Merge pull request #132935 from benluddy/cbor-bump-custom-marshalers
KEP-4222: Adopt text and JSON transcoding support for CBOR. Kubernetes-commit: dfc0998baa4d6c2cd630aa3c5b8def4e9b1fcd8e
2 parents 58c4eb0 + fa4b7ae commit 50e39b1

File tree

15 files changed

+529
-756
lines changed

15 files changed

+529
-756
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ godebug default=go1.24
99
require (
1010
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
1111
github.com/davecgh/go-spew v1.1.1
12-
github.com/fxamacker/cbor/v2 v2.8.0
12+
github.com/fxamacker/cbor/v2 v2.9.0
1313
github.com/gogo/protobuf v1.3.2
1414
github.com/google/gnostic-models v0.7.0
1515
github.com/google/go-cmp v0.7.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/fxamacker/cbor/v2 v2.8.0 h1:fFtUGXUzXPHTIUdne5+zzMPTfffl3RD5qYnkY40vtxU=
8-
github.com/fxamacker/cbor/v2 v2.8.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
7+
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
8+
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
99
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
1010
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
1111
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=

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)