recognizer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2014 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 recognizer
  14. import (
  15. "bufio"
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. )
  22. type RecognizingDecoder interface {
  23. runtime.Decoder
  24. // RecognizesData should return true if the input provided in the provided reader
  25. // belongs to this decoder, or an error if the data could not be read or is ambiguous.
  26. // Unknown is true if the data could not be determined to match the decoder type.
  27. // Decoders should assume that they can read as much of peek as they need (as the caller
  28. // provides) and may return unknown if the data provided is not sufficient to make a
  29. // a determination. When peek returns EOF that may mean the end of the input or the
  30. // end of buffered input - recognizers should return the best guess at that time.
  31. RecognizesData(peek io.Reader) (ok, unknown bool, err error)
  32. }
  33. // NewDecoder creates a decoder that will attempt multiple decoders in an order defined
  34. // by:
  35. //
  36. // 1. The decoder implements RecognizingDecoder and identifies the data
  37. // 2. All other decoders, and any decoder that returned true for unknown.
  38. //
  39. // The order passed to the constructor is preserved within those priorities.
  40. func NewDecoder(decoders ...runtime.Decoder) runtime.Decoder {
  41. return &decoder{
  42. decoders: decoders,
  43. }
  44. }
  45. type decoder struct {
  46. decoders []runtime.Decoder
  47. }
  48. var _ RecognizingDecoder = &decoder{}
  49. func (d *decoder) RecognizesData(peek io.Reader) (bool, bool, error) {
  50. var (
  51. lastErr error
  52. anyUnknown bool
  53. )
  54. data, _ := bufio.NewReaderSize(peek, 1024).Peek(1024)
  55. for _, r := range d.decoders {
  56. switch t := r.(type) {
  57. case RecognizingDecoder:
  58. ok, unknown, err := t.RecognizesData(bytes.NewBuffer(data))
  59. if err != nil {
  60. lastErr = err
  61. continue
  62. }
  63. anyUnknown = anyUnknown || unknown
  64. if !ok {
  65. continue
  66. }
  67. return true, false, nil
  68. }
  69. }
  70. return false, anyUnknown, lastErr
  71. }
  72. func (d *decoder) Decode(data []byte, gvk *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
  73. var (
  74. lastErr error
  75. skipped []runtime.Decoder
  76. )
  77. // try recognizers, record any decoders we need to give a chance later
  78. for _, r := range d.decoders {
  79. switch t := r.(type) {
  80. case RecognizingDecoder:
  81. buf := bytes.NewBuffer(data)
  82. ok, unknown, err := t.RecognizesData(buf)
  83. if err != nil {
  84. lastErr = err
  85. continue
  86. }
  87. if unknown {
  88. skipped = append(skipped, t)
  89. continue
  90. }
  91. if !ok {
  92. continue
  93. }
  94. return r.Decode(data, gvk, into)
  95. default:
  96. skipped = append(skipped, t)
  97. }
  98. }
  99. // try recognizers that returned unknown or didn't recognize their data
  100. for _, r := range skipped {
  101. out, actual, err := r.Decode(data, gvk, into)
  102. if err != nil {
  103. lastErr = err
  104. continue
  105. }
  106. return out, actual, nil
  107. }
  108. if lastErr == nil {
  109. lastErr = fmt.Errorf("no serialization format matched the provided data")
  110. }
  111. return nil, nil, lastErr
  112. }