Skip to content

Commit 7384c79

Browse files
committed
Improve documentation for option and result packages
1 parent 6f7779a commit 7384c79

File tree

4 files changed

+53
-39
lines changed

4 files changed

+53
-39
lines changed

iter/ops/unary.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"github.com/BooleanCat/go-functional/result"
66
)
77

8-
// UnwrapOption calls unwrap on an [option.Option].
8+
// UnwrapOption calls Unwrap on an [option.Option].
99
func UnwrapOption[T any](o option.Option[T]) T {
1010
return o.Unwrap()
1111
}
1212

13-
// UnwrapResult calls unwrap on a [result.Result].
13+
// UnwrapResult calls Unwrap on a [result.Result].
1414
func UnwrapResult[T any](r result.Result[T]) T {
1515
return r.Unwrap()
1616
}

option/json.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package option
22

33
import "encoding/json"
44

5+
// MarshalJSON implements the [json.Marshaler] interface.
6+
//
7+
// - [Some] variants will be marshaled as their underlying value.
8+
// - [None] variants will be marshaled as "null".
59
func (o Option[T]) MarshalJSON() ([]byte, error) {
610
if value, ok := o.Value(); ok {
711
return json.Marshal(value)
@@ -10,8 +14,10 @@ func (o Option[T]) MarshalJSON() ([]byte, error) {
1014
return []byte("null"), nil
1115
}
1216

13-
var _ json.Marshaler = Option[struct{}]{}
14-
17+
// UnmarshalJSON implements the [json.Unmarshaler] interface.
18+
//
19+
// - Values will be marshed as [Some] variants.
20+
// - "null"s will be marshaled as [None] variants.
1521
func (o *Option[T]) UnmarshalJSON(data []byte) error {
1622
*o = None[T]()
1723

@@ -26,4 +32,7 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error {
2632
return nil
2733
}
2834

29-
var _ json.Unmarshaler = &Option[struct{}]{}
35+
var (
36+
_ json.Unmarshaler = &Option[struct{}]{}
37+
_ json.Marshaler = Option[struct{}]{}
38+
)

option/option.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@ package option
22

33
import "fmt"
44

5-
// Option represents an optional value. The Some variant contains a value and
6-
// the None variant represents the absence of a value.
5+
// Option represents an optional value. The [Some] variant contains a value and
6+
// the [None] variant represents the absence of a value.
77
type Option[T any] struct {
88
value T
99
present bool
1010
}
1111

12-
// Some instantiates an Option with a value.
12+
// Some instantiates an [Option] with a value.
1313
func Some[T any](value T) Option[T] {
1414
return Option[T]{value, true}
1515
}
1616

17-
// None instantiates an Option with no value.
17+
// None instantiates an [Option] with no value.
1818
func None[T any]() Option[T] {
1919
return Option[T]{}
2020
}
2121

22+
// String implements the [fmt.Stringer] interface.
2223
func (o Option[T]) String() string {
2324
if o.present {
2425
return fmt.Sprintf("Some(%v)", o.value)
@@ -29,8 +30,8 @@ func (o Option[T]) String() string {
2930

3031
var _ fmt.Stringer = Option[struct{}]{}
3132

32-
// Unwrap returns the underlying value of a Some variant, or panics if called
33-
// on a None variant.
33+
// Unwrap returns the underlying value of a [Some] variant, or panics if called
34+
// on a [None] variant.
3435
func (o Option[T]) Unwrap() T {
3536
if o.present {
3637
return o.value
@@ -39,8 +40,8 @@ func (o Option[T]) Unwrap() T {
3940
panic("called `Option.Unwrap()` on a `None` value")
4041
}
4142

42-
// UnwrapOr returns the underlying value of a Some variant, or the provided
43-
// value on a None variant.
43+
// UnwrapOr returns the underlying value of a [Some] variant, or the provided
44+
// value on a [None] variant.
4445
func (o Option[T]) UnwrapOr(value T) T {
4546
if o.present {
4647
return o.value
@@ -49,8 +50,8 @@ func (o Option[T]) UnwrapOr(value T) T {
4950
return value
5051
}
5152

52-
// UnwrapOrElse returns the underlying value of a Some variant, or the result
53-
// of calling the provided function on a None variant.
53+
// UnwrapOrElse returns the underlying value of a [Some] variant, or the result
54+
// of calling the provided function on a [None] variant.
5455
func (o Option[T]) UnwrapOrElse(f func() T) T {
5556
if o.present {
5657
return o.value
@@ -59,8 +60,8 @@ func (o Option[T]) UnwrapOrElse(f func() T) T {
5960
return f()
6061
}
6162

62-
// UnwrapOrZero returns the underlying value of a Some variant, or the zero
63-
// value on a None variant.
63+
// UnwrapOrZero returns the underlying value of a [Some] variant, or the zero
64+
// value on a [None] variant.
6465
func (o Option[T]) UnwrapOrZero() T {
6566
if o.present {
6667
return o.value
@@ -70,18 +71,18 @@ func (o Option[T]) UnwrapOrZero() T {
7071
return value
7172
}
7273

73-
// IsSome returns true if the Option contains a value.
74+
// IsSome returns true if the [Option] is a [Some] variant.
7475
func (o Option[T]) IsSome() bool {
7576
return o.present
7677
}
7778

78-
// IsNone returns true if the Option does not contain a value.
79+
// IsNone returns true if the [Option] is a [None] varaint.
7980
func (o Option[T]) IsNone() bool {
8081
return !o.present
8182
}
8283

83-
// Value returns the underlying value and true for a Some variant, or the zero
84-
// value and false for a None variant.
84+
// Value returns the underlying value and true for a [Some] variant, or the
85+
// zero value and false for a [None] variant.
8586
func (o Option[T]) Value() (T, bool) {
8687
return o.value, o.present
8788
}

result/result.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@ package result
22

33
import "fmt"
44

5-
// Result represents failure or success. The Ok variant represents success and
6-
// contains a value. The Err variant represent a failure and contains an error.
5+
// Result represents failure or success. The [Ok] variant represents success
6+
// and contains a value. The [Err] variant represent a failure and contains an
7+
// error.
78
type Result[T any] struct {
89
value T
910
err error
1011
}
1112

12-
// Ok instanriates a successful result with a value.
13+
// Ok instantiates a [Result] with a value.
1314
func Ok[T any](value T) Result[T] {
1415
return Result[T]{value, nil}
1516
}
1617

17-
// Err instanitates a failed result with an error.
18+
// Err instantiates a [Result] with an error.
1819
func Err[T any](err error) Result[T] {
1920
return Result[T]{err: err}
2021
}
2122

23+
// String implements the [fmt.Stringer] interface.
2224
func (r Result[T]) String() string {
2325
if r.err == nil {
2426
return fmt.Sprintf("Ok(%v)", r.value)
@@ -27,8 +29,10 @@ func (r Result[T]) String() string {
2729
return fmt.Sprintf("Err(%s)", r.err.Error())
2830
}
2931

30-
// Unwrap returns the underlying value of an Ok variant, or panics if called
31-
// on an Err variant.
32+
var _ fmt.Stringer = Result[struct{}]{}
33+
34+
// Unwrap returns the underlying value of an [Ok] variant, or panics if called
35+
// on an [Err] variant.
3236
func (r Result[T]) Unwrap() T {
3337
if r.err == nil {
3438
return r.value
@@ -37,8 +41,8 @@ func (r Result[T]) Unwrap() T {
3741
panic("called `Result.Unwrap()` on an `Err` value")
3842
}
3943

40-
// UnwrapOr returns the underlying value of an Ok variant, or the provided
41-
// value on an Err variant.
44+
// UnwrapOr returns the underlying value of an [Ok] variant, or the provided
45+
// value on an [Err] variant.
4246
func (r Result[T]) UnwrapOr(value T) T {
4347
if r.err == nil {
4448
return r.value
@@ -47,8 +51,8 @@ func (r Result[T]) UnwrapOr(value T) T {
4751
return value
4852
}
4953

50-
// UnwrapOrElse returns the underlying value of an Ok variant, or the result
51-
// of calling the provided function on an Err variant.
54+
// UnwrapOrElse returns the underlying value of an [Ok] variant, or the result
55+
// of calling the provided function on an [Err] variant.
5256
func (r Result[T]) UnwrapOrElse(f func() T) T {
5357
if r.err == nil {
5458
return r.value
@@ -57,8 +61,8 @@ func (r Result[T]) UnwrapOrElse(f func() T) T {
5761
return f()
5862
}
5963

60-
// UnwrapOrZero returns the underlying value of an Ok variant, or the zero
61-
// value of an Err variant.
64+
// UnwrapOrZero returns the underlying value of an [Ok] variant, or the zero
65+
// value of an [Err] variant.
6266
func (r Result[T]) UnwrapOrZero() T {
6367
if r.err == nil {
6468
return r.value
@@ -68,24 +72,24 @@ func (r Result[T]) UnwrapOrZero() T {
6872
return value
6973
}
7074

71-
// IsOk returns true if the Result is an Ok variant.
75+
// IsOk returns true if the [Result] is an [Ok] variant.
7276
func (r Result[T]) IsOk() bool {
7377
return r.err == nil
7478
}
7579

76-
// IsErr returns true if the Result is an Err variant.
80+
// IsErr returns true if the [Result] is an [Err] variant.
7781
func (r Result[T]) IsErr() bool {
7882
return r.err != nil
7983
}
8084

81-
// Value returns the underlying value and nil for an Ok variant, or the zero
82-
// value and an error for an Error variant.
85+
// Value returns the underlying value and nil for an [Ok] variant, or the zero
86+
// value and an error for an [Err] variant.
8387
func (r Result[T]) Value() (T, error) {
8488
return r.value, r.err
8589
}
8690

87-
// Unwrap returns the underlying error of an Err variant, or panics if called
88-
// on an Ok variant.
91+
// UnwrapErr returns the underlying error of an [Err] variant, or panics if called
92+
// on an [Ok] variant.
8993
func (r Result[T]) UnwrapErr() error {
9094
if r.IsOk() {
9195
panic("called `Result.UnwrapErr()` on an `Ok` value")

0 commit comments

Comments
 (0)