help.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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
  14. // You *probably* don't want to write these structs by hand
  15. // -- use cmd/helpgen if you can write Godoc, and {Simple,Deprecated}Help
  16. // otherwise.
  17. // DetailedHelp contains brief help, as well as more details.
  18. // For the "full" help, join the two together.
  19. type DetailedHelp struct {
  20. Summary string
  21. Details string
  22. }
  23. // DefinitionHelp contains overall help for a marker Definition,
  24. // as well as per-field help.
  25. type DefinitionHelp struct {
  26. // DetailedHelp contains the overall help for the marker.
  27. DetailedHelp
  28. // Category describes what kind of marker this is.
  29. Category string
  30. // DeprecatedInFavorOf marks the marker as deprecated.
  31. // If non-nil & empty, it's assumed to just mean deprecated permanently.
  32. // If non-empty, it's assumed to be a marker name.
  33. DeprecatedInFavorOf *string
  34. // NB(directxman12): we make FieldHelp be in terms of the Go struct field
  35. // names so that we don't have to know the conversion or processing rules
  36. // for struct fields at compile-time for help generation.
  37. // FieldHelp defines the per-field help for this marker, *in terms of the
  38. // go struct field names. Use the FieldsHelp method to map this to
  39. // marker argument names.
  40. FieldHelp map[string]DetailedHelp
  41. }
  42. // FieldsHelp maps per-field help to the actual marker argument names from the
  43. // given definition.
  44. func (d *DefinitionHelp) FieldsHelp(def *Definition) map[string]DetailedHelp {
  45. fieldsHelp := make(map[string]DetailedHelp, len(def.FieldNames))
  46. for fieldName, argName := range def.FieldNames {
  47. fieldsHelp[fieldName] = d.FieldHelp[argName]
  48. }
  49. return fieldsHelp
  50. }
  51. // SimpleHelp returns help that just has marker-level summary information
  52. // (e.g. for use with empty or primitive-typed markers, where Godoc-based
  53. // generation isn't possible).
  54. func SimpleHelp(category, summary string) *DefinitionHelp {
  55. return &DefinitionHelp{
  56. Category: category,
  57. DetailedHelp: DetailedHelp{Summary: summary},
  58. }
  59. }
  60. // DeprecatedHelp returns simple help (a la SimpleHelp), except marked as
  61. // deprecated in favor of the given marker (or an empty string for just
  62. // deprecated).
  63. func DeprecatedHelp(inFavorOf, category, summary string) *DefinitionHelp {
  64. return &DefinitionHelp{
  65. Category: category,
  66. DetailedHelp: DetailedHelp{Summary: summary},
  67. DeprecatedInFavorOf: &inFavorOf,
  68. }
  69. }