github.com/djarvur/go-swagger@v0.18.0/examples/todo-list/restapi/operations/todos/find_parameters.go (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  package todos
     4  
     5  // This file was generated by the swagger tool.
     6  // Editing this file might prove futile when you re-run the swagger generate command
     7  
     8  import (
     9  	"fmt"
    10  	"net/http"
    11  
    12  	"github.com/go-openapi/errors"
    13  	"github.com/go-openapi/runtime"
    14  	"github.com/go-openapi/runtime/middleware"
    15  	"github.com/go-openapi/swag"
    16  	"github.com/go-openapi/validate"
    17  
    18  	strfmt "github.com/go-openapi/strfmt"
    19  )
    20  
    21  // NewFindParams creates a new FindParams object
    22  // no default values defined in spec.
    23  func NewFindParams() FindParams {
    24  
    25  	return FindParams{}
    26  }
    27  
    28  // FindParams contains all the bound params for the find operation
    29  // typically these are obtained from a http.Request
    30  //
    31  // swagger:parameters find
    32  type FindParams struct {
    33  
    34  	// HTTP Request Object
    35  	HTTPRequest *http.Request `json:"-"`
    36  
    37  	/*
    38  	  Required: true
    39  	  In: header
    40  	*/
    41  	XRateLimit int32
    42  	/*
    43  	  Required: true
    44  	  In: formData
    45  	*/
    46  	Limit int32
    47  	/*
    48  	  Required: true
    49  	  In: formData
    50  	  Collection Format: multi
    51  	*/
    52  	Tags []int32
    53  }
    54  
    55  // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
    56  // for simple values it will use straight method calls.
    57  //
    58  // To ensure default values, the struct must have been initialized with NewFindParams() beforehand.
    59  func (o *FindParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
    60  	var res []error
    61  
    62  	o.HTTPRequest = r
    63  
    64  	if err := r.ParseMultipartForm(32 << 20); err != nil {
    65  		if err != http.ErrNotMultipart {
    66  			return errors.New(400, "%v", err)
    67  		} else if err := r.ParseForm(); err != nil {
    68  			return errors.New(400, "%v", err)
    69  		}
    70  	}
    71  	fds := runtime.Values(r.Form)
    72  
    73  	if err := o.bindXRateLimit(r.Header[http.CanonicalHeaderKey("X-Rate-Limit")], true, route.Formats); err != nil {
    74  		res = append(res, err)
    75  	}
    76  
    77  	fdLimit, fdhkLimit, _ := fds.GetOK("limit")
    78  	if err := o.bindLimit(fdLimit, fdhkLimit, route.Formats); err != nil {
    79  		res = append(res, err)
    80  	}
    81  
    82  	fdTags, fdhkTags, _ := fds.GetOK("tags")
    83  	if err := o.bindTags(fdTags, fdhkTags, route.Formats); err != nil {
    84  		res = append(res, err)
    85  	}
    86  
    87  	if len(res) > 0 {
    88  		return errors.CompositeValidationError(res...)
    89  	}
    90  	return nil
    91  }
    92  
    93  // bindXRateLimit binds and validates parameter XRateLimit from header.
    94  func (o *FindParams) bindXRateLimit(rawData []string, hasKey bool, formats strfmt.Registry) error {
    95  	if !hasKey {
    96  		return errors.Required("X-Rate-Limit", "header")
    97  	}
    98  	var raw string
    99  	if len(rawData) > 0 {
   100  		raw = rawData[len(rawData)-1]
   101  	}
   102  
   103  	// Required: true
   104  
   105  	if err := validate.RequiredString("X-Rate-Limit", "header", raw); err != nil {
   106  		return err
   107  	}
   108  
   109  	value, err := swag.ConvertInt32(raw)
   110  	if err != nil {
   111  		return errors.InvalidType("X-Rate-Limit", "header", "int32", raw)
   112  	}
   113  	o.XRateLimit = value
   114  
   115  	return nil
   116  }
   117  
   118  // bindLimit binds and validates parameter Limit from formData.
   119  func (o *FindParams) bindLimit(rawData []string, hasKey bool, formats strfmt.Registry) error {
   120  	if !hasKey {
   121  		return errors.Required("limit", "formData")
   122  	}
   123  	var raw string
   124  	if len(rawData) > 0 {
   125  		raw = rawData[len(rawData)-1]
   126  	}
   127  
   128  	// Required: true
   129  
   130  	if raw == "" { // empty values pass all other validations
   131  		return nil
   132  	}
   133  
   134  	value, err := swag.ConvertInt32(raw)
   135  	if err != nil {
   136  		return errors.InvalidType("limit", "formData", "int32", raw)
   137  	}
   138  	o.Limit = value
   139  
   140  	return nil
   141  }
   142  
   143  // bindTags binds and validates array parameter Tags from formData.
   144  //
   145  // Arrays are parsed according to CollectionFormat: "multi" (defaults to "csv" when empty).
   146  func (o *FindParams) bindTags(rawData []string, hasKey bool, formats strfmt.Registry) error {
   147  	if !hasKey {
   148  		return errors.Required("tags", "formData")
   149  	}
   150  
   151  	// CollectionFormat: multi
   152  	tagsIC := rawData
   153  
   154  	if len(tagsIC) == 0 {
   155  		return nil
   156  	}
   157  
   158  	var tagsIR []int32
   159  	for i, tagsIV := range tagsIC {
   160  		// items.Format: "int32"
   161  		tagsI, err := swag.ConvertInt32(tagsIV)
   162  		if err != nil {
   163  			return errors.InvalidType(fmt.Sprintf("%s.%v", "tags", i), "formData", "int32", tagsI)
   164  		}
   165  
   166  		tagsIR = append(tagsIR, tagsI)
   167  	}
   168  
   169  	o.Tags = tagsIR
   170  
   171  	return nil
   172  }