serializer.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "k8s.io/apimachinery/pkg/runtime"
  16. "k8s.io/apimachinery/pkg/runtime/serializer/versioning"
  17. )
  18. // NewSingleContentTypeSerializer wraps a serializer in a NegotiatedSerializer that handles one content type
  19. func NewSingleContentTypeSerializer(scheme *runtime.Scheme, info runtime.SerializerInfo) runtime.StorageSerializer {
  20. return &wrappedSerializer{
  21. scheme: scheme,
  22. info: info,
  23. }
  24. }
  25. type wrappedSerializer struct {
  26. scheme *runtime.Scheme
  27. info runtime.SerializerInfo
  28. }
  29. var _ runtime.StorageSerializer = &wrappedSerializer{}
  30. func (s *wrappedSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
  31. return []runtime.SerializerInfo{s.info}
  32. }
  33. func (s *wrappedSerializer) UniversalDeserializer() runtime.Decoder {
  34. return s.info.Serializer
  35. }
  36. func (s *wrappedSerializer) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
  37. return versioning.NewCodec(encoder, nil, s.scheme, s.scheme, s.scheme, s.scheme, gv, nil, s.scheme.Name())
  38. }
  39. func (s *wrappedSerializer) DecoderToVersion(decoder runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder {
  40. return versioning.NewCodec(nil, decoder, s.scheme, s.scheme, s.scheme, s.scheme, nil, gv, s.scheme.Name())
  41. }