Skip to content

Commit

Permalink
Fix ownership on referenced links
Browse files Browse the repository at this point in the history
Signed-off-by: Alba Hita Catala <[email protected]>
  • Loading branch information
ahitacat committed Dec 18, 2024
1 parent bf480d0 commit 07233bc
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 138 deletions.
13 changes: 11 additions & 2 deletions pkg/concepts/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ type Attribute struct {
namedSupport
typedSupport

owner *Type
link bool
owner *Type
link bool
linkOwner *Version
}

func (a *Attribute) SetLinkOwner(v *Version) {
a.linkOwner = v
}

func (a *Attribute) LinkOwner() *Version {
return a.linkOwner
}

// NewAttribute creates a new attribute.
Expand Down
36 changes: 21 additions & 15 deletions pkg/generators/golang/builders_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,24 @@ func (g *BuildersGenerator) fieldName(attribute *concepts.Attribute) string {
func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeReference {
typ := attribute.Type()
var ref *TypeReference
referencedVersion := ""
if attribute.LinkOwner() != nil {
referencedVersion = attribute.LinkOwner().Name().String()
}
switch {
case typ.IsScalar():
ref = g.types.ValueReference(typ)
case typ.IsStruct():
ref = g.types.BuilderReference(typ)
ref = g.types.BuilderReference(typ, referencedVersion)
case typ.IsList():
if attribute.Link() {
ref = g.types.BuilderReference(typ)
ref = g.types.BuilderReference(typ, referencedVersion)
} else {
element := typ.Element()
if element.IsScalar() {
ref = g.types.NullableReference(typ)
ref = g.types.NullableReference(typ, referencedVersion)
} else {
ref = g.types.BuilderReference(element)
ref = g.types.BuilderReference(element, referencedVersion)
ref = g.types.Reference(
ref.Import(),
ref.Selector(),
Expand All @@ -667,9 +671,9 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere
case typ.IsMap():
element := typ.Element()
if element.IsScalar() {
ref = g.types.NullableReference(typ)
ref = g.types.NullableReference(typ, referencedVersion)
} else {
ref = g.types.BuilderReference(element)
ref = g.types.BuilderReference(element, referencedVersion)
ref = g.types.Reference(
ref.Import(),
ref.Selector(),
Expand All @@ -689,12 +693,10 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere
}

func (g *BuildersGenerator) selectorType(attribute *concepts.Attribute) string {
ref := g.fieldType(attribute)
pkgName := g.packages.VersionSelector(attribute.Owner().Owner())
if pkgName != ref.selector {
return fmt.Sprintf("%s.", ref.selector)
if attribute.LinkOwner() == nil {
return ""
}
return ""
return fmt.Sprintf("%s.", g.packages.VersionSelector(attribute.LinkOwner()))
}

func (g *BuildersGenerator) setterName(attribute *concepts.Attribute) string {
Expand All @@ -708,20 +710,24 @@ func (g *BuildersGenerator) setterName(attribute *concepts.Attribute) string {
func (g *BuildersGenerator) setterType(attribute *concepts.Attribute) *TypeReference {
typ := attribute.Type()
var ref *TypeReference
referencedVersion := ""
if attribute.LinkOwner() != nil {
referencedVersion = attribute.LinkOwner().Name().String()
}
switch {
case typ.IsScalar():
ref = g.types.ValueReference(typ)
case typ.IsStruct():
ref = g.types.BuilderReference(typ)
ref = g.types.BuilderReference(typ, referencedVersion)
case typ.IsList():
if attribute.Link() {
ref = g.types.BuilderReference(typ)
ref = g.types.BuilderReference(typ, referencedVersion)
} else {
element := typ.Element()
if element.IsScalar() {
ref = g.types.ValueReference(typ)
} else {
ref = g.types.BuilderReference(element)
ref = g.types.BuilderReference(element, referencedVersion)
ref = g.types.Reference(
ref.Import(),
ref.Selector(),
Expand All @@ -735,7 +741,7 @@ func (g *BuildersGenerator) setterType(attribute *concepts.Attribute) *TypeRefer
if element.IsScalar() {
ref = g.types.ValueReference(typ)
} else {
ref = g.types.BuilderReference(element)
ref = g.types.BuilderReference(element, referencedVersion)
ref = g.types.Reference(
ref.Import(),
ref.Selector(),
Expand Down
6 changes: 3 additions & 3 deletions pkg/generators/golang/clients_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ func (g *ClientsGenerator) fieldType(parameter *concepts.Parameter) *TypeReferen
case parameter.IsItems():
ref = g.types.ListReference(typ)
case typ.IsScalar() || typ.IsStruct() || typ.IsList() || typ.IsMap():
ref = g.types.NullableReference(typ)
ref = g.types.NullableReference(typ, "")
}
if ref == nil {
g.reporter.Errorf(
Expand Down Expand Up @@ -1091,13 +1091,13 @@ func (g *ClientsGenerator) accessorType(parameter *concepts.Parameter) *TypeRefe
typ := parameter.Type()
switch {
case typ.IsList() && typ.Element().IsScalar():
ref = g.types.NullableReference(typ)
ref = g.types.NullableReference(typ, "")
case parameter.IsItems():
ref = g.types.ListReference(typ)
case typ.IsScalar():
ref = g.types.ValueReference(typ)
case typ.IsStruct() || typ.IsList() || typ.IsMap():
ref = g.types.NullableReference(typ)
ref = g.types.NullableReference(typ, "")
}
if ref == nil {
g.reporter.Errorf(
Expand Down
Loading

0 comments on commit 07233bc

Please sign in to comment.