Compare commits

...

2 Commits

4 changed files with 39 additions and 27 deletions

View File

@ -90,12 +90,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint {
} }
type Config struct { type Config struct {
Method string `json:"method" validate:"http_method"` // custom validations: http_method Method string `json:"method" validate:"http_method"` // custom validations: http_method
URL string `json:"url" validate:"http_url,required"` URL string `json:"url" validate:"http_url,required"`
Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"` Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"`
DodosCount uint `json:"dodos_count" validate:"gte=1"` DodosCount uint `json:"dodos_count" validate:"gte=1"`
RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"` RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"`
NoProxyCheck utils.Option[bool] `json:"no_proxy_check"` NoProxyCheck utils.IOption[bool] `json:"no_proxy_check"`
} }
func (config *Config) MergeConfigs(newConfig *Config) { func (config *Config) MergeConfigs(newConfig *Config) {

View File

@ -12,8 +12,8 @@ import (
func CLIConfigReader() (*config.CLIConfig, error) { func CLIConfigReader() (*config.CLIConfig, error) {
var ( var (
returnNil = false returnNil = false
cliConfig = &config.CLIConfig{ cliConfig = &config.CLIConfig{
Config: config.Config{ Config: config.Config{
NoProxyCheck: utils.NewNoneOption[bool](), NoProxyCheck: utils.NewNoneOption[bool](),
}, },

View File

@ -47,15 +47,15 @@ func getClients(
} }
clients = append(clients, &fasthttp.HostClient{ clients = append(clients, &fasthttp.HostClient{
MaxConns: int(maxConns), MaxConns: int(maxConns),
IsTLS: isTLS, IsTLS: isTLS,
Addr: addr, Addr: addr,
Dial: dialFunc, Dial: dialFunc,
MaxIdleConnDuration: timeout, MaxIdleConnDuration: timeout,
MaxConnDuration: timeout, MaxConnDuration: timeout,
WriteTimeout: timeout, WriteTimeout: timeout,
ReadTimeout: timeout, ReadTimeout: timeout,
}, },
) )
} }
return clients return clients

View File

@ -5,20 +5,32 @@ import (
"errors" "errors"
) )
type NonNilConcrete interface {
~int | ~float64 | ~string | ~bool
}
type IOption[T NonNilConcrete] 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. // Don't call this struct directly, use NewOption[T] or NewNoneOption[T] instead.
type Option[T any] struct { type option[T NonNilConcrete] struct {
// value holds the actual value of the Option if it is not None. // value holds the actual value of the Option if it is not None.
value T value T
// none indicates whether the Option is None (i.e., has no value). // none indicates whether the Option is None (i.e., has no value).
none bool none bool
} }
func (o *Option[T]) IsNone() bool { func (o *option[T]) IsNone() bool {
return o.none return o.none
} }
// The returned value can be nil, if the Option is None, it will return nil and an error. // 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() { if o.IsNone() {
return nil, errors.New("Option is None") return nil, errors.New("Option is None")
} }
@ -26,7 +38,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. // 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() { if o.IsNone() {
return def return def
} }
@ -34,14 +46,14 @@ func (o *Option[T]) ValueOr(def *T) *T {
} }
// The returned value can't be nil, if the Option is None, it will panic. // 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() { if o.IsNone() {
panic("Option is None") panic("Option is None")
} }
return &o.value return &o.value
} }
func (o *Option[T]) UnmarshalJSON(data []byte) error { func (o *option[T]) UnmarshalJSON(data []byte) error {
if string(data) == "null" { if string(data) == "null" {
o.none = true o.none = true
return nil return nil
@ -50,10 +62,10 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &o.value) return json.Unmarshal(data, &o.value)
} }
func NewOption[T any](value T) Option[T] { func NewOption[T NonNilConcrete](value T) *option[T] {
return Option[T]{value: value} return &option[T]{value: value}
} }
func NewNoneOption[T any]() Option[T] { func NewNoneOption[T NonNilConcrete]() *option[T] {
return Option[T]{none: true} return &option[T]{none: true}
} }