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