Skip to content

Commit

Permalink
Do not process a type if it is supposed to be ignored (elastic#73)
Browse files Browse the repository at this point in the history
* Do not process a type if it is ignored

* Update tests
  • Loading branch information
barkbay committed Mar 18, 2024
1 parent 15438a1 commit f3a2725
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
11 changes: 8 additions & 3 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,12 @@ func (p *processor) extractPkgDocumentation(pkg *loader.Package) string {
}

func (p *processor) processType(pkg *loader.Package, parentType *types.Type, t gotypes.Type, depth int) *types.Type {
typeDef := mkType(pkg, t)
typeDef, rawType := mkType(pkg, t)
typeID := types.Identifier(typeDef)
if !rawType && p.shouldIgnoreType(typeID) {
zap.S().Debugw("Skipping excluded type", "type", typeID)
return nil
}

if processed, ok := p.types[typeDef.UID]; ok {
return processed
Expand Down Expand Up @@ -430,7 +435,7 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
}
}

func mkType(pkg *loader.Package, t gotypes.Type) *types.Type {
func mkType(pkg *loader.Package, t gotypes.Type) (*types.Type, bool) {
qualifier := gotypes.RelativeTo(pkg.Types)
cleanTypeName := strings.TrimLeft(gotypes.TypeString(t, qualifier), "*[]")

Expand All @@ -449,7 +454,7 @@ func mkType(pkg *loader.Package, t gotypes.Type) *types.Type {
typeDef.Imported = true
}

return typeDef
return typeDef, rawType
}

// Every child that has a reference to 'originalType', will also get a reference to 'additionalType'.
Expand Down
4 changes: 2 additions & 2 deletions test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Embedded struct {
type Embedded1 struct {
Embedded2 `json:",inline"`
E string `json:"e,omitempty"`
EmbeddedX `json:",inline"`
}
type Embedded2 struct {
B string `json:"b,omitempty"`
Expand All @@ -46,8 +47,7 @@ type Embedded3 struct {
D string `json:"d,omitempty"`
}
type Embedded4 struct {
C string `json:"c,omitempty"`
EmbeddedX `json:",inline"`
C string `json:"c,omitempty"`
}
type EmbeddedX struct {
X string `json:"x,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion test/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processor:
ignoreGroupVersions:
- "GVK"
ignoreTypes:
- "Embedded[0-9]$"
- "Embedded[2-4]$"
ignoreFields:
- "status$"
- "TypeMeta$"
Expand Down
28 changes: 22 additions & 6 deletions test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,30 @@ Package v1 contains API Schema definitions for the webapp v1 API group
| *`metadata`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#objectmeta-v1-meta[$$ObjectMeta$$]__ | Refer to Kubernetes API documentation for fields of `metadata`.
| |
| *`a`* __string__ | | |
| *`b`* __string__ | | |
| *`c`* __string__ | | |
| *`e`* __string__ | | |
| *`x`* __string__ | | |
| *`d`* __string__ | | |
|===


[id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded1"]
==== Embedded1







.Appears In:
****
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded[$$Embedded$$]
****

[cols="20a,50a,15a,15a", options="header"]
|===
| Field | Description | Default | Validation
| *`e`* __string__ | | |
| *`x`* __string__ | | |
|===


Expand All @@ -87,9 +106,6 @@ Package v1 contains API Schema definitions for the webapp v1 API group
****
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded[$$Embedded$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded1[$$Embedded1$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded2[$$Embedded2$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded3[$$Embedded3$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded4[$$Embedded4$$]
****

[cols="20a,50a,15a,15a", options="header"]
Expand Down
23 changes: 17 additions & 6 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,25 @@ Package v1 contains API Schema definitions for the webapp v1 API group
| `kind` _string_ | `Embedded` | | |
| `metadata` _[ObjectMeta](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#objectmeta-v1-meta)_ | Refer to Kubernetes API documentation for fields of `metadata`. | | |
| `a` _string_ | | | |
| `b` _string_ | | | |
| `c` _string_ | | | |
| `e` _string_ | | | |
| `x` _string_ | | | |
| `d` _string_ | | | |


#### Embedded1







_Appears in:_
- [Embedded](#embedded)

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `e` _string_ | | | |
| `x` _string_ | | | |


#### EmbeddedX
Expand All @@ -72,9 +86,6 @@ Package v1 contains API Schema definitions for the webapp v1 API group
_Appears in:_
- [Embedded](#embedded)
- [Embedded1](#embedded1)
- [Embedded2](#embedded2)
- [Embedded3](#embedded3)
- [Embedded4](#embedded4)

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
Expand Down

0 comments on commit f3a2725

Please sign in to comment.