github.com/circl-dev/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  
    13  	"github.com/go-openapi/errors"
    14  	"github.com/go-openapi/strfmt"
    15  	"github.com/go-openapi/swag"
    16  	"github.com/circl-dev/go-swagger/examples/external-types/fred"
    17  	"github.com/circl-dev/validate"
    18  )
    19  
    20  // MyTuple Demonstrates references to some external type in the context of a tuple.
    21  //
    22  // Notice that "additionalItems" is not a construct that pass swagger validation,
    23  // but is supported by go-swagger.
    24  //
    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  					return err
   189  				}
   190  			}
   191  
   192  		}
   193  
   194  	}
   195  
   196  	return nil
   197  }
   198  
   199  // ContextValidate validate this my tuple based on the context it is used
   200  func (m *MyTuple) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
   201  	var res []error
   202  
   203  	if err := m.contextValidateP0(ctx, formats); err != nil {
   204  		res = append(res, err)
   205  	}
   206  
   207  	if len(res) > 0 {
   208  		return errors.CompositeValidationError(res...)
   209  	}
   210  	return nil
   211  }
   212  
   213  func (m *MyTuple) contextValidateP0(ctx context.Context, formats strfmt.Registry) error {
   214  
   215  	if m.P0 != nil {
   216  		if err := m.P0.ContextValidate(ctx, formats); err != nil {
   217  			if ve, ok := err.(*errors.Validation); ok {
   218  				return ve.ValidateName("0")
   219  			} else if ce, ok := err.(*errors.CompositeError); ok {
   220  				return ce.ValidateName("0")
   221  			}
   222  			return err
   223  		}
   224  	}
   225  
   226  	return nil
   227  }
   228  
   229  // MarshalBinary interface implementation
   230  func (m *MyTuple) MarshalBinary() ([]byte, error) {
   231  	if m == nil {
   232  		return nil, nil
   233  	}
   234  	return swag.WriteJSON(m)
   235  }
   236  
   237  // UnmarshalBinary interface implementation
   238  func (m *MyTuple) UnmarshalBinary(b []byte) error {
   239  	var res MyTuple
   240  	if err := swag.ReadJSON(b, &res); err != nil {
   241  		return err
   242  	}
   243  	*m = res
   244  	return nil
   245  }