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