// Code generated by mdatagen. DO NOT EDIT.

package configgrpc

import (
	"errors"
	"time"

	"go.opentelemetry.io/collector/config/configauth"
	"go.opentelemetry.io/collector/config/configcompression"
	"go.opentelemetry.io/collector/config/configmiddleware"
	"go.opentelemetry.io/collector/config/confignet"
	"go.opentelemetry.io/collector/config/configopaque"
	"go.opentelemetry.io/collector/config/configoptional"
	"go.opentelemetry.io/collector/config/configtls"
)

// ClientConfig defines common settings for a gRPC client configuration.
type ClientConfig struct {
	// Auth configuration for outgoing RPCs.
	Auth configoptional.Optional[configauth.Config] `mapstructure:"auth,omitempty"`

	// Authority parameter that configures client to rewrite ":authority" header
	// (godoc.org/google.golang.org/grpc#WithAuthority)
	Authority string `mapstructure:"authority,omitempty"`

	// BalancerName sets the balancer in grpclb_policy to discover the servers.
	// Default is pick_first.
	// https://github.com/grpc/grpc-go/blob/master/examples/features/load_balancing/README.md
	BalancerName string `mapstructure:"balancer_name"`

	// Compression the compression key for supported compression types within collector.
	Compression configcompression.Type `mapstructure:"compression,omitempty"`

	// Endpoint the target to which the exporter is going to send traces or metrics, using the gRPC protocol.
	// The valid syntax is described at https://github.com/grpc/grpc/blob/master/doc/naming.md.
	Endpoint string `mapstructure:"endpoint,omitempty"`

	// Headers the headers associated with gRPC requests.
	Headers configopaque.MapList `mapstructure:"headers,omitempty"`

	// Keepalive the keepalive parameters for gRPC client. See grpc.WithKeepaliveParams.
	// (https://godoc.org/google.golang.org/grpc#WithKeepaliveParams).
	Keepalive configoptional.Optional[KeepaliveClientConfig] `mapstructure:"keepalive,omitempty"`

	// DialerMiddleware dialer middleware is a handler customizing how the client will dial connections over TCP.
	DialerMiddleware configoptional.Optional[configmiddleware.Config] `mapstructure:"dialer_middleware,omitempty"`

	// Middlewares middlewares for the gRPC client.
	Middlewares []configmiddleware.Config `mapstructure:"middlewares,omitempty"`

	// ReadBufferSize readBufferSize for gRPC client. See grpc.WithReadBufferSize.
	// (https://godoc.org/google.golang.org/grpc#WithReadBufferSize).
	ReadBufferSize int `mapstructure:"read_buffer_size,omitempty"`

	// TLS tLS struct exposes TLS client configuration.
	TLS configtls.ClientConfig `mapstructure:"tls,omitempty"`

	// UserAgent userAgent overrides the default user-agent header sent on gRPC requests.
	// The default is derived from the build info. When empty, the caller controls the user-agent
	// via grpc.WithUserAgent or similar options.
	UserAgent string `mapstructure:"user_agent,omitempty"`

	// WaitForReady a parameter configures client to wait for ready state before sending data.
	// (https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md)
	WaitForReady bool `mapstructure:"wait_for_ready,omitempty"`

	// WriteBufferSize write buffer size for gRPC gRPC. See grpc.WithWriteBufferSize.
	// (https://godoc.org/google.golang.org/grpc#WithWriteBufferSize).
	WriteBufferSize int `mapstructure:"write_buffer_size,omitempty"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// Validate validates the ClientConfig fields according to schema annotations.
// Called by confmap on config resolution. Don't call it explicitly to avoid duplicate errors.
func (c *ClientConfig) Validate() error {
	var err error

	if inner_err := validateClientConfig(c); inner_err != nil {
		err = errors.Join(err, inner_err)
	}

	return err
}

// NewDefaultClientConfig returns a new ClientConfig with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultClientConfig() ClientConfig {
	return ClientConfig{
		Auth:             configoptional.None[configauth.Config](),
		BalancerName:     "round_robin",
		Keepalive:        configoptional.Some(NewDefaultKeepaliveClientConfig()),
		DialerMiddleware: configoptional.None[configmiddleware.Config](),
		TLS:              configtls.NewDefaultClientConfig(),
	}
}

// KeepaliveClientConfig exposes the keepalive.ClientParameters to be used by the exporter.
// Refer to the original data-structure for the meaning of each parameter:
// https://godoc.org/google.golang.org/grpc/keepalive#ClientParameters'
type KeepaliveClientConfig struct {
	PermitWithoutStream bool `mapstructure:"permit_without_stream,omitempty"`

	Time time.Duration `mapstructure:"time"`

	Timeout time.Duration `mapstructure:"timeout"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// NewDefaultKeepaliveClientConfig returns a new KeepaliveClientConfig with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultKeepaliveClientConfig() KeepaliveClientConfig {
	return KeepaliveClientConfig{
		Time:    10 * time.Second,
		Timeout: 10 * time.Second,
	}
}

