Skip to content

Commit 551890a

Browse files
authored
⚠️ Trim leading/trailing whitespace on lines in documentation (#517)
* Trim whitespace in doc-comments This is important for input files that use `/*…*/`-style comments, where `go/ast` preserves the leading and trailing (and inner) whitespace and this ends up in the generated CRD schema descriptions. * Fix spelling * Use trimspace * Update golden file
1 parent 384bf3e commit 551890a

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
---
32
apiVersion: apiextensions.k8s.io/v1
43
kind: CustomResourceDefinition
@@ -5387,9 +5386,9 @@ spec:
53875386
the spread constraint. - DoNotSchedule (default)
53885387
tells the scheduler not to schedule it. -
53895388
ScheduleAnyway tells the scheduler to schedule
5390-
the pod in any location, but giving higher
5389+
the pod in any location, but giving higher
53915390
precedence to topologies that would help reduce
5392-
the skew. A constraint is considered "Unsatisfiable"
5391+
the skew. A constraint is considered "Unsatisfiable"
53935392
for an incoming pod if and only if every possible
53945393
node assigment for that pod would violate
53955394
"MaxSkew" on some topology. For example, in
@@ -5879,13 +5878,13 @@ spec:
58795878
when the pod is removed. \n Use this if: a)
58805879
the volume is only needed while the pod runs,
58815880
b) features of normal volumes like restoring
5882-
from snapshot or capacity tracking are
5883-
needed, c) the storage driver is specified
5884-
through a storage class, and d) the storage
5885-
driver supports dynamic volume provisioning
5886-
through a PersistentVolumeClaim (see EphemeralVolumeSource
5887-
for more information on the connection
5888-
between this volume type and PersistentVolumeClaim).
5881+
from snapshot or capacity tracking are needed,
5882+
c) the storage driver is specified through
5883+
a storage class, and d) the storage driver
5884+
supports dynamic volume provisioning through
5885+
a PersistentVolumeClaim (see EphemeralVolumeSource
5886+
for more information on the connection between
5887+
this volume type and PersistentVolumeClaim).
58895888
\n Use PersistentVolumeClaim or one of the
58905889
vendor-specific APIs for volumes that persist
58915890
for longer than the lifecycle of an individual
@@ -7339,23 +7338,23 @@ spec:
73397338
description: 'ObjectReference contains enough information to let
73407339
you inspect or modify the referred object. --- New uses of this
73417340
type are discouraged because of difficulty describing its usage
7342-
when embedded in APIs. 1. Ignored fields. It includes many fields
7341+
when embedded in APIs. 1. Ignored fields. It includes many fields
73437342
which are not generally honored. For instance, ResourceVersion
7344-
and FieldPath are both very rarely valid in actual usage. 2.
7345-
Invalid usage help. It is impossible to add specific help for
7346-
individual usage. In most embedded usages, there are particular restrictions
7343+
and FieldPath are both very rarely valid in actual usage. 2. Invalid
7344+
usage help. It is impossible to add specific help for individual
7345+
usage. In most embedded usages, there are particular restrictions
73477346
like, "must refer only to types A and B" or "UID not honored"
7348-
or "name must be restricted". Those cannot be well described
7349-
when embedded. 3. Inconsistent validation. Because the usages
7350-
are different, the validation rules are different by usage, which
7351-
makes it hard for users to predict what will happen. 4. The fields
7347+
or "name must be restricted". Those cannot be well described when
7348+
embedded. 3. Inconsistent validation. Because the usages are
7349+
different, the validation rules are different by usage, which
7350+
makes it hard for users to predict what will happen. 4. The fields
73527351
are both imprecise and overly precise. Kind is not a precise
7353-
mapping to a URL. This can produce ambiguity during interpretation
7352+
mapping to a URL. This can produce ambiguity during interpretation
73547353
and require a REST mapping. In most cases, the dependency is
7355-
on the group,resource tuple and the version of the actual
7356-
struct is irrelevant. 5. We cannot easily change it. Because
7357-
this type is embedded in many locations, updates to this type will
7358-
affect numerous schemas. Don''t make new APIs embed an underspecified
7354+
on the group,resource tuple and the version of the actual struct
7355+
is irrelevant. 5. We cannot easily change it. Because this type
7356+
is embedded in many locations, updates to this type will affect
7357+
numerous schemas. Don''t make new APIs embed an underspecified
73597358
API type they do not control. Instead of using this type, create
73607359
a locally provided and used type that is well-focused on your
73617360
reference. For example, ServiceReferences for admission registration:

