From 098c1d8cc489a45d0164c1bcafa1e63a010e3bad Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sat, 23 Nov 2024 17:53:11 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Make=20the=20'Option'=20type=20p?= =?UTF-8?q?rivate=20and=20add=20the=20'IOption'=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 12 ++++++------ readers/cli.go | 4 ++-- requests/client.go | 18 +++++++++--------- utils/types.go | 28 ++++++++++++++++++---------- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/config/config.go b/config/config.go index 99dae75..1f85d92 100644 --- a/config/config.go +++ b/config/config.go @@ -90,12 +90,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint { } type Config struct { - Method string `json:"method" validate:"http_method"` // custom validations: http_method - URL string `json:"url" validate:"http_url,required"` - Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` - DodosCount uint `json:"dodos_count" validate:"gte=1"` - RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` - NoProxyCheck utils.Option[bool] `json:"no_proxy_check"` + Method string `json:"method" validate:"http_method"` // custom validations: http_method + URL string `json:"url" validate:"http_url,required"` + Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` + DodosCount uint `json:"dodos_count" validate:"gte=1"` + RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` + NoProxyCheck utils.IOption[bool] `json:"no_proxy_check"` } func (config *Config) MergeConfigs(newConfig *Config) { diff --git a/readers/cli.go b/readers/cli.go index 82e9c62..2dfe975 100644 --- a/readers/cli.go +++ b/readers/cli.go @@ -12,8 +12,8 @@ import ( func CLIConfigReader() (*config.CLIConfig, error) { var ( - returnNil = false - cliConfig = &config.CLIConfig{ + returnNil = false + cliConfig = &config.CLIConfig{ Config: config.Config{ NoProxyCheck: utils.NewNoneOption[bool](), }, diff --git a/requests/client.go b/requests/client.go index da6010c..36a9859 100644 --- a/requests/client.go +++ b/requests/client.go @@ -47,15 +47,15 @@ func getClients( } clients = append(clients, &fasthttp.HostClient{ - MaxConns: int(maxConns), - IsTLS: isTLS, - Addr: addr, - Dial: dialFunc, - MaxIdleConnDuration: timeout, - MaxConnDuration: timeout, - WriteTimeout: timeout, - ReadTimeout: timeout, - }, + MaxConns: int(maxConns), + IsTLS: isTLS, + Addr: addr, + Dial: dialFunc, + MaxIdleConnDuration: timeout, + MaxConnDuration: timeout, + WriteTimeout: timeout, + ReadTimeout: timeout, + }, ) } return clients diff --git a/utils/types.go b/utils/types.go index 4f1d18e..109b3f0 100644 --- a/utils/types.go +++ b/utils/types.go @@ -5,20 +5,28 @@ import ( "errors" ) +type IOption[T any] interface { + IsNone() bool + ValueOrErr() (*T, error) + ValueOr(def *T) *T + ValueOrPanic() *T + UnmarshalJSON(data []byte) error +} + // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead. -type Option[T any] struct { +type option[T any] struct { // value holds the actual value of the Option if it is not None. value T // none indicates whether the Option is None (i.e., has no value). none bool } -func (o *Option[T]) IsNone() bool { +func (o *option[T]) IsNone() bool { return o.none } // The returned value can be nil, if the Option is None, it will return nil and an error. -func (o *Option[T]) ValueOrErr() (*T, error) { +func (o *option[T]) ValueOrErr() (*T, error) { if o.IsNone() { return nil, errors.New("Option is None") } @@ -26,7 +34,7 @@ func (o *Option[T]) ValueOrErr() (*T, error) { } // The returned value can't be nil, if the Option is None, it will return the default value. -func (o *Option[T]) ValueOr(def *T) *T { +func (o *option[T]) ValueOr(def *T) *T { if o.IsNone() { return def } @@ -34,14 +42,14 @@ func (o *Option[T]) ValueOr(def *T) *T { } // The returned value can't be nil, if the Option is None, it will panic. -func (o *Option[T]) ValueOrPanic() *T { +func (o *option[T]) ValueOrPanic() *T { if o.IsNone() { panic("Option is None") } return &o.value } -func (o *Option[T]) UnmarshalJSON(data []byte) error { +func (o *option[T]) UnmarshalJSON(data []byte) error { if string(data) == "null" { o.none = true return nil @@ -50,10 +58,10 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error { return json.Unmarshal(data, &o.value) } -func NewOption[T any](value T) Option[T] { - return Option[T]{value: value} +func NewOption[T any](value T) *option[T] { + return &option[T]{value: value} } -func NewNoneOption[T any]() Option[T] { - return Option[T]{none: true} +func NewNoneOption[T any]() *option[T] { + return &option[T]{none: true} }