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

package run_model

// 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"
)

// APIRun api run
//
// swagger:model apiRun
type APIRun struct {

	// Output. The time that the run created.
	// Format: date-time
	CreatedAt strfmt.DateTime `json:"created_at,omitempty"`

	// Optional input field. Describing the purpose of the run
	Description string `json:"description,omitempty"`

	// In case any error happens retrieving a run field, only run ID
	// and the error message is returned. Client has the flexibility of choosing
	// how to handle error. This is especially useful during listing call.
	Error string `json:"error,omitempty"`

	// Output. The time this run is finished.
	// Format: date-time
	FinishedAt strfmt.DateTime `json:"finished_at,omitempty"`

	// Output. Unique run ID. Generated by API server.
	ID string `json:"id,omitempty"`

	// Output. The metrics of the run. The metrics are reported by ReportMetrics
	// API.
	Metrics []*APIRunMetric `json:"metrics"`

	// Required input field. Name provided by user,
	// or auto generated if run is created by scheduled job. Not unique.
	Name string `json:"name,omitempty"`

	// Required input field.
	// Describing what the pipeline manifest and parameters to use for the run.
	PipelineSpec *APIPipelineSpec `json:"pipeline_spec,omitempty"`

	// Optional input field. Specify which resource this run belongs to.
	// When creating a run from a particular pipeline version, the pipeline
	// version can be specified here.
	ResourceReferences []*APIResourceReference `json:"resource_references"`

	// Output. When this run is scheduled to run. This could be different from
	// created_at. For example, if a run is from a backfilling job that was
	// supposed to run 2 month ago, the scheduled_at is 2 month ago,
	// v.s. created_at is the current time.
	// Format: date-time
	ScheduledAt strfmt.DateTime `json:"scheduled_at,omitempty"`

	// Optional input field. Specify which Kubernetes service account this run uses.
	ServiceAccount string `json:"service_account,omitempty"`

	// Output. The status of the run.
	// One of [Pending, Running, Succeeded, Skipped, Failed, Error]
	Status string `json:"status,omitempty"`

	// Output. Specify whether this run is in archived or available mode.
	StorageState *APIRunStorageState `json:"storage_state,omitempty"`
}

// Validate validates this api run
func (m *APIRun) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

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

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

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

func (m *APIRun) validateCreatedAt(formats strfmt.Registry) error {
	if swag.IsZero(m.CreatedAt) { // not required
		return nil
	}

	if err := validate.FormatOf("created_at", "body", "date-time", m.CreatedAt.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *APIRun) validateFinishedAt(formats strfmt.Registry) error {
	if swag.IsZero(m.FinishedAt) { // not required
		return nil
	}

	if err := validate.FormatOf("finished_at", "body", "date-time", m.FinishedAt.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *APIRun) validateMetrics(formats strfmt.Registry) error {
	if swag.IsZero(m.Metrics) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *APIRun) validatePipelineSpec(formats strfmt.Registry) error {
	if swag.IsZero(m.PipelineSpec) { // not required
		return nil
	}

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

	return nil
}

func (m *APIRun) validateResourceReferences(formats strfmt.Registry) error {
	if swag.IsZero(m.ResourceReferences) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *APIRun) validateScheduledAt(formats strfmt.Registry) error {
	if swag.IsZero(m.ScheduledAt) { // not required
		return nil
	}

	if err := validate.FormatOf("scheduled_at", "body", "date-time", m.ScheduledAt.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *APIRun) validateStorageState(formats strfmt.Registry) error {
	if swag.IsZero(m.StorageState) { // not required
		return nil
	}

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

	return nil
}

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

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

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

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

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

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

func (m *APIRun) contextValidateMetrics(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Metrics[i] != nil {

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

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

	}

	return nil
}

func (m *APIRun) contextValidatePipelineSpec(ctx context.Context, formats strfmt.Registry) error {

	if m.PipelineSpec != nil {

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

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

	return nil
}

func (m *APIRun) contextValidateResourceReferences(ctx context.Context, formats strfmt.Registry) error {

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

		if m.ResourceReferences[i] != nil {

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

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

	}

	return nil
}

func (m *APIRun) contextValidateStorageState(ctx context.Context, formats strfmt.Registry) error {

	if m.StorageState != nil {

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

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

	return nil
}

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

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