Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline fields of inlined types #25

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type groupVersionInfo struct {
*loader.Package
doc string
kinds map[string]struct{}
types map[string]*types.Type
types types.TypeMap
}

func Process(config *config.Config) ([]types.GroupVersionDetails, error) {
Expand All @@ -61,6 +61,8 @@ func Process(config *config.Config) ([]types.GroupVersionDetails, error) {
return nil, fmt.Errorf("failed to find API types in directory %s:%w", config.SourcePath, err)
}

p.types.InlineTypes()

// collect references between types
for typeName, refs := range p.references {
typeDef, ok := p.types[typeName]
Expand All @@ -83,11 +85,16 @@ func Process(config *config.Config) ([]types.GroupVersionDetails, error) {
details.Kinds = append(details.Kinds, k)
}

details.Types = make(map[string]*types.Type)
for t, _ := range gvi.types {
key := fmt.Sprintf("%s.%s", gvi.Package.PkgPath, t)
details.Types = make(types.TypeMap)
for name, t := range gvi.types {
key := types.Key(t)

if p.shouldIgnoreType(key) {
Copy link
Contributor

@barkbay barkbay Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too late as it does not prevent some excluded types from being referenced.

zap.S().Debugw("Skipping excluded type", "type", name)
continue
}
if typeDef, ok := p.types[key]; ok && typeDef != nil {
details.Types[t] = typeDef
details.Types[name] = typeDef
} else {
zap.S().Fatalw("Type not loaded", "type", key)
}
Expand Down Expand Up @@ -121,7 +128,7 @@ func newProcessor(compiledConfig *compiledConfig, maxDepth int) *processor {
Checker: &loader.TypeChecker{},
},
groupVersions: make(map[schema.GroupVersion]*groupVersionInfo),
types: make(map[string]*types.Type),
types: make(types.TypeMap),
references: make(map[string]map[string]struct{}),
}

Expand All @@ -134,7 +141,7 @@ type processor struct {
maxDepth int
parser *crd.Parser
groupVersions map[schema.GroupVersion]*groupVersionInfo
types map[string]*types.Type
types types.TypeMap
references map[string]map[string]struct{}
}

Expand Down Expand Up @@ -167,7 +174,7 @@ func (p *processor) findAPITypes(directory string) error {
}

if gvInfo.types == nil {
gvInfo.types = make(map[string]*types.Type)
gvInfo.types = make(types.TypeMap)
}

// locate the kinds
Expand Down Expand Up @@ -265,11 +272,6 @@ func (p *processor) processType(pkg *loader.Package, info *markers.TypeInfo, dep
Doc: info.Doc,
}

if p.shouldIgnoreType(types.Key(typeDef)) {
zap.S().Debugw("Skipping excluded type", "type", typeDef.String())
return nil
}

Comment on lines -268 to -272
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this breaks the ignoreTypes setting which seems to be now ignored: https://github.com/elastic/cloud-on-k8s/pull/7608/files#discussion_r1528299014

// if the field list is non-empty, this is a struct
if len(info.Fields) > 0 {
typeDef.Kind = types.StructKind
Expand Down Expand Up @@ -301,11 +303,6 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
parentTypeKey := types.Key(parentType)

for _, f := range info.Fields {
if p.shouldIgnoreField(parentTypeKey, f.Name) {
zap.S().Debugw("Skipping excluded field", "type", parentType.String(), "field", f.Name)
continue
}

t := pkg.TypesInfo.TypeOf(f.RawField.Type)
if t == nil {
zap.S().Debugw("Failed to determine type of field", "field", f.Name)
Expand All @@ -331,11 +328,15 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
continue
}

if fieldDef.Embedded && fieldDef.Name == "" {
fieldDef.Name = fieldDef.Type.Name
if fieldDef.Embedded {
fieldDef.Inlined = fieldDef.Name == ""
if fieldDef.Name == "" {
fieldDef.Name = fieldDef.Type.Name
}
}

if p.shouldIgnoreField(parentTypeKey, fieldDef.Name) {
zap.S().Debugw("Skipping excluded field", "type", parentType.String(), "field", fieldDef.Name)
continue
}

Expand All @@ -355,10 +356,6 @@ func (p *processor) loadType(pkg *loader.Package, t gotypes.Type, depth int) *ty
}

typeDef := mkType(pkg, t)
if p.shouldIgnoreType(types.Key(typeDef)) {
Copy link
Contributor

@barkbay barkbay Mar 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this breaks the ignoreTypes setting which seems to be now ignored: https://github.com/elastic/cloud-on-k8s/pull/7608/files#discussion_r1528299014

Or maybe it's this one... I'm lost...

zap.S().Debugw("Skipping excluded type", "type", t.String())
return nil
}

zap.S().Debugw("Load", "package", typeDef.Package, "name", typeDef.Name)

Expand Down
22 changes: 22 additions & 0 deletions test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:object:root=true

type Embedded struct {
A string `json:"a,omitempty"`
Embedded1 `json:",inline"`
}
type Embedded1 struct {
Embedded2 `json:",inline"`
E string `json:"e,omitempty"`
}
type Embedded2 struct {
B string `json:"b,omitempty"`
Embedded3 `json:",inline"`
}
type Embedded3 struct {
Embedded4 `json:",inline"`
D string `json:"d,omitempty"`
}
type Embedded4 struct {
C string `json:"c,omitempty"`
}

// GuestbookSpec defines the desired state of Guestbook.
type GuestbookSpec struct {
// Page indicates the page number
Expand Down
88 changes: 88 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.

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:
- "Status$"
- "Embedded[0-9]$"
ignoreFields:
- "status$"
- "TypeMeta$"
Expand Down
23 changes: 23 additions & 0 deletions test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@
Package v1 contains API Schema definitions for the webapp v1 API group

.Resource Types
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-embedded[$$Embedded$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbook[$$Guestbook$$]
- xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-guestbooklist[$$GuestbookList$$]



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





[cols="25a,75a", options="header"]
|===
| Field | Description
| *`apiVersion`* __string__ | `webapp.test.k8s.elastic.co/v1`
| *`kind`* __string__ | `Embedded`
| *`a`* __string__ |
| *`b`* __string__ |
| *`c`* __string__ |
| *`d`* __string__ |
| *`e`* __string__ |
|===


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

Expand Down Expand Up @@ -110,6 +131,8 @@ GuestbookSpec defines the desired state of Guestbook.
|===




[id="{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-rating"]
==== Rating (string)

Expand Down
22 changes: 22 additions & 0 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
Package v1 contains API Schema definitions for the webapp v1 API group

### Resource Types
- [Embedded](#embedded)
- [Guestbook](#guestbook)
- [GuestbookList](#guestbooklist)



#### Embedded







| Field | Description |
| --- | --- |
| `apiVersion` _string_ | `webapp.test.k8s.elastic.co/v1`
| `kind` _string_ | `Embedded`
| `a` _string_ | |
| `b` _string_ | |
| `c` _string_ | |
| `d` _string_ | |
| `e` _string_ | |


#### Guestbook


Expand Down Expand Up @@ -92,6 +112,8 @@ _Appears in:_
| `headers` _[GuestbookHeader](#guestbookheader) array_ | Headers contains a list of header items to include in the page |




#### Rating

_Underlying type:_ `string`
Expand Down
Loading