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