Skip to content

Commit 7d0ef32

Browse files
authored
feat: Add Option[T] and Result[T] types via samber/mo (#189)
1 parent 1dcf4ec commit 7d0ef32

File tree

6 files changed

+897
-1
lines changed

6 files changed

+897
-1
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ require (
1414
github.com/lib/pq v1.10.9
1515
github.com/prometheus/client_golang v1.23.2
1616
github.com/redis/go-redis/v9 v9.16.0
17+
github.com/samber/lo v1.52.0
18+
github.com/samber/mo v1.16.0
1719
github.com/shopspring/decimal v1.4.0
1820
github.com/sony/gobreaker/v2 v2.0.0
1921
github.com/spf13/cobra v1.10.1
@@ -42,7 +44,6 @@ require (
4244
github.com/prometheus/client_model v0.6.2 // indirect
4345
github.com/prometheus/common v0.66.1 // indirect
4446
github.com/prometheus/procfs v0.16.1 // indirect
45-
github.com/samber/lo v1.52.0 // indirect
4647
github.com/spf13/pflag v1.0.10 // indirect
4748
github.com/stretchr/objx v0.5.2 // indirect
4849
go.yaml.in/yaml/v2 v2.4.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,8 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
12571257
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
12581258
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
12591259
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
1260+
github.com/samber/mo v1.16.0 h1:qpEPCI63ou6wXlsNDMLE0IIN8A+devbGX/K1xdgr4b4=
1261+
github.com/samber/mo v1.16.0/go.mod h1:DlgzJ4SYhOh41nP1L9kh9rDNERuf8IqWSAs+gj2Vxag=
12601262
github.com/secure-systems-lab/go-securesystemslib v0.4.0 h1:b23VGrQhTA8cN2CbBw7/FulN9fTtqYUdS5+Oxzt+DUE=
12611263
github.com/secure-systems-lab/go-securesystemslib v0.4.0/go.mod h1:FGBZgq2tXWICsxWQW1msNf49F0Pf2Op5Htayx335Qbs=
12621264
github.com/serialx/hashring v0.0.0-20200727003509-22c0c7ab6b1b h1:h+3JX2VoWTFuyQEo87pStk/a99dzIO1mM9KxIyLPGTU=

pkg/platform/types/doc.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Package types provides type aliases and helpers for explicit error and nil handling.
2+
//
3+
// This package re-exports types from github.com/samber/mo for consistent usage
4+
// across the Meridian codebase. The samber/mo library provides battle-tested
5+
// implementations of functional programming primitives.
6+
//
7+
// # Option[T] - Explicit nil handling
8+
//
9+
// Option represents an optional value that may or may not be present.
10+
// Use it to eliminate nil pointer dereference bugs at compile time.
11+
//
12+
// import "github.com/samber/mo"
13+
//
14+
// // Creating Options
15+
// opt := mo.Some(42) // Contains a value
16+
// opt := mo.None[int]() // Empty option
17+
//
18+
// // Checking and extracting values
19+
// if opt.IsPresent() {
20+
// value := opt.MustGet() // Safe after IsPresent check
21+
// }
22+
//
23+
// // Safe extraction with defaults
24+
// value := opt.OrElse(0) // Returns 0 if None
25+
//
26+
// // Functional composition
27+
// doubled := opt.Map(func(x int) int { return x * 2 })
28+
//
29+
// # Result[T] - Explicit error handling
30+
//
31+
// Result represents either a successful value or an error.
32+
// Use it to make error handling explicit and prevent ignored errors.
33+
//
34+
// import "github.com/samber/mo"
35+
//
36+
// // Creating Results
37+
// result := mo.Ok(42) // Success
38+
// result := mo.Err[int](errors.New("")) // Failure
39+
//
40+
// // Wrapping traditional Go returns
41+
// result := mo.TupleToResult(strconv.Atoi("123"))
42+
//
43+
// // Checking and extracting
44+
// if result.IsOk() {
45+
// value := result.MustGet() // Safe after IsOk check
46+
// }
47+
//
48+
// // Functional composition
49+
// mapped := result.Map(func(x int) int { return x * 2 })
50+
//
51+
// # Why use mo types?
52+
//
53+
// - Compile-time safety: Prevents nil dereferences and ignored errors
54+
// - Self-documenting: Function signatures clearly show optional/fallible operations
55+
// - Composable: Chain operations without nested if/else blocks
56+
// - Zero overhead: Benchmarks show no performance cost vs traditional patterns
57+
//
58+
// For more details, see: https://github.com/samber/mo
59+
package types //nolint:revive // "types" is intentional, similar to go/types in stdlib

0 commit comments

Comments
 (0)