opt

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package opt provides generic optional values with JSON and BSON encoding. The opt/nullable subpackage provides a separate nullable value type.

Index

Examples

Constants

This section is empty.

Variables

View Source
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 None

func None[T any]() Option[T]

None returns an Option with a None value.

func Some

func Some[T any](value T) Option[T]

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]) IsSome

func (o *Option[T]) IsSome() bool

Methods IsSome returns true if the option is a Some value.

func (Option[T]) MarshalBSONValue

func (o Option[T]) MarshalBSONValue() (t bsontype.Type, data []byte, err error)

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

func (o Option[T]) MarshalJSON() (data []byte, err error)

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

func (o *Option[T]) UnmarshalBSONValue(t bsontype.Type, data []byte) (err error)

UnmarshalBSONValue indicates how to unmarshal a bson value into an Option.

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) (err error)

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

func (*Option[T]) Unwrap

func (o *Option[T]) Unwrap() (t T)

Unwrap returns a copy of the inner value of a Some. If the Option is a None, Unwrap panics.

Directories

Path Synopsis
Package nullable provides a generic value with an explicit null state.
Package nullable provides a generic value with an explicit null state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL