github.com/go-swagger/go-swagger@v0.31.0/examples/external-types/models/my_tuple.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  	"bytes"
    10  	"context"
    11  	"encoding/json"
    12  	"strconv"
    13  
    14  	"github.com/go-openapi/errors"
    15  	"github.com/go-openapi/strfmt"
    16  	"github.com/go-openapi/swag"
    17  	"github.com/go-openapi/validate"
    18  	"github.com/go-swagger/go-swagger/examples/external-types/fred"
    19  )
    20  
    21  // MyTuple Demonstrates references to some external type in the context of a tuple.
    22  //
    23  // Notice that "additionalItems" is not a construct that pass swagger validation,
    24  // but is supported by go-swagger.
    25  //
    26  // swagger:model MyTuple
    27  type MyTuple struct {
    28  
    29  	// p0
    30  	// Required: true
    31  	P0 *MyType `json:"-"` // custom serializer
    32  
    33  	// Second element of the tuple, defined as follows.
    34  	//
    35  	// P1 *fred.MyAlternateType `json:"-"` // custom serializer
    36  	//
    37  	// Required: true
    38  	P1 *fred.MyAlternateType `json:"-"` // custom serializer
    39  
    40  	// Additional items to a tuple, from an external type.
    41  	// This defines the following field in the tuple
    42  	//
    43  	// MyTupleItems []map[string]fred.MyAlternateType
    44  	//
    45  	MyTupleItems []map[string]fred.MyAlternateType `json:"-"`
    46  }
    47  
    48  // UnmarshalJSON unmarshals this tuple type from a JSON array
    49  func (m *MyTuple) UnmarshalJSON(raw []byte) error {
    50  	// stage 1, get the array but just the array
    51  	var stage1 []json.RawMessage
    52  	buf := bytes.NewBuffer(raw)
    53  	dec := json.NewDecoder(buf)
    54  	dec.UseNumber()
    55  
    56  	if err := dec.Decode(&stage1); err != nil {
    57  		return err
    58  	}
    59  
    60  	// stage 2: hydrates struct members with tuple elements
    61  	var lastIndex int
    62  
    63  	if len(stage1) > 0 {
    64  		var dataP0 MyType
    65  		buf = bytes.NewBuffer(stage1[0])
    66  		dec := json.NewDecoder(buf)
    67  		dec.UseNumber()
    68  		if err := dec.Decode(&dataP0); err != nil {
    69  			return err
    70  		}
    71  		m.P0 = &dataP0
    72  
    73  		lastIndex = 0
    74  
    75  	}
    76  	if len(stage1) > 1 {
    77  		var dataP1 fred.MyAlternateType
    78  		buf = bytes.NewBuffer(stage1[1])
    79  		dec := json.NewDecoder(buf)
    80  		dec.UseNumber()
    81  		if err := dec.Decode(&dataP1); err != nil {
    82  			return err
    83  		}
    84  		m.P1 = &dataP1
    85  
    86  		lastIndex = 1
    87  
    88  	}
    89  
    90  	// stage 3: hydrates AdditionalItems
    91  	if len(stage1) > lastIndex+1 {
    92  		for _, val := range stage1[lastIndex+1:] {
    93  			var toadd map[string]fred.MyAlternateType
    94  			buf = bytes.NewBuffer(val)
    95  			dec := json.NewDecoder(buf)
    96  			dec.UseNumber()
    97  			if err := dec.Decode(&toadd); err != nil {
    98  				return err
    99  			}
   100  			m.MyTupleItems = append(m.MyTupleItems, toadd)
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  // MarshalJSON marshals this tuple type into a JSON array
   107  func (m MyTuple) MarshalJSON() ([]byte, error) {
   108  	data := []interface{}{
   109  		m.P0, m.P1,
   110  	}
   111  
   112  	for _, v := range m.MyTupleItems {
   113  		data = append(data, v)
   114  	}
   115  	return json.Marshal(data)
   116  }
   117  
   118  // Validate validates this my tuple
   119  func (m *MyTuple) Validate(formats strfmt.Registry) error {
   120  	var res []error
   121  
   122  	if err := m.validateP0(formats); err != nil {
   123  		res = append(res, err)
   124  	}
   125  
   126  	if err := m.validateP1(formats); err != nil {
   127  		res = append(res, err)
   128  	}
   129  
   130  	if err := m.validateMyTupleItems(formats); err != nil {
   131  		res = append(res, err)
   132  	}
   133  
   134  	if len(res) > 0 {
   135  		return errors.CompositeValidationError(res...)
   136  	}
   137  	return nil
   138  }
   139  
   140  func (m *MyTuple) validateP0(formats strfmt.Registry) error {
   141  
   142  	if err := validate.Required("0", "body", m.P0); err != nil {
   143  		return err
   144  	}
   145  
   146  	if m.P0 != nil {
   147  		if err := m.P0.Validate(formats); err != nil {
   148  			if ve, ok := err.(*errors.Validation); ok {
   149  				return ve.ValidateName("0")
   150  			} else if ce, ok := err.(*errors.CompositeError); ok {
   151  				return ce.ValidateName("0")
   152  			}
   153  			return err
   154  		}
   155  	}
   156  
   157  	return nil
   158  }
   159  
   160  func (m *MyTuple) validateP1(formats strfmt.Registry) error {
   161  
   162  	if err := validate.Required("1", "body", m.P1); err != nil {
   163  		return err
   164  	}
   165  
   166  	if m.P1 != nil {
   167  		if err := m.P1.Validate(formats); err != nil {
   168  			if ve, ok := err.(*errors.Validation); ok {
   169  				return ve.ValidateName("1")
   170  			} else if ce, ok := err.(*errors.CompositeError); ok {
   171  				return ce.ValidateName("1")
   172  			}
   173  			return err
   174  		}
   175  	}
   176  
   177  	return nil
   178  }
   179  
   180  func (m *MyTuple) validateMyTupleItems(formats strfmt.Registry) error {
   181  
   182  	for i := range m.MyTupleItems {
   183  
   184  		for k := range m.MyTupleItems[i] {
   185  
   186  			if val, ok := m.MyTupleItems[i][k]; ok {
   187  				if err := val.Validate(formats); err != nil {
   188  					if ve, ok := err.(*errors.Validation); ok {
   189  						return ve.ValidateName(strconv.Itoa(i+2) + "." + k)
   190  					} else if ce, ok := err.(*errors.CompositeError); ok {
   191  						return ce.ValidateName(strconv.Itoa(i+2) + "." + k)
   192  					}
   193  					return err
   194  				}
   195  			}
   196  
   197  		}
   198  
   199  	}
   200  
   201  	return nil
   202  }
   203  
   204  // ContextValidate validate this my tuple based on the context it is used
   205  func (m *MyTuple) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
   206  	var res []error
   207  
   208  	if err := m.contextValidateP0(ctx, formats); err != nil {
   209  		res = append(res, err)
   210  	}
   211  
   212  	if len(res) > 0 {
   213  		return errors.CompositeValidationError(res...)
   214  	}
   215  	return nil
   216  }
   217  
   218  func (m *MyTuple) contextValidateP0(ctx context.Context, formats strfmt.Registry) error {
   219  
   220  	if m.P0 != nil {
   221  
   222  		if err := m.P0.ContextValidate(ctx, formats); err != nil {
   223  			if ve, ok := err.(*errors.Validation); ok {
   224  				return ve.ValidateName("0")
   225  			} else if ce, ok := err.(*errors.CompositeError); ok {
   226  				return ce.ValidateName("0")
   227  			}
   228  			return err
   229  		}
   230  	}
   231  
   232  	return nil
   233  }
   234  
   235  // MarshalBinary interface implementation
   236  func (m *MyTuple) MarshalBinary() ([]byte, error) {
   237  	if m == nil {
   238  		return nil, nil
   239  	}
   240  	return swag.WriteJSON(m)
   241  }
   242  
   243  // UnmarshalBinary interface implementation
   244  func (m *MyTuple) UnmarshalBinary(b []byte) error {
   245  	var res MyTuple
   246  	if err := swag.ReadJSON(b, &res); err != nil {
   247  		return err
   248  	}
   249  	*m = res
   250  	return nil
   251  }