mirror of https://github.com/coder/coder.git
maybe?
This commit is contained in:
parent
7824bee25f
commit
a5dc1fdac7
|
@ -0,0 +1,42 @@
|
|||
// Package maybe contains utility functions for an Optional data type.
|
||||
package maybe
|
||||
|
||||
// Maybe is a generic type that holds a pointer to a value of type T
|
||||
// and an error. Either *T will be nil, or error will be nil.
|
||||
type Maybe[T any] struct {
|
||||
val *T
|
||||
err error
|
||||
}
|
||||
|
||||
// Valid() returns true if Maybe contains a value of type *T.
|
||||
func (m *Maybe[T]) Valid() bool {
|
||||
return m.err == nil && m.val != nil
|
||||
}
|
||||
|
||||
// Error() returns the error contained in Maybe.
|
||||
func (m *Maybe[T]) Error() error {
|
||||
return m.err
|
||||
}
|
||||
|
||||
// Value() returns the value contained in Maybe.
|
||||
func (m *Maybe[T]) Value() *T {
|
||||
return m.val
|
||||
}
|
||||
|
||||
// Of is a function that takes a value of type T and returns
|
||||
// a valid Maybe holding a pointer to that value.
|
||||
func Of[T any](v T) *Maybe[T] {
|
||||
return &Maybe[T]{
|
||||
val: &v,
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// Not is a function that returns an invalid Maybe that
|
||||
// only contains the given error.
|
||||
func Not[T any](err error) *Maybe[T] {
|
||||
return &Maybe[T]{
|
||||
val: nil,
|
||||
err: err,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package maybe_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/util/maybe"
|
||||
)
|
||||
|
||||
func ExampleMaybe() {
|
||||
m1 := maybe.Of("hello")
|
||||
m2 := maybe.Not[string](xerrors.New("goodbye"))
|
||||
_, _ = fmt.Println(m1.Valid())
|
||||
_, _ = fmt.Println(*m1.Value())
|
||||
_, _ = fmt.Println(m1.Error())
|
||||
_, _ = fmt.Println(m2.Valid())
|
||||
_, _ = fmt.Println(m2.Value())
|
||||
_, _ = fmt.Println(m2.Error())
|
||||
// Output: true
|
||||
// hello
|
||||
// <nil>
|
||||
// false
|
||||
// <nil>
|
||||
// goodbye
|
||||
}
|
Loading…
Reference in New Issue