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

Add support for 'ref' annotation #210

Merged
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: 14 additions & 0 deletions pkg/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ func GoName(concept concepts.Annotated) string {
}
return fmt.Sprintf("%s", name)
}

// Reference checks if the given concept has a `reference` annotation. If it has it then it returns the value
// of the `path` parameter. It returns an empty string if there is no such annotation or parameter.
func ReferencePath(concept concepts.Annotated) string {
annotation := concept.GetAnnotation("ref")
if annotation == nil {
return ""
}
name := annotation.FindParameter("path")
if name == nil {
return ""
}
return fmt.Sprintf("%s", name)
}
8 changes: 8 additions & 0 deletions pkg/concepts/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ func (t *Type) IsScalar() bool {
return t.kind == ScalarType || t.kind == EnumType || t.kind == InterfaceType
}

func (t *Type) IsBasicType() bool {
if t == nil {
return false
}

return t.kind == ScalarType
}

// IsStruct returns true iff this type is an struct type. Note that class types are also considered
// struct types.
func (t *Type) IsStruct() bool {
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
Loading