Make Serializer.options private and immutable and improve godoc
This commit is contained in:
		@@ -37,7 +37,7 @@ import (
 | 
				
			|||||||
// is not nil, the object has the group, version, and kind fields set.
 | 
					// is not nil, the object has the group, version, and kind fields set.
 | 
				
			||||||
// Deprecated: use NewSerializerWithOptions instead.
 | 
					// Deprecated: use NewSerializerWithOptions instead.
 | 
				
			||||||
func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
 | 
					func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, pretty bool) *Serializer {
 | 
				
			||||||
	return NewSerializerWithOptions(meta, creater, typer, &SerializerOptions{false, pretty, false})
 | 
						return NewSerializerWithOptions(meta, creater, typer, SerializerOptions{false, pretty, false})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
 | 
					// NewYAMLSerializer creates a YAML serializer that handles encoding versioned objects into the proper YAML form. If typer
 | 
				
			||||||
@@ -45,40 +45,46 @@ func NewSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtim
 | 
				
			|||||||
// matches JSON, and will error if constructs are used that do not serialize to JSON.
 | 
					// matches JSON, and will error if constructs are used that do not serialize to JSON.
 | 
				
			||||||
// Deprecated: use NewSerializerWithOptions instead.
 | 
					// Deprecated: use NewSerializerWithOptions instead.
 | 
				
			||||||
func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
 | 
					func NewYAMLSerializer(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper) *Serializer {
 | 
				
			||||||
	return NewSerializerWithOptions(meta, creater, typer, &SerializerOptions{true, false, false})
 | 
						return NewSerializerWithOptions(meta, creater, typer, SerializerOptions{true, false, false})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
 | 
					// NewSerializerWithOptions creates a JSON/YAML serializer that handles encoding versioned objects into the proper JSON/YAML
 | 
				
			||||||
// form. If typer is not nil, the object has the group, version, and kind fields set.
 | 
					// form. If typer is not nil, the object has the group, version, and kind fields set. Options are copied into the Serializer
 | 
				
			||||||
func NewSerializerWithOptions(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, serializerOptions *SerializerOptions) *Serializer {
 | 
					// and are immutable.
 | 
				
			||||||
 | 
					func NewSerializerWithOptions(meta MetaFactory, creater runtime.ObjectCreater, typer runtime.ObjectTyper, options SerializerOptions) *Serializer {
 | 
				
			||||||
	return &Serializer{
 | 
						return &Serializer{
 | 
				
			||||||
		meta:              meta,
 | 
							meta:    meta,
 | 
				
			||||||
		creater:           creater,
 | 
							creater: creater,
 | 
				
			||||||
		typer:             typer,
 | 
							typer:   typer,
 | 
				
			||||||
		SerializerOptions: serializerOptions,
 | 
							options: options,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SerializerOptions holds the options which are used to creating a JSON/YAML serializer.
 | 
					// SerializerOptions holds the options which are used to configure a JSON/YAML serializer.
 | 
				
			||||||
// For example:
 | 
					// example:
 | 
				
			||||||
// (1) we can creates a JSON serializer once we set `Yaml` to `false`.
 | 
					// (1) To configure a JSON serializer, set `Yaml` to `false`.
 | 
				
			||||||
// (2) we can creates a YAML serializer once we set `Yaml` to `true`. This serializer supports only the subset of YAML that
 | 
					// (2) To configure a YAML serializer, set `Yaml` to `true`.
 | 
				
			||||||
//     matches JSON, and will error if constructs are used that do not serialize to JSON.
 | 
					// (3) To configure a strict serializer that can return strictDecodingError, set `Strict` to `true`.
 | 
				
			||||||
//     Please note that `Pretty` is silently ignored when `Yaml` is `true`.
 | 
					 | 
				
			||||||
// (3) we can creates a strict JSON/YAML serializer that can also return errors of type strictDecodingError, once we set
 | 
					 | 
				
			||||||
//    `Strict` to `true`. And note that this serializer is not as performant as the non-strict variant, and should not be
 | 
					 | 
				
			||||||
//    used in fast paths.
 | 
					 | 
				
			||||||
type SerializerOptions struct {
 | 
					type SerializerOptions struct {
 | 
				
			||||||
	Yaml   bool
 | 
						// Yaml: configures the Serializer to work with JSON(false) or YAML(true).
 | 
				
			||||||
 | 
						// When `Yaml` is enabled, this serializer only supports the subset of YAML that
 | 
				
			||||||
 | 
						// matches JSON, and will error if constructs are used that do not serialize to JSON.
 | 
				
			||||||
 | 
						Yaml bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Pretty: configures a JSON enabled Serializer(`Yaml: false`) to produce human-readable output.
 | 
				
			||||||
 | 
						// This option is silently ignored when `Yaml` is `true`.
 | 
				
			||||||
	Pretty bool
 | 
						Pretty bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Strict: configures the Serializer to return strictDecodingError's when duplicate fields are present decoding JSON or YAML.
 | 
				
			||||||
 | 
						// Note that enabling this option is not as performant as the non-strict variant, and should not be used in fast paths.
 | 
				
			||||||
	Strict bool
 | 
						Strict bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Serializer struct {
 | 
					type Serializer struct {
 | 
				
			||||||
	meta    MetaFactory
 | 
						meta    MetaFactory
 | 
				
			||||||
 | 
						options SerializerOptions
 | 
				
			||||||
	creater runtime.ObjectCreater
 | 
						creater runtime.ObjectCreater
 | 
				
			||||||
	typer   runtime.ObjectTyper
 | 
						typer   runtime.ObjectTyper
 | 
				
			||||||
	*SerializerOptions
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Serializer implements Serializer
 | 
					// Serializer implements Serializer
 | 
				
			||||||
@@ -193,7 +199,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data := originalData
 | 
						data := originalData
 | 
				
			||||||
	if s.Yaml {
 | 
						if s.options.Yaml {
 | 
				
			||||||
		altered, err := yaml.YAMLToJSON(data)
 | 
							altered, err := yaml.YAMLToJSON(data)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return nil, nil, err
 | 
				
			||||||
@@ -251,7 +257,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// If the deserializer is non-strict, return successfully here.
 | 
						// If the deserializer is non-strict, return successfully here.
 | 
				
			||||||
	if !s.Strict {
 | 
						if !s.options.Strict {
 | 
				
			||||||
		return obj, actual, nil
 | 
							return obj, actual, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -280,7 +286,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Encode serializes the provided object to the given writer.
 | 
					// Encode serializes the provided object to the given writer.
 | 
				
			||||||
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
 | 
					func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
 | 
				
			||||||
	if s.Yaml {
 | 
						if s.options.Yaml {
 | 
				
			||||||
		json, err := caseSensitiveJsonIterator.Marshal(obj)
 | 
							json, err := caseSensitiveJsonIterator.Marshal(obj)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
@@ -293,7 +299,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if s.Pretty {
 | 
						if s.options.Pretty {
 | 
				
			||||||
		data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", "  ")
 | 
							data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", "  ")
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
@@ -307,7 +313,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// RecognizesData implements the RecognizingDecoder interface.
 | 
					// RecognizesData implements the RecognizingDecoder interface.
 | 
				
			||||||
func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
 | 
					func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error) {
 | 
				
			||||||
	if s.Yaml {
 | 
						if s.options.Yaml {
 | 
				
			||||||
		// we could potentially look for '---'
 | 
							// we could potentially look for '---'
 | 
				
			||||||
		return false, true, nil
 | 
							return false, true, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -418,9 +418,9 @@ func TestDecode(t *testing.T) {
 | 
				
			|||||||
	for i, test := range testCases {
 | 
						for i, test := range testCases {
 | 
				
			||||||
		var s runtime.Serializer
 | 
							var s runtime.Serializer
 | 
				
			||||||
		if test.yaml {
 | 
							if test.yaml {
 | 
				
			||||||
			s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, &json.SerializerOptions{Yaml: test.yaml, Pretty: false, Strict: test.strict})
 | 
								s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: false, Strict: test.strict})
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, &json.SerializerOptions{Yaml: test.yaml, Pretty: test.pretty, Strict: test.strict})
 | 
								s = json.NewSerializerWithOptions(json.DefaultMetaFactory, test.creater, test.typer, json.SerializerOptions{Yaml: test.yaml, Pretty: test.pretty, Strict: test.strict})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		obj, gvk, err := s.Decode([]byte(test.data), test.defaultGVK, test.into)
 | 
							obj, gvk, err := s.Decode([]byte(test.data), test.defaultGVK, test.into)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user