Skip to content

Commit

Permalink
Merge pull request #203 from mnecas/add_list_scalar
Browse files Browse the repository at this point in the history
OCM-7117 | add capability of list resource with a scalar items
  • Loading branch information
mnecas committed Apr 3, 2024
2 parents e8668df + 964dc71 commit 510571e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/concepts/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ func (t *Type) IsMap() bool {
// IsScalar returns true iff this type is an scalar type. Note that interface types are also considered
// scalar types due to their opaque nature in the SDK.
func (t *Type) IsScalar() bool {
if t == nil {
return false
}
return t.kind == ScalarType || t.kind == EnumType || t.kind == InterfaceType
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/generators/golang/clients_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,11 @@ func (g *ClientsGenerator) generateResponseSource(method *concepts.Method) {
header http.Header
err *errors.Error
{{ range $responseParameters }}
{{ fieldName . }} {{ fieldType . }}
{{ if and .Type.IsList .Type.Element.IsScalar }}
{{ fieldName . }} []{{ valueType .Type.Element }}
{{ else }}
{{ fieldName . }} {{ fieldType . }}
{{ end }}
{{ end }}
}
Expand Down Expand Up @@ -1086,6 +1090,8 @@ func (g *ClientsGenerator) accessorType(parameter *concepts.Parameter) *TypeRefe
var ref *TypeReference
typ := parameter.Type()
switch {
case typ.IsList() && typ.Element().IsScalar():
ref = g.types.NullableReference(typ)
case parameter.IsItems():
ref = g.types.ListReference(typ)
case typ.IsScalar():
Expand Down
16 changes: 12 additions & 4 deletions pkg/generators/golang/json_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,10 +979,14 @@ func (g *JSONSupportGenerator) generateListMethodSource(method *concepts.Method)
{{ end }}
{{ end }}
case "items":
{{ generateReadValue "items" .Items.Type false }}
{{ generateReadValue "items" .Items.Type false }}
{{ if and .Items.Type.IsList .Items.Type.Element.IsScalar }}
response.items = items
{{ else }}
response.items = &{{ structName .Items.Type }}{
items: items,
}
{{ end }}
default:
iterator.ReadAny()
}
Expand Down Expand Up @@ -1107,9 +1111,13 @@ func (g *JSONSupportGenerator) generateSearchMethodSource(method *concepts.Metho
{{ end }}
case "items":
{{ generateReadValue "items" .Items.Type false }}
response.items = &{{ structName .Items.Type }}{
items: items,
}
{{ if and .Items.Type.IsList .Items.Type.Element.IsScalar }}
response.items = items
{{ else }}
response.items = &{{ structName .Items.Type }}{
items: items,
}
{{ end }}
default:
iterator.ReadAny()
}
Expand Down
24 changes: 24 additions & 0 deletions tests/go/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,28 @@ var _ = Describe("Client", func() {
Expect(ok).To(BeTrue())
Expect(value).To(BeTrue())
})

It("Can get a resource list with scalar items", func() {
server.AppendHandlers(RespondWith(http.StatusOK, `{
"kind": "LoadBalancerQuotaValueList",
"size": 5,
"page": 1,
"total": 5,
"items": [
1,
2,
3,
4,
5
]
}
`))
client := cmv1.NewClient(transport, "/api/clusters_mgmt/v1")
response, err := client.LoadBalancerQuotaValues().List().Send()
Expect(err).ToNot(HaveOccurred())
Expect(response).ToNot(BeNil())
items := response.Items()
Expect(items).ToNot(BeNil())
Expect(items[0]).To(Equal(1))
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright (c) 2024 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 load balancer quota values.
resource LoadBalancerQuotaValues {
// Retrieves the list of Load Balancer Quota Values.
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

// Total number of items of the collection.
out Total Integer

// Retrieved list of values.
out Items []Integer
}
}
5 changes: 5 additions & 0 deletions tests/model/clusters_mgmt/v1/root_resource.model
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ resource Root {
locator Nil {
target Nil
}

// Reference to the resource that manages the load balancer quota values.
locator LoadBalancerQuotaValues{
target LoadBalancerQuotaValues
}
}

0 comments on commit 510571e

Please sign in to comment.