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

Make inlined structs get references from where they are inlined #33

Merged
merged 3 commits into from
Apr 21, 2023
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
14 changes: 13 additions & 1 deletion processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ 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()
p.types.InlineTypes(p.propagateReference)

// collect references between types
for typeName, refs := range p.references {
Expand Down Expand Up @@ -490,6 +490,18 @@ func (p *processor) loadAliasType(typeDef *types.Type, pkg *loader.Package, unde
return p.processType(tPkg, tInfo, depth+1)
}

// Every child that has a reference to 'originalType', will also get a reference to 'additionalType'.
func (p *processor) propagateReference(originalType *types.Type, additionalType *types.Type) {
originalTypeKey := types.Key(originalType)
additionalTypeKey := types.Key(additionalType)

for _, parentRefs := range p.references {
if _, ok := parentRefs[originalTypeKey]; ok {
parentRefs[additionalTypeKey] = struct{}{}
}
}
}

func (p *processor) addReference(parent *types.Type, child *types.Type) {
if child == nil || child.Kind == types.BasicKind {
return
Expand Down
6 changes: 5 additions & 1 deletion test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ type Embedded3 struct {
D string `json:"d,omitempty"`
}
type Embedded4 struct {
C string `json:"c,omitempty"`
C string `json:"c,omitempty"`
EmbeddedX `json:",inline"`
}
type EmbeddedX struct {
X string `json:"x,omitempty"`
}

// GuestbookSpec defines the desired state of Guestbook.
Expand Down
22 changes: 22 additions & 0 deletions test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,33 @@ Package v1 contains API Schema definitions for the webapp v1 API group
| *`a`* __string__ |
| *`b`* __string__ |
| *`c`* __string__ |
| *`x`* __string__ |
| *`d`* __string__ |
| *`e`* __string__ |
|===


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



.Appears In:
****
- 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="25a,75a", options="header"]
|===
| Field | Description
| *`x`* __string__ |
|===


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

Expand Down
19 changes: 19 additions & 0 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,29 @@ Package v1 contains API Schema definitions for the webapp v1 API group
| `a` _string_ | |
| `b` _string_ | |
| `c` _string_ | |
| `x` _string_ | |
| `d` _string_ | |
| `e` _string_ | |


#### EmbeddedX





_Appears in:_
- [Embedded](#embedded)
- [Embedded1](#embedded1)
- [Embedded2](#embedded2)
- [Embedded3](#embedded3)
- [Embedded4](#embedded4)

| Field | Description |
| --- | --- |
| `x` _string_ | |


#### Guestbook


Expand Down
3 changes: 2 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (t *Type) ContainsInlinedTypes() bool {
// TypeMap is a map of Type elements
type TypeMap map[string]*Type

func (types TypeMap) InlineTypes() {
func (types TypeMap) InlineTypes(propagateReference func(original *Type, additional *Type)) {
// If C is inlined in B, and B is inlined in A; the fields of C are copied
// into B before the fields of B is copied into A. The ideal order of
// iterating and inlining fields is NOT known. Worst-case, only one type's
Expand Down Expand Up @@ -254,6 +254,7 @@ func (types TypeMap) InlineTypes() {
zap.S().Debugw("Inlining embedded type", "type", t,
"embeddedType", t.Fields[i].Type)
t.Fields.inlineType(i, embeddedType)
propagateReference(embeddedType, t)
}
}
}
Expand Down