doc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2019 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 markers contains utilities for defining and parsing "marker
  14. // comments", also occasionally called tag comments (we use the term marker to
  15. // avoid confusing with struct tags). Parsed result (output) values take the
  16. // form of Go values, much like the "encoding/json" package.
  17. //
  18. // Definitions and Parsing
  19. //
  20. // Markers are defined as structured Definitions which can be used to
  21. // consistently parse marker comments. A Definition contains an concrete
  22. // output type for the marker, which can be a simple type (like string), a
  23. // struct, or a wrapper type (useful for defining additional methods on marker
  24. // types).
  25. //
  26. // Markers take the general form
  27. //
  28. // +path:to:marker=val
  29. //
  30. // +path:to:marker:arg1=val,arg2=val2
  31. //
  32. // +path:to:marker
  33. //
  34. // Arguments may be ints, bools, strings, and slices. Ints and bool take their
  35. // standard form from Go. Strings may take any of their standard forms, or any
  36. // sequence of unquoted characters up until a `,` or `;` is encountered. Lists
  37. // take either of the following forms:
  38. //
  39. // val;val;val
  40. //
  41. // {val, val, val}
  42. //
  43. // Note that the first form will not properly parse nested slices, but is
  44. // generally convenient and is the form used in many existing markers.
  45. //
  46. // Each of those argument types maps to the corresponding go type. Pointers
  47. // mark optional fields (a struct tag, below, may also be used). The empty
  48. // interface will match any type.
  49. //
  50. // Struct fields may optionally be annotated with the `marker` struct tag. The
  51. // first argument is a name override. If it's left blank (or the tag isn't
  52. // present), the camelCase version of the name will be used. The only
  53. // additional argument defined is `optional`, which marks a field as optional
  54. // without using a pointer.
  55. //
  56. // All parsed values are unmarshalled into the output type. If any
  57. // non-optional fields aren't mentioned, an error will be raised unless
  58. // `Strict` is set to false.
  59. //
  60. // Registries and Lookup
  61. //
  62. // Definitions can be added to registries to facilitate lookups. Each
  63. // definition is marked as either describing a type, struct field, or package
  64. // (unassociated). The same marker name may be registered multiple times, as
  65. // long as each describes a different construct (type, field, or package).
  66. // Definitions can then be looked up by passing unparsed markers.
  67. //
  68. // Collection and Extraction
  69. //
  70. // Markers can be collected from a loader.Package using a Collector. The
  71. // Collector will read from a given Registry, collecting comments that look
  72. // like markers and parsing them if they match some definition on the registry.
  73. //
  74. // Markers are considered associated with a particular field or type if they
  75. // exist in the Godoc, or the closest non-godoc comment. Any other markers not
  76. // inside a some other block (e.g. a struct definition, interface definition,
  77. // etc) are considered package level. Markers in a "closest non-Go comment
  78. // block" may also be considered package level if registered as such and no
  79. // identical type-level definition exists.
  80. //
  81. // Like loader.Package, Collector's methods are idempotent and will not
  82. // reperform work.
  83. //
  84. // Traversal
  85. //
  86. // EachType function iterates over each type in a Package, providing
  87. // conveniently structured type and field information with marker values
  88. // associated.
  89. //
  90. // PackageMarkers can be used to fetch just package-level markers.
  91. //
  92. // Help
  93. //
  94. // Help can be defined for each marker using the DefinitionHelp struct. It's
  95. // mostly intended to be generated off of godocs using cmd/helpgen, which takes
  96. // the first line as summary (removing the type/field name), and considers the
  97. // rest as details. It looks for the
  98. //
  99. // +controllertools:generateHelp[:category=<string>]
  100. //
  101. // marker to start generation.
  102. //
  103. // If you can't use godoc-based generation for whatever reasons (e.g.
  104. // primitive-typed markers), you can use the SimpleHelp and DeprecatedHelp
  105. // helper functions to generate help structs.
  106. //
  107. // Help is then registered into a registry as associated with the actual
  108. // definition, and can then be later retrieved from the registry.
  109. package markers