Skip to content

Commit

Permalink
Add ability to generate collection actions
Browse files Browse the repository at this point in the history
Problem:
Collections have actions but are not accessable through the types
clients

Solution:
Update template and generator to output actions living on collections
  • Loading branch information
dramich authored and ibuildthecloud committed Mar 31, 2018
1 parent e9373e3 commit 7edec77
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
22 changes: 19 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ func getResourceActions(schema *types.Schema, schemas *types.Schemas) map[string
return result
}

func getCollectionActions(schema *types.Schema, schemas *types.Schemas) map[string]types.Action {
result := map[string]types.Action{}
for name, action := range schema.CollectionActions {
if action.Output != "" {
output := strings.TrimSuffix(action.Output, "Collection")
if schemas.Schema(&schema.Version, output) != nil {
result[name] = action
}
} else {
result[name] = action
}
}
return result
}

func generateType(outputDir string, schema *types.Schema, schemas *types.Schemas) error {
filePath := strings.ToLower("zz_generated_" + addUnderscore(schema.ID) + ".go")
output, err := os.Create(path.Join(outputDir, filePath))
Expand All @@ -138,9 +153,10 @@ func generateType(outputDir string, schema *types.Schema, schemas *types.Schemas
}

return typeTemplate.Execute(output, map[string]interface{}{
"schema": schema,
"structFields": getTypeMap(schema, schemas),
"resourceActions": getResourceActions(schema, schemas),
"schema": schema,
"structFields": getTypeMap(schema, schemas),
"resourceActions": getResourceActions(schema, schemas),
"collectionActions": getCollectionActions(schema, schemas),
})
}

Expand Down
48 changes: 42 additions & 6 deletions generator/type_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ type {{.schema.CodeName}}Operations interface {
Create(opts *{{.schema.CodeName}}) (*{{.schema.CodeName}}, error)
Update(existing *{{.schema.CodeName}}, updates interface{}) (*{{.schema.CodeName}}, error)
ByID(id string) (*{{.schema.CodeName}}, error)
Delete(container *{{.schema.CodeName}}) error{{range $key, $value := .resourceActions}}
{{if eq $value.Input "" }}
Action{{$key | capitalize}} (*{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error)
{{else}}
Action{{$key | capitalize}} (*{{$.schema.CodeName}}, *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error)
{{end}}{{end}}
Delete(container *{{.schema.CodeName}}) error
{{range $key, $value := .resourceActions}}
{{if eq $value.Input "" }}
Action{{$key | capitalize}} (*{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error)
{{else}}
Action{{$key | capitalize}} (*{{$.schema.CodeName}}, *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error)
{{end}}
{{end}}
{{range $key, $value := .collectionActions}}
{{if (and (eq $value.Input "") (eq $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (error)
{{else if (and (eq $value.Input "") (ne $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error)
{{else if (and (ne $value.Input "") (eq $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (error)
{{else}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error)
{{end}}
{{end}}
}
func new{{.schema.CodeName}}Client(apiClient *Client) *{{.schema.CodeName}}Client {
Expand Down Expand Up @@ -109,4 +122,27 @@ func (c *{{.schema.CodeName}}Client) Delete(container *{{.schema.CodeName}}) err
return resp, err
}
{{end}}
{{range $key, $value := .collectionActions}}
{{if (and (eq $value.Input "") (eq $value.Output ""))}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (error) {
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, nil)
return err
{{else if (and (eq $value.Input "") (ne $value.Output ""))}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error) {
resp := &{{.Output | capitalize}}{}
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, resp)
return resp, err
{{else if (and (ne $value.Input "") (eq $value.Output ""))}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (error) {
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, nil)
return err
{{else}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error) {
resp := &{{.Output | capitalize}}{}
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, resp)
return resp, err
{{end}}
}
{{end}}
{{end}}`

0 comments on commit 7edec77

Please sign in to comment.