From 5c1de528d1149c50846e044ab4f1db30f33c3282 Mon Sep 17 00:00:00 2001 From: Alba Hita Catala Date: Tue, 28 Jan 2025 12:37:48 +0100 Subject: [PATCH 1/4] Enhancement of 'ref' mechanism Signed-off-by: Alba Hita Catala --- pkg/concepts/type.go | 32 ++++++-- pkg/generators/golang/builders_generator.go | 6 +- pkg/generators/golang/json_generator.go | 33 ++++---- pkg/generators/golang/types_generator.go | 4 +- pkg/language/reader.go | 53 ++++++++++++- .../aro_hcp/v1_alpha1/cluster_resource.model | 7 +- .../v1_alpha1/cluster_status_resource.model | 23 ++++++ .../v1_alpha1/cluster_status_type.model | 19 +++++ .../aro_hcp/v1_alpha1/cluster_type.model | 2 +- .../aro_hcp/v1_alpha1/clusters_resource.model | 2 +- .../aro_hcp/v1_alpha1/node_pool_type.model | 2 +- .../v1_alpha1/node_pools_resource.model | 2 +- .../clusters_mgmt/v1/cluster_resource.model | 10 +++ .../v1/cluster_status_resource.model | 23 ++++++ .../v1/cluster_status_type.model | 24 ++++++ .../model/clusters_mgmt/v1/cluster_type.model | 10 ++- .../clusters_mgmt/v1/node_pool_resource.model | 32 ++++++++ .../clusters_mgmt/v1/node_pool_type.model | 6 +- .../v1/node_pools_resource.model | 75 +++++++++++++++++++ 19 files changed, 329 insertions(+), 36 deletions(-) create mode 100644 tests/model/aro_hcp/v1_alpha1/cluster_status_resource.model create mode 100644 tests/model/aro_hcp/v1_alpha1/cluster_status_type.model create mode 100644 tests/model/clusters_mgmt/v1/cluster_status_resource.model create mode 100644 tests/model/clusters_mgmt/v1/cluster_status_type.model create mode 100644 tests/model/clusters_mgmt/v1/node_pool_resource.model create mode 100644 tests/model/clusters_mgmt/v1/node_pools_resource.model diff --git a/pkg/concepts/type.go b/pkg/concepts/type.go index 8a67a6b..b375081 100644 --- a/pkg/concepts/type.go +++ b/pkg/concepts/type.go @@ -72,12 +72,24 @@ type Type struct { annotatedSupport namedSupport - owner *Version - kind TypeKind - attributes AttributeSlice - values EnumValueSlice - element *Type - index *Type + owner *Version + kind TypeKind + attributes AttributeSlice + values EnumValueSlice + element *Type + index *Type + explicitDeclared bool +} + +// ExplicitDeclared returns true if this type is explicitDeclared +// with a reference +func (t *Type) ExplicitDeclared() bool { + return t.explicitDeclared +} + +// SetExplicitDeclared sets ExplicitDeclared +func (t *Type) SetExplicitDeclared(value bool) { + t.explicitDeclared = value } // Owner returns the version that owns this type. @@ -193,6 +205,14 @@ func (t *Type) AddAttribute(attribute *Attribute) { } } +func (t *Type) AddAttributes(attributes AttributeSlice) { + if attributes.Len() > 0 { + for _, att := range attributes { + t.AddAttribute(att) + } + } +} + // 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 { diff --git a/pkg/generators/golang/builders_generator.go b/pkg/generators/golang/builders_generator.go index 643d3d1..dec6920 100644 --- a/pkg/generators/golang/builders_generator.go +++ b/pkg/generators/golang/builders_generator.go @@ -643,7 +643,7 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere typ := attribute.Type() var ref *TypeReference referencedVersion := "" - if attribute.LinkOwner() != nil { + if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { referencedVersion = attribute.LinkOwner().Name().String() } switch { @@ -693,7 +693,7 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere } func (g *BuildersGenerator) selectorType(attribute *concepts.Attribute) string { - if attribute.LinkOwner() == nil { + if attribute.LinkOwner() == nil || attribute.Type().ExplicitDeclared() { return "" } return fmt.Sprintf("%s.", g.packages.VersionSelector(attribute.LinkOwner())) @@ -711,7 +711,7 @@ func (g *BuildersGenerator) setterType(attribute *concepts.Attribute) *TypeRefer typ := attribute.Type() var ref *TypeReference referencedVersion := "" - if attribute.LinkOwner() != nil { + if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { referencedVersion = attribute.LinkOwner().Name().String() } switch { diff --git a/pkg/generators/golang/json_generator.go b/pkg/generators/golang/json_generator.go index de6c314..67e3491 100644 --- a/pkg/generators/golang/json_generator.go +++ b/pkg/generators/golang/json_generator.go @@ -173,7 +173,7 @@ func (g *JSONSupportGenerator) Run() error { } for _, typ := range version.Types() { for _, att := range typ.Attributes() { - if att.LinkOwner() != nil { + if att.LinkOwner() != nil && !att.Type().ExplicitDeclared() { importRefs = append(importRefs, struct { path string @@ -700,7 +700,7 @@ func (g *JSONSupportGenerator) generateStructTypeSource(typ *concepts.Type) { {{ $fieldTag := fieldTag . }} {{ $fieldMask := bitMask . }} case "{{ $fieldTag }}": - {{ generateReadValue "value" .Type .Link .LinkOwner}} + {{ generateReadValue "value" .Type .Link .LinkOwner }} object.{{ $fieldName }} = value object.bitmap_ |= {{ $fieldMask }} {{ end }} @@ -712,6 +712,7 @@ func (g *JSONSupportGenerator) generateStructTypeSource(typ *concepts.Type) { } `, "Type", typ, + "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -825,6 +826,7 @@ func (g *JSONSupportGenerator) generateListTypeSource( `, "Type", typ, "linkOwner", linkOwner, + "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1348,10 +1350,10 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. text := iterator.ReadString() {{ .Variable }} := {{ enumName .Type }}(text) {{ else if .Type.IsStruct }} - {{ .Variable }} := {{ readRefTypeFunc .Type .LinkOwner }}(iterator) + {{ .Variable }} := {{ readRefTypeFunc .Type .LinkOwner .ExplicitDeclared}}(iterator) {{ else if .Type.IsList }} {{ if .Link }} - {{ $selectorFromLinkOwner := selectorFromLinkOwner .LinkOwner}} + {{ $selectorFromLinkOwner := selectorFromLinkOwner .LinkOwner .ExplicitDeclared}} {{ $structName := structName .Type }} {{ .Variable }} := &{{ $selectorFromLinkOwner }}{{ $structName }}{} for { @@ -1366,7 +1368,7 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. case "href": {{ .Variable }}.SetHREF(iterator.ReadString()) case "items": - {{ .Variable }}.SetItems({{ readRefTypeFunc .Type .LinkOwner }}(iterator)) + {{ .Variable }}.SetItems({{ readRefTypeFunc .Type .LinkOwner .ExplicitDeclared}}(iterator)) default: iterator.ReadAny() } @@ -1392,6 +1394,7 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. "Type", typ, "Link", link, "LinkOwner", linkOwner, + "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1432,6 +1435,7 @@ func (g *JSONSupportGenerator) generateWriteBodyParameter(object string, "Value", value, "Type", typ, "LinkOwner", linkOwner, + "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1459,12 +1463,12 @@ func (g *JSONSupportGenerator) generateWriteValue(value string, {{ else if .Type.IsInterface }} stream.WriteVal({{ .Value }}) {{ else if .Type.IsStruct }} - {{ writeRefTypeFunc .Type .LinkOwner }}({{ .Value}}, stream) + {{ writeRefTypeFunc .Type .LinkOwner .ExplicitDeclared }}({{ .Value}}, stream) {{ else if .Type.IsList }} {{ if .Link }} stream.WriteObjectStart() stream.WriteObjectField("items") - {{ writeRefTypeFunc .Type .LinkOwner }}({{ .Value }}.Items(), stream) + {{ writeRefTypeFunc .Type .LinkOwner .ExplicitDeclared }}({{ .Value }}.Items(), stream) stream.WriteObjectEnd() {{ else }} {{ writeTypeFunc .Type }}({{ .Value }}, stream) @@ -1485,7 +1489,7 @@ func (g *JSONSupportGenerator) generateWriteValue(value string, } item := {{ .Value }}[key] stream.WriteObjectField(key) - {{ generateWriteValue "item" .Type.Element false .LinkOwner}} + {{ generateWriteValue "item" .Type.Element false .LinkOwner }} } stream.WriteObjectEnd() } else { @@ -1497,6 +1501,7 @@ func (g *JSONSupportGenerator) generateWriteValue(value string, "Type", typ, "Link", link, "LinkOwner", linkOwner, + "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1530,9 +1535,9 @@ func (g *JSONSupportGenerator) writeTypeFunc(typ *concepts.Type) string { return g.names.Public(name) } -func (g *JSONSupportGenerator) writeRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version) string { +func (g *JSONSupportGenerator) writeRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version, explicitDeclared bool) string { name := names.Cat(nomenclator.Write, typ.Name()) - if refVersion != nil { + if refVersion != nil && !explicitDeclared { version := g.packages.VersionSelector(refVersion) return fmt.Sprintf("%s.%s", version, g.names.Public(name)) } @@ -1553,9 +1558,9 @@ func (g *JSONSupportGenerator) readTypeFunc(typ *concepts.Type) string { return g.names.Public(name) } -func (g *JSONSupportGenerator) readRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version) string { +func (g *JSONSupportGenerator) readRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version, explicitDeclared bool) string { name := names.Cat(nomenclator.Read, typ.Name()) - if refVersion != nil { + if refVersion != nil && !explicitDeclared { version := g.packages.VersionSelector(refVersion) return fmt.Sprintf("%s.%s", version, g.names.Public(name)) } @@ -1675,8 +1680,8 @@ func (g *JSONSupportGenerator) defaultValue(parameter *concepts.Parameter) strin } } -func (g JSONSupportGenerator) selectorFromLinkOwner(linkOwner *concepts.Version) string { - if linkOwner == nil { +func (g JSONSupportGenerator) selectorFromLinkOwner(linkOwner *concepts.Version, explicitDeclared bool) string { + if linkOwner == nil || explicitDeclared { return "" } return fmt.Sprintf("%s.", g.packages.VersionSelector(linkOwner)) diff --git a/pkg/generators/golang/types_generator.go b/pkg/generators/golang/types_generator.go index 6307fb4..ceda0c5 100644 --- a/pkg/generators/golang/types_generator.go +++ b/pkg/generators/golang/types_generator.go @@ -607,7 +607,7 @@ func (g *TypesGenerator) getterType(attribute *concepts.Attribute) *TypeReferenc var ref *TypeReference typ := attribute.Type() referencedVersion := "" - if attribute.LinkOwner() != nil { + if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { referencedVersion = attribute.LinkOwner().Name().String() } switch { @@ -670,7 +670,7 @@ func (g *TypesGenerator) fieldType(attribute *concepts.Attribute) *TypeReference var ref *TypeReference typ := attribute.Type() referencedVersion := "" - if attribute.LinkOwner() != nil { + if attribute.LinkOwner() != nil && !attribute.Type().ExplicitDeclared() { referencedVersion = attribute.LinkOwner().Name().String() } switch { diff --git a/pkg/language/reader.go b/pkg/language/reader.go index 4640da4..ace58ac 100644 --- a/pkg/language/reader.go +++ b/pkg/language/reader.go @@ -431,10 +431,32 @@ func (r *Reader) ExitClassDecl(ctx *ClassDeclContext) { } if path := annotations.ReferencePath(typ); path != "" { + // If the type has a reference set ExplicitDeclared + typ.SetExplicitDeclared(true) + r.removeLinkedAttributes(typ) r.handleClassRef(typ, path) } } +func (r *Reader) removeLinkedAttributes(typ *concepts.Type) { + for _, types := range r.version.Types() { + for _, attribute := range types.Attributes() { + // It could be that the type is already in an attribute of the service + // It needs to add the SetExplicitDeclared to it. { + if attribute.Type().Name() == typ.Name() { + attribute.Type().SetExplicitDeclared(true) + } + if attribute.Type().IsList() || attribute.Type().IsMap() { + if attribute.Type().Element().Name().String() == typ.Name().String() { + attribute.Type().SetExplicitDeclared(true) + attribute.Type().Element().SetExplicitDeclared(true) + attribute.Type().Element().SetOwner(typ.Owner()) + } + } + } + } +} + func (r *Reader) handleClassRef(typ *concepts.Type, path string) { if len(r.inputs) > 1 { panic("referenced service with multiple inputs in undefined") @@ -462,7 +484,9 @@ func (r *Reader) handleClassRef(typ *concepts.Type, path string) { // Once loading the service, we find the reference type // then recursively iterate the type tree and add the types to the current version. if referencedType := refVersion.FindType(names.ParseUsingSeparator(referencedTypeName, "_")); referencedType != nil { - r.version.AddType(referencedType) + if !typ.ExplicitDeclared() { + r.version.AddType(referencedType) + } r.recursivelyAddTypeToVersion(typ, referencedType) } } @@ -474,9 +498,25 @@ func (r *Reader) recursivelyAddTypeToVersion(currType *concepts.Type, return } for _, attribute := range referencedType.Attributes() { + // If attribute is explicitDeclared, it needs to change it's owner to be the same as currType if attribute.Link() { // If the attribute is a Link set the LinkOwner - // to the attribute type Owner + // to the attribute type Owner, if it is also a list it should change the owner of the + // element type + existingAttType := r.version.FindType(attribute.Type().Name()) + if existingAttType != nil && existingAttType.ExplicitDeclared() { + attribute.Type().SetOwner(r.version) + // Stop the iteration over the attributes of this type + continue + } else if attribute.Type().IsList() || attribute.Type().IsMap() { + existingAttType = r.version.FindType(attribute.Type().Element().Name()) + if existingAttType != nil && existingAttType.ExplicitDeclared() { + attribute.Type().SetOwner(r.version) + attribute.Type().Element().SetOwner(r.version) + continue + } + } + // Otherwise add the setLinkOwner to the attribute attribute.SetLinkOwner(attribute.Type().Owner()) } else if attribute.Type().IsList() || attribute.Type().IsMap() { r.version.AddType(attribute.Type()) @@ -485,9 +525,14 @@ func (r *Reader) recursivelyAddTypeToVersion(currType *concepts.Type, r.recursivelyAddTypeToVersion(currType, attribute.Type()) } } - if r.version.FindType(referencedType.Name()) == nil { - r.version.AddType(referencedType) + + existingType := r.version.FindType(referencedType.Name()) + if existingType != nil && existingType.ExplicitDeclared() { + //r.version.AddExplicitDeclaredType(referencedType) + referencedType.SetExplicitDeclared(true) } + r.version.AddType(referencedType) + } func (r *Reader) ExitStructDecl(ctx *StructDeclContext) { diff --git a/tests/model/aro_hcp/v1_alpha1/cluster_resource.model b/tests/model/aro_hcp/v1_alpha1/cluster_resource.model index d45afaa..4be4abf 100644 --- a/tests/model/aro_hcp/v1_alpha1/cluster_resource.model +++ b/tests/model/aro_hcp/v1_alpha1/cluster_resource.model @@ -39,4 +39,9 @@ resource Cluster { locator NodePools { target NodePools } -} \ No newline at end of file + + // Reference to the resource that manages the detailed status of the cluster. + locator Status { + target ClusterStatus + } +} diff --git a/tests/model/aro_hcp/v1_alpha1/cluster_status_resource.model b/tests/model/aro_hcp/v1_alpha1/cluster_status_resource.model new file mode 100644 index 0000000..62b41b6 --- /dev/null +++ b/tests/model/aro_hcp/v1_alpha1/cluster_status_resource.model @@ -0,0 +1,23 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Manages a specific cluster status. +resource ClusterStatus { + // Retrieves the details of the node pool. + method Get { + out Body ClusterStatus + } +} diff --git a/tests/model/aro_hcp/v1_alpha1/cluster_status_type.model b/tests/model/aro_hcp/v1_alpha1/cluster_status_type.model new file mode 100644 index 0000000..e00a2eb --- /dev/null +++ b/tests/model/aro_hcp/v1_alpha1/cluster_status_type.model @@ -0,0 +1,19 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +@ref(path = "/clusters_mgmt/v1/cluster_status") +class ClusterStatus { +} diff --git a/tests/model/aro_hcp/v1_alpha1/cluster_type.model b/tests/model/aro_hcp/v1_alpha1/cluster_type.model index 793cc1f..a9e2c02 100644 --- a/tests/model/aro_hcp/v1_alpha1/cluster_type.model +++ b/tests/model/aro_hcp/v1_alpha1/cluster_type.model @@ -16,4 +16,4 @@ limitations under the License. @ref(path = "/clusters_mgmt/v1/cluster") class Cluster { -} \ No newline at end of file +} diff --git a/tests/model/aro_hcp/v1_alpha1/clusters_resource.model b/tests/model/aro_hcp/v1_alpha1/clusters_resource.model index 136b081..b1f311f 100644 --- a/tests/model/aro_hcp/v1_alpha1/clusters_resource.model +++ b/tests/model/aro_hcp/v1_alpha1/clusters_resource.model @@ -76,4 +76,4 @@ resource Clusters { target Cluster variable ID } -} \ No newline at end of file +} diff --git a/tests/model/aro_hcp/v1_alpha1/node_pool_type.model b/tests/model/aro_hcp/v1_alpha1/node_pool_type.model index c94a048..ef6df16 100644 --- a/tests/model/aro_hcp/v1_alpha1/node_pool_type.model +++ b/tests/model/aro_hcp/v1_alpha1/node_pool_type.model @@ -16,4 +16,4 @@ limitations under the License. @ref(path = "/clusters_mgmt/v1/node_pool") class NodePool { -} \ No newline at end of file +} diff --git a/tests/model/aro_hcp/v1_alpha1/node_pools_resource.model b/tests/model/aro_hcp/v1_alpha1/node_pools_resource.model index 09c370b..4408870 100644 --- a/tests/model/aro_hcp/v1_alpha1/node_pools_resource.model +++ b/tests/model/aro_hcp/v1_alpha1/node_pools_resource.model @@ -72,4 +72,4 @@ resource NodePools { target NodePool variable ID } -} \ No newline at end of file +} diff --git a/tests/model/clusters_mgmt/v1/cluster_resource.model b/tests/model/clusters_mgmt/v1/cluster_resource.model index 936609f..fe9f2c3 100644 --- a/tests/model/clusters_mgmt/v1/cluster_resource.model +++ b/tests/model/clusters_mgmt/v1/cluster_resource.model @@ -41,4 +41,14 @@ resource Cluster { locator IdentityProviders { target IdentityProviders } + + // Reference to the NodePool resource + locator NodePools{ + target NodePools + } + + // Reference to the resource that manages the detailed status of the cluster. + locator Status { + target ClusterStatus + } } diff --git a/tests/model/clusters_mgmt/v1/cluster_status_resource.model b/tests/model/clusters_mgmt/v1/cluster_status_resource.model new file mode 100644 index 0000000..62b41b6 --- /dev/null +++ b/tests/model/clusters_mgmt/v1/cluster_status_resource.model @@ -0,0 +1,23 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Manages a specific cluster status. +resource ClusterStatus { + // Retrieves the details of the node pool. + method Get { + out Body ClusterStatus + } +} diff --git a/tests/model/clusters_mgmt/v1/cluster_status_type.model b/tests/model/clusters_mgmt/v1/cluster_status_type.model new file mode 100644 index 0000000..be4f510 --- /dev/null +++ b/tests/model/clusters_mgmt/v1/cluster_status_type.model @@ -0,0 +1,24 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Overall state of a cluster. +class ClusterStatus { + // State value + State ClusterState + + // Human readable value + Message String +} diff --git a/tests/model/clusters_mgmt/v1/cluster_type.model b/tests/model/clusters_mgmt/v1/cluster_type.model index 7fe405f..310f542 100644 --- a/tests/model/clusters_mgmt/v1/cluster_type.model +++ b/tests/model/clusters_mgmt/v1/cluster_type.model @@ -35,7 +35,10 @@ class Cluster { // User defined properties for tagging and querying. Properties [String]String - // Overall state of the cluster. + // Overall status of the cluster. + Status ClusterStatus + + // Overall state of the cluster State ClusterState // Flag indicating if the cluster is managed (by Red Hat) or @@ -67,4 +70,9 @@ class Cluster { // Floating point value used for tests. Factor Float + + // List of node pools on this cluster. + // NodePool is a scalable set of worker nodes attached to a hosted cluster. + link NodePools []NodePool + } diff --git a/tests/model/clusters_mgmt/v1/node_pool_resource.model b/tests/model/clusters_mgmt/v1/node_pool_resource.model new file mode 100644 index 0000000..0ad1969 --- /dev/null +++ b/tests/model/clusters_mgmt/v1/node_pool_resource.model @@ -0,0 +1,32 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Manages a specific nodepool. +resource NodePool { + // Retrieves the details of the node pool. + method Get { + out Body NodePool + } + + // Updates the node pool. + method Update { + in out Body NodePool + } + + // Deletes the node pool. + method Delete { + } +} diff --git a/tests/model/clusters_mgmt/v1/node_pool_type.model b/tests/model/clusters_mgmt/v1/node_pool_type.model index 9c64ffa..a14473a 100644 --- a/tests/model/clusters_mgmt/v1/node_pool_type.model +++ b/tests/model/clusters_mgmt/v1/node_pool_type.model @@ -14,4 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ -Class NodePool {} \ No newline at end of file +class NodePool { + // The number of Machines (and Nodes) to create. + // Replicas and autoscaling cannot be used together. + Replicas Integer +} diff --git a/tests/model/clusters_mgmt/v1/node_pools_resource.model b/tests/model/clusters_mgmt/v1/node_pools_resource.model new file mode 100644 index 0000000..461ac93 --- /dev/null +++ b/tests/model/clusters_mgmt/v1/node_pools_resource.model @@ -0,0 +1,75 @@ +/* +Copyright (c) 2025 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Manages the collection of node pools of a cluster. +resource NodePools { + // Retrieves the list of node pools. + method List { + // Index of the requested page, where one corresponds to the first page. + in out Page Integer = 1 + + // Number of items contained in the returned page. + in out Size Integer = 100 + + // Search criteria. + // + // The syntax of this parameter is similar to the syntax of the _where_ clause of a + // SQL statement, but using the names of the attributes of the node pools instead of + // the names of the columns of a table. For example, in order to retrieve all the + // node pools with replicas of two the following is required: + // + // ```sql + // replicas = 2 + // ``` + // + // If the parameter isn't provided, or if the value is empty, then all the + // node pools that the user has permission to see will be returned. + in Search String + + // Order criteria. + // + // The syntax of this parameter is similar to the syntax of the _order by_ clause of + // a SQL statement, but using the names of the attributes of the node pools instead of + // the names of the columns of a table. For example, in order to sort the node pools + // descending by identifier the value should be: + // + // ```sql + // id desc + // ``` + // + // If the parameter isn't provided, or if the value is empty, then the order of the + // results is undefined. + in Order String + + // Total number of items of the collection. + out Total Integer + + // Retrieved list of node pools. + out Items []NodePool + } + + // Adds a new node pool to the cluster. + method Add { + // Description of the node pool + in out Body NodePool + } + + // Reference to the service that manages a specific node pool. + locator NodePool { + target NodePool + variable ID + } +} From 38de0fbd2e2af1396e6f95dc7ebde3d3269c83ba Mon Sep 17 00:00:00 2001 From: Alba Hita Catala Date: Thu, 30 Jan 2025 13:32:17 +0100 Subject: [PATCH 2/4] Change the tests for ref subresources Signed-off-by: Alba Hita Catala --- pkg/language/ref_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/language/ref_test.go b/pkg/language/ref_test.go index 0e5690a..3c45db1 100644 --- a/pkg/language/ref_test.go +++ b/pkg/language/ref_test.go @@ -206,8 +206,7 @@ var _ = Describe("Read Model with ref annotation", func() { attributeFoo := class.FindAttribute(names.ParseUsingCase("Foo")) Expect(attributeFoo).ToNot(BeNil()) Expect(attributeFoo.Type().IsList()).To(BeTrue()) - Expect(attributeFoo.LinkOwner().Name().String()).To(Equal("v1")) - Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1")) + Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1_alpha1")) myAttributeType := version.FindType(names.ParseUsingCase("MyAttribute")) Expect(myAttributeType).ToNot(BeNil()) Expect(myAttributeType.Owner().Name().String()).To(Equal("v1_alpha1")) @@ -267,8 +266,7 @@ var _ = Describe("Read Model with ref annotation", func() { attributeFoo := class.FindAttribute(names.ParseUsingCase("Foo")) Expect(attributeFoo).ToNot(BeNil()) Expect(attributeFoo.Type().IsList()).To(BeTrue()) - Expect(attributeFoo.LinkOwner().Name().String()).To(Equal("v1")) - Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1")) + Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1_alpha1")) myAttributeType := version.FindType(names.ParseUsingCase("MyAttribute")) Expect(myAttributeType).ToNot(BeNil()) Expect(myAttributeType.Owner().Name().String()).To(Equal("v1_alpha1")) @@ -331,8 +329,7 @@ var _ = Describe("Read Model with ref annotation", func() { attributeFoo := class.FindAttribute(names.ParseUsingCase("Foo")) Expect(attributeFoo).ToNot(BeNil()) Expect(attributeFoo.Type().IsList()).To(BeTrue()) - Expect(attributeFoo.LinkOwner().Name().String()).To(Equal("v1")) - Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1")) + Expect(attributeFoo.Type().Owner().Name().String()).To(Equal("v1_alpha1")) myAttributeType := version.FindType(names.ParseUsingCase("MyAttribute")) Expect(myAttributeType).ToNot(BeNil()) Expect(myAttributeType.Owner().Name().String()).To(Equal("v1_alpha1")) From 58738ea359c6ead2309a9aa6203e9fec43e35ef2 Mon Sep 17 00:00:00 2001 From: Alba Hita Catala Date: Thu, 30 Jan 2025 13:59:42 +0100 Subject: [PATCH 3/4] Remove linkOwner, simplify generators Signed-off-by: Alba Hita Catala --- pkg/generators/golang/builders_generator.go | 6 ++--- pkg/generators/golang/json_generator.go | 29 +++++++++------------ pkg/generators/golang/types_generator.go | 4 +-- pkg/language/reader.go | 2 ++ 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/pkg/generators/golang/builders_generator.go b/pkg/generators/golang/builders_generator.go index dec6920..643d3d1 100644 --- a/pkg/generators/golang/builders_generator.go +++ b/pkg/generators/golang/builders_generator.go @@ -643,7 +643,7 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere typ := attribute.Type() var ref *TypeReference referencedVersion := "" - if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { + if attribute.LinkOwner() != nil { referencedVersion = attribute.LinkOwner().Name().String() } switch { @@ -693,7 +693,7 @@ func (g *BuildersGenerator) fieldType(attribute *concepts.Attribute) *TypeRefere } func (g *BuildersGenerator) selectorType(attribute *concepts.Attribute) string { - if attribute.LinkOwner() == nil || attribute.Type().ExplicitDeclared() { + if attribute.LinkOwner() == nil { return "" } return fmt.Sprintf("%s.", g.packages.VersionSelector(attribute.LinkOwner())) @@ -711,7 +711,7 @@ func (g *BuildersGenerator) setterType(attribute *concepts.Attribute) *TypeRefer typ := attribute.Type() var ref *TypeReference referencedVersion := "" - if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { + if attribute.LinkOwner() != nil { referencedVersion = attribute.LinkOwner().Name().String() } switch { diff --git a/pkg/generators/golang/json_generator.go b/pkg/generators/golang/json_generator.go index 67e3491..97d5fa7 100644 --- a/pkg/generators/golang/json_generator.go +++ b/pkg/generators/golang/json_generator.go @@ -173,7 +173,7 @@ func (g *JSONSupportGenerator) Run() error { } for _, typ := range version.Types() { for _, att := range typ.Attributes() { - if att.LinkOwner() != nil && !att.Type().ExplicitDeclared() { + if att.LinkOwner() != nil { importRefs = append(importRefs, struct { path string @@ -712,7 +712,6 @@ func (g *JSONSupportGenerator) generateStructTypeSource(typ *concepts.Type) { } `, "Type", typ, - "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -826,7 +825,6 @@ func (g *JSONSupportGenerator) generateListTypeSource( `, "Type", typ, "linkOwner", linkOwner, - "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1350,10 +1348,10 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. text := iterator.ReadString() {{ .Variable }} := {{ enumName .Type }}(text) {{ else if .Type.IsStruct }} - {{ .Variable }} := {{ readRefTypeFunc .Type .LinkOwner .ExplicitDeclared}}(iterator) + {{ .Variable }} := {{ readRefTypeFunc .Type .LinkOwner }}(iterator) {{ else if .Type.IsList }} {{ if .Link }} - {{ $selectorFromLinkOwner := selectorFromLinkOwner .LinkOwner .ExplicitDeclared}} + {{ $selectorFromLinkOwner := selectorFromLinkOwner .LinkOwner }} {{ $structName := structName .Type }} {{ .Variable }} := &{{ $selectorFromLinkOwner }}{{ $structName }}{} for { @@ -1368,7 +1366,7 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. case "href": {{ .Variable }}.SetHREF(iterator.ReadString()) case "items": - {{ .Variable }}.SetItems({{ readRefTypeFunc .Type .LinkOwner .ExplicitDeclared}}(iterator)) + {{ .Variable }}.SetItems({{ readRefTypeFunc .Type .LinkOwner }}(iterator)) default: iterator.ReadAny() } @@ -1394,7 +1392,6 @@ func (g *JSONSupportGenerator) generateReadValue(variable string, typ *concepts. "Type", typ, "Link", link, "LinkOwner", linkOwner, - "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1435,7 +1432,6 @@ func (g *JSONSupportGenerator) generateWriteBodyParameter(object string, "Value", value, "Type", typ, "LinkOwner", linkOwner, - "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1463,12 +1459,12 @@ func (g *JSONSupportGenerator) generateWriteValue(value string, {{ else if .Type.IsInterface }} stream.WriteVal({{ .Value }}) {{ else if .Type.IsStruct }} - {{ writeRefTypeFunc .Type .LinkOwner .ExplicitDeclared }}({{ .Value}}, stream) + {{ writeRefTypeFunc .Type .LinkOwner }}({{ .Value}}, stream) {{ else if .Type.IsList }} {{ if .Link }} stream.WriteObjectStart() stream.WriteObjectField("items") - {{ writeRefTypeFunc .Type .LinkOwner .ExplicitDeclared }}({{ .Value }}.Items(), stream) + {{ writeRefTypeFunc .Type .LinkOwner }}({{ .Value }}.Items(), stream) stream.WriteObjectEnd() {{ else }} {{ writeTypeFunc .Type }}({{ .Value }}, stream) @@ -1501,7 +1497,6 @@ func (g *JSONSupportGenerator) generateWriteValue(value string, "Type", typ, "Link", link, "LinkOwner", linkOwner, - "ExplicitDeclared", typ.ExplicitDeclared(), ) } @@ -1535,9 +1530,9 @@ func (g *JSONSupportGenerator) writeTypeFunc(typ *concepts.Type) string { return g.names.Public(name) } -func (g *JSONSupportGenerator) writeRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version, explicitDeclared bool) string { +func (g *JSONSupportGenerator) writeRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version) string { name := names.Cat(nomenclator.Write, typ.Name()) - if refVersion != nil && !explicitDeclared { + if refVersion != nil { version := g.packages.VersionSelector(refVersion) return fmt.Sprintf("%s.%s", version, g.names.Public(name)) } @@ -1558,9 +1553,9 @@ func (g *JSONSupportGenerator) readTypeFunc(typ *concepts.Type) string { return g.names.Public(name) } -func (g *JSONSupportGenerator) readRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version, explicitDeclared bool) string { +func (g *JSONSupportGenerator) readRefTypeFunc(typ *concepts.Type, refVersion *concepts.Version) string { name := names.Cat(nomenclator.Read, typ.Name()) - if refVersion != nil && !explicitDeclared { + if refVersion != nil { version := g.packages.VersionSelector(refVersion) return fmt.Sprintf("%s.%s", version, g.names.Public(name)) } @@ -1680,8 +1675,8 @@ func (g *JSONSupportGenerator) defaultValue(parameter *concepts.Parameter) strin } } -func (g JSONSupportGenerator) selectorFromLinkOwner(linkOwner *concepts.Version, explicitDeclared bool) string { - if linkOwner == nil || explicitDeclared { +func (g JSONSupportGenerator) selectorFromLinkOwner(linkOwner *concepts.Version) string { + if linkOwner == nil { return "" } return fmt.Sprintf("%s.", g.packages.VersionSelector(linkOwner)) diff --git a/pkg/generators/golang/types_generator.go b/pkg/generators/golang/types_generator.go index ceda0c5..6307fb4 100644 --- a/pkg/generators/golang/types_generator.go +++ b/pkg/generators/golang/types_generator.go @@ -607,7 +607,7 @@ func (g *TypesGenerator) getterType(attribute *concepts.Attribute) *TypeReferenc var ref *TypeReference typ := attribute.Type() referencedVersion := "" - if attribute.LinkOwner() != nil && !typ.ExplicitDeclared() { + if attribute.LinkOwner() != nil { referencedVersion = attribute.LinkOwner().Name().String() } switch { @@ -670,7 +670,7 @@ func (g *TypesGenerator) fieldType(attribute *concepts.Attribute) *TypeReference var ref *TypeReference typ := attribute.Type() referencedVersion := "" - if attribute.LinkOwner() != nil && !attribute.Type().ExplicitDeclared() { + if attribute.LinkOwner() != nil { referencedVersion = attribute.LinkOwner().Name().String() } switch { diff --git a/pkg/language/reader.go b/pkg/language/reader.go index ace58ac..c7a0e3f 100644 --- a/pkg/language/reader.go +++ b/pkg/language/reader.go @@ -444,10 +444,12 @@ func (r *Reader) removeLinkedAttributes(typ *concepts.Type) { // It could be that the type is already in an attribute of the service // It needs to add the SetExplicitDeclared to it. { if attribute.Type().Name() == typ.Name() { + attribute.SetLinkOwner(nil) attribute.Type().SetExplicitDeclared(true) } if attribute.Type().IsList() || attribute.Type().IsMap() { if attribute.Type().Element().Name().String() == typ.Name().String() { + attribute.SetLinkOwner(nil) attribute.Type().SetExplicitDeclared(true) attribute.Type().Element().SetExplicitDeclared(true) attribute.Type().Element().SetOwner(typ.Owner()) From e58f55f6e68a8a6a1c427af8cb01f8c37c4e1f7d Mon Sep 17 00:00:00 2001 From: Alba Hita Catala Date: Fri, 31 Jan 2025 09:25:30 +0100 Subject: [PATCH 4/4] Remove unused code Signed-off-by: Alba Hita Catala --- pkg/language/reader.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/language/reader.go b/pkg/language/reader.go index c7a0e3f..2972465 100644 --- a/pkg/language/reader.go +++ b/pkg/language/reader.go @@ -486,9 +486,6 @@ func (r *Reader) handleClassRef(typ *concepts.Type, path string) { // Once loading the service, we find the reference type // then recursively iterate the type tree and add the types to the current version. if referencedType := refVersion.FindType(names.ParseUsingSeparator(referencedTypeName, "_")); referencedType != nil { - if !typ.ExplicitDeclared() { - r.version.AddType(referencedType) - } r.recursivelyAddTypeToVersion(typ, referencedType) } } @@ -530,7 +527,6 @@ func (r *Reader) recursivelyAddTypeToVersion(currType *concepts.Type, existingType := r.version.FindType(referencedType.Name()) if existingType != nil && existingType.ExplicitDeclared() { - //r.version.AddExplicitDeclaredType(referencedType) referencedType.SetExplicitDeclared(true) } r.version.AddType(referencedType)