aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-10-18 19:33:33 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-10-18 19:33:33 +0200
commit29b3b63b404c6a2cec28ff5eb067efe0b054a37f (patch)
tree8086cc0a5bcf0bffb1458c19d7fae45f3b50a1df
parent793e1705b4c34405f24d0f95ab532b68e8db673a (diff)
downloadgo-gg-29b3b63b404c6a2cec28ff5eb067efe0b054a37f.tar.gz
add option.Option.Map()
-rw-r--r--CHANGELOG.md2
-rw-r--r--go.mod2
-rw-r--r--option/option.go11
-rw-r--r--option/option_test.go6
-rw-r--r--options/options.go7
-rw-r--r--options/options_test.go6
6 files changed, 22 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e968d4..9494b6e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ Changes:
- The minimum Go version was increased from 1.26 to 1.27 to facilitate the use of new APIs surrounding encoding/json/v2.
- jsonmatch: Pointer is now a synonym for jsontext.Pointer from the standard library.
+- option: Map() from the options package is now available as a method on type Option directly.
+ It is synonymous to options.Map(), which will be deprecated in a future release.
- All implementations of encoding/json.Marshaler and encoding/json.Unmarshaler on types in this module
have been replaced with implementations of encoding/json/v2.MarshalerTo and encoding/json/v2.UnmarshalerFrom.
This is technically a backwards-incompatibility for applications that call these methods directly,
diff --git a/go.mod b/go.mod
index 1c9c215..92a742a 100644
--- a/go.mod
+++ b/go.mod
@@ -1,4 +1,4 @@
module go.xyrillian.de/gg
-// TODO for 1.27: deprecate gg/options and offer methods on Option instead; add pathrouter.Context.Value[T]()
+// TODO for 1.27: add pathrouter.Context.Value[T]()
go 1.27
diff --git a/option/option.go b/option/option.go
index fc4cf3e..f89acf7 100644
--- a/option/option.go
+++ b/option/option.go
@@ -117,6 +117,8 @@ import (
"iter"
)
+// TODO: with 1.27 support for type variables on methods, consider additional methods referencing U from Rust's Option type
+
// Option is a type that contains either one or no instances of T.
type Option[T any] struct {
// NOTE: `None()` must yield the zero value of this type.
@@ -222,6 +224,15 @@ func (o Option[T]) Iter() iter.Seq[T] {
}
}
+// Map applies the given function to the value contained in o, if there is one.
+func (o Option[T]) Map[U any](mapping func(T) U) Option[U] {
+ if t, ok := o.Unpack(); ok {
+ return Some(mapping(t))
+ } else {
+ return None[U]()
+ }
+}
+
// Or returns the option itself if it contains a value, or otherwise returns "other".
//
// If you are passing the result of a function call, consider using OrElse() to avoid calling the function unless necessary.
diff --git a/option/option_test.go b/option/option_test.go
index 43d114e..20782a5 100644
--- a/option/option_test.go
+++ b/option/option_test.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"slices"
+ "strconv"
"testing"
"go.xyrillian.de/gg/assert"
@@ -74,6 +75,11 @@ func TestIter(t *testing.T) {
assert.Equal(t, slices.Collect(Some(42).Iter()), []int{42})
}
+func TestMap(t *testing.T) {
+ assert.Equal(t, None[int]().Map(strconv.Itoa), None[string]())
+ assert.Equal(t, Some(42).Map(strconv.Itoa), Some("42"))
+}
+
func TestOr(t *testing.T) {
none := None[int]()
assert.Equal(t, none.Or(none), None[int]())
diff --git a/options/options.go b/options/options.go
index 429f31a..c96cdf9 100644
--- a/options/options.go
+++ b/options/options.go
@@ -32,11 +32,8 @@ func IsNoneOrZero[T comparable](o Option[T]) bool {
// Map applies the given function to the value contained in o, if there is one.
func Map[T, U any](o Option[T], mapping func(T) U) Option[U] {
- if t, ok := o.Unpack(); ok {
- return Some(mapping(t))
- } else {
- return None[U]()
- }
+ // TODO: deprecate
+ return o.Map(mapping)
}
// Max returns the largest of its input values, while disregarding None values.
diff --git a/options/options_test.go b/options/options_test.go
index 89b9e14..acb11e4 100644
--- a/options/options_test.go
+++ b/options/options_test.go
@@ -4,7 +4,6 @@
package options
import (
- "strconv"
"testing"
"go.xyrillian.de/gg/assert"
@@ -22,11 +21,6 @@ func TestIsNoneOrZero(t *testing.T) {
assert.Equal(t, IsNoneOrZero(Some(1)), false)
}
-func TestMap(t *testing.T) {
- assert.Equal(t, Map(None[int](), strconv.Itoa), None[string]())
- assert.Equal(t, Map(Some(42), strconv.Itoa), Some("42"))
-}
-
func TestMax(t *testing.T) {
assert.Equal(t, Max[int](), None[int]())
assert.Equal(t, Max(None[int]()), None[int]())