1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-17 07:40:10 +00:00

Update generator to handle collection as an output

This commit is contained in:
Dan Ramich
2018-04-02 11:27:44 -07:00
committed by Craig Jellick
parent 5c92744615
commit 9f2b71df50
3 changed files with 24 additions and 13 deletions

View File

@@ -11,12 +11,13 @@ import (
func funcs() template.FuncMap { func funcs() template.FuncMap {
return template.FuncMap{ return template.FuncMap{
"capitalize": convert.Capitalize, "capitalize": convert.Capitalize,
"unCapitalize": convert.Uncapitalize, "unCapitalize": convert.Uncapitalize,
"upper": strings.ToUpper, "upper": strings.ToUpper,
"toLower": strings.ToLower, "toLower": strings.ToLower,
"hasGet": hasGet, "hasGet": hasGet,
"hasPost": hasPost, "hasPost": hasPost,
"getCollectionOutput": getCollectionOutput,
} }
} }
@@ -40,3 +41,10 @@ func contains(list []string, needle string) bool {
} }
return false return false
} }
func getCollectionOutput(output, codeName string) string {
if output == "collection" {
return codeName + "Collection"
}
return convert.Capitalize(output)
}

View File

@@ -126,7 +126,10 @@ func getCollectionActions(schema *types.Schema, schemas *types.Schemas) map[stri
result := map[string]types.Action{} result := map[string]types.Action{}
for name, action := range schema.CollectionActions { for name, action := range schema.CollectionActions {
if action.Output != "" { if action.Output != "" {
output := strings.TrimSuffix(action.Output, "Collection") output := action.Output
if action.Output == "collection" {
output = strings.ToLower(schema.CodeName)
}
if schemas.Schema(&schema.Version, output) != nil { if schemas.Schema(&schema.Version, output) != nil {
result[name] = action result[name] = action
} }

View File

@@ -53,11 +53,11 @@ type {{.schema.CodeName}}Operations interface {
{{if (and (eq $value.Input "") (eq $value.Output ""))}} {{if (and (eq $value.Input "") (eq $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (error) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (error)
{{else if (and (eq $value.Input "") (ne $value.Output ""))}} {{else if (and (eq $value.Input "") (ne $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{getCollectionOutput $value.Output $.schema.CodeName}}, error)
{{else if (and (ne $value.Input "") (eq $value.Output ""))}} {{else if (and (ne $value.Input "") (eq $value.Output ""))}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (error) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (error)
{{else}} {{else}}
Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{getCollectionOutput $value.Output $.schema.CodeName}}, error)
{{end}} {{end}}
{{end}} {{end}}
} }
@@ -129,8 +129,8 @@ func (c *{{.schema.CodeName}}Client) Delete(container *{{.schema.CodeName}}) err
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, nil) err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, nil)
return err return err
{{else if (and (eq $value.Input "") (ne $value.Output ""))}} {{else if (and (eq $value.Input "") (ne $value.Output ""))}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{.Output | capitalize}}, error) { func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}) (*{{getCollectionOutput $value.Output $.schema.CodeName}}, error) {
resp := &{{.Output | capitalize}}{} resp := &{{getCollectionOutput $value.Output $.schema.CodeName}}{}
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, resp) err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, nil, resp)
return resp, err return resp, err
{{else if (and (ne $value.Input "") (eq $value.Output ""))}} {{else if (and (ne $value.Input "") (eq $value.Output ""))}}
@@ -138,8 +138,8 @@ func (c *{{.schema.CodeName}}Client) Delete(container *{{.schema.CodeName}}) err
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, nil) err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, nil)
return err return err
{{else}} {{else}}
func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{.Output | capitalize}}, error) { func (c *{{$.schema.CodeName}}Client) Action{{$key | capitalize}} (resource *{{$.schema.CodeName}}, input *{{$value.Input | capitalize}}) (*{{getCollectionOutput $value.Output $.schema.CodeName}}, error) {
resp := &{{.Output | capitalize}}{} resp := &{{getCollectionOutput $value.Output $.schema.CodeName}}{}
err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, resp) err := c.apiClient.Ops.DoAction({{$.schema.CodeName}}Type, "{{$key}}", &resource.Resource, input, resp)
return resp, err return resp, err
{{end}} {{end}}