zip.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. import (
  15. "go/ast"
  16. "go/token"
  17. "reflect"
  18. "strings"
  19. "sigs.k8s.io/controller-tools/pkg/loader"
  20. )
  21. // extractDoc extracts documentation from the given node, skipping markers
  22. // in the godoc and falling back to the decl if necessary (for single-line decls).
  23. func extractDoc(node ast.Node, decl *ast.GenDecl) string {
  24. var docs *ast.CommentGroup
  25. switch docced := node.(type) {
  26. case *ast.Field:
  27. docs = docced.Doc
  28. case *ast.File:
  29. docs = docced.Doc
  30. case *ast.GenDecl:
  31. docs = docced.Doc
  32. case *ast.TypeSpec:
  33. docs = docced.Doc
  34. // type Ident expr expressions get docs attached to the decl,
  35. // so check for that case (missing Lparen == single line type decl)
  36. if docs == nil && decl.Lparen == token.NoPos {
  37. docs = decl.Doc
  38. }
  39. }
  40. if docs == nil {
  41. return ""
  42. }
  43. // filter out markers
  44. var outGroup ast.CommentGroup
  45. outGroup.List = make([]*ast.Comment, 0, len(docs.List))
  46. for _, comment := range docs.List {
  47. if isMarkerComment(comment.Text) {
  48. continue
  49. }
  50. outGroup.List = append(outGroup.List, comment)
  51. }
  52. // split lines, and re-join together as a single
  53. // paragraph, respecting double-newlines as
  54. // paragraph markers.
  55. outLines := strings.Split(outGroup.Text(), "\n")
  56. if outLines[len(outLines)-1] == "" {
  57. // chop off the extraneous last part
  58. outLines = outLines[:len(outLines)-1]
  59. }
  60. // respect double-newline meaning actual newline
  61. for i, line := range outLines {
  62. if line == "" {
  63. outLines[i] = "\n"
  64. }
  65. }
  66. return strings.Join(outLines, " ")
  67. }
  68. // PackageMarkers collects all the package-level marker values for the given package.
  69. func PackageMarkers(col *Collector, pkg *loader.Package) (MarkerValues, error) {
  70. markers, err := col.MarkersInPackage(pkg)
  71. if err != nil {
  72. return nil, err
  73. }
  74. res := make(MarkerValues)
  75. for _, file := range pkg.Syntax {
  76. fileMarkers := markers[file]
  77. for name, vals := range fileMarkers {
  78. res[name] = append(res[name], vals...)
  79. }
  80. }
  81. return res, nil
  82. }
  83. // FieldInfo contains marker values and commonly used information for a struct field.
  84. type FieldInfo struct {
  85. // Name is the name of the field (or "" for embedded fields)
  86. Name string
  87. // Doc is the Godoc of the field, pre-processed to remove markers and joine
  88. // single newlines together.
  89. Doc string
  90. // Tag struct tag associated with this field (or "" if non existed).
  91. Tag reflect.StructTag
  92. // Markers are all registered markers associated with this field.
  93. Markers MarkerValues
  94. // RawField is the raw, underlying field AST object that this field represents.
  95. RawField *ast.Field
  96. }
  97. // TypeInfo contains marker values and commonly used information for a type declaration.
  98. type TypeInfo struct {
  99. // Name is the name of the type.
  100. Name string
  101. // Doc is the Godoc of the type, pre-processed to remove markers and joine
  102. // single newlines together.
  103. Doc string
  104. // Markers are all registered markers associated with the type.
  105. Markers MarkerValues
  106. // Fields are all the fields associated with the type, if it's a struct.
  107. // (if not, Fields will be nil).
  108. Fields []FieldInfo
  109. // RawDecl contains the raw GenDecl that the type was declared as part of.
  110. RawDecl *ast.GenDecl
  111. // RawSpec contains the raw Spec that declared this type.
  112. RawSpec *ast.TypeSpec
  113. // RawFile contains the file in which this type was declared.
  114. RawFile *ast.File
  115. }
  116. // TypeCallback is a callback called for each type declaration in a package.
  117. type TypeCallback func(info *TypeInfo)
  118. // EachType collects all markers, then calls the given callback for each type declaration in a package.
  119. // Each individual spec is considered separate, so
  120. //
  121. // type (
  122. // Foo string
  123. // Bar int
  124. // Baz struct{}
  125. // )
  126. //
  127. // yields three calls to the callback.
  128. func EachType(col *Collector, pkg *loader.Package, cb TypeCallback) error {
  129. markers, err := col.MarkersInPackage(pkg)
  130. if err != nil {
  131. return err
  132. }
  133. loader.EachType(pkg, func(file *ast.File, decl *ast.GenDecl, spec *ast.TypeSpec) {
  134. var fields []FieldInfo
  135. if structSpec, isStruct := spec.Type.(*ast.StructType); isStruct {
  136. for _, field := range structSpec.Fields.List {
  137. for _, name := range field.Names {
  138. fields = append(fields, FieldInfo{
  139. Name: name.Name,
  140. Doc: extractDoc(field, nil),
  141. Tag: loader.ParseAstTag(field.Tag),
  142. Markers: markers[field],
  143. RawField: field,
  144. })
  145. }
  146. if field.Names == nil {
  147. fields = append(fields, FieldInfo{
  148. Doc: extractDoc(field, nil),
  149. Tag: loader.ParseAstTag(field.Tag),
  150. Markers: markers[field],
  151. RawField: field,
  152. })
  153. }
  154. }
  155. }
  156. cb(&TypeInfo{
  157. Name: spec.Name.Name,
  158. Markers: markers[spec],
  159. Doc: extractDoc(spec, decl),
  160. Fields: fields,
  161. RawDecl: decl,
  162. RawSpec: spec,
  163. RawFile: file,
  164. })
  165. })
  166. return nil
  167. }