Remove EncodeToStream(..., []unversioned.GroupVersion)
Was not being used.
This commit is contained in:
@@ -339,7 +339,7 @@ type DirectCodec struct {
|
||||
}
|
||||
|
||||
// EncodeToStream does not do conversion. It sets the gvk during serialization. overrides are ignored.
|
||||
func (c DirectCodec) EncodeToStream(obj runtime.Object, stream io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
func (c DirectCodec) Encode(obj runtime.Object, stream io.Writer) error {
|
||||
gvks, _, err := c.ObjectTyper.ObjectKinds(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -347,7 +347,7 @@ func (c DirectCodec) EncodeToStream(obj runtime.Object, stream io.Writer, overri
|
||||
kind := obj.GetObjectKind()
|
||||
oldGVK := kind.GroupVersionKind()
|
||||
kind.SetGroupVersionKind(gvks[0])
|
||||
err = c.Serializer.EncodeToStream(obj, stream, overrides...)
|
||||
err = c.Serializer.Encode(obj, stream)
|
||||
kind.SetGroupVersionKind(oldGVK)
|
||||
return err
|
||||
}
|
||||
|
@@ -250,31 +250,28 @@ func TestTypes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVersionedEncoding(t *testing.T) {
|
||||
s, codec := GetTestScheme()
|
||||
out, err := runtime.Encode(codec, &TestType1{}, unversioned.GroupVersion{Version: "v2"})
|
||||
s, _ := GetTestScheme()
|
||||
cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
|
||||
encoder, _ := cf.SerializerForFileExtension("json")
|
||||
|
||||
codec := cf.CodecForVersions(encoder, nil, []unversioned.GroupVersion{{Version: "v2"}}, nil)
|
||||
out, err := runtime.Encode(codec, &TestType1{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(out) != `{"myVersionKey":"v2","myKindKey":"TestType1"}`+"\n" {
|
||||
t.Fatal(string(out))
|
||||
}
|
||||
_, err = runtime.Encode(codec, &TestType1{}, unversioned.GroupVersion{Version: "v3"})
|
||||
|
||||
codec = cf.CodecForVersions(encoder, nil, []unversioned.GroupVersion{{Version: "v3"}}, nil)
|
||||
_, err = runtime.Encode(codec, &TestType1{})
|
||||
if err == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cf := newCodecFactory(s, newSerializersForScheme(s, testMetaFactory{}))
|
||||
encoder, _ := cf.SerializerForFileExtension("json")
|
||||
|
||||
// codec that is unversioned uses the target version
|
||||
unversionedCodec := cf.CodecForVersions(encoder, nil, nil, nil)
|
||||
_, err = runtime.Encode(unversionedCodec, &TestType1{}, unversioned.GroupVersion{Version: "v3"})
|
||||
if err == nil || !runtime.IsNotRegisteredError(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// unversioned encode with no versions is written directly to wire
|
||||
out, err = runtime.Encode(unversionedCodec, &TestType1{})
|
||||
codec = cf.CodecForVersions(encoder, nil, nil, nil)
|
||||
out, err = runtime.Encode(codec, &TestType1{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -426,7 +423,7 @@ func TestDirectCodec(t *testing.T) {
|
||||
}
|
||||
directEncoder := df.EncoderForVersion(serializer, ignoredGV)
|
||||
directDecoder := df.DecoderToVersion(serializer, ignoredGV)
|
||||
out, err := runtime.Encode(directEncoder, &ExternalTestType1{}, ignoredGV)
|
||||
out, err := runtime.Encode(directEncoder, &ExternalTestType1{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@@ -159,8 +159,8 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
return obj, actual, nil
|
||||
}
|
||||
|
||||
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
|
||||
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
// Encode serializes the provided object to the given writer.
|
||||
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
|
||||
if s.yaml {
|
||||
json, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
|
@@ -165,8 +165,8 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
return unmarshalToObject(s.typer, s.creater, &actual, into, unk.Raw)
|
||||
}
|
||||
|
||||
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
|
||||
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
// Encode serializes the provided object to the given writer.
|
||||
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
|
||||
var unk runtime.Unknown
|
||||
kind := obj.GetObjectKind().GroupVersionKind()
|
||||
unk = runtime.Unknown{
|
||||
@@ -388,8 +388,8 @@ func unmarshalToObject(typer runtime.ObjectTyper, creater runtime.ObjectCreater,
|
||||
return obj, actual, nil
|
||||
}
|
||||
|
||||
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
|
||||
func (s *RawSerializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
// Encode serializes the provided object to the given writer. Overrides is ignored.
|
||||
func (s *RawSerializer) Encode(obj runtime.Object, w io.Writer) error {
|
||||
switch t := obj.(type) {
|
||||
case bufferedMarshaller:
|
||||
// this path performs a single allocation during write but requires the caller to implement
|
||||
|
@@ -30,8 +30,8 @@ import (
|
||||
// Encoder is a runtime.Encoder on a stream.
|
||||
type Encoder interface {
|
||||
// Encode will write the provided object to the stream or return an error. It obeys the same
|
||||
// contract as runtime.Encoder.
|
||||
Encode(obj runtime.Object, overrides ...unversioned.GroupVersion) error
|
||||
// contract as runtime.VersionedEncoder.
|
||||
Encode(obj runtime.Object) error
|
||||
}
|
||||
|
||||
// Decoder is a runtime.Decoder from a stream.
|
||||
@@ -127,8 +127,8 @@ func NewEncoder(w io.Writer, e runtime.Encoder) Encoder {
|
||||
}
|
||||
|
||||
// Encode writes the provided object to the nested writer.
|
||||
func (e *encoder) Encode(obj runtime.Object, overrides ...unversioned.GroupVersion) error {
|
||||
if err := e.encoder.EncodeToStream(obj, e.buf, overrides...); err != nil {
|
||||
func (e *encoder) Encode(obj runtime.Object) error {
|
||||
if err := e.encoder.Encode(obj, e.buf); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := e.writer.Write(e.buf.Bytes())
|
||||
|
@@ -222,11 +222,11 @@ func (c *codec) Decode(data []byte, defaultGVK *unversioned.GroupVersionKind, in
|
||||
return out, gvk, nil
|
||||
}
|
||||
|
||||
// EncodeToStream ensures the provided object is output in the right scheme. If overrides are specified, when
|
||||
// encoding the object the first override that matches the object's group is used. Other overrides are ignored.
|
||||
func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
// Encode ensures the provided object is output in the appropriate group and version, invoking
|
||||
// conversion if necessary. Unversioned objects (according to the ObjectTyper) are output as is.
|
||||
func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
|
||||
if _, ok := obj.(*runtime.Unknown); ok {
|
||||
return c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
return c.encoder.Encode(obj, w)
|
||||
}
|
||||
gvks, isUnversioned, err := c.typer.ObjectKinds(obj)
|
||||
if err != nil {
|
||||
@@ -234,38 +234,21 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
|
||||
}
|
||||
gvk := gvks[0]
|
||||
|
||||
if (c.encodeVersion == nil && len(overrides) == 0) || isUnversioned {
|
||||
if c.encodeVersion == nil || isUnversioned {
|
||||
objectKind := obj.GetObjectKind()
|
||||
old := objectKind.GroupVersionKind()
|
||||
objectKind.SetGroupVersionKind(gvk)
|
||||
err = c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
err = c.encoder.Encode(obj, w)
|
||||
objectKind.SetGroupVersionKind(old)
|
||||
return err
|
||||
}
|
||||
|
||||
targetGV, ok := c.encodeVersion[gvk.Group]
|
||||
// use override if provided
|
||||
for i, override := range overrides {
|
||||
if override.Group == gvk.Group {
|
||||
ok = true
|
||||
targetGV = override
|
||||
// swap the position of the override
|
||||
overrides[0], overrides[i] = targetGV, overrides[0]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// attempt a conversion to the sole encode version
|
||||
if !ok && c.preferredEncodeVersion != nil {
|
||||
ok = true
|
||||
targetGV = c.preferredEncodeVersion[0]
|
||||
if len(overrides) > 0 {
|
||||
// ensure the target override is first
|
||||
overrides = promoteOrPrependGroupVersion(targetGV, overrides)
|
||||
} else {
|
||||
// avoids allocating a new array for each call to EncodeToVersion
|
||||
overrides = c.preferredEncodeVersion
|
||||
}
|
||||
}
|
||||
|
||||
// if no fallback is available, error
|
||||
@@ -285,21 +268,8 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
|
||||
obj = out
|
||||
}
|
||||
// Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
|
||||
err = c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
err = c.encoder.Encode(obj, w)
|
||||
// restore the old GVK, in case conversion returned the same object
|
||||
objectKind.SetGroupVersionKind(old)
|
||||
return err
|
||||
}
|
||||
|
||||
// promoteOrPrependGroupVersion finds the group version in the provided group versions that has the same group as target.
|
||||
// If the group is found the returned array will have that group version in the first position - if the group is not found
|
||||
// the returned array will have target in the first position.
|
||||
func promoteOrPrependGroupVersion(target unversioned.GroupVersion, gvs []unversioned.GroupVersion) []unversioned.GroupVersion {
|
||||
for i, gv := range gvs {
|
||||
if gv.Group == target.Group {
|
||||
gvs[0], gvs[i] = gvs[i], gvs[0]
|
||||
return gvs
|
||||
}
|
||||
}
|
||||
return append([]unversioned.GroupVersion{target}, gvs...)
|
||||
}
|
||||
|
@@ -261,9 +261,8 @@ func (c *checkConvertor) ConvertFieldLabel(version, kind, label, value string) (
|
||||
}
|
||||
|
||||
type mockSerializer struct {
|
||||
err error
|
||||
obj runtime.Object
|
||||
versions []unversioned.GroupVersion
|
||||
err error
|
||||
obj runtime.Object
|
||||
|
||||
defaults, actual *unversioned.GroupVersionKind
|
||||
into runtime.Object
|
||||
@@ -275,9 +274,8 @@ func (s *mockSerializer) Decode(data []byte, defaults *unversioned.GroupVersionK
|
||||
return s.obj, s.actual, s.err
|
||||
}
|
||||
|
||||
func (s *mockSerializer) EncodeToStream(obj runtime.Object, w io.Writer, versions ...unversioned.GroupVersion) error {
|
||||
func (s *mockSerializer) Encode(obj runtime.Object, w io.Writer) error {
|
||||
s.obj = obj
|
||||
s.versions = versions
|
||||
return s.err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user