From 7edec776199d7aee6d52bb1b90dead26ee05f90b Mon Sep 17 00:00:00 2001 From: Dan Ramich Date: Fri, 30 Mar 2018 15:22:40 -0700 Subject: [PATCH] Add ability to generate collection actions Problem: Collections have actions but are not accessable through the types clients Solution: Update template and generator to output actions living on collections --- generator/generator.go | 22 ++++++++++++++--- generator/type_template.go | 48 +++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/generator/generator.go b/generator/generator.go index 412225135..b5d10614e 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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)) @@ -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), }) } diff --git a/generator/type_template.go b/generator/type_template.go index 7b28fadf0..b5a887df4 100644 --- a/generator/type_template.go +++ b/generator/type_template.go @@ -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 { @@ -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}}`