Skip to content

Commit

Permalink
Remove unecessary print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrodshn committed Dec 8, 2024
1 parent ab9daad commit 1af7b0d
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 138 deletions.
14 changes: 0 additions & 14 deletions pkg/concepts/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package concepts

import (
"log"
"sort"

"github.com/openshift-online/ocm-api-metamodel/pkg/names"
Expand Down Expand Up @@ -186,19 +185,6 @@ func (t *Type) AddAttribute(attribute *Attribute) {
}
}

// Remove attribute removes an attribute with a given name.
func (t *Type) RemoveAttribute(name *names.Name) {
if name == nil {
return
}
for i, attribute := range t.attributes {
if attribute.Name().Equals(name) {
log.Printf("---------- Deleting attribute %s", name.String())
t.attributes = append(t.attributes[:i], t.attributes[i+1:]...)
}
}
}

// FindAttribute returns the attribute with the given name, or nil if no such attribute exists.
func (t *Type) FindAttribute(name *names.Name) *Attribute {
for _, attribute := range t.attributes {
Expand Down
9 changes: 9 additions & 0 deletions pkg/concepts/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func (v *Version) AddType(typ *Type) {
}
}

// AddTypeWithoutOwner adds the given type to the version *without* changing
// its owner. This is crucial when were adding references to other types from different
// versions.
func (v *Version) AddTypeWithoutOwner(typ *Type) {
if typ != nil {
v.types[typ.Name().String()] = typ
}
}

// AddTypes adds the given types to the version.
func (v *Version) AddTypes(types []*Type) {
for _, typ := range types {
Expand Down
32 changes: 22 additions & 10 deletions pkg/generators/golang/builders_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (g *BuildersGenerator) generateStructBuilderFile(typ *concepts.Type) error
Function("builderName", g.builderName).
Function("fieldName", g.fieldName).
Function("fieldType", g.fieldType).
Function("selectorType", g.selectorType).
Function("objectName", g.objectName).
Function("setterName", g.setterName).
Function("setterType", g.setterType).
Expand Down Expand Up @@ -274,6 +275,7 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
{{ $setterName := setterName . }}
{{ $setterType := setterType . }}
{{ $fieldMask := bitMask . }}
{{ $selectorType := selectorType . }}
{{ if .Type.IsList }}
// {{ $setterName }} sets the value of the '{{ .Name }}' attribute to the given values.
Expand All @@ -296,8 +298,8 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
}
{{ else }}
{{ $elementBuilderName := builderName .Type.Element }}
func (b *{{ $builderName }}) {{ $setterName }}(values ...*{{ $elementBuilderName }}) *{{ $builderName }} {
b.{{ $fieldName }} = make([]*{{ $elementBuilderName }}, len(values))
func (b *{{ $builderName }}) {{ $setterName }}(values ...*{{ selectorType . }}{{ $elementBuilderName }}) *{{ $builderName }} {
b.{{ $fieldName }} = make([]*{{ selectorType . }}{{ $elementBuilderName }}, len(values))
copy(b.{{ $fieldName }}, values)
b.bitmap_ |= {{ $fieldMask }}
return b
Expand Down Expand Up @@ -337,26 +339,27 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
{{ range .Type.Attributes }}
{{ $fieldName := fieldName . }}
{{ $fieldType := fieldType . }}
{{ $selectorType := selectorType . }}
{{ if .Type.IsScalar }}
b.{{ $fieldName }} = object.{{ $fieldName }}
{{ else if .Type.IsStruct }}
if object.{{ $fieldName }} != nil {
b.{{ $fieldName }} = {{ builderCtor .Type }}().Copy(object.{{ $fieldName }})
b.{{ $fieldName }} = {{ selectorType . }}{{ builderCtor .Type }}().Copy(object.{{ $fieldName }})
} else {
b.{{ $fieldName }} = nil
}
{{ else if .Type.IsList }}
if object.{{ $fieldName }} != nil {
{{ if .Link }}
b.{{ $fieldName }} = {{ builderCtor .Type }}().Copy(object.{{ $fieldName }})
b.{{ $fieldName }} = {{ selectorType . }}{{ builderCtor .Type }}().Copy(object.{{ $fieldName }})
{{ else }}
{{ if .Type.Element.IsScalar }}
b.{{ $fieldName }} = make({{ $fieldType }}, len(object.{{ $fieldName }}))
copy(b.{{ $fieldName }}, object.{{ $fieldName }})
{{ else if .Type.Element.IsStruct }}
b.{{ $fieldName }} = make([]*{{ builderName .Type.Element }}, len(object.{{ $fieldName }}))
b.{{ $fieldName }} = make([]*{{ selectorType . }}{{ builderName .Type.Element }}, len(object.{{ $fieldName }}))
for i, v := range object.{{ $fieldName }} {
b.{{ $fieldName }}[i] = {{ builderCtor .Type.Element }}().Copy(v)
b.{{ $fieldName }}[i] = {{ selectorType . }}{{ builderCtor .Type.Element }}().Copy(v)
}
{{ end }}
{{ end }}
Expand All @@ -371,9 +374,9 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
b.{{ $fieldName }}[k] = v
}
{{ else if .Type.Element.IsStruct }}
b.{{ $fieldName }} = map[string]*{{ builderName .Type.Element }}{}
b.{{ $fieldName }} = map[string]*{{ selectorType . }}{{ builderName .Type.Element }}{}
for k, v := range object.{{ $fieldName }} {
b.{{ $fieldName }}[k] = {{ builderCtor .Type.Element }}().Copy(v)
b.{{ $fieldName }}[k] = {{ selectorType . }}{{ builderCtor .Type.Element }}().Copy(v)
}
{{ end }}
} else {
Expand Down Expand Up @@ -416,7 +419,7 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
object.{{ $fieldName }} = make({{ $fieldType }}, len(b.{{ $fieldName }}))
copy(object.{{ $fieldName }}, b.{{ $fieldName }})
{{ else if .Type.Element.IsStruct }}
object.{{ $fieldName }} = make([]*{{ objectName .Type.Element }}, len(b.{{ $fieldName }}))
object.{{ $fieldName }} = make([]*{{ selectorType . }}{{ objectName .Type.Element }}, len(b.{{ $fieldName }}))
for i, v := range b.{{ $fieldName }} {
object.{{ $fieldName }}[i], err = v.Build()
if err != nil {
Expand All @@ -434,7 +437,7 @@ func (g *BuildersGenerator) generateStructBuilderSource(typ *concepts.Type) {
object.{{ $fieldName }}[k] = v
}
{{ else if .Type.Element.IsStruct }}
object.{{ $fieldName }} = make(map[string]*{{ objectName .Type.Element }})
object.{{ $fieldName }} = make(map[string]*{{ selectorType . }}{{ objectName .Type.Element }})
for k, v := range b.{{ $fieldName }} {
object.{{ $fieldName }}[k], err = v.Build()
if err != nil {
Expand Down Expand Up @@ -685,6 +688,15 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere
return ref
}

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)
}
return ""
}

func (g *BuildersGenerator) setterName(attribute *concepts.Attribute) string {
name := annotations.GoName(attribute)
if name == "" {
Expand Down
Loading

0 comments on commit 1af7b0d

Please sign in to comment.