Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
databus23 committed Nov 7, 2017
1 parent 75b6143 commit 95b1192
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 46 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -64,7 +64,8 @@ gh-pages:
pkg/api/rest/operations/kubernikus_api.go: swagger.yml
ifneq (,$(wildcard $(SWAGGER_BIN)))
$(SWAGGER_BIN) generate server --name kubernikus --target pkg/api --model-package models \
--server-package rest --flag-strategy pflag --principal models.Principal --exclude-main
--server-package rest --flag-strategy pflag --principal models.Principal --exclude-main \
--template-dir pkg/api/templates
else
$(warning WARNING: $(SWAGGER_BIN) missing. Run `make bootstrap` to fix.)
endif
Expand Down
14 changes: 1 addition & 13 deletions pkg/api/client/operations/terminate_cluster_responses.go

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

5 changes: 1 addition & 4 deletions pkg/api/rest/embedded_spec.go

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

5 changes: 0 additions & 5 deletions pkg/api/rest/operations/terminate_cluster.go

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

23 changes: 2 additions & 21 deletions pkg/api/rest/operations/terminate_cluster_responses.go

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

195 changes: 195 additions & 0 deletions pkg/api/templates/server/responses.gotmpl
@@ -0,0 +1,195 @@
{{ define "serverheaderbuilder" }}
{{ if not .IsArray }}{{ template "simpleserverheaderbuilder" . }}{{ else }}{{ template "sliceserverheaderbuilder" . }}{{ end }}
{{- end }}
{{ define "simpleserverheaderbuilder" }}
{{ if .IsNullable -}}
var {{ varname .ID }} string
if {{ .ReceiverName }}.{{ pascalize .ID }} != nil {
{{ varname .ID }} = {{ if .Formatter }}{{ .Formatter }}(*{{ .ReceiverName }}.{{ pascalize .ID }}){{ else }}{{ if not .IsCustomFormatter }}*{{ end }}{{ .ReceiverName }}.{{ pascalize .ID }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}
}
{{ else }}{{ varname .ID }} := {{ if .Formatter }}{{ .Formatter }}({{ .ReceiverName }}.{{ pascalize .ID }}){{ else }}{{ .ReceiverName }}.{{ pascalize .ID }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}
{{ end -}}
if {{ varname .ID }} != "" {
rw.Header().Set({{ printf "%q" .Name }}, {{ varname .ID }})
}
{{ end }}
{{ define "sliceitemserverheaderbuilder" }}
{{ if .IsNullable -}}
var {{ .ValueExpression }}S string
if {{ .ValueExpression }} != nil {
{{ .ValueExpression }}S = {{ if .Formatter }}{{ .Formatter }}(*{{ .ValueExpression }}){{ else }}*{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}
}
{{ else -}}
{{ .ValueExpression }}S := {{ if .Formatter }}{{ .Formatter }}({{ .ValueExpression }}){{ else }}{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}
{{ end -}}
if {{ .ValueExpression }}S != "" {
{{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}S)
}
{{ end }}
{{define "sliceserverheaderbuilder" }}
var {{ varname .Child.ValueExpression }}R []string
for _, {{ varname .Child.ValueExpression }} := range {{ .ValueExpression }} {
{{- if not .Child.IsArray }}{{ template "sliceitemserverheaderbuilder" .Child }}{{ else }}{{ template "sliceserverheaderbuilder" .Child }}{{ end -}}
}

