Documentation
¶
Overview ¶
Package opt provides generic optional values with JSON and BSON encoding. The opt/nullable subpackage provides a separate nullable value type.
Index ¶
- Variables
- type Option
- func (o *Option[T]) IsSome() bool
- func (o Option[T]) MarshalBSONValue() (t bsontype.Type, data []byte, err error)
- func (o Option[T]) MarshalJSON() (data []byte, err error)
- func (o *Option[T]) UnmarshalBSONValue(t bsontype.Type, data []byte) (err error)
- func (o *Option[T]) UnmarshalJSON(data []byte) (err error)
- func (o *Option[T]) Unwrap() (t T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnwrapNone is retained from the original optional package. // Unwrap currently panics with a string, not this error value. ErrUnwrapNone = errors.New("cannot unwrap None") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option represents a value that may be absent. Its zero value is None. Some and Unwrap copy T without deep copying reference values. JSON and BSON decoding mutate the option; callers must synchronize concurrent mutation.
func Some ¶
Constructors Some returns an Option with a Some value.
Example ¶
package main
import (
"fmt"
"github.com/cohesian-tools/typy/opt"
)
func main() {
value := opt.Some(0)
missing := opt.None[int]()
fmt.Println(value.IsSome(), value.Unwrap())
fmt.Println(missing.IsSome())
}
Output: true 0 false
func (Option[T]) MarshalBSONValue ¶
MarshalBSONValue indicates how to marshal an Option into a bson value.
Example ¶
package main
import (
"fmt"
"github.com/cohesian-tools/typy/opt"
"go.mongodb.org/mongo-driver/bson"
)
func main() {
type document struct {
Name opt.Option[string] `bson:"name"`
}
data, err := bson.Marshal(document{Name: opt.Some("Lucas")})
if err != nil {
panic(err)
}
var decoded document
if err := bson.Unmarshal(data, &decoded); err != nil {
panic(err)
}
fmt.Println(decoded.Name.Unwrap())
}
Output: Lucas
func (Option[T]) MarshalJSON ¶
MarshalJSON indicates how to marshal an Option into a json value. marshalling (always work with value) json.Marshal: always sends a non-ptr of the field
- interface: not always accepts non-ptr > case non-ptr: works > case ptr: does not work (go rules)
cases - method [non-ptr] -> works ()
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/cohesian-tools/typy/opt"
)
func main() {
for _, value := range []opt.Option[int]{opt.Some(42), opt.None[int]()} {
data, err := json.Marshal(value)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}
}
Output: 42 null
func (*Option[T]) UnmarshalBSONValue ¶
UnmarshalBSONValue indicates how to unmarshal a bson value into an Option.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON indicates how to unmarshal a json value into an Option. unmarshalling (always work with reference) json.Unmarshal: always sends a ptr of the field - interface: always accepts ptr | no matter method receiver
cases - method [ptr-receiver] -> works - method [non-ptr] -> does not work (can't access to the field of &o.Value)
Example ¶
package main
import (
"encoding/json"
"fmt"
"github.com/cohesian-tools/typy/opt"
)
func main() {
var payload struct {
Name opt.Option[string] `json:"name"`
}
for _, input := range []string{`{"name":"Lucas"}`, `{}`, `{"name":null}`} {
if err := json.Unmarshal([]byte(input), &payload); err != nil {
panic(err)
}
fmt.Println(payload.Name.IsSome())
}
}
Output: true true false