pkg/markers/collect_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ type fieldPath struct {
3131
var _ = Describe("Collecting", func() {
3232
var col *Collector
3333
var markersByType map[string]MarkerValues
34+
var docsByType map[string]string
35+
3436
var markersByField map[fieldPath]MarkerValues
37+
var docsByField map[fieldPath]string
3538

3639
JustBeforeEach(func() {
3740
By("setting up the registry and collector")
@@ -45,15 +48,23 @@ var _ = Describe("Collecting", func() {
4548

4649
col = &Collector{Registry: reg}
4750

48-
By("gathering markers by type name")
51+
By("gathering markers/docs by type/field name")
4952
markersByType = make(map[string]MarkerValues)
53+
docsByType = make(map[string]string)
5054
markersByField = make(map[fieldPath]MarkerValues)
55+
docsByField = make(map[fieldPath]string)
56+
5157
err := EachType(col, fakePkg, func(info *TypeInfo) {
5258
markersByType[info.Name] = info.Markers
59+
docsByType[info.Name] = info.Doc
60+
5361
for _, field := range info.Fields {
54-
markersByField[fieldPath{typ: info.Name, field: field.Name}] = field.Markers
62+
fieldPath := fieldPath{typ: info.Name, field: field.Name}
63+
markersByField[fieldPath] = field.Markers
64+
docsByField[fieldPath] = field.Doc
5565
}
5666
})
67+
5768
Expect(err).NotTo(HaveOccurred())
5869
Expect(fakePkg.Errors).To(HaveLen(0))
5970
})
@@ -94,6 +105,10 @@ var _ = Describe("Collecting", func() {
94105
HaveKeyWithValue("testing:typelvl", ContainElement("here on type"))))
95106
})
96107

108+
It("should have docs without markers", func() {
109+
Expect(docsByType).To(HaveKeyWithValue("Foo", "normal godoc normal godoc"))
110+
})
111+
97112
It("should associate markers in the closest non-godoc block", func() {
98113
Expect(markersByType).To(HaveKeyWithValue("Foo",
99114
HaveKeyWithValue("testing:typelvl", ContainElement("here before type"))))
@@ -109,6 +124,19 @@ var _ = Describe("Collecting", func() {
109124
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated")))
110125
})
111126
})
127+
128+
Context("types with /*…*/-style comments", func() {
129+
It("should have doc without extraneous spaces", func() {
130+
Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces",
131+
"This type of doc has spaces preserved in go-ast, but we'd like to trim them."))
132+
})
133+
134+
It("should have doc without extraneous spaces, even over multiple lines", func() {
135+
Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces2",
136+
"This type of doc has spaces preserved in go-ast, but we'd like to trim them, especially when formatted like this."))
137+
})
138+
})
139+
112140
Context("without godoc", func() {
113141
It("should associate markers in the closest block", func() {
114142
Expect(markersByType).To(HaveKeyWithValue("Quux",

pkg/markers/markers_suite_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ var _ = BeforeSuite(func() {
8888
Bar = "foo"
8989
)
9090
91+
/* This type of doc has spaces preserved in go-ast, but we'd like to trim them. */
92+
type HasDocsWithSpaces struct {
93+
}
94+
95+
/*
96+
This type of doc has spaces preserved in go-ast, but we'd like to trim them,
97+
especially when formatted like this.
98+
*/
99+
type HasDocsWithSpaces2 struct {
100+
}
101+
91102
type Baz interface {
92103
// +testing:pkglvl="not here in interface"
93104
}

pkg/markers/zip.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,22 @@ func extractDoc(node ast.Node, decl *ast.GenDecl) string {
6767
// chop off the extraneous last part
6868
outLines = outLines[:len(outLines)-1]
6969
}
70-
// respect double-newline meaning actual newline
70+
7171
for i, line := range outLines {
72+
// Trim any extranous whitespace,
73+
// for handling /*…*/-style comments,
74+
// which have whitespace preserved in go/ast:
75+
line = strings.TrimSpace(line)
76+
77+
// Respect that double-newline means
78+
// actual newline:
7279
if line == "" {
7380
outLines[i] = "\n"
81+
} else {
82+
outLines[i] = line
7483
}
7584
}
85+
7686
return strings.Join(outLines, " ")
7787
}
7888

0 commit comments

Comments
 (0)