// KeepaliveEnforcementPolicy configuration of the keepalive.EnforcementPolicy. The same default values
// as keepalive.EnforcementPolicy are applicable and get applied by the server.
// See https://godoc.org/google.golang.org/grpc/keepalive#EnforcementPolicy for details.
type KeepaliveEnforcementPolicy struct {
	MinTime time.Duration `mapstructure:"min_time,omitempty"`

	PermitWithoutStream bool `mapstructure:"permit_without_stream,omitempty"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// NewDefaultKeepaliveEnforcementPolicy returns a new KeepaliveEnforcementPolicy with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultKeepaliveEnforcementPolicy() KeepaliveEnforcementPolicy {
	return KeepaliveEnforcementPolicy{}
}

// KeepaliveServerConfig configuration for keepalive.
type KeepaliveServerConfig struct {
	// EnforcementPolicy configuration of the keepalive.EnforcementPolicy. The same default values
	// as keepalive.EnforcementPolicy are applicable and get applied by the server.
	// See https://godoc.org/google.golang.org/grpc/keepalive#EnforcementPolicy for details.
	EnforcementPolicy configoptional.Optional[KeepaliveEnforcementPolicy] `mapstructure:"enforcement_policy,omitempty"`

	// ServerParameters configuration of the keepalive.ServerParameters. The same default values
	// as keepalive.ServerParameters are applicable and get applied by the server.
	// See https://godoc.org/google.golang.org/grpc/keepalive#ServerParameters for details.
	ServerParameters configoptional.Optional[KeepaliveServerParameters] `mapstructure:"server_parameters,omitempty"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// NewDefaultKeepaliveServerConfig returns a new KeepaliveServerConfig with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultKeepaliveServerConfig() KeepaliveServerConfig {
	return KeepaliveServerConfig{
		EnforcementPolicy: configoptional.Some(NewDefaultKeepaliveEnforcementPolicy()),
		ServerParameters:  configoptional.Some(NewDefaultKeepaliveServerParameters()),
	}
}

// KeepaliveServerParameters configuration of the keepalive.ServerParameters. The same default values
// as keepalive.ServerParameters are applicable and get applied by the server.
// See https://godoc.org/google.golang.org/grpc/keepalive#ServerParameters for details.
type KeepaliveServerParameters struct {
	MaxConnectionAge time.Duration `mapstructure:"max_connection_age,omitempty"`

	MaxConnectionAgeGrace time.Duration `mapstructure:"max_connection_age_grace,omitempty"`

	MaxConnectionIdle time.Duration `mapstructure:"max_connection_idle,omitempty"`

	Time time.Duration `mapstructure:"time,omitempty"`

	Timeout time.Duration `mapstructure:"timeout,omitempty"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// NewDefaultKeepaliveServerParameters returns a new KeepaliveServerParameters with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultKeepaliveServerParameters() KeepaliveServerParameters {
	return KeepaliveServerParameters{}
}

// ServerConfig defines common settings for a gRPC server configuration.
type ServerConfig struct {
	// Auth authentication for this receiver
	Auth configoptional.Optional[configauth.Config] `mapstructure:"auth,omitempty"`

	// IncludeMetadata propagates the incoming connection's metadata to downstream consumers.
	IncludeMetadata bool `mapstructure:"include_metadata,omitempty"`

	// Keepalive anchor for all the settings related to keepalive.
	Keepalive configoptional.Optional[KeepaliveServerConfig] `mapstructure:"keepalive,omitempty"`

	// MaxConcurrentStreams sets the limit on the number of concurrent streams to each ServerTransport.
	// It has effect only for streaming RPCs.
	MaxConcurrentStreams uint32 `mapstructure:"max_concurrent_streams,omitempty"`

	// MaxRecvMsgSizeMiB sets the maximum size (in MiB) of messages accepted by the server.
	MaxRecvMsgSizeMiB int `mapstructure:"max_recv_msg_size_mib,omitempty"`

	// Middlewares middlewares for the gRPC server.
	Middlewares []configmiddleware.Config `mapstructure:"middlewares,omitempty"`

	// ReadBufferSize readBufferSize for gRPC server. See grpc.ReadBufferSize.
	// (https://godoc.org/google.golang.org/grpc#ReadBufferSize).
	ReadBufferSize int `mapstructure:"read_buffer_size,omitempty"`

	// TLS configures the protocol to use TLS. The default value is nil, which will cause the protocol to not use TLS.
	TLS configoptional.Optional[configtls.ServerConfig] `mapstructure:"tls,omitempty"`

	// WriteBufferSize writeBufferSize for gRPC server. See grpc.WriteBufferSize.
	// (https://godoc.org/google.golang.org/grpc#WriteBufferSize).
	WriteBufferSize int `mapstructure:"write_buffer_size,omitempty"`

	// NetAddr represents a network endpoint address.
	NetAddr confignet.AddrConfig `mapstructure:",squash"`

	// prevent unkeyed literal initialization
	_ struct{}
}

// Validate validates the ServerConfig fields according to schema annotations.
// Called by confmap on config resolution. Don't call it explicitly to avoid duplicate errors.
func (c *ServerConfig) Validate() error {
	var err error

	if inner_err := validateMaxRecvMsgSizeMiB(c.MaxRecvMsgSizeMiB); inner_err != nil {
		err = errors.Join(err, inner_err)
	}

	if c.ReadBufferSize < 0 {
		err = errors.Join(err, errors.New("read_buffer_size value must be greater than or equal to 0"))
	}

	if c.WriteBufferSize < 0 {
		err = errors.Join(err, errors.New("write_buffer_size value must be greater than or equal to 0"))
	}

	return err
}

// NewDefaultServerConfig returns a new ServerConfig with its default values applied.
// Prefer this over an empty struct literal so default changes stay in sync automatically.
func NewDefaultServerConfig() ServerConfig {
	addrConfig := confignet.NewDefaultAddrConfig()
	addrConfig.Transport = "tcp"
	return ServerConfig{
		Auth:      configoptional.None[configauth.Config](),
		Keepalive: configoptional.Some(NewDefaultKeepaliveServerConfig()),
		TLS:       configoptional.None[configtls.ServerConfig](),
		NetAddr:   addrConfig,
	}
}
