// Code generated by go-swagger; DO NOT EDIT.

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"
	"strconv"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// CommonMetric common metric
//
// swagger:model commonMetric
type CommonMetric struct {

	// Datasets.
	Datasets []*CommonMetricDataset `json:"datasets"`

	// Kind.
	Kind *CommonMetricKind `json:"kind,omitempty"`

	// Name.
	Name string `json:"name,omitempty"`

	// Timestamps.
	Timestamps []strfmt.DateTime `json:"timestamps"`
}

// Validate validates this common metric
func (m *CommonMetric) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateDatasets(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateKind(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateTimestamps(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *CommonMetric) validateDatasets(formats strfmt.Registry) error {
	if swag.IsZero(m.Datasets) { // not required
		return nil
	}

	for i := 0; i < len(m.Datasets); i++ {
		if swag.IsZero(m.Datasets[i]) { // not required
			continue
		}

		if m.Datasets[i] != nil {
			if err := m.Datasets[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("datasets" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("datasets" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *CommonMetric) validateKind(formats strfmt.Registry) error {
	if swag.IsZero(m.Kind) { // not required
		return nil
	}

	if m.Kind != nil {
		if err := m.Kind.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("kind")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("kind")
			}
			return err
		}
	}

	return nil
}

func (m *CommonMetric) validateTimestamps(formats strfmt.Registry) error {
	if swag.IsZero(m.Timestamps) { // not required
		return nil
	}

	for i := 0; i < len(m.Timestamps); i++ {

		if err := validate.FormatOf("timestamps"+"."+strconv.Itoa(i), "body", "date-time", m.Timestamps[i].String(), formats); err != nil {
			return err
		}

	}

	return nil
}

// ContextValidate validate this common metric based on the context it is used
func (m *CommonMetric) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateDatasets(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateKind(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *CommonMetric) contextValidateDatasets(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(m.Datasets); i++ {

		if m.Datasets[i] != nil {

			if swag.IsZero(m.Datasets[i]) { // not required
				return nil
			}

			if err := m.Datasets[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("datasets" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("datasets" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *CommonMetric) contextValidateKind(ctx context.Context, formats strfmt.Registry) error {

	if m.Kind != nil {

		if swag.IsZero(m.Kind) { // not required
			return nil
		}

		if err := m.Kind.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("kind")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("kind")
			}
			return err
		}
	}

	return nil
}

// MarshalBinary interface implementation
func (m *CommonMetric) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *CommonMetric) UnmarshalBinary(b []byte) error {
	var res CommonMetric
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
