From 851eb91653260c1a07c529c0b9603d4b161c8c0a Mon Sep 17 00:00:00 2001 From: Connor Rogers Date: Tue, 12 Jan 2021 08:05:43 +0000 Subject: [PATCH] Fix processing & linking of alias types (#6) * Fix processing & linking of alias types Prior to this change, alias types would be added to the `processor.Types` map twice, in the case where an API defines both the alias type, and another struct which uses either a pointer to, or an array / slice of, an alias type. Depending on which element is processed first, the alias type would be added to the map as an AliasKind, or a SliceKind/ArrayKind/PointerKind. This means that where templates attempt to render an underlying type of an Alias using `IsAlias`, the alias may be registered as a non-alias type. In addition, alias types were treated as Basic in the renderer if their underlying types were basic. This means in the above case, links are not generated to the alias type in the generated documentation, despite them being a valid type as part of the API. * Add & fix unit test for alias array --- processor/processor.go | 3 +++ test/api/v1/guestbook_types.go | 5 +++++ test/api/v1/zz_generated.deepcopy.go | 5 +++++ test/expected.asciidoc | 15 ++++++++++++++- types/types.go | 2 +- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/processor/processor.go b/processor/processor.go index ceeaf98..2da3a71 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -377,6 +377,7 @@ func (p *processor) loadType(pkg *loader.Package, t gotypes.Type, depth int) *ty if typeDef.UnderlyingType != nil && typeDef.UnderlyingType.Kind == types.BasicKind { typeDef.Package = "" } + return typeDef case *gotypes.Slice: typeDef.Kind = types.SliceKind @@ -384,6 +385,7 @@ func (p *processor) loadType(pkg *loader.Package, t gotypes.Type, depth int) *ty if typeDef.UnderlyingType != nil && typeDef.UnderlyingType.Kind == types.BasicKind { typeDef.Package = "" } + return typeDef case *gotypes.Array: typeDef.Kind = types.ArrayKind @@ -391,6 +393,7 @@ func (p *processor) loadType(pkg *loader.Package, t gotypes.Type, depth int) *ty if typeDef.UnderlyingType != nil && typeDef.UnderlyingType.Kind == types.BasicKind { typeDef.Package = "" } + return typeDef case *gotypes.Map: typeDef.Kind = types.MapKind diff --git a/test/api/v1/guestbook_types.go b/test/api/v1/guestbook_types.go index 487bcfb..c24df25 100644 --- a/test/api/v1/guestbook_types.go +++ b/test/api/v1/guestbook_types.go @@ -29,6 +29,8 @@ type GuestbookSpec struct { Entries []GuestbookEntry `json:"entries,omitempty"` // Selector selects something Selector metav1.LabelSelector `json:"selector,omitempty"` + // Headers contains a list of header items to include in the page + Headers []GuestbookHeader `json:"headers,omitempty"` } // GuestbookEntry defines an entry in a guest book. @@ -46,6 +48,9 @@ type GuestbookStatus struct { Status string `json:"status"` } +// GuestbookHeaders are strings to include at the top of a page. +type GuestbookHeader string + // +kubebuilder:object:root=true // Guestbook is the Schema for the guestbooks API. diff --git a/test/api/v1/zz_generated.deepcopy.go b/test/api/v1/zz_generated.deepcopy.go index ae2a760..bde5968 100644 --- a/test/api/v1/zz_generated.deepcopy.go +++ b/test/api/v1/zz_generated.deepcopy.go @@ -116,6 +116,11 @@ func (in *GuestbookSpec) DeepCopyInto(out *GuestbookSpec) { } } in.Selector.DeepCopyInto(&out.Selector) + if in.Headers != nil { + in, out := &in.Headers, &out.Headers + *out = make([]GuestbookHeader, len(*in)) + copy(*out, *in) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GuestbookSpec. diff --git a/test/expected.asciidoc b/test/expected.asciidoc index 276ae3a..571a98f 100644 --- a/test/expected.asciidoc +++ b/test/expected.asciidoc @@ -43,7 +43,7 @@ Guestbook is the Schema for the guestbooks API. [id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbookentry"] ==== GuestbookEntry - +GuestbookEntry defines an entry in a guest book. .Appears In: **** @@ -59,6 +59,18 @@ Guestbook is the Schema for the guestbooks API. |=== +[id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbookheader"] +==== GuestbookHeader (string) + + + +.Appears In: +**** +- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbookspec[$$GuestbookSpec$$] +**** + + + [id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbooklist"] ==== GuestbookList @@ -93,6 +105,7 @@ GuestbookSpec defines the desired state of Guestbook. | *`page`* __integer__ | Page indicates the page number | *`entries`* __xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbookentry[$$GuestbookEntry$$] array__ | Entries contain guest book entries for the page | *`selector`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.15/#labelselector-v1-meta[$$LabelSelector$$]__ | Selector selects something +| *`headers`* __xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbookheader[$$GuestbookHeader$$] array__ | Headers contains a list of header items to include in the page |=== diff --git a/types/types.go b/types/types.go index ce819e1..b3a1542 100644 --- a/types/types.go +++ b/types/types.go @@ -115,7 +115,7 @@ func (t *Type) IsBasic() bool { switch t.Kind { case BasicKind: return true - case AliasKind, SliceKind, ArrayKind, PointerKind: + case SliceKind, ArrayKind, PointerKind: return t.UnderlyingType != nil && t.UnderlyingType.IsBasic() case MapKind: return t.KeyType != nil && t.KeyType.IsBasic() && t.ValueType != nil && t.ValueType.IsBasic()