github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/generator/templates/server/responses.gotmpl (about) 1 {{ define "serverheaderbuilder" }} 2 {{ if not .IsArray }}{{ template "simpleserverheaderbuilder" . }}{{ else }}{{ template "sliceserverheaderbuilder" . }}{{ end }} 3 {{- end }} 4 {{ define "simpleserverheaderbuilder" }} 5 {{ if .IsNullable -}} 6 var {{ varname .ID }} string 7 if {{ .ReceiverName }}.{{ pascalize .ID }} != nil { 8 {{ varname .ID }} = {{ if .Formatter }}{{ .Formatter }}(*{{ .ReceiverName }}.{{ pascalize .ID }}){{ else }}{{ if not .IsCustomFormatter }}*{{ end }}{{ .ReceiverName }}.{{ pascalize .ID }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 9 } 10 {{ else }}{{ varname .ID }} := {{ if .Formatter }}{{ .Formatter }}({{ .ReceiverName }}.{{ pascalize .ID }}){{ else }}{{ .ReceiverName }}.{{ pascalize .ID }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 11 {{ end -}} 12 if {{ varname .ID }} != "" { 13 rw.Header().Set({{ printf "%q" .Name }}, {{ varname .ID }}) 14 } 15 {{ end }} 16 {{ define "sliceitemserverheaderbuilder" }} 17 {{ if .IsNullable -}} 18 var {{ .ValueExpression }}S string 19 if {{ .ValueExpression }} != nil { 20 {{ .ValueExpression }}S = {{ if .Formatter }}{{ .Formatter }}(*{{ .ValueExpression }}){{ else }}*{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 21 } 22 {{ else -}} 23 {{ .ValueExpression }}S := {{ if .Formatter }}{{ .Formatter }}({{ .ValueExpression }}){{ else }}{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 24 {{ end -}} 25 if {{ .ValueExpression }}S != "" { 26 {{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}S) 27 } 28 {{ end }} 29 {{define "sliceserverheaderbuilder" }} 30 var {{ varname .Child.ValueExpression }}R []string 31 for _, {{ varname .Child.ValueExpression }} := range {{ .ValueExpression }} { 32 {{- if not .Child.IsArray }}{{ template "sliceitemserverheaderbuilder" .Child }}{{ else }}{{ template "sliceserverheaderbuilder" .Child }}{{ end -}} 33 } 34 {{ if not .Child.Parent -}} 35 {{ varname .ID }} := swag.JoinByFormat({{ varname .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }}) 36 if len({{ varname .ID }}) > 0 { 37 hv := {{ varname .ID }}[0] 38 if hv != "" { 39 rw.Header().Set({{ printf "%q" .Name }}, hv) 40 } 41 } 42 {{ else -}} 43 {{ .ValueExpression }}S := swag.JoinByFormat({{ varname .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }}) 44 if len({{ .ValueExpression }}S) > 0 { 45 {{ .ValueExpression }}Ss := {{ .ValueExpression }}S[0] 46 if {{ .ValueExpression }}Ss != "" { 47 {{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}Ss) 48 } 49 } 50 {{ end -}} 51 {{ end -}} 52 {{ define "serverresponse" }} 53 {{ if ne .Code -1 }}// {{pascalize .Name}}Code is the HTTP code returned for type {{ pascalize .Name}} 54 const {{ pascalize .Name}}Code int = {{ .Code }}{{ end }} 55 56 /*{{ if .Description }}{{ pascalize .Name }} {{ blockcomment .Description }}{{else}}{{ pascalize .Name }} {{ humanize .Name }}{{end}} 57 58 swagger:response {{ camelize .Name }} 59 */ 60 type {{ pascalize .Name }} struct { 61 {{ if eq .Code -1 }} 62 _statusCode int 63 {{ end }}{{ range .Headers }}/*{{if .Description }}{{ blockcomment .Description }}{{ end }} 64 {{ if .Maximum }} 65 Maximum: {{ if .ExclusiveMaximum }}< {{ end }}{{ .Maximum }}{{ end }}{{ if .Minimum }} 66 Minimum: {{ if .ExclusiveMinimum }}> {{ end }}{{ .Minimum }}{{ end }}{{ if .MultipleOf }} 67 Multiple Of: {{ .MultipleOf }}{{ end }}{{ if .MaxLength }} 68 Max Length: {{ .MaxLength }}{{ end }}{{ if .MinLength }} 69 Min Length: {{ .MinLength }}{{ end }}{{ if .Pattern }} 70 Pattern: {{ .Pattern }}{{ end }}{{ if .MaxItems }} 71 Max Items: {{ .MaxItems }}{{ end }}{{ if .MinItems }} 72 Min Items: {{ .MinItems }}{{ end }}{{ if .UniqueItems }} 73 Unique: true{{ end }}{{ if .HasDefault }} 74 Default: {{ printf "%#v" .Default }}{{ end }} 75 */ 76 {{ pascalize .Name }} {{ .GoType }} `json:"{{.Name}}{{ if not .Required }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"` 77 {{ end }} 78 {{ if .Schema }}{{ with .Schema }} 79 /*{{if .Description }}{{ blockcomment .Description }}{{ end }}{{ if .Maximum }} 80 Maximum: {{ if .ExclusiveMaximum }}< {{ end }}{{ .Maximum }}{{ end }}{{ if .Minimum }} 81 Minimum: {{ if .ExclusiveMinimum }}> {{ end }}{{ .Minimum }}{{ end }}{{ if .MultipleOf }} 82 Multiple Of: {{ .MultipleOf }}{{ end }}{{ if .MaxLength }} 83 Max Length: {{ .MaxLength }}{{ end }}{{ if .MinLength }} 84 Min Length: {{ .MinLength }}{{ end }}{{ if .Pattern }} 85 Pattern: {{ .Pattern }}{{ end }}{{ if .MaxItems }} 86 Max Items: {{ .MaxItems }}{{ end }}{{ if .MinItems }} 87 Min Items: {{ .MinItems }}{{ end }}{{ if .UniqueItems }} 88 Unique: true{{ end }} 89 In: Body 90 */{{ end }} 91 Payload {{ if and (not .Schema.IsBaseType) .Schema.IsComplexObject }}*{{ end }}{{ .Schema.GoType }} `json:"body,omitempty"` 92 {{ end }} 93 } 94 95 // New{{ pascalize .Name }} creates {{ pascalize .Name }} with default headers values 96 func New{{ pascalize .Name }}({{ if eq .Code -1 }}code int{{ end }}) *{{ pascalize .Name }} { {{ if eq .Code -1 }} 97 if code <= 0 { 98 code = 500 99 } 100 {{ end }} 101 {{ if .Headers.HasSomeDefaults }} 102 var ( 103 // initialize headers with default values 104 {{ range .Headers }} 105 {{ if .HasDefault -}} 106 {{ varname .ID}}Default = 107 {{- if and .IsPrimitive .IsCustomFormatter (not (stringContains .Zero "(\"" )) }}{{ .Zero }}{{/* strfmt type initializer requires UnmarshalText(), e.g. Date, Datetime, Duration */}} 108 {{- else if and .IsPrimitive .IsCustomFormatter (stringContains .Zero "(\"" ) }}{{.GoType}}({{- printf "%#v" .Default }}){{/* strfmt type initializer takes string */}} 109 {{- else if and .IsPrimitive (not .IsCustomFormatter) -}}{{.GoType}}({{- printf "%#v" .Default }}){{/* regular go primitive type initializer */}} 110 {{- else if .IsArray -}}{{- /* Do not initialize from possible defaults in nested arrays */ -}} 111 {{- if and .Child.IsPrimitive .Child.IsCustomFormatter }}{{ .Zero }}{{/* initialization strategy with UnmarshalText() */}} 112 {{- else if .Child.IsArray -}}{{ .Zero }}{{/* initialization strategy with json.Unmarshal() */}} 113 {{- else if and .Child.IsPrimitive (not .Child.IsCustomFormatter) -}}{{.GoType}}{{- arrayInitializer .Default }}{{/* regular go primitive type initializer: simple slice initializer */}} 114 {{- else }}{{ printf "%#v" .Default }}{{/* all other cases (e.g. schema) [should not occur] */}} 115 {{- end }} 116 {{- else }}{{ printf "%#v" .Default }}{{/* case .Schema */}} 117 {{- end }} 118 {{- end }} 119 {{- end }} 120 ) 121 122 {{ range .Headers }}{{ if .HasDefault -}}{{- /* carry out UnmarshalText initialization strategy */ -}} 123 {{ if and .IsPrimitive .IsCustomFormatter (not (stringContains .Zero "(\"")) }}{{ varname .ID}}Default.UnmarshalText([]byte({{ printf "%q" .Default }})) 124 {{ else if .IsArray -}} 125 {{ if or ( and .Child.IsPrimitive .Child.IsCustomFormatter ) .Child.IsArray -}} 126 if err := json.Unmarshal([]byte(`{{printf "%s" (json .Default)}}`), &{{ varname .ID }}Default); err != nil { 127 // panics if specification is invalid 128 msg := fmt.Sprintf("invalid default value for header {{ varname .ID }}: %v",err) 129 panic(msg) 130 } 131 {{ end -}} 132 {{- end }} 133 {{- end }} 134 {{- end }} 135 {{ end }} 136 return &{{ pascalize .Name }}{ 137 {{ if eq .Code -1 }}_statusCode: code,{{ end }} 138 {{ range .Headers }}{{ if .HasDefault }} 139 {{ pascalize .Name}}: {{ if and (not .IsArray) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) .IsNullable }}&{{ end }}{{ varname .ID }}Default, 140 {{ end }} 141 {{ end -}} 142 } 143 } 144 145 {{ if eq .Code -1 }} 146 // WithStatusCode adds the status to the {{ humanize .Name }} response 147 func ({{ .ReceiverName }} *{{ pascalize .Name }}) WithStatusCode(code int) *{{ pascalize .Name }} { 148 {{ .ReceiverName }}._statusCode = code 149 return {{ .ReceiverName }} 150 } 151 152 // SetStatusCode sets the status to the {{ humanize .Name }} response 153 func ({{ .ReceiverName }} *{{ pascalize .Name }}) SetStatusCode(code int) { 154 {{ .ReceiverName }}._statusCode = code 155 } 156 {{ end }}{{ range .Headers }} 157 // With{{ pascalize .Name }} adds the {{ camelize .Name }} to the {{ humanize $.Name }} response 158 func ({{ $.ReceiverName }} *{{ pascalize $.Name }}) With{{ pascalize .Name }}({{ varname .Name }} {{ .GoType}}) *{{ pascalize $.Name }} { 159 {{ $.ReceiverName }}.{{ pascalize .Name }} = {{ varname .Name }} 160 return {{ .ReceiverName }} 161 } 162 163 // Set{{ pascalize .Name }} sets the {{ camelize .Name }} to the {{ humanize $.Name }} response 164 func ({{ $.ReceiverName }} *{{ pascalize $.Name }}) Set{{ pascalize .Name }}({{ varname .Name }} {{ .GoType}}) { 165 {{ $.ReceiverName }}.{{ pascalize .Name }} = {{ varname .Name }} 166 } 167 {{ end }}{{ if .Schema }} 168 // WithPayload adds the payload to the {{ humanize .Name }} response 169 func ({{ .ReceiverName }} *{{ pascalize .Name }}) WithPayload(payload {{ if and .Schema.IsComplexObject (not .Schema.IsBaseType) }}*{{ end }}{{ .Schema.GoType }}) *{{ pascalize .Name }} { 170 {{ .ReceiverName }}.Payload = payload 171 return {{ .ReceiverName }} 172 } 173 174 // SetPayload sets the payload to the {{ humanize .Name }} response 175 func ({{ .ReceiverName }} *{{ pascalize .Name }}) SetPayload(payload {{ if and .Schema.IsComplexObject (not .Schema.IsBaseType) }}*{{ end }}{{ .Schema.GoType }}) { 176 {{ .ReceiverName }}.Payload = payload 177 } 178 {{ end }} 179 180 // WriteResponse to the client 181 func ({{ .ReceiverName }} *{{ pascalize .Name }}) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { 182 {{ range .Headers }} 183 // response header {{.Name}} 184 {{ template "serverheaderbuilder" . -}} 185 {{ end }} 186 {{ if not .Schema }} 187 rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses 188 {{ end }} 189 rw.WriteHeader({{ if eq .Code -1 }}{{ .ReceiverName }}._statusCode{{ else }}{{ .Code }}{{ end }}) 190 {{- if .Schema }} 191 {{- if .Schema.IsComplexObject }} 192 if {{ .ReceiverName }}.Payload != nil { 193 {{- end }} 194 payload := {{ .ReceiverName }}.Payload 195 {{- if and (not .Schema.IsInterface) (or .Schema.IsArray .Schema.IsMap) }} 196 if payload == nil { 197 // return empty {{ if .Schema.IsArray }}array{{ else if .Schema.IsMap }}map{{ end }} 198 payload = 199 {{- if or .Schema.IsAliased .Schema.IsComplexObject }} 200 {{- if and (not .Schema.IsBaseType) .Schema.IsComplexObject }}&{{ end }}{{ .Schema.GoType -}} {} 201 {{- else }} 202 {{- .Schema.Zero }} 203 {{- end }} 204 } 205 {{ end }} 206 if err := producer.Produce(rw, payload); err != nil { 207 panic(err) // let the recovery middleware deal with this 208 } 209 {{- if .Schema.IsComplexObject }} 210 } 211 {{- end }} 212 {{- end }} 213 } 214 215 {{ if $.StrictResponders }} 216 func ({{ .ReceiverName }} *{{ pascalize .Name }}) {{ pascalize .OperationName }}Responder() {} 217 {{- end }} 218 {{ end }}// Code generated by go-swagger; DO NOT EDIT. 219 220 221 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 222 223 224 package {{ .Package }} 225 226 // This file was generated by the swagger tool. 227 // Editing this file might prove futile when you re-run the swagger generate command 228 229 230 import ( 231 "fmt" 232 "net/http" 233 234 "github.com/go-openapi/errors" 235 "github.com/go-openapi/runtime" 236 "github.com/go-openapi/runtime/security" 237 "github.com/go-openapi/swag" 238 "github.com/go-openapi/validate" 239 "github.com/go-openapi/runtime/middleware" 240 241 {{ imports .DefaultImports }} 242 {{ imports .Imports }} 243 ) 244 245 {{ range .Responses }} 246 {{ template "serverresponse" . }} 247 {{ end }} 248 {{ if .DefaultResponse }} 249 {{ template "serverresponse" .DefaultResponse }} 250 {{ end }} 251 252 {{ if $.StrictResponders }} 253 type {{ pascalize .Name }}NotImplementedResponder struct { 254 middleware.Responder 255 } 256 257 func (*{{ pascalize .Name }}NotImplementedResponder) {{ pascalize .Name }}Responder() {} 258 259 func {{ pascalize .Name }}NotImplemented() {{ pascalize .Name }}Responder { 260 return &{{ pascalize .Name }}NotImplementedResponder{ 261 middleware.NotImplemented( 262 "operation authentication.{{ pascalize .Name }} has not yet been implemented", 263 ), 264 } 265 } 266 267 type {{ pascalize .Name }}Responder interface { 268 middleware.Responder 269 {{ pascalize .Name }}Responder() 270 } 271 {{ end }}