github.com/AngusLu/go-swagger@v0.28.0/generator/templates/client/parameter.gotmpl (about) 1 // Code generated by go-swagger; DO NOT EDIT. 2 3 4 {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }} 5 6 7 package {{ .Package }} 8 9 // This file was generated by the swagger tool. 10 // Editing this file might prove futile when you re-run the swagger generate command 11 12 import ( 13 "context" 14 "fmt" 15 "net/http" 16 "time" 17 18 "github.com/go-openapi/errors" 19 "github.com/go-openapi/runtime" 20 cr "github.com/go-openapi/runtime/client" 21 "github.com/go-openapi/strfmt" 22 "github.com/go-openapi/swag" 23 "github.com/go-openapi/validate" 24 25 {{ imports .DefaultImports }} 26 {{ imports .Imports }} 27 ) 28 29 // New{{ pascalize .Name }}Params creates a new {{ pascalize .Name }}Params object, 30 // with the default timeout for this client. 31 // 32 // Default values are not hydrated, since defaults are normally applied by the API server side. 33 // 34 // To enforce default values in parameter, use SetDefaults or WithDefaults. 35 func New{{ pascalize .Name }}Params() *{{ pascalize .Name }}Params { 36 return &{{ pascalize .Name}}Params{ 37 {{ camelize .TimeoutName }}: cr.DefaultTimeout, 38 } 39 } 40 41 // New{{ pascalize .Name }}ParamsWithTimeout creates a new {{ pascalize .Name }}Params object 42 // with the ability to set a timeout on a request. 43 func New{{ pascalize .Name }}ParamsWithTimeout(timeout time.Duration) *{{ pascalize .Name }}Params { 44 return &{{ pascalize .Name}}Params{ 45 {{ camelize .TimeoutName }}: timeout, 46 } 47 } 48 49 // New{{ pascalize .Name }}ParamsWithContext creates a new {{ pascalize .Name }}Params object 50 // with the ability to set a context for a request. 51 func New{{ pascalize .Name }}ParamsWithContext(ctx context.Context) *{{ pascalize .Name }}Params { 52 return &{{ pascalize .Name}}Params{ 53 Context: ctx, 54 } 55 } 56 57 // New{{ pascalize .Name }}ParamsWithHTTPClient creates a new {{ pascalize .Name }}Params object 58 // with the ability to set a custom HTTPClient for a request. 59 func New{{ pascalize .Name }}ParamsWithHTTPClient(client *http.Client) *{{ pascalize .Name }}Params { 60 return &{{ pascalize .Name}}Params{ 61 HTTPClient: client, 62 } 63 } 64 65 /* {{ pascalize .Name }}Params contains all the parameters to send to the API endpoint 66 for the {{ humanize .Name }} operation. 67 68 Typically these are written to a http.Request. 69 */ 70 type {{ pascalize .Name }}Params struct { 71 {{- range .Params }} 72 {{- if .Description }} 73 74 /* {{ pascalize .Name }}. 75 76 {{ blockcomment .Description }} 77 {{- if or .SwaggerFormat .Default }} 78 {{ print "" }} 79 {{- if .SwaggerFormat }} 80 Format: {{ .SwaggerFormat }} 81 {{- end }} 82 {{- if .Default }} 83 Default: {{ json .Default }} 84 {{- end }} 85 {{- end }} 86 */ 87 {{- else }} 88 89 // {{ pascalize .Name }}. 90 {{- if or .SwaggerFormat .Default }} 91 // 92 {{- if .SwaggerFormat }} 93 // Format: {{ .SwaggerFormat }} 94 {{- end }} 95 {{- if .Default }} 96 // Default: {{ json .Default }} 97 {{- end }} 98 {{- end }} 99 {{- end }} 100 {{ pascalize .ID }} {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (not .IsInterface) (not .IsStream) (or .IsNullable ) }}*{{ end }}{{ if not .IsFileParam }}{{ .GoType }}{{ else }}runtime.NamedReadCloser{{ end }} 101 {{- end }} 102 103 {{ camelize .TimeoutName }} time.Duration 104 Context context.Context 105 HTTPClient *http.Client 106 } 107 108 // WithDefaults hydrates default values in the {{ humanize .Name }} params (not the query body). 109 // 110 // All values with no default are reset to their zero value. 111 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WithDefaults() *{{ pascalize .Name }}Params { 112 {{ .ReceiverName }}.SetDefaults() 113 return {{ .ReceiverName }} 114 } 115 116 // SetDefaults hydrates default values in the {{ humanize .Name }} params (not the query body). 117 // 118 // All values with no default are reset to their zero value. 119 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) SetDefaults() { 120 {{- if .Params.HasSomeDefaults }} 121 var ( 122 {{- range .Params }} 123 {{- if .HasDefault }} 124 {{- if .IsFileParam }} 125 // NOTE: no default supported for file parameter {{ .ID }} 126 {{- else if .IsStream }} 127 // NOTE: no default supported for stream parameter {{ .ID }} 128 {{- else if not .IsBodyParam }} 129 {{ template "simpleschemaDefaultsvar" . }} 130 {{- end }} 131 {{- end }} 132 {{- end }} 133 ) 134 {{- range .Params }} 135 {{- if and .HasDefault (not .IsFileParam) (not .IsStream) (not .IsBodyParam) }} 136 {{ template "simpleschemaDefaultsinit" . }} 137 {{- end }} 138 {{- end }} 139 140 val := {{ pascalize .Name }}Params{ 141 {{- range .Params }} 142 {{- if and .HasDefault (not .IsFileParam) (not .IsStream) (not .IsBodyParam) }} 143 {{ pascalize .Name}}: {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (or .IsNullable ) }}&{{ end }}{{ varname .ID }}Default, 144 {{- end }} 145 {{- end }} 146 } 147 148 val.{{ camelize .TimeoutName }} = {{ .ReceiverName }}.{{ camelize .TimeoutName }} 149 val.Context = {{ .ReceiverName }}.Context 150 val.HTTPClient = {{ .ReceiverName }}.HTTPClient 151 *{{ .ReceiverName }} = val 152 {{- else }} 153 // no default values defined for this parameter 154 {{- end }} 155 } 156 157 // With{{ pascalize .TimeoutName }} adds the timeout to the {{ humanize .Name }} params 158 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) With{{ pascalize .TimeoutName }}(timeout time.Duration) *{{ pascalize .Name }}Params { 159 {{ .ReceiverName }}.Set{{ pascalize .TimeoutName }}(timeout) 160 return {{ .ReceiverName }} 161 } 162 163 // Set{{ pascalize .TimeoutName }} adds the timeout to the {{ humanize .Name }} params 164 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) Set{{ pascalize .TimeoutName }}(timeout time.Duration) { 165 {{ .ReceiverName }}.{{ camelize .TimeoutName }} = timeout 166 } 167 168 // WithContext adds the context to the {{ humanize .Name }} params 169 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WithContext(ctx context.Context) *{{ pascalize .Name }}Params { 170 {{ .ReceiverName }}.SetContext(ctx) 171 return {{ .ReceiverName }} 172 } 173 174 // SetContext adds the context to the {{ humanize .Name }} params 175 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) SetContext(ctx context.Context) { 176 {{ .ReceiverName }}.Context = ctx 177 } 178 179 // WithHTTPClient adds the HTTPClient to the {{ humanize .Name }} params 180 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WithHTTPClient(client *http.Client) *{{ pascalize .Name }}Params { 181 {{ .ReceiverName }}.SetHTTPClient(client) 182 return {{ .ReceiverName }} 183 } 184 185 // SetHTTPClient adds the HTTPClient to the {{ humanize .Name }} params 186 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) SetHTTPClient(client *http.Client) { 187 {{ .ReceiverName }}.HTTPClient = client 188 } 189 190 {{- range .Params }} 191 192 // With{{ pascalize .ID }} adds the {{ varname .Name }} to the {{ humanize $.Name }} params 193 func ({{ $.ReceiverName }} *{{ pascalize $.Name }}Params) With{{ pascalize .ID }}({{ varname .Name }} {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (not .IsStream) (or .IsNullable ) }}*{{ end }}{{ if not .IsFileParam }}{{ .GoType }}{{ else }}runtime.NamedReadCloser{{ end }}) *{{ pascalize $.Name }}Params { 194 {{ $.ReceiverName }}.Set{{ pascalize .ID }}({{ varname .Name }}) 195 return {{ .ReceiverName }} 196 } 197 198 // Set{{ pascalize .ID }} adds the {{ camelize .Name }} to the {{ humanize $.Name }} params 199 func ({{ $.ReceiverName }} *{{ pascalize $.Name }}Params) Set{{ pascalize .ID }}({{ varname .Name }} {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (not .IsStream) (or .IsNullable ) }}*{{ end }}{{ if not .IsFileParam }}{{ .GoType }}{{ else }}runtime.NamedReadCloser{{ end }}) { 200 {{ $.ReceiverName }}.{{ pascalize .ID }} = {{ varname .Name }} 201 } 202 {{- end }} 203 204 // WriteToRequest writes these params to a swagger request 205 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { 206 207 if err := r.SetTimeout({{ .ReceiverName }}.{{ camelize .TimeoutName }}); err != nil { 208 return err 209 } 210 var res []error 211 {{- range .Params }} 212 {{- if not (or .IsArray .IsMap .IsBodyParam) }} 213 {{- if and .IsNullable (not .AllowEmptyValue) }} 214 215 if {{ .ValueExpression }} != nil { 216 {{- end}} 217 218 {{- if .IsQueryParam }} 219 220 // query param {{ .Name }} 221 {{- if .IsNullable }} 222 var qr{{ pascalize .Name }} {{ .GoType }} 223 224 if {{ .ValueExpression }} != nil { 225 qr{{ pascalize .Name }} = *{{ .ValueExpression }} 226 } 227 {{- else }} 228 qr{{ pascalize .Name }} := {{ .ValueExpression }} 229 {{- end}} 230 q{{ pascalize .Name}} := {{ if .Formatter }}{{ .Formatter }}(qr{{ pascalize .Name }}){{ else }}qr{{ pascalize .Name }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 231 {{- if not .AllowEmptyValue }} 232 if q{{ pascalize .Name }} != "" { 233 {{- end }} 234 235 if err := r.SetQueryParam({{ printf "%q" .Name }}, q{{ pascalize .Name }}); err != nil { 236 return err 237 } 238 {{- if not .AllowEmptyValue }} 239 } 240 {{- end }} 241 242 {{- else if .IsPathParam }} 243 244 // path param {{ .Name }} 245 if err := r.SetPathParam({{ printf "%q" .Name }}, {{ if .Formatter }}{{ .Formatter }}({{ if .IsNullable }}*{{end}}{{ .ValueExpression }}){{ else }}{{ if and (not .IsCustomFormatter) .IsNullable }}*{{end}}{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}); err != nil { 246 return err 247 } 248 249 {{- else if .IsHeaderParam }} 250 251 // header param {{ .Name }} 252 if err := r.SetHeaderParam({{ printf "%q" .Name }}, {{ if .Formatter }}{{ .Formatter }}({{ if .IsNullable }}*{{end}}{{ .ValueExpression }}){{ else }}{{ if and (not .IsCustomFormatter) .IsNullable }}*{{end}}{{ .ValueExpression }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}); err != nil { 253 return err 254 } 255 {{- else if .IsFormParam }} 256 {{- if .IsFileParam }} 257 {{- if .IsNullable }} 258 259 if {{ .ValueExpression }} != nil { 260 {{- end }} 261 // form file param {{ .Name }} 262 if err := r.SetFileParam({{ printf "%q" .Name }}, {{ .ValueExpression }}); err != nil { 263 return err 264 } 265 {{- if .IsNullable}} 266 } 267 {{- end }} 268 {{- else }} 269 270 // form param {{ .Name }} 271 {{- if .IsNullable }} 272 var fr{{ pascalize .Name }} {{ .GoType }} 273 if {{ .ValueExpression }} != nil { 274 fr{{ pascalize .Name }} = *{{ .ValueExpression }} 275 } 276 {{- else }} 277 fr{{ pascalize .Name }} := {{ .ValueExpression }} 278 {{- end}} 279 f{{ pascalize .Name}} := {{ if .Formatter }}{{ .Formatter }}(fr{{ pascalize .Name }}){{ else }}fr{{ pascalize .Name }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}} 280 {{- if not .AllowEmptyValue }} 281 if f{{ pascalize .Name }} != "" { 282 {{- end }} 283 if err := r.SetFormParam({{ printf "%q" .Name }}, f{{ pascalize .Name }}); err != nil { 284 return err 285 } 286 {{- if not .AllowEmptyValue }} 287 } 288 {{- end }} 289 {{- end }} 290 {{- end }} 291 {{- if and .IsNullable (not .AllowEmptyValue) }} 292 } 293 {{- end }} 294 {{- else if .IsArray }} 295 296 {{- if not .IsBodyParam }} 297 298 if {{ .ValueExpression }} != nil { 299 300 {{- if .Child }}{{/* bind complex parameters (arrays and nested structures) */}} 301 302 // binding items for {{ .Name }} 303 joined{{ pascalize .Name }} := {{ .ReceiverName }}.bindParam{{ pascalize .Name }}(reg) 304 {{- else }} 305 values{{ pascalize .Name }} := {{ if and (not .IsArray) (not .IsStream) (not .IsMap) (.IsNullable) }}*{{end}}{{ .ValueExpression }} 306 joined{{ pascalize .Name}} := swag.JoinByFormat(values{{ pascalize .Name }}, "{{.CollectionFormat}}") 307 {{- end }} 308 309 {{- if .IsQueryParam }} 310 311 // query array param {{ .Name }} 312 if err := r.SetQueryParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}...); err != nil { 313 return err 314 } 315 316 {{- else if and .IsFormParam }} 317 318 // form array param {{ .Name }} 319 if err := r.SetFormParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}...); err != nil { 320 return err 321 } 322 323 {{- else if and .IsPathParam }} 324 325 // path array param {{ .Name }} 326 // SetPathParam does not support variadic arguments, since we used JoinByFormat 327 // we can send the first item in the array as it's all the items of the previous 328 // array joined together 329 if len(joined{{ pascalize .Name }}) > 0 { 330 if err := r.SetPathParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}[0]); err != nil { 331 return err 332 } 333 } 334 335 {{- else if .IsHeaderParam }} 336 337 // header array param {{ .Name }} 338 if len(joined{{ pascalize .Name }}) > 0 { 339 if err := r.SetHeaderParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}[0]); err != nil { 340 return err 341 } 342 } 343 {{- end }} 344 } 345 {{- end }} 346 {{- end }} 347 348 {{- if .IsBodyParam }} 349 {{- if or .Schema.IsInterface .Schema.IsStream (and .Schema.IsArray .Child) (and .Schema.IsMap .Child) (and .Schema.IsNullable (not .HasDiscriminator)) }} 350 if {{ .ValueExpression }} != nil { 351 {{- end }} 352 if err := r.SetBodyParam({{ .ValueExpression }}); err != nil { 353 return err 354 } 355 {{- if or .Schema.IsInterface .Schema.IsStream (and .Schema.IsArray .Child) (and .Schema.IsMap .Child) (and .Schema.IsNullable (not .HasDiscriminator)) }} 356 } 357 {{- end }} 358 {{- end }} 359 360 {{- end }} 361 362 if len(res) > 0 { 363 return errors.CompositeValidationError(res...) 364 } 365 return nil 366 } 367 368 {{- range .Params }} 369 {{- if and (not .IsBodyParam) (not .IsFileParam) .IsArray }} 370 // bindParam{{ pascalize $.Name }} binds the parameter {{ .Name }} 371 func ({{ .ReceiverName }} *{{ pascalize $.Name }}Params) bindParam{{ pascalize .Name }}(formats strfmt.Registry) []string { 372 {{ varname .Child.ValueExpression }}R := {{ if and (not .IsArray) (not .IsStream) (not .IsMap) (.IsNullable) }}*{{end}}{{ .ValueExpression }} 373 374 {{ template "sliceclientparambinder" . }} 375 376 return {{ varname .Child.ValueExpression }}S 377 } 378 {{- end }} 379 {{- end }} 380 {{- define "sliceclientparambinder" }} 381 {{- if .IsArray }} 382 var {{ varname .Child.ValueExpression }}C []string 383 for _, {{ varname .Child.ValueExpression }}IR := range {{ varname .Child.ValueExpression }}R { // explode {{ .GoType }} 384 {{ template "sliceclientparambinder" .Child }} 385 {{ varname .Child.ValueExpression }}C = append({{ varname .Child.ValueExpression }}C, {{ varname .Child.ValueExpression }}IV) 386 } 387 388 // {{ .Child.ItemsDepth }}CollectionFormat: {{ printf "%q" .CollectionFormat }} 389 {{ varname .Child.ValueExpression }}S := swag.JoinByFormat({{ varname .Child.ValueExpression }}C, {{ printf "%q" .CollectionFormat }}) 390 {{- if .Child.Parent }}{{/* NOTE: we cannot expect a "multi" CollectionFormat within an inner array */}} 391 {{ varname .Child.ValueExpression }}V := {{ varname .Child.ValueExpression }}S[0] 392 {{- end }} 393 394 {{- else }} 395 {{ varname .ValueExpression }}IV := 396 {{- if .IsCustomFormatter }} 397 {{- print " " }}{{ varname .ValueExpression }}IR.String() 398 {{- else if eq .GoType "string" }} 399 {{- print " " }}{{ varname .ValueExpression }}IR 400 {{- else if .Formatter }} 401 {{- print " "}}{{ .Formatter }}({{ varname .ValueExpression }}IR) 402 {{- else }} 403 {{- print " " }}fmt.Sprintf("%v", {{ varname .ValueExpression }}IR) 404 {{- end }} // {{ .GoType }} as string 405 {{- end }} 406 {{- end }}