fields_printer.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright 2017 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 explain
  14. import "k8s.io/kube-openapi/pkg/util/proto"
  15. // indentDesc is the level of indentation for descriptions.
  16. const indentDesc = 2
  17. // regularFieldsPrinter prints fields with their type and description.
  18. type regularFieldsPrinter struct {
  19. Writer *Formatter
  20. Error error
  21. }
  22. var _ proto.SchemaVisitor = &regularFieldsPrinter{}
  23. var _ fieldsPrinter = &regularFieldsPrinter{}
  24. // VisitArray prints a Array type. It is just a passthrough.
  25. func (f *regularFieldsPrinter) VisitArray(a *proto.Array) {
  26. a.SubType.Accept(f)
  27. }
  28. // VisitKind prints a Kind type. It prints each key in the kind, with
  29. // the type, the required flag, and the description.
  30. func (f *regularFieldsPrinter) VisitKind(k *proto.Kind) {
  31. for _, key := range k.Keys() {
  32. v := k.Fields[key]
  33. required := ""
  34. if k.IsRequired(key) {
  35. required = " -required-"
  36. }
  37. if err := f.Writer.Write("%s\t<%s>%s", key, GetTypeName(v), required); err != nil {
  38. f.Error = err
  39. return
  40. }
  41. if err := f.Writer.Indent(indentDesc).WriteWrapped("%s", v.GetDescription()); err != nil {
  42. f.Error = err
  43. return
  44. }
  45. if err := f.Writer.Write(""); err != nil {
  46. f.Error = err
  47. return
  48. }
  49. }
  50. }
  51. // VisitMap prints a Map type. It is just a passthrough.
  52. func (f *regularFieldsPrinter) VisitMap(m *proto.Map) {
  53. m.SubType.Accept(f)
  54. }
  55. // VisitPrimitive prints a Primitive type. It stops the recursion.
  56. func (f *regularFieldsPrinter) VisitPrimitive(p *proto.Primitive) {
  57. // Nothing to do. Shouldn't really happen.
  58. }
  59. // VisitReference prints a Reference type. It is just a passthrough.
  60. func (f *regularFieldsPrinter) VisitReference(r proto.Reference) {
  61. r.SubSchema().Accept(f)
  62. }
  63. // PrintFields will write the types from schema.
  64. func (f *regularFieldsPrinter) PrintFields(schema proto.Schema) error {
  65. schema.Accept(f)
  66. return f.Error
  67. }