Skip to content

Commit 4a14152

Browse files
committed
Make Serializer.options private and immutable and improve godoc
1 parent 84a859f commit 4a14152

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,54 @@ import (
3737
// is not nil, the object has the group, version, and kind fields set.
3838
// Deprecated: use NewSerializerWithOptions instead.
3939
func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
40-
return NewSerializerWithOptions(meta, creater, typer, &SerializerOptions{false, pretty, false})
40+
return NewSerializerWithOptions(meta, creater, typer, SerializerOptions{false, pretty, false})
4141
}
4242

4343
// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
4444
// is not nil, the object has the group, version, and kind fields set. This serializer supports only the subset of YAML that
4545
// matches JSON, and will error if constructs are used that do not serialize to JSON.
4646
// Deprecated: use NewSerializerWithOptions instead.
4747
func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
48-
return NewSerializerWithOptions(meta, creater, typer, &SerializerOptions{true, false, false})
48+
return NewSerializerWithOptions(meta, creater, typer, SerializerOptions{true, false, false})
4949
}
5050

5151
// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
52-
// form. If typer is not nil, the object has the group, version, and kind fields set.
53-
func NewSerializerWithOptions(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, serializerOptions *SerializerOptions) *Serializer {
52+
// form. If typer is not nil, the object has the group, version, and kind fields set. Options are copied into the Serializer
53+
// and are immutable.
54+
func NewSerializerWithOptions(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, options SerializerOptions) *Serializer {
5455
return &Serializer{
55-
meta: meta,
56-
creater: creater,
57-
typer: typer,
58-
SerializerOptions: serializerOptions,
56+
meta: meta,
57+
creater: creater,
58+
typer: typer,
59+
options: options,
5960
}
6061
}
6162

62-
// SerializerOptions holds the options which are used to creating a JSON/YAML serializer.
63-
// For example:
64-
// (1) we can creates a JSON serializer once we set `Yaml` to `false`.
65-
// (2) we can creates a YAML serializer once we set `Yaml` to `true`. This serializer supports only the subset of YAML that
66-
// matches JSON, and will error if constructs are used that do not serialize to JSON.
67-
// Please note that `Pretty` is silently ignored when `Yaml` is `true`.
68-
// (3) we can creates a strict JSON/YAML serializer that can also return errors of type strictDecodingError, once we set
69-
// `Strict` to `true`. And note that this serializer is not as performant as the non-strict variant, and should not be
70-
// used in fast paths.
63+
// SerializerOptions holds the options which are used to configure a JSON/YAML serializer.
64+
// example:
65+
// (1) To configure a JSON serializer, set `Yaml` to `false`.
66+
// (2) To configure a YAML serializer, set `Yaml` to `true`.
67+
// (3) To configure a strict serializer that can return strictDecodingError, set `Strict` to `true`.
7168
type SerializerOptions struct {
72-
Yaml bool
69+
// Yaml: configures the Serializer to work with JSON(false) or YAML(true).
70+
// When `Yaml` is enabled, this serializer only supports the subset of YAML that
71+
// matches JSON, and will error if constructs are used that do not serialize to JSON.
72+
Yaml bool
73+
74+
// Pretty: configures a JSON enabled Serializer(`Yaml: false`) to produce human-readable output.
75+
// This option is silently ignored when `Yaml` is `true`.
7376
Pretty bool
77+
78+
// Strict: configures the Serializer to return strictDecodingError's when duplicate fields are present decoding JSON or YAML.
79+
// Note that enabling this option is not as performant as the non-strict variant, and should not be used in fast paths.
7480
Strict bool
7581
}
7682

7783
type Serializer struct {
7884
meta MetaFactory
85+
options SerializerOptions
7986
creater runtime.ObjectCreater
8087
typer runtime.ObjectTyper
81-
*SerializerOptions
8288
}
8389

8490
// Serializer implements Serializer
@@ -193,7 +199,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
193199
}
194200

195201
data := originalData
196-
if s.Yaml {
202+
if s.options.Yaml {
197203
altered, err := yaml.YAMLToJSON(data)
198204
if err != nil {
199205
return nil, nil, err
@@ -251,7 +257,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
251257
}
252258

253259
// If the deserializer is non-strict, return successfully here.
254-
if !s.Strict {
260+
if !s.options.Strict {
255261
return obj, actual, nil
256262
}
257263

@@ -280,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
280286

281287
// Encode serializes the provided object to the given writer.
282288
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
283-
if s.Yaml {
289+
if s.options.Yaml {
284290
json, err := caseSensitiveJsonIterator.Marshal(obj)
285291
if err != nil {
286292
return err
@@ -293,7 +299,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
293299
return err
294300
}
295301

296-
if s.Pretty {
302+
if s.options.Pretty {
297303
data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", " ")
298304
if err != nil {
299305
return err
@@ -307,7 +313,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
307313

308314
// RecognizesData implements the RecognizingDecoder interface.
309315
func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
310-
if s.Yaml {
316+
if s.options.Yaml {
311317
// we could potentially look for '---'
312318
return false, true, nil
313319
}

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ func TestDecode(t *testing.T) {
418418
for i, test := range testCases {
419419
var s runtime.Serializer
420420
if test.yaml {
421-
s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, &json.SerializerOptions{Yaml: test.yaml, Pretty: false, Strict: test.strict})
421+
s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: false, Strict: test.strict})
422422
} else {
423-
s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, &json.SerializerOptions{Yaml: test.yaml, Pretty: test.pretty, Strict: test.strict})
423+
s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: test.pretty, Strict: test.strict})
424424
}
425425
obj, gvk, err := s.Decode([]byte(test.data), test.defaultGVK, test.into)
426426

0 commit comments

Comments
 (0)