Skip to content

Commit

Permalink
Fix processing & linking of alias types (elastic#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
coro committed Jan 12, 2021
1 parent be60e15 commit 851eb91
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,23 @@ 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
typeDef.UnderlyingType = p.loadType(pkg, x.Elem(), depth+1)
if typeDef.UnderlyingType != nil && typeDef.UnderlyingType.Kind == types.BasicKind {
typeDef.Package = ""
}
return typeDef

case *gotypes.Array:
typeDef.Kind = types.ArrayKind
typeDef.UnderlyingType = p.loadType(pkg, x.Elem(), depth+1)
if typeDef.UnderlyingType != nil && typeDef.UnderlyingType.Kind == types.BasicKind {
typeDef.Package = ""
}
return typeDef

case *gotypes.Map:
typeDef.Kind = types.MapKind
Expand Down
5 changes: 5 additions & 0 deletions test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions test/api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
****
Expand All @@ -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

Expand Down Expand Up @@ -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
|===


Expand Down
2 changes: 1 addition & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 851eb91

Please sign in to comment.