// 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 (
	"bytes"
	"context"
	"encoding/json"

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

// OpenMetricDiscoveryMethod open metric discovery method
//
// swagger:model OpenMetricDiscoveryMethod
type OpenMetricDiscoveryMethod struct {

	// connect timeout
	ConnectTimeout int32 `json:"connectTimeout,omitempty"`

	// follow redirect
	// Required: true
	FollowRedirect *bool `json:"followRedirect"`

	// group label
	GroupLabel string `json:"groupLabel,omitempty"`

	// headers
	// Required: true
	Headers *string `json:"headers"`

	// instance label
	// Required: true
	InstanceLabel *string `json:"instanceLabel"`

	// metric name
	// Required: true
	MetricName *string `json:"metricName"`

	// read timeout
	ReadTimeout int32 `json:"readTimeout,omitempty"`

	// url
	// Required: true
	URL *string `json:"url"`
}

// Name gets the name of this subtype
func (m *OpenMetricDiscoveryMethod) Name() string {
	return "OpenMetricDiscoveryMethod"
}

// SetName sets the name of this subtype
func (m *OpenMetricDiscoveryMethod) SetName(val string) {
}

// UnmarshalJSON unmarshals this object with a polymorphic type from a JSON structure
func (m *OpenMetricDiscoveryMethod) UnmarshalJSON(raw []byte) error {
	var data struct {

		// connect timeout
		ConnectTimeout int32 `json:"connectTimeout,omitempty"`

		// follow redirect
		// Required: true
		FollowRedirect *bool `json:"followRedirect"`

		// group label
		GroupLabel string `json:"groupLabel,omitempty"`

		// headers
		// Required: true
		Headers *string `json:"headers"`

		// instance label
		// Required: true
		InstanceLabel *string `json:"instanceLabel"`

		// metric name
		// Required: true
		MetricName *string `json:"metricName"`

		// read timeout
		ReadTimeout int32 `json:"readTimeout,omitempty"`

		// url
		// Required: true
		URL *string `json:"url"`
	}
	buf := bytes.NewBuffer(raw)
	dec := json.NewDecoder(buf)
	dec.UseNumber()

	if err := dec.Decode(&data); err != nil {
		return err
	}

	var base struct {
		/* Just the base type fields. Used for unmashalling polymorphic types.*/

		Name string `json:"name"`
	}
	buf = bytes.NewBuffer(raw)
	dec = json.NewDecoder(buf)
	dec.UseNumber()

	if err := dec.Decode(&base); err != nil {
		return err
	}

	var result OpenMetricDiscoveryMethod

	if base.Name != result.Name() {
		/* Not the type we're looking for. */
		return errors.New(422, "invalid name value: %q", base.Name)
	}

	result.ConnectTimeout = data.ConnectTimeout
	result.FollowRedirect = data.FollowRedirect
	result.GroupLabel = data.GroupLabel
	result.Headers = data.Headers
	result.InstanceLabel = data.InstanceLabel
	result.MetricName = data.MetricName
	result.ReadTimeout = data.ReadTimeout
	result.URL = data.URL

	*m = result

	return nil
}

// MarshalJSON marshals this object with a polymorphic type to a JSON structure
func (m OpenMetricDiscoveryMethod) MarshalJSON() ([]byte, error) {
	var b1, b2, b3 []byte
	var err error
	b1, err = json.Marshal(struct {

		// connect timeout
		ConnectTimeout int32 `json:"connectTimeout,omitempty"`

		// follow redirect
		// Required: true
		FollowRedirect *bool `json:"followRedirect"`

		// group label
		GroupLabel string `json:"groupLabel,omitempty"`

		// headers
		// Required: true
		Headers *string `json:"headers"`

		// instance label
		// Required: true
		InstanceLabel *string `json:"instanceLabel"`

		// metric name
		// Required: true
		MetricName *string `json:"metricName"`

		// read timeout
		ReadTimeout int32 `json:"readTimeout,omitempty"`

		// url
		// Required: true
		URL *string `json:"url"`
	}{

		ConnectTimeout: m.ConnectTimeout,

		FollowRedirect: m.FollowRedirect,

		GroupLabel: m.GroupLabel,

		Headers: m.Headers,

		InstanceLabel: m.InstanceLabel,

		MetricName: m.MetricName,

		ReadTimeout: m.ReadTimeout,

		URL: m.URL,
	})
	if err != nil {
		return nil, err
	}
	b2, err = json.Marshal(struct {
		Name string `json:"name"`
	}{

		Name: m.Name(),
	})
	if err != nil {
		return nil, err
	}

	return swag.ConcatJSON(b1, b2, b3), nil
}

// Validate validates this open metric discovery method
func (m *OpenMetricDiscoveryMethod) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

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

func (m *OpenMetricDiscoveryMethod) validateFollowRedirect(formats strfmt.Registry) error {

	if err := validate.Required("followRedirect", "body", m.FollowRedirect); err != nil {
		return err
	}

	return nil
}

func (m *OpenMetricDiscoveryMethod) validateHeaders(formats strfmt.Registry) error {

	if err := validate.Required("headers", "body", m.Headers); err != nil {
		return err
	}

	return nil
}

func (m *OpenMetricDiscoveryMethod) validateInstanceLabel(formats strfmt.Registry) error {

	if err := validate.Required("instanceLabel", "body", m.InstanceLabel); err != nil {
		return err
	}

	return nil
}

func (m *OpenMetricDiscoveryMethod) validateMetricName(formats strfmt.Registry) error {

	if err := validate.Required("metricName", "body", m.MetricName); err != nil {
		return err
	}

	return nil
}

func (m *OpenMetricDiscoveryMethod) validateURL(formats strfmt.Registry) error {

	if err := validate.Required("url", "body", m.URL); err != nil {
		return err
	}

	return nil
}

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

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

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

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