github.com/kaisawind/go-swagger@v0.19.0/generator/templates/server/urlbuilder.gotmpl (about)

     1  {{ define "queryparambuilder" }}
     2  {{ if not .IsArray }}{{ template "simplequeryparambuilder" . }}{{ else }}{{ template "slicequeryparambuilder" . }}{{ end }}
     3  {{- end }}
     4  {{ define "simplequeryparambuilder" }}
     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    qs.Set({{ printf "%q" .Name }}, {{ varname .ID }})
    14  }
    15  {{ end }}
    16  {{ define "sliceitemqueryparambuilder" }}
    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 "slicequeryparambuilder" }}
    30  var {{ .Child.ValueExpression }}R []string
    31  for _, {{ .Child.ValueExpression }} := range {{ .ValueExpression }} {
    32    {{- if not .Child.IsArray }}{{ template "sliceitemqueryparambuilder" .Child }}{{ else }}{{ template "slicequeryparambuilder" .Child }}{{ end -}}
    33  }
    34  
    35  {{ if not .Child.Parent -}}
    36  {{ varname .ID }} := swag.JoinByFormat({{ .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
    37  {{ if eq .CollectionFormat "multi" }}
    38  for _, qsv := range {{ varname .ID }} {
    39    qs.Add({{ printf "%q" .Name }}, qsv)
    40  }
    41  {{ else }}
    42  if len({{ varname .ID }}) > 0 {
    43    qsv := {{ varname .ID }}[0]
    44    if qsv != "" {
    45      qs.Set({{ printf "%q" .Name }}, qsv)
    46    }
    47  }
    48  {{ end }}
    49  {{ else -}}
    50  {{ .ValueExpression }}S := swag.JoinByFormat({{ .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
    51  if len({{ .ValueExpression }}S) > 0 {
    52    {{ .ValueExpression }}Ss := {{ .ValueExpression }}S[0]
    53    if {{ .ValueExpression }}Ss != "" {
    54      {{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}Ss)
    55    }
    56  }
    57  {{ end -}}
    58  {{ end -}}
    59  {{ define "slicepathparambuilder" }}
    60  var {{ .Child.ValueExpression }}R []string
    61  for _, {{ .Child.ValueExpression }} := range {{ .ValueExpression }} {
    62    {{- if not .Child.IsArray }}{{ template "sliceitemqueryparambuilder" .Child }}{{ else }}{{ template "slicepathparambuilder" .Child }}{{ end -}}
    63  }
    64  
    65  {{ if not .Child.Parent -}}
    66  {{ varname .ID }} := swag.JoinByFormat({{ .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
    67  if len({{ varname .ID }}) > 0 {
    68    psv := {{ varname .ID }}[0]
    69    if psv != "" {
    70      _path = strings.Replace(_path, "{{ printf "{%s}" .Name }}", psv, -1)
    71    } else {
    72      return nil, errors.New("{{ camelize .ID }} is required on {{ pascalize $.Name }}URL")
    73    }
    74  }
    75  {{ else -}}
    76  {{ .ValueExpression }}S := swag.JoinByFormat({{ .Child.ValueExpression }}R, {{ printf "%q" .CollectionFormat }})
    77  if len({{ .ValueExpression }}S) > 0 {
    78    {{ .ValueExpression }}Ss := {{ .ValueExpression }}S[0]
    79    if {{ .ValueExpression }}Ss != "" {
    80      {{ .ValueExpression }}R = append({{ .ValueExpression }}R, {{ .ValueExpression }}Ss)
    81    }
    82  }
    83  {{ end -}}
    84  {{ end }}
    85  // Code generated by go-swagger; DO NOT EDIT.
    86  
    87  
    88  {{ if .Copyright -}}// {{ comment .Copyright -}}{{ end }}
    89  
    90  
    91  package {{ .Package }}
    92  
    93  // This file was generated by the swagger tool.
    94  // Editing this file might prove futile when you re-run the generate command
    95  
    96  import (
    97    "fmt"
    98    "errors"
    99    golangswaggerpaths "path"
   100    "strings"
   101    "net/url"
   102  
   103    "github.com/go-openapi/swag"
   104    "github.com/go-openapi/strfmt"
   105  )
   106  
   107  // {{ pascalize .Name }}URL generates an URL for the {{ humanize .Name }} operation
   108  type {{ pascalize .Name }}URL struct {
   109    {{ range .PathParams }}
   110    {{ pascalize .ID }} {{.GoType}}
   111    {{- end }}
   112    {{ range .QueryParams }}
   113    {{ pascalize .ID }} {{ if and (not .IsArray) .IsNullable }}*{{ end }}{{.GoType}}
   114    {{- end }}
   115  
   116    _basePath string
   117    {{ if or (gt (len .PathParams ) 0) (gt (len .QueryParams) 0) -}}
   118    // avoid unkeyed usage
   119    _ struct{}
   120    {{- end }}
   121  
   122  }
   123  
   124  // WithBasePath sets the base path for this url builder, only required when it's different from the
   125  // base path specified in the swagger spec.
   126  // When the value of the base path is an empty string
   127  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) WithBasePath(bp string) *{{pascalize .Name}}URL {
   128    {{ .ReceiverName }}.SetBasePath(bp)
   129    return {{ .ReceiverName }}
   130  }
   131  
   132  
   133  // SetBasePath sets the base path for this url builder, only required when it's different from the
   134  // base path specified in the swagger spec.
   135  // When the value of the base path is an empty string
   136  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) SetBasePath(bp string) {
   137    {{ .ReceiverName }}._basePath = bp
   138  }
   139  
   140  // Build a url path and query string
   141  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) Build() (*url.URL, error) {
   142    var _result url.URL
   143  
   144    var _path = {{ printf "%q" .Path }}
   145    {{ range .PathParams }}{{ if .IsArray }}
   146      {{ template "slicepathparambuilder" . -}}
   147    {{ else }}
   148    {{ varname .ID }} := {{ if .Formatter }}{{ .Formatter }}({{ .ReceiverName }}.{{ pascalize .ID }}){{ else }}{{ .ReceiverName }}.{{ pascalize .ID }}{{ if .IsCustomFormatter }}.String(){{end}}{{end}}
   149    if {{ varname .ID }} != "" {
   150      _path = strings.Replace(_path, "{{ printf "{%s}" .Name }}", {{ varname .ID }}, -1)
   151    } else {
   152      return nil, errors.New("{{ camelize .ID }} is required on {{ pascalize $.Name }}URL")
   153    }
   154    {{ end }}
   155  
   156    {{- end }}
   157    _basePath := {{ .ReceiverName }}._basePath
   158    {{ if .BasePath }}if _basePath == "" {
   159      _basePath = {{ printf "%q" .BasePath }}
   160    }
   161    {{ end -}}
   162    _result.Path = golangswaggerpaths.Join(_basePath, _path)
   163  
   164    {{ if gt (len .QueryParams) 0 -}}
   165    qs := make(url.Values)
   166    {{ range .QueryParams }}
   167    {{ template "queryparambuilder" . -}}
   168    {{- end }}
   169    _result.RawQuery = qs.Encode()
   170    {{- end }}
   171  
   172    return &_result, nil
   173  }
   174  
   175  // Must is a helper function to panic when the url builder returns an error
   176  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) Must(u *url.URL, err error) *url.URL {
   177    if err != nil {
   178      panic(err)
   179    }
   180    if u == nil {
   181      panic("url can't be nil")
   182    }
   183    return u
   184  }
   185  
   186  // String returns the string representation of the path with query string
   187  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) String() string {
   188    return {{ .ReceiverName }}.Must({{ .ReceiverName }}.Build()).String()
   189  }
   190  
   191  // BuildFull builds a full url with scheme, host, path and query string
   192  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) BuildFull(scheme, host string) (*url.URL, error) {
   193    if scheme == "" {
   194      return nil, errors.New("scheme is required for a full url on {{ pascalize .Name }}URL")
   195    }
   196    if host == "" {
   197      return nil, errors.New("host is required for a full url on {{ pascalize .Name }}URL")
   198    }
   199  
   200    base, err := {{ .ReceiverName }}.Build()
   201    if err != nil {
   202      return nil, err
   203    }
   204  
   205    base.Scheme = scheme
   206    base.Host = host
   207    return base, nil
   208  }
   209  
   210  // StringFull returns the string representation of a complete url
   211  func ({{ .ReceiverName }} *{{ pascalize .Name }}URL) StringFull(scheme, host string) string {
   212    return {{ .ReceiverName }}.Must( {{ .ReceiverName }}.BuildFull(scheme, host)).String()
   213  }