{{ if not .Child.Parent -}}
{{ varname .ID }} := swag.JoinByFormat({{ varname .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
if len({{ varname .ID }}) > 0 {
hv := {{ varname .ID }}[0]
if hv != "" {
rw.Header().Set({{ printf "%q" .Name }}, hv)
}
}
{{ else -}}
{{ .ValueExpression }}S := swag.JoinByFormat({{ varname .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
if len({{ .ValueExpression }}S) > 0 {
{{ .ValueExpression }}Ss := {{ .ValueExpression }}S[0]
if {{ .ValueExpression }}Ss != "" {
{{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}Ss)
}
}
{{ end -}}
{{ end -}}
{{ define "serverresponse" }}
{{ if ne .Code -1 }}// {{pascalize .Name}}Code is the HTTP code returned for type {{ pascalize .Name}}
const {{ pascalize .Name}}Code int = {{ .Code }}{{ end }}

/*{{ if .Description }}{{ pascalize .Name }} {{ .Description }}{{else}}{{ pascalize .Name }} {{ humanize .Name }}{{end}}

swagger:response {{ camelize .Name }}
*/
type {{ pascalize .Name }} struct {
{{ if eq .Code -1 }}
_statusCode int
{{ end }}{{ range .Headers }}/*{{if .Description }}{{ .Description }}{{ end }}{{ if .Required }}
Required: true{{ end }}{{ if .Maximum }}
Maximum: {{ if .ExclusiveMaximum }}< {{ end }}{{ .Maximum }}{{ end }}{{ if .Minimum }}
Minimum: {{ if .ExclusiveMinimum }}> {{ end }}{{ .Minimum }}{{ end }}{{ if .MultipleOf }}
Multiple Of: {{ .MultipleOf }}{{ end }}{{ if .MaxLength }}
Max Length: {{ .MaxLength }}{{ end }}{{ if .MinLength }}
Min Length: {{ .MinLength }}{{ end }}{{ if .Pattern }}
Pattern: {{ .Pattern }}{{ end }}{{ if .MaxItems }}
Max Items: {{ .MaxItems }}{{ end }}{{ if .MinItems }}
Min Items: {{ .MinItems }}{{ end }}{{ if .UniqueItems }}
Unique: true{{ end }}{{ if .HasDefault }}
Default: {{ printf "%#v" .Default }}{{ end }}
*/
{{ pascalize .Name }} {{ .GoType }} `json:"{{.Name}}{{ if not .Required }},omitempty{{ end }}"`
{{ end }}
{{ if .Schema }}{{ with .Schema }}
/*{{if .Description }}{{ .Description }}{{ end }}{{ if .Maximum }}
Maximum: {{ if .ExclusiveMaximum }}< {{ end }}{{ .Maximum }}{{ end }}{{ if .Minimum }}
Minimum: {{ if .ExclusiveMinimum }}> {{ end }}{{ .Minimum }}{{ end }}{{ if .MultipleOf }}
Multiple Of: {{ .MultipleOf }}{{ end }}{{ if .MaxLength }}
Max Length: {{ .MaxLength }}{{ end }}{{ if .MinLength }}
Min Length: {{ .MinLength }}{{ end }}{{ if .Pattern }}
Pattern: {{ .Pattern }}{{ end }}{{ if .MaxItems }}
Max Items: {{ .MaxItems }}{{ end }}{{ if .MinItems }}
Min Items: {{ .MinItems }}{{ end }}{{ if .UniqueItems }}
Unique: true{{ end }}
In: Body
*/{{ end }}
Payload {{ if and (not .Schema.IsBaseType) .Schema.IsComplexObject }}*{{ end }}{{ .Schema.GoType }} `json:"body,omitempty"`
{{ end }}
}

// New{{ pascalize .Name }} creates {{ pascalize .Name }} with default headers values
func New{{ pascalize .Name }}({{ if eq .Code -1 }}code int{{ end }}) *{{ pascalize .Name }} { {{ if eq .Code -1 }}
if code <= 0 {
code = 500
}
{{ end }}
return &{{ pascalize .Name }}{
{{ if eq .Code -1 }}_statusCode: code,
{{ end }}{{ range .Headers }}{{ if .HasDefault }}{{ pascalize .Name }}: {{ printf "%#v" .Default }},{{ end }}
{{ end }}}
}

{{ if eq .Code -1 }}
// WithStatusCode adds the status to the {{ humanize .Name }} response
func ({{ .ReceiverName }} *{{ pascalize .Name }}) WithStatusCode(code int) *{{ pascalize .Name }} {
{{ .ReceiverName }}._statusCode = code
return {{ .ReceiverName }}
}

// SetStatusCode sets the status to the {{ humanize .Name }} response
func ({{ .ReceiverName }} *{{ pascalize .Name }}) SetStatusCode(code int) {
{{ .ReceiverName }}._statusCode = code
}
{{ end }}{{ range .Headers }}
// With{{ pascalize .Name }} adds the {{ camelize .Name }} to the {{ humanize $.Name }} response
func ({{ $.ReceiverName }} *{{ pascalize $.Name }}) With{{ pascalize .Name }}({{ varname .Name }} {{ .GoType}}) *{{ pascalize $.Name }} {
{{ $.ReceiverName }}.{{ pascalize .Name }} = {{ varname .Name }}
return {{ .ReceiverName }}
}

// Set{{ pascalize .Name }} sets the {{ camelize .Name }} to the {{ humanize $.Name }} response
func ({{ $.ReceiverName }} *{{ pascalize $.Name }}) Set{{ pascalize .Name }}({{ varname .Name }} {{ .GoType}}) {
{{ $.ReceiverName }}.{{ pascalize .Name }} = {{ varname .Name }}
}
{{ end }}{{ if .Schema }}
// WithPayload adds the payload to the {{ humanize .Name }} response
func ({{ .ReceiverName }} *{{ pascalize .Name }}) WithPayload(payload {{ if and .Schema.IsComplexObject (not .Schema.IsBaseType) }}*{{ end }}{{ .Schema.GoType }}) *{{ pascalize .Name }} {
{{ .ReceiverName }}.Payload = payload
return {{ .ReceiverName }}
}

// SetPayload sets the payload to the {{ humanize .Name }} response
func ({{ .ReceiverName }} *{{ pascalize .Name }}) SetPayload(payload {{ if and .Schema.IsComplexObject (not .Schema.IsBaseType) }}*{{ end }}{{ .Schema.GoType }}) {
{{ .ReceiverName }}.Payload = payload
}
{{ end }}

// WriteResponse to the client
func ({{ .ReceiverName }} *{{ pascalize .Name }}) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) {
{{ range .Headers }}
// response header {{.Name}}
{{ template "serverheaderbuilder" . -}}
{{ end }}
{{ if not .Schema }}
rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses
{{ end }}
rw.WriteHeader({{ if eq .Code -1 }}{{ .ReceiverName }}._statusCode{{ else }}{{ .Code }}{{ end }}){{ if .Schema }}{{ if .Schema.IsComplexObject }}
if {{ .ReceiverName }}.Payload != nil { {{ end }}
payload := {{ .ReceiverName }}.Payload{{ if .Schema.IsArray }}
if payload == nil {
payload = {{ .Schema.Zero }}
}
{{ end }}
if err := producer.Produce(rw, payload); err != nil {
panic(err) // let the recovery middleware deal with this
}
{{ if .Schema.IsComplexObject }} } {{ end }}{{ end }}
}
{{ end }}// Code generated by go-swagger; DO NOT EDIT.


{{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}


package {{ .Package }}

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command


import (
"net/http"
"github.com/go-openapi/swag"
"github.com/go-openapi/errors"
"github.com/go-openapi/validate"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/security"

{{ range .DefaultImports }}{{ printf "%q" .}}
{{ end }}
{{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }}
{{ end }}
)

{{ range .Responses }}
{{ template "serverresponse" . }}
{{ end }}
{{ if .DefaultResponse }}
{{ template "serverresponse" .DefaultResponse }}
{{ end }}
2 changes: 0 additions & 2 deletions swagger.yml
Expand Up @@ -95,8 +95,6 @@ paths:
responses:
'202':
description: OK
schema:
type: object
default:
$ref: '#/responses/errorResponse'
security:
Expand Down

0 comments on commit 95b1192

Please sign in to comment.