github.com/kaisawind/go-swagger@v0.19.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 "net/http" 15 16 "github.com/go-openapi/runtime" 17 "github.com/go-openapi/swag" 18 "github.com/go-openapi/errors" 19 "github.com/go-openapi/validate" 20 cr "github.com/go-openapi/runtime/client" 21 22 strfmt "github.com/go-openapi/strfmt" 23 24 {{ range .DefaultImports }}{{ printf "%q" .}} 25 {{ end }} 26 {{ range $key, $value := .Imports }}{{ $key }} {{ printf "%q" $value }} 27 {{ end }} 28 ) 29 30 // New{{ pascalize .Name }}Params creates a new {{ pascalize .Name }}Params object 31 // with the default values initialized. 32 func New{{ pascalize .Name }}Params() *{{ pascalize .Name }}Params { 33 {{ if .Params }}var ( 34 {{ range .Params }}{{ if .HasDefault }}{{ if not .IsFileParam }}{{ varname .ID}}Default = {{ if .IsPrimitive}}{{.GoType}}({{ end}}{{ printf "%#v" .Default }}{{ if .IsPrimitive }}){{ end }} 35 {{ end }}{{ end }}{{end}} 36 ){{ end }} 37 return &{{ pascalize .Name}}Params{ 38 {{ range .Params }}{{ if .HasDefault }}{{ pascalize .Name}}: {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (or .IsNullable ) }}&{{ end }}{{ varname .ID }}Default, 39 {{ end }}{{ end }} 40 {{ camelize .TimeoutName }}: cr.DefaultTimeout, 41 } 42 } 43 44 // New{{ pascalize .Name }}ParamsWithTimeout creates a new {{ pascalize .Name }}Params object 45 // with the default values initialized, and the ability to set a timeout on a request 46 func New{{ pascalize .Name }}ParamsWithTimeout(timeout time.Duration) *{{ pascalize .Name }}Params { 47 {{ if .Params }}var ( 48 {{ range .Params }}{{ if .HasDefault }}{{ if not .IsFileParam }}{{ varname .ID}}Default = {{ if .IsPrimitive}}{{.GoType}}({{ end}}{{ printf "%#v" .Default }}{{ if .IsPrimitive }}){{ end }} 49 {{ end }}{{ end }}{{end}} 50 ){{ end }} 51 return &{{ pascalize .Name}}Params{ 52 {{ range .Params }}{{ if .HasDefault }}{{ pascalize .ID}}: {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (or .IsNullable ) }}&{{ end }}{{ varname .ID }}Default, 53 {{ end }}{{ end }} 54 {{ camelize .TimeoutName }}: timeout, 55 } 56 } 57 58 // New{{ pascalize .Name }}ParamsWithContext creates a new {{ pascalize .Name }}Params object 59 // with the default values initialized, and the ability to set a context for a request 60 func New{{ pascalize .Name }}ParamsWithContext(ctx context.Context) *{{ pascalize .Name }}Params { 61 {{ if .Params }}var ( 62 {{ range .Params }}{{ if .HasDefault }}{{ if not .IsFileParam }}{{ camelize .Name}}Default = {{ if .IsPrimitive}}{{.GoType}}({{ end}}{{ printf "%#v" .Default }}{{ if .IsPrimitive }}){{ end }} 63 {{ end }}{{ end }}{{end}} 64 ){{ end }} 65 return &{{ pascalize .Name}}Params{ 66 {{ range .Params }}{{ if .HasDefault }}{{ pascalize .Name}}: {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (or .IsNullable ) }}&{{ end }}{{ camelize .Name }}Default, 67 {{ end }}{{ end }} 68 Context: ctx, 69 } 70 } 71 72 // New{{ pascalize .Name }}ParamsWithHTTPClient creates a new {{ pascalize .Name }}Params object 73 // with the default values initialized, and the ability to set a custom HTTPClient for a request 74 func New{{ pascalize .Name }}ParamsWithHTTPClient(client *http.Client) *{{ pascalize .Name }}Params { 75 {{ if .Params }}var ( 76 {{ range .Params }}{{ if .HasDefault }}{{ if not .IsFileParam }}{{ camelize .Name}}Default = {{ if .IsPrimitive}}{{.GoType}}({{ end}}{{ printf "%#v" .Default }}{{ if .IsPrimitive }}){{ end }} 77 {{ end }}{{ end }}{{end}} 78 ){{ end }} 79 return &{{ pascalize .Name}}Params{ 80 {{ range .Params }}{{ if .HasDefault }}{{ pascalize .Name}}: {{ if and (not .IsArray) (not .IsMap) (not .HasDiscriminator) (or .IsNullable ) }}&{{ end }}{{ camelize .Name }}Default, 81 {{ end }}{{ end }}HTTPClient: client, 82 } 83 } 84 85 /*{{ pascalize .Name }}Params contains all the parameters to send to the API endpoint 86 for the {{ humanize .Name }} operation typically these are written to a http.Request 87 */ 88 type {{ pascalize .Name }}Params struct { 89 90 {{ range .Params }}/*{{ pascalize .Name }}{{if .Description }} 91 {{ blockcomment .Description }} 92 93 {{ end }}*/ 94 {{ 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}} 95 {{ end }} 96 97 {{ camelize .TimeoutName }} time.Duration 98 Context context.Context 99 HTTPClient *http.Client 100 } 101 102 // With{{ pascalize .TimeoutName }} adds the timeout to the {{ humanize .Name }} params 103 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) With{{ pascalize .TimeoutName }}(timeout time.Duration) *{{ pascalize .Name }}Params { 104 {{ .ReceiverName }}.Set{{ pascalize .TimeoutName }}(timeout) 105 return {{ .ReceiverName }} 106 } 107 108 // Set{{ pascalize .TimeoutName }} adds the timeout to the {{ humanize .Name }} params 109 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) Set{{ pascalize .TimeoutName }}(timeout time.Duration) { 110 {{ .ReceiverName }}.{{ camelize .TimeoutName }} = timeout 111 } 112 113 // WithContext adds the context to the {{ humanize .Name }} params 114 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WithContext(ctx context.Context) *{{ pascalize .Name }}Params { 115 {{ .ReceiverName }}.SetContext(ctx) 116 return {{ .ReceiverName }} 117 } 118 119 // SetContext adds the context to the {{ humanize .Name }} params 120 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) SetContext(ctx context.Context) { 121 {{ .ReceiverName }}.Context = ctx 122 } 123 124 // WithHTTPClient adds the HTTPClient to the {{ humanize .Name }} params 125 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WithHTTPClient(client *http.Client) *{{ pascalize .Name }}Params { 126 {{ .ReceiverName }}.SetHTTPClient(client) 127 return {{ .ReceiverName }} 128 } 129 130 // SetHTTPClient adds the HTTPClient to the {{ humanize .Name }} params 131 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) SetHTTPClient(client *http.Client) { 132 {{ .ReceiverName }}.HTTPClient = client 133 } 134 135 {{ range .Params }} 136 // With{{ pascalize .ID }} adds the {{ varname .Name }} to the {{ humanize $.Name }} params 137 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 { 138 {{ $.ReceiverName }}.Set{{ pascalize .ID }}({{ varname .Name }}) 139 return {{ .ReceiverName }} 140 } 141 142 // Set{{ pascalize .ID }} adds the {{ camelize .Name }} to the {{ humanize $.Name }} params 143 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 }}) { 144 {{ $.ReceiverName }}.{{ pascalize .ID }} = {{ varname .Name }} 145 } 146 147 {{ end }} 148 // WriteToRequest writes these params to a swagger request 149 func ({{ .ReceiverName }} *{{ pascalize .Name }}Params) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { 150 151 if err := r.SetTimeout({{ .ReceiverName }}.{{ camelize .TimeoutName }}); err != nil { 152 return err 153 } 154 var res []error 155 {{range .Params}} 156 157 {{if not (or .IsArray .IsMap .IsBodyParam) }} 158 {{ if and .IsNullable (not .AllowEmptyValue) }}if {{ .ValueExpression }} != nil { {{ end}} 159 {{ if .IsQueryParam }} 160 // query param {{ .Name }} 161 {{ if .IsNullable }}var qr{{ pascalize .Name }} {{ .GoType }} 162 if {{ .ValueExpression }} != nil { 163 qr{{ pascalize .Name }} = *{{ .ValueExpression }} 164 }{{ else }}qr{{ pascalize .Name }} := {{ .ValueExpression }}{{ end}} 165 q{{ pascalize .Name}} := {{ if .Formatter }}{{ .Formatter }}(qr{{ pascalize .Name }}){{ else }}qr{{ pascalize .Name }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}{{ if not .AllowEmptyValue }} 166 if q{{ pascalize .Name }} != "" { {{ end }} 167 if err := r.SetQueryParam({{ printf "%q" .Name }}, q{{ pascalize .Name }}); err != nil { 168 return err 169 } 170 {{ if not .AllowEmptyValue }}}{{ end }} 171 {{ else if .IsPathParam }} 172 // path param {{ .Name }} 173 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 { 174 return err 175 } 176 {{ else if .IsHeaderParam }} 177 // header param {{ .Name }} 178 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 { 179 return err 180 } 181 {{ else if .IsFormParam }} 182 {{ if .IsFileParam }} 183 {{ if .IsNullable}} 184 if {{ .ValueExpression }} != nil { 185 {{end}} 186 // form file param {{ .Name }} 187 if err := r.SetFileParam({{ printf "%q" .Name }}, {{ .ValueExpression }}); err != nil { 188 return err 189 } 190 {{ if .IsNullable}} 191 } 192 {{ end }} 193 {{ else }} 194 // form param {{ .Name }} 195 {{ if .IsNullable }}var fr{{ pascalize .Name }} {{ .GoType }} 196 if {{ .ValueExpression }} != nil { 197 fr{{ pascalize .Name }} = *{{ .ValueExpression }} 198 }{{ else }}fr{{ pascalize .Name }} := {{ .ValueExpression }}{{ end}} 199 f{{ pascalize .Name}} := {{ if .Formatter }}{{ .Formatter }}(fr{{ pascalize .Name }}){{ else }}fr{{ pascalize .Name }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}{{ if not .AllowEmptyValue }} 200 if f{{ pascalize .Name }} != "" { {{ end }} 201 if err := r.SetFormParam({{ printf "%q" .Name }}, f{{ pascalize .Name }}); err != nil { 202 return err 203 } 204 {{ if not .AllowEmptyValue }}}{{ end }} 205 {{ end }} 206 {{ end }} 207 {{ if and .IsNullable (not .AllowEmptyValue) }}}{{end}} 208 {{else if .IsArray }} 209 {{ if not .IsBodyParam }}{{ if .Child }}{{ if or .Child.Formatter .Child.IsCustomFormatter }}var values{{ pascalize .Name }} []string 210 for _, v := range {{ if and (not .IsArray) (not .IsMap) (not .IsStream) (.IsNullable) }}*{{end}}{{ .ValueExpression }} { 211 values{{ pascalize .Name }} = append(values{{ pascalize .Name }}, {{ .Child.Formatter }}{{ if .Child.Formatter }}({{ end }}v{{ if .Child.IsCustomFormatter }}.String(){{ end }}{{ if .Child.Formatter }}){{ end }}) 212 } 213 {{ else }}values{{ pascalize .Name }} := {{ if and (not .IsArray) (not .IsStream) (not .IsMap) (.IsNullable) }}*{{end}}{{ .ValueExpression }}{{ end }} 214 {{ else }}values{{ pascalize .Name }} := {{ if and (not .IsArray) (not .IsStream) (not .IsMap) (.IsNullable) }}*{{end}}{{ .ValueExpression }}{{ end }} 215 joined{{ pascalize .Name}} := swag.JoinByFormat(values{{ pascalize .Name }}, "{{.CollectionFormat}}") 216 {{ if .IsQueryParam }}// query array param {{ .Name }} 217 if err := r.SetQueryParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}...); err != nil { 218 return err 219 } 220 {{ else if and .IsFormParam }}// form array param {{ .Name }} 221 if err := r.SetFormParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}...); err != nil { 222 return err 223 } 224 {{ else if and .IsPathParam }}// path array param {{ .Name }} 225 // SetPathParam does not support variadric arguments, since we used JoinByFormat 226 // we can send the first item in the array as it's all the items of the previous 227 // array joined together 228 if len(joined{{ pascalize .Name }}) > 0 { 229 if err := r.SetPathParam({{ printf "%q" .Name }}, joined{{ pascalize .Name }}[0]); err != nil { 230 return err 231 } 232 } 233 {{ end }}{{ end }} 234 235 {{ end }} 236 237 {{if .IsBodyParam}} 238 {{ if or .Schema.IsInterface .Schema.IsStream (and .Schema.IsArray .Child) (and .Schema.IsMap .Child) (and .Schema.IsNullable (not .HasDiscriminator)) }} if {{ .ValueExpression }} != nil { {{end}} 239 if err := r.SetBodyParam({{ .ValueExpression }}); err != nil { 240 return err 241 } 242 {{ if or .Schema.IsInterface .Schema.IsStream (and .Schema.IsArray .Child) (and .Schema.IsMap .Child) (and .Schema.IsNullable (not .HasDiscriminator)) }} } {{end}} 243 {{end}} 244 245 {{end}} 246 if len(res) > 0 { 247 return errors.CompositeValidationError(res...) 248 } 249 return nil 250 }