github.com/dirkm/go-swagger@v0.19.0/examples/task-tracker/restapi/operations/tasks/upload_task_file_parameters.go (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  package tasks
     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  	"io"
    10  	"mime/multipart"
    11  	"net/http"
    12  
    13  	"github.com/go-openapi/errors"
    14  	"github.com/go-openapi/runtime"
    15  	"github.com/go-openapi/runtime/middleware"
    16  	"github.com/go-openapi/swag"
    17  
    18  	strfmt "github.com/go-openapi/strfmt"
    19  )
    20  
    21  // NewUploadTaskFileParams creates a new UploadTaskFileParams object
    22  // no default values defined in spec.
    23  func NewUploadTaskFileParams() UploadTaskFileParams {
    24  
    25  	return UploadTaskFileParams{}
    26  }
    27  
    28  // UploadTaskFileParams contains all the bound params for the upload task file operation
    29  // typically these are obtained from a http.Request
    30  //
    31  // swagger:parameters uploadTaskFile
    32  type UploadTaskFileParams struct {
    33  
    34  	// HTTP Request Object
    35  	HTTPRequest *http.Request `json:"-"`
    36  
    37  	/*Extra information describing the file
    38  	  In: formData
    39  	*/
    40  	Description *string
    41  	/*The file to upload
    42  	  In: formData
    43  	*/
    44  	File io.ReadCloser
    45  	/*The id of the item
    46  	  Required: true
    47  	  In: path
    48  	*/
    49  	ID int64
    50  }
    51  
    52  // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
    53  // for simple values it will use straight method calls.
    54  //
    55  // To ensure default values, the struct must have been initialized with NewUploadTaskFileParams() beforehand.
    56  func (o *UploadTaskFileParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
    57  	var res []error
    58  
    59  	o.HTTPRequest = r
    60  
    61  	if err := r.ParseMultipartForm(32 << 20); err != nil {
    62  		if err != http.ErrNotMultipart {
    63  			return errors.New(400, "%v", err)
    64  		} else if err := r.ParseForm(); err != nil {
    65  			return errors.New(400, "%v", err)
    66  		}
    67  	}
    68  	fds := runtime.Values(r.Form)
    69  
    70  	fdDescription, fdhkDescription, _ := fds.GetOK("description")
    71  	if err := o.bindDescription(fdDescription, fdhkDescription, route.Formats); err != nil {
    72  		res = append(res, err)
    73  	}
    74  
    75  	file, fileHeader, err := r.FormFile("file")
    76  	if err != nil && err != http.ErrMissingFile {
    77  		res = append(res, errors.New(400, "reading file %q failed: %v", "file", err))
    78  	} else if err == http.ErrMissingFile {
    79  		// no-op for missing but optional file parameter
    80  	} else if err := o.bindFile(file, fileHeader); err != nil {
    81  		res = append(res, err)
    82  	} else {
    83  		o.File = &runtime.File{Data: file, Header: fileHeader}
    84  	}
    85  
    86  	rID, rhkID, _ := route.Params.GetOK("id")
    87  	if err := o.bindID(rID, rhkID, route.Formats); err != nil {
    88  		res = append(res, err)
    89  	}
    90  
    91  	if len(res) > 0 {
    92  		return errors.CompositeValidationError(res...)
    93  	}
    94  	return nil
    95  }
    96  
    97  // bindDescription binds and validates parameter Description from formData.
    98  func (o *UploadTaskFileParams) bindDescription(rawData []string, hasKey bool, formats strfmt.Registry) error {
    99  	var raw string
   100  	if len(rawData) > 0 {
   101  		raw = rawData[len(rawData)-1]
   102  	}
   103  
   104  	// Required: false
   105  
   106  	if raw == "" { // empty values pass all other validations
   107  		return nil
   108  	}
   109  
   110  	o.Description = &raw
   111  
   112  	return nil
   113  }
   114  
   115  // bindFile binds file parameter File.
   116  //
   117  // The only supported validations on files are MinLength and MaxLength
   118  func (o *UploadTaskFileParams) bindFile(file multipart.File, header *multipart.FileHeader) error {
   119  	return nil
   120  }
   121  
   122  // bindID binds and validates parameter ID from path.
   123  func (o *UploadTaskFileParams) bindID(rawData []string, hasKey bool, formats strfmt.Registry) error {
   124  	var raw string
   125  	if len(rawData) > 0 {
   126  		raw = rawData[len(rawData)-1]
   127  	}
   128  
   129  	// Required: true
   130  	// Parameter is provided by construction from the route
   131  
   132  	value, err := swag.ConvertInt64(raw)
   133  	if err != nil {
   134  		return errors.InvalidType("id", "path", "int64", raw)
   135  	}
   136  	o.ID = value
   137  
   138  	return nil
   139  }