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

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

// OpenapiTableDefinition TableDefinition
//
// TableDefinition is the definition of a table so that the table can be created with this information.
//
// swagger:model openapiTableDefinition
type OpenapiTableDefinition struct {

	// The database name of the table.
	// Example: db01
	// Required: true
	DatabaseName *string `json:"database_name"`

	// schema
	// Required: true
	Schema *OpenapiTableDefinitionSchema `json:"schema"`

	// The table name of the table.
	// Example: table01
	// Required: true
	TableName *string `json:"table_name"`
}

// Validate validates this openapi table definition
func (m *OpenapiTableDefinition) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

func (m *OpenapiTableDefinition) validateDatabaseName(formats strfmt.Registry) error {

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

	return nil
}

func (m *OpenapiTableDefinition) validateSchema(formats strfmt.Registry) error {

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

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

	return nil
}

func (m *OpenapiTableDefinition) validateTableName(formats strfmt.Registry) error {

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

	return nil
}

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

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

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

func (m *OpenapiTableDefinition) contextValidateSchema(ctx context.Context, formats strfmt.Registry) error {

	if m.Schema != nil {

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

	return nil
}

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

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

// OpenapiTableDefinitionSchema TableSchema
//
// The schema for the table.
//
// swagger:model OpenapiTableDefinitionSchema
type OpenapiTableDefinitionSchema struct {

	// The column definition for each column in the table.
	// Example: [{"column_name":"id","column_type":"INTEGER"},{"column_name":"column01","column_type":"VARCHAR(255)"}]
	// Required: true
	ColumnDefinitions []*OpenapiTableDefinitionSchemaColumnDefinitionsItems0 `json:"column_definitions"`

	// The primary key column names for the table. This is optional. The primary key is taken into account when the table is pre-created before an import task is started.
	// Example: ["id"]
	PrimaryKeyColumns []string `json:"primary_key_columns"`
}

// Validate validates this openapi table definition schema
func (m *OpenapiTableDefinitionSchema) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (m *OpenapiTableDefinitionSchema) validateColumnDefinitions(formats strfmt.Registry) error {

	if err := validate.Required("schema"+"."+"column_definitions", "body", m.ColumnDefinitions); err != nil {
		return err
	}

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

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

	}

	return nil
}

// ContextValidate validate this openapi table definition schema based on the context it is used
func (m *OpenapiTableDefinitionSchema) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

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

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

func (m *OpenapiTableDefinitionSchema) contextValidateColumnDefinitions(ctx context.Context, formats strfmt.Registry) error {

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

		if m.ColumnDefinitions[i] != nil {

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

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

	}

	return nil
}

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

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

// OpenapiTableDefinitionSchemaColumnDefinitionsItems0 ColumnDefinition
//
// ColumnDefinition is the definition of a column in a table.
//
// swagger:model OpenapiTableDefinitionSchemaColumnDefinitionsItems0
type OpenapiTableDefinitionSchemaColumnDefinitionsItems0 struct {

	// The column name.
	// Example: column01
	// Required: true
	ColumnName *string `json:"column_name"`

	// The column type.
	// Example: VARCHAR(255)
	// Required: true
	ColumnType *string `json:"column_type"`
}

// Validate validates this openapi table definition schema column definitions items0
func (m *OpenapiTableDefinitionSchemaColumnDefinitionsItems0) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (m *OpenapiTableDefinitionSchemaColumnDefinitionsItems0) validateColumnName(formats strfmt.Registry) error {

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

	return nil
}

func (m *OpenapiTableDefinitionSchemaColumnDefinitionsItems0) validateColumnType(formats strfmt.Registry) error {

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

	return nil
}

// ContextValidate validates this openapi table definition schema column definitions items0 based on context it is used
func (m *OpenapiTableDefinitionSchemaColumnDefinitionsItems0) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	return nil
}

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

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