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

     1  // Code generated by go-swagger; DO NOT EDIT.
     2  
     3  package models
     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  	"encoding/json"
    11  	"strconv"
    12  
    13  	"github.com/go-openapi/errors"
    14  	"github.com/go-openapi/strfmt"
    15  	"github.com/go-openapi/swag"
    16  	"github.com/go-openapi/validate"
    17  )
    18  
    19  // Pet pet
    20  //
    21  // swagger:model Pet
    22  type Pet struct {
    23  
    24  	// category
    25  	Category *Category `json:"category,omitempty"`
    26  
    27  	// id
    28  	ID int64 `json:"id,omitempty" gorm:"primary_key" query:"filter,sort"`
    29  
    30  	// name
    31  	// Example: doggie
    32  	// Required: true
    33  	Name *string `json:"name" query:"filter,sort"`
    34  
    35  	// photo urls
    36  	// Required: true
    37  	PhotoUrls []string `json:"photoUrls" gorm:"-"`
    38  
    39  	// pet status in the store
    40  	// Enum: [available pending sold]
    41  	Status string `json:"status,omitempty" query:"filter,sort"`
    42  
    43  	// tags
    44  	Tags []*Tag `json:"tags"`
    45  }
    46  
    47  // Validate validates this pet
    48  func (m *Pet) Validate(formats strfmt.Registry) error {
    49  	var res []error
    50  
    51  	if err := m.validateCategory(formats); err != nil {
    52  		res = append(res, err)
    53  	}
    54  
    55  	if err := m.validateName(formats); err != nil {
    56  		res = append(res, err)
    57  	}
    58  
    59  	if err := m.validatePhotoUrls(formats); err != nil {
    60  		res = append(res, err)
    61  	}
    62  
    63  	if err := m.validateStatus(formats); err != nil {
    64  		res = append(res, err)
    65  	}
    66  
    67  	if err := m.validateTags(formats); err != nil {
    68  		res = append(res, err)
    69  	}
    70  
    71  	if len(res) > 0 {
    72  		return errors.CompositeValidationError(res...)
    73  	}
    74  	return nil
    75  }
    76  
    77  func (m *Pet) validateCategory(formats strfmt.Registry) error {
    78  	if swag.IsZero(m.Category) { // not required
    79  		return nil
    80  	}
    81  
    82  	if m.Category != nil {
    83  		if err := m.Category.Validate(formats); err != nil {
    84  			if ve, ok := err.(*errors.Validation); ok {
    85  				return ve.ValidateName("category")
    86  			} else if ce, ok := err.(*errors.CompositeError); ok {
    87  				return ce.ValidateName("category")
    88  			}
    89  			return err
    90  		}
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  func (m *Pet) validateName(formats strfmt.Registry) error {
    97  
    98  	if err := validate.Required("name", "body", m.Name); err != nil {
    99  		return err
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func (m *Pet) validatePhotoUrls(formats strfmt.Registry) error {
   106  
   107  	if err := validate.Required("photoUrls", "body", m.PhotoUrls); err != nil {
   108  		return err
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  var petTypeStatusPropEnum []interface{}
   115  
   116  func init() {
   117  	var res []string
   118  	if err := json.Unmarshal([]byte(`["available","pending","sold"]`), &res); err != nil {
   119  		panic(err)
   120  	}
   121  	for _, v := range res {
   122  		petTypeStatusPropEnum = append(petTypeStatusPropEnum, v)
   123  	}
   124  }
   125  
   126  const (
   127  
   128  	// PetStatusAvailable captures enum value "available"
   129  	PetStatusAvailable string = "available"
   130  
   131  	// PetStatusPending captures enum value "pending"
   132  	PetStatusPending string = "pending"
   133  
   134  	// PetStatusSold captures enum value "sold"
   135  	PetStatusSold string = "sold"
   136  )
   137  
   138  // prop value enum
   139  func (m *Pet) validateStatusEnum(path, location string, value string) error {
   140  	if err := validate.EnumCase(path, location, value, petTypeStatusPropEnum, true); err != nil {
   141  		return err
   142  	}
   143  	return nil
   144  }
   145  
   146  func (m *Pet) validateStatus(formats strfmt.Registry) error {
   147  	if swag.IsZero(m.Status) { // not required
   148  		return nil
   149  	}
   150  
   151  	// value enum
   152  	if err := m.validateStatusEnum("status", "body", m.Status); err != nil {
   153  		return err
   154  	}
   155  
   156  	return nil
   157  }
   158  
   159  func (m *Pet) validateTags(formats strfmt.Registry) error {
   160  	if swag.IsZero(m.Tags) { // not required
   161  		return nil
   162  	}
   163  
   164  	for i := 0; i < len(m.Tags); i++ {
   165  		if swag.IsZero(m.Tags[i]) { // not required
   166  			continue
   167  		}
   168  
   169  		if m.Tags[i] != nil {
   170  			if err := m.Tags[i].Validate(formats); err != nil {
   171  				if ve, ok := err.(*errors.Validation); ok {
   172  					return ve.ValidateName("tags" + "." + strconv.Itoa(i))
   173  				} else if ce, ok := err.(*errors.CompositeError); ok {
   174  					return ce.ValidateName("tags" + "." + strconv.Itoa(i))
   175  				}
   176  				return err
   177  			}
   178  		}
   179  
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  // ContextValidate validate this pet based on the context it is used
   186  func (m *Pet) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
   187  	var res []error
   188  
   189  	if err := m.contextValidateCategory(ctx, formats); err != nil {
   190  		res = append(res, err)
   191  	}
   192  
   193  	if err := m.contextValidateTags(ctx, formats); err != nil {
   194  		res = append(res, err)
   195  	}
   196  
   197  	if len(res) > 0 {
   198  		return errors.CompositeValidationError(res...)
   199  	}
   200  	return nil
   201  }
   202  
   203  func (m *Pet) contextValidateCategory(ctx context.Context, formats strfmt.Registry) error {
   204  
   205  	if m.Category != nil {
   206  		if err := m.Category.ContextValidate(ctx, formats); err != nil {
   207  			if ve, ok := err.(*errors.Validation); ok {
   208  				return ve.ValidateName("category")
   209  			} else if ce, ok := err.(*errors.CompositeError); ok {
   210  				return ce.ValidateName("category")
   211  			}
   212  			return err
   213  		}
   214  	}
   215  
   216  	return nil
   217  }
   218  
   219  func (m *Pet) contextValidateTags(ctx context.Context, formats strfmt.Registry) error {
   220  
   221  	for i := 0; i < len(m.Tags); i++ {
   222  
   223  		if m.Tags[i] != nil {
   224  			if err := m.Tags[i].ContextValidate(ctx, formats); err != nil {
   225  				if ve, ok := err.(*errors.Validation); ok {
   226  					return ve.ValidateName("tags" + "." + strconv.Itoa(i))
   227  				} else if ce, ok := err.(*errors.CompositeError); ok {
   228  					return ce.ValidateName("tags" + "." + strconv.Itoa(i))
   229  				}
   230  				return err
   231  			}
   232  		}
   233  
   234  	}
   235  
   236  	return nil
   237  }
   238  
   239  // MarshalBinary interface implementation
   240  func (m *Pet) MarshalBinary() ([]byte, error) {
   241  	if m == nil {
   242  		return nil, nil
   243  	}
   244  	return swag.WriteJSON(m)
   245  }
   246  
   247  // UnmarshalBinary interface implementation
   248  func (m *Pet) UnmarshalBinary(b []byte) error {
   249  	var res Pet
   250  	if err := swag.ReadJSON(b, &res); err != nil {
   251  		return err
   252  	}
   253  	*m = res
   254  	return nil
   255  }