github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/examples/contributed-templates/stratoscale/restapi/operations/pet/pet_create_parameters.go (about)

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  package pet
     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  	"context"
    10  	"io"
    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/validate"
    17  
    18  	"github.com/go-swagger/go-swagger/examples/contributed-templates/stratoscale/models"
    19  )
    20  
    21  // NewPetCreateParams creates a new PetCreateParams object
    22  //
    23  // There are no default values defined in the spec.
    24  func NewPetCreateParams() PetCreateParams {
    25  
    26  	return PetCreateParams{}
    27  }
    28  
    29  // PetCreateParams contains all the bound params for the pet create operation
    30  // typically these are obtained from a http.Request
    31  //
    32  // swagger:parameters PetCreate
    33  type PetCreateParams struct {
    34  
    35  	// HTTP Request Object
    36  	HTTPRequest *http.Request `json:"-"`
    37  
    38  	/*Pet object that needs to be added to the store
    39  	  Required: true
    40  	  In: body
    41  	*/
    42  	Body *models.Pet
    43  }
    44  
    45  // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
    46  // for simple values it will use straight method calls.
    47  //
    48  // To ensure default values, the struct must have been initialized with NewPetCreateParams() beforehand.
    49  func (o *PetCreateParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error {
    50  	var res []error
    51  
    52  	o.HTTPRequest = r
    53  
    54  	if runtime.HasBody(r) {
    55  		defer r.Body.Close()
    56  		var body models.Pet
    57  		if err := route.Consumer.Consume(r.Body, &body); err != nil {
    58  			if err == io.EOF {
    59  				res = append(res, errors.Required("body", "body", ""))
    60  			} else {
    61  				res = append(res, errors.NewParseError("body", "body", "", err))
    62  			}
    63  		} else {
    64  			// validate body object
    65  			if err := body.Validate(route.Formats); err != nil {
    66  				res = append(res, err)
    67  			}
    68  
    69  			ctx := validate.WithOperationRequest(context.Background())
    70  			if err := body.ContextValidate(ctx, route.Formats); err != nil {
    71  				res = append(res, err)
    72  			}
    73  
    74  			if len(res) == 0 {
    75  				o.Body = &body
    76  			}
    77  		}
    78  	} else {
    79  		res = append(res, errors.Required("body", "body", ""))
    80  	}
    81  	if len(res) > 0 {
    82  		return errors.CompositeValidationError(res...)
    83  	}
    84  	return nil
    85  }