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

v3: Add more listable on Name or(not and) ID #642

Merged
merged 5 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Unreleased
----------

- v3: Add more listable on Name or(not and) ID #642

3.1.0
----------

Expand Down
10 changes: 5 additions & 5 deletions v3/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions v3/generator/client/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (c Client) GetZoneAPIEndpoint(ctx context.Context, zoneName ZoneName) (Endp
if err != nil {
return "", fmt.Errorf("get zone api endpoint: %w", err)
}
for _, zone := range resp.Zones {
if zone.Name == zoneName {
return zone.APIEndpoint, nil
}

zone, err := resp.FindZone(string(zoneName))
if err != nil {
return "", fmt.Errorf("get zone api endpoint: %w", err)
}

return "", fmt.Errorf("get zone api endpoint: zone name %s not found", zoneName)
return zone.APIEndpoint, nil
}

// Client represents an Exoscale API client.
Expand Down
24 changes: 19 additions & 5 deletions v3/generator/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,24 @@ func renderRequestParametersSchema(name string, op *v3.Operation) ([]byte, error
}

const findableTemplate = `
// Find{{ .TypeName }} attempts to find an {{ .TypeName }} by name or ID.
func (l {{ .ListTypeName }}) Find{{ .TypeName }}(nameOrID string) ({{ .TypeName }}, error) {
// Find{{ .TypeName }} attempts to find an {{ .TypeName }} by {{ .ParamName }}.
func (l {{ .ListTypeName }}) Find{{ .TypeName }}({{ .ParamName }} string) ({{ .TypeName }}, error) {
for i, elem := range l.{{ .ListFieldName }} {
if elem.Name == nameOrID || elem.ID.String() == nameOrID {
if {{ .Condition }} {
return l.{{ .ListFieldName }}[i], nil
}
}

return {{ .TypeName }}{}, fmt.Errorf("%q not found in {{ .ListTypeName }}: %w", nameOrID, ErrNotFound)
return {{ .TypeName }}{}, fmt.Errorf("%q not found in {{ .ListTypeName }}: %w", {{ .ParamName }}, ErrNotFound)
}
`

type Findable struct {
ParamName string
TypeName string
ListTypeName string
ListFieldName string
Condition string
}

// renderFindable renders a find method on listable resource.
Expand Down Expand Up @@ -343,16 +345,28 @@ func renderFindable(funcName string, s *base.SchemaProxy) ([]byte, error) {
}
_, hasName := item.Properties.Get("name")
_, hasID := item.Properties.Get("id")
if hasName && hasID {
if hasName || hasID {
output := bytes.NewBuffer([]byte{})
t, err := template.New("Findable").Parse(findableTemplate)
if err != nil {
return nil, err
}
paramName := "nameOrID"
condition := "string(elem.Name) == nameOrID || elem.ID.String() == nameOrID"
if !hasID {
paramName = "name"
condition = "string(elem.Name) == " + paramName
}
if !hasName {
paramName = "ID"
condition = "elem.ID.String() == " + paramName
}
if err := t.Execute(output, Findable{
ListTypeName: funcName + "Response",
ListFieldName: helpers.ToCamel(propName),
TypeName: typeName,
Condition: condition,
ParamName: paramName,
}); err != nil {
return nil, err
}
Expand Down
Loading
Loading