Compare commits

..

1 Commits

2 changed files with 19 additions and 30 deletions

View File

@ -81,7 +81,7 @@ func main() {
Proxies: jsonConf.Proxies, Proxies: jsonConf.Proxies,
Body: jsonConf.Body, Body: jsonConf.Body,
Yes: cliConf.Yes, Yes: cliConf.Yes,
NoProxyCheck: conf.NoProxyCheck.ValueOrPanic(), NoProxyCheck: *conf.NoProxyCheck.ValueOrPanic(),
} }
requestConf.Print() requestConf.Print()
if !cliConf.Yes { if !cliConf.Yes {

View File

@ -5,20 +5,20 @@ import (
"errors" "errors"
) )
type NonNilT interface { type NonNilConcrete interface {
~int | ~float64 | ~string | ~bool ~int | ~float64 | ~string | ~bool
} }
type Option[T NonNilT] interface { type Option[T NonNilConcrete] interface {
IsNone() bool IsNone() bool
ValueOrErr() (T, error) ValueOrErr() (*T, error)
ValueOr(def T) T ValueOr(def *T) *T
ValueOrPanic() T ValueOrPanic() *T
UnmarshalJSON(data []byte) error 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 NonNilT] 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).
@ -29,39 +29,28 @@ func (o *option[T]) IsNone() bool {
return o.none return o.none
} }
// If the Option is None, it will return zero value of the type 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 o.value, errors.New("Option is None") return nil, errors.New("Option is None")
} }
return o.value, nil return &o.value, 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
} }
return o.value return &o.value
} }
// 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]) SetValue(value T) {
o.value = value
o.none = false
}
func (o *option[T]) SetNone() {
var zeroValue T
o.value = zeroValue
o.none = true
} }
func (o *option[T]) UnmarshalJSON(data []byte) error { func (o *option[T]) UnmarshalJSON(data []byte) error {
@ -73,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 NonNilT](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 NonNilT]() *option[T] { func NewNoneOption[T NonNilConcrete]() *option[T] {
return &option[T]{none: true} return &option[T]{none: true}
} }