Since this version, it begins supporting to handle the values of any type which supported by database/sql
on Scan
and Value
functions in that sql package. Thank you for the contributions, @RangelReale and @nikhrom.
go:linkname
for database/sql.convertAssign
#33 @moznionPublished by moznion over 1 year ago
database/sql/driver.Valuer
and database/sql.Scanner
on Option type #30UnwrapAsPtr()
method to unwrap the Option value as a pointer #28UnwrapAsPtr
#31Published by moznion almost 2 years ago
#OrElse()
function #22This feature changes the method signature of the #OrElse()
that was introduced by v0.9.0, becoming to take a function that provides the fallback optional value. And the old implementation has been renamed #Or()
.
Published by moznion almost 2 years ago
#OrElse()
that supports to return the Option
value according to the actual value existence #21
Published by moznion almost 2 years ago
Stringer
interface on Optional[T]
#19Published by moznion almost 2 years ago
FromNillable()
and PtrFromNillable()
#18These methods accept the nillable pointer value as an argument and make the Optional[T]
type value.
FromNillable()
If the given value is not nil, this returns Some[T]
value with doing value-dereference.
On the other hand, if the value is nil, this returns None[T]
.
example:
num := 123
some := FromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome()) // => true
fmt.Printf("%v\n", some.Unwrap()) // => 123
none := FromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => 0 (the default value of int)
PtrFromNillable()
If the given value is not nil, this returns Some[*T]
value without doing value-dereference.
On the other hand, if the value is nil, this returns None[*T]
.
example:
num := 123
some := PtrFromNillable[int](&num)
fmt.Printf("%v\n", some.IsSome()) // => true
fmt.Printf("%v\n", *some.Unwrap()) // => 123 (NOTE: it needs doing dereference)
none := PtrFromNillable[int](nil)
fmt.Printf("%v\n", none.IsSome()) // => false
fmt.Printf("%v\n", none.Unwrap()) // => nil
Published by moznion almost 2 years ago
omitempty
option on JSON marshaling #17This version brings up the support for the omitempty
option on JSON marshaling. If the property has that option and the value is None[T]
, it omits that property from the serialized JSON string.
example:
type JSONStruct struct {
OmitemptyVal Option[string] `json:"omitemptyVal,omitempty"` // this should be omitted
}
jsonStruct := &JSONStruct{OmitemptyVal: None[string]()}
marshal, err := json.Marshal(jsonStruct)
if err != nil {
return err
}
fmt.Printf("%s\n", marshal) // => {}
Published by moznion about 2 years ago
Published by moznion over 2 years ago
This version begins to support the following new methods:
FlatMap[T, U any](option Option[T], mapper func(v T) Option[U]) Option[U]
FlatMapOr[T, U any](option Option[T], fallbackValue U, mapper func(v T) Option[U]) U
FlatMapWithError[T, U any](option Option[T], mapper func(v T) (Option[U], error)) (Option[U], error)
FlatMapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (Option[U], error)) (U, error)
Please refer to the godoc for more details: https://pkg.go.dev/github.com/moznion/go-optional
Published by moznion over 2 years ago
Option[T]#Unwrap()
method #9 (@moznion)Published by moznion over 2 years ago
This version begins to support the following new methods:
func (o Option[T]) IfSome(f func(v T))
func (o Option[T]) IfSomeWithError(f func(v T) error) error
func (o Option[T]) IfNone(f func())
func (o Option[T]) IfNoneWithError(f func() error) erro
Please refer to the godoc for more details: https://pkg.go.dev/github.com/moznion/go-optional
Published by moznion over 2 years ago
MapWithError()
and MapOrWithError()
to map the optional value with error throwing #4 (@moznion)Published by moznion over 2 years ago
Initial Release