1
0
mirror of https://github.com/IBM/fp-go.git synced 2026-01-29 10:36:04 +02:00

Compare commits

...

7 Commits

Author SHA1 Message Date
Dr. Carsten Leue
eafc008798 fix: doc for lens generation
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-23 16:00:11 +01:00
Dr. Carsten Leue
46bf065e34 fix: migrate CLI to github.com/urfave/v3
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-23 12:56:23 +01:00
renovate[bot]
b4e303423b chore(deps): update actions/checkout action to v6.0.2 (#153)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2026-01-23 12:39:53 +01:00
renovate[bot]
7afc098f58 fix(deps): update go dependencies (major) (#144)
* fix(deps): update go dependencies

* fix: fix renovate config

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>

---------

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-23 11:28:56 +01:00
Dr. Carsten Leue
617e43de19 fix: improve codec
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-23 11:12:40 +01:00
Dr. Carsten Leue
0f7a6c0589 fix: prism doc
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-22 13:57:07 +01:00
Dr. Carsten Leue
e7f78e1a33 fix: more codecs and cleanup of type hints
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2026-01-21 09:23:48 +01:00
95 changed files with 13385 additions and 375 deletions

View File

@@ -28,11 +28,11 @@ jobs:
fail-fast: false # Continue with other versions if one fails
steps:
# full checkout for semantic-release
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
cache: true # Enable Go module caching
@@ -66,11 +66,11 @@ jobs:
matrix:
go-version: ['1.24.x', '1.25.x']
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
cache: true # Enable Go module caching
@@ -126,17 +126,17 @@ jobs:
steps:
# full checkout for semantic-release
- name: Full checkout
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
with:
node-version: ${{ env.NODE_VERSION }}
- name: Set up Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: ${{ env.LATEST_GO_VERSION }}
cache: true # Enable Go module caching

16
go.sum
View File

@@ -1,7 +1,3 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -10,20 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.0 h1:ib4sjIrwZKxE5u/Japgo/7SJV3PvgjGiRNAvTVGqQl8=
github.com/stretchr/testify v1.11.0/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=

View File

@@ -22,7 +22,8 @@
"matchDepTypes": [
"golang"
],
"enabled": false
"enabled": false,
"description": "Disable updates to the go directive in go.mod files - the directive identifies the minimum compatible Go version and should stay as small as possible for maximum compatibility"
},
{
"matchUpdateTypes": [

View File

@@ -465,7 +465,7 @@ func process() IOResult[string] {
- **ReaderIOResult** - Combine Reader, IO, and Result for complex workflows
- **Array** - Functional array operations
- **Record** - Functional record/map operations
- **Optics** - Lens, Prism, Optional, and Traversal for immutable updates
- **[Optics](./optics/README.md)** - Lens, Prism, Optional, and Traversal for immutable updates
#### Idiomatic Packages (Tuple-based, High Performance)
- **idiomatic/option** - Option monad using native Go `(value, bool)` tuples

View File

@@ -190,6 +190,11 @@ func MonadReduce[A, B any](fa []A, f func(B, A) B, initial B) B {
return G.MonadReduce(fa, f, initial)
}
//go:inline
func MonadReduceWithIndex[A, B any](fa []A, f func(int, B, A) B, initial B) B {
return G.MonadReduceWithIndex(fa, f, initial)
}
// Reduce folds an array from left to right, applying a function to accumulate a result.
//
// Example:

View File

@@ -764,14 +764,14 @@ func TestFoldMap(t *testing.T) {
t.Run("FoldMap with sum semigroup", func(t *testing.T) {
sumSemigroup := N.SemigroupSum[int]()
arr := From(1, 2, 3, 4)
result := FoldMap[int, int](sumSemigroup)(func(x int) int { return x * 2 })(arr)
result := FoldMap[int](sumSemigroup)(func(x int) int { return x * 2 })(arr)
assert.Equal(t, 20, result) // (1*2) + (2*2) + (3*2) + (4*2) = 20
})
t.Run("FoldMap with string concatenation", func(t *testing.T) {
concatSemigroup := STR.Semigroup
arr := From(1, 2, 3)
result := FoldMap[int, string](concatSemigroup)(func(x int) string { return fmt.Sprintf("%d", x) })(arr)
result := FoldMap[int](concatSemigroup)(func(x int) string { return fmt.Sprintf("%d", x) })(arr)
assert.Equal(t, "123", result)
})
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func generateTraverseTuple(f *os.File, i int) {
@@ -422,10 +423,10 @@ func ApplyCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateApplyHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func createCombinations(n int, all, prev []int) [][]int {
@@ -284,10 +285,10 @@ func BindCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateBindHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,7 +16,7 @@
package cli
import (
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func Commands() []*C.Command {

View File

@@ -16,7 +16,7 @@
package cli
import (
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
const (

View File

@@ -16,6 +16,7 @@
package cli
import (
"context"
"fmt"
"log"
"os"
@@ -23,7 +24,7 @@ import (
"strings"
A "github.com/IBM/fp-go/v2/array"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
// Deprecated:
@@ -261,10 +262,10 @@ func ContextReaderIOEitherCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateContextReaderIOEitherHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func generateMakeProvider(f *os.File, i int) {
@@ -221,10 +222,10 @@ func DICommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateDIHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func eitherHKT(typeE string) func(typeA string) string {
@@ -190,10 +191,10 @@ func EitherCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateEitherHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func identityHKT(typeA string) string {
@@ -93,10 +94,10 @@ func IdentityCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateIdentityHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,6 +16,7 @@
package cli
import (
"context"
"fmt"
"log"
"os"
@@ -23,7 +24,7 @@ import (
"time"
A "github.com/IBM/fp-go/v2/array"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func nonGenericIO(param string) string {
@@ -102,10 +103,10 @@ func IOCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateIOHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,6 +16,7 @@
package cli
import (
"context"
"fmt"
"log"
"os"
@@ -23,7 +24,7 @@ import (
"time"
A "github.com/IBM/fp-go/v2/array"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
// [GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GTAB ~func() ET.Either[E, T.Tuple2[A, B]], E, A, B any](a GA, b GB) GTAB {
@@ -273,10 +274,10 @@ func IOEitherCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateIOEitherHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,6 +16,7 @@
package cli
import (
"context"
"fmt"
"log"
"os"
@@ -23,7 +24,7 @@ import (
"time"
A "github.com/IBM/fp-go/v2/array"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func nonGenericIOOption(param string) string {
@@ -107,10 +108,10 @@ func IOOptionCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateIOOptionHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -17,6 +17,7 @@ package cli
import (
"bytes"
"context"
"go/ast"
"go/parser"
"go/token"
@@ -28,7 +29,7 @@ import (
"text/template"
S "github.com/IBM/fp-go/v2/string"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
const (
@@ -934,12 +935,12 @@ func LensCommand() *C.Command {
flagVerbose,
flagIncludeTestFiles,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateLensHelpers(
ctx.String(keyLensDir),
ctx.String(keyFilename),
ctx.Bool(keyVerbose),
ctx.Bool(keyIncludeTestFile),
cmd.String(keyLensDir),
cmd.String(keyFilename),
cmd.Bool(keyVerbose),
cmd.Bool(keyIncludeTestFile),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func optionHKT(typeA string) string {
@@ -200,10 +201,10 @@ func OptionCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateOptionHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func generateUnsliced(f *os.File, i int) {
@@ -423,10 +424,10 @@ func PipeCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generatePipeHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func generateReaderFrom(f, fg *os.File, i int) {
@@ -154,10 +155,10 @@ func ReaderCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateReaderHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,13 +16,14 @@
package cli
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func generateReaderIOEitherFrom(f, fg *os.File, i int) {
@@ -284,10 +285,10 @@ func ReaderIOEitherCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateReaderIOEitherHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -16,6 +16,7 @@
package cli
import (
"context"
"fmt"
"log"
"os"
@@ -23,7 +24,7 @@ import (
"strings"
"time"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func writeTupleType(f *os.File, symbol string, i int) {
@@ -615,10 +616,10 @@ func TupleCommand() *C.Command {
flagCount,
flagFilename,
},
Action: func(ctx *C.Context) error {
Action: func(ctx context.Context, cmd *C.Command) error {
return generateTupleHelpers(
ctx.String(keyFilename),
ctx.Int(keyCount),
cmd.String(keyFilename),
cmd.Int(keyCount),
)
},
}

View File

@@ -177,3 +177,255 @@ func Local[R1, R2 any](f func(R2) R1) Operator[R1, R2] {
}
}
}
// Compose is an alias for Local that emphasizes the composition aspect of consumer transformation.
// It composes a preprocessing function with a consumer, creating a new consumer that applies
// the function before consuming the value.
//
// This function is semantically identical to Local but uses terminology that may be more familiar
// to developers coming from functional programming backgrounds where "compose" is a common operation.
//
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#profunctor
//
// The name "Compose" highlights that we're composing two operations:
// 1. The transformation function f: R2 -> R1
// 2. The consumer c: R1 -> ()
//
// Result: A composed consumer: R2 -> ()
//
// Type Parameters:
// - R1: The input type of the original Consumer (what it expects)
// - R2: The input type of the new Consumer (what you have)
//
// Parameters:
// - f: A function that converts R2 to R1 (preprocessing function)
//
// Returns:
// - An Operator that transforms Consumer[R1] into Consumer[R2]
//
// Example - Basic composition:
//
// // Consumer that logs integers
// logInt := func(x int) {
// fmt.Printf("Value: %d\n", x)
// }
//
// // Compose with a string-to-int parser
// parseToInt := func(s string) int {
// n, _ := strconv.Atoi(s)
// return n
// }
//
// logString := consumer.Compose(parseToInt)(logInt)
// logString("42") // Logs: "Value: 42"
//
// Example - Composing multiple transformations:
//
// type Data struct {
// Value string
// }
//
// type Wrapper struct {
// Data Data
// }
//
// // Consumer that logs strings
// logString := func(s string) {
// fmt.Println(s)
// }
//
// // Compose transformations step by step
// extractData := func(w Wrapper) Data { return w.Data }
// extractValue := func(d Data) string { return d.Value }
//
// logData := consumer.Compose(extractValue)(logString)
// logWrapper := consumer.Compose(extractData)(logData)
//
// logWrapper(Wrapper{Data: Data{Value: "Hello"}}) // Logs: "Hello"
//
// Example - Function composition style:
//
// // Compose is particularly useful when thinking in terms of function composition
// type Request struct {
// Body []byte
// }
//
// // Consumer that processes strings
// processString := func(s string) {
// fmt.Printf("Processing: %s\n", s)
// }
//
// // Compose byte-to-string conversion with processing
// bytesToString := func(b []byte) string {
// return string(b)
// }
// extractBody := func(r Request) []byte {
// return r.Body
// }
//
// // Chain compositions
// processBytes := consumer.Compose(bytesToString)(processString)
// processRequest := consumer.Compose(extractBody)(processBytes)
//
// processRequest(Request{Body: []byte("test")}) // Logs: "Processing: test"
//
// Relationship to Local:
// - Compose and Local are identical in implementation
// - Compose emphasizes the functional composition aspect
// - Local emphasizes the environment/context transformation aspect
// - Use Compose when thinking about function composition
// - Use Local when thinking about adapting to different contexts
//
// Use Cases:
// - Building processing pipelines with clear composition semantics
// - Adapting consumers in a functional programming style
// - Creating reusable consumer transformations
// - Chaining multiple preprocessing steps
func Compose[R1, R2 any](f func(R2) R1) Operator[R1, R2] {
return Local(f)
}
// Contramap is the categorical name for the contravariant functor operation on Consumers.
// It transforms a Consumer by preprocessing its input, making it the dual of the covariant
// functor's map operation.
//
// See: https://github.com/fantasyland/fantasy-land?tab=readme-ov-file#contravariant
//
// In category theory, a contravariant functor reverses the direction of morphisms.
// While a covariant functor maps f: A -> B to map(f): F[A] -> F[B],
// a contravariant functor maps f: A -> B to contramap(f): F[B] -> F[A].
//
// For Consumers:
// - Consumer[A] is contravariant in A
// - Given f: R2 -> R1, contramap(f) transforms Consumer[R1] to Consumer[R2]
// - The direction is reversed: we go from Consumer[R1] to Consumer[R2]
//
// This is semantically identical to Local and Compose, but uses the standard
// categorical terminology that emphasizes the contravariant nature of the transformation.
//
// Type Parameters:
// - R1: The input type of the original Consumer (what it expects)
// - R2: The input type of the new Consumer (what you have)
//
// Parameters:
// - f: A function that converts R2 to R1 (preprocessing function)
//
// Returns:
// - An Operator that transforms Consumer[R1] into Consumer[R2]
//
// Example - Basic contravariant mapping:
//
// // Consumer that logs integers
// logInt := func(x int) {
// fmt.Printf("Value: %d\n", x)
// }
//
// // Contramap with a string-to-int parser
// parseToInt := func(s string) int {
// n, _ := strconv.Atoi(s)
// return n
// }
//
// logString := consumer.Contramap(parseToInt)(logInt)
// logString("42") // Logs: "Value: 42"
//
// Example - Demonstrating contravariance:
//
// // In covariant functors (like Option, Array), map goes "forward":
// // map: (A -> B) -> F[A] -> F[B]
// //
// // In contravariant functors (like Consumer), contramap goes "backward":
// // contramap: (B -> A) -> F[A] -> F[B]
//
// type Animal struct{ Name string }
// type Dog struct{ Animal Animal; Breed string }
//
// // Consumer for animals
// consumeAnimal := func(a Animal) {
// fmt.Printf("Animal: %s\n", a.Name)
// }
//
// // Function from Dog to Animal (B -> A)
// dogToAnimal := func(d Dog) Animal {
// return d.Animal
// }
//
// // Contramap creates Consumer[Dog] from Consumer[Animal]
// // Direction is reversed: Consumer[Animal] -> Consumer[Dog]
// consumeDog := consumer.Contramap(dogToAnimal)(consumeAnimal)
//
// consumeDog(Dog{
// Animal: Animal{Name: "Buddy"},
// Breed: "Golden Retriever",
// }) // Logs: "Animal: Buddy"
//
// Example - Contravariant functor laws:
//
// // Law 1: Identity
// // contramap(identity) = identity
// identity := func(x int) int { return x }
// consumer1 := consumer.Contramap(identity)(consumeInt)
// // consumer1 behaves identically to consumeInt
//
// // Law 2: Composition
// // contramap(f . g) = contramap(g) . contramap(f)
// // Note: composition order is reversed compared to covariant map
// f := func(s string) int { n, _ := strconv.Atoi(s); return n }
// g := func(b bool) string { if b { return "1" } else { return "0" } }
//
// // These two are equivalent:
// consumer2 := consumer.Contramap(func(b bool) int { return f(g(b)) })(consumeInt)
// consumer3 := consumer.Contramap(g)(consumer.Contramap(f)(consumeInt))
//
// Example - Practical use with type hierarchies:
//
// type Logger interface {
// Log(string)
// }
//
// type Message struct {
// Text string
// Timestamp time.Time
// }
//
// // Consumer that logs strings
// logString := func(s string) {
// fmt.Println(s)
// }
//
// // Contramap to handle Message types
// extractText := func(m Message) string {
// return fmt.Sprintf("[%s] %s", m.Timestamp.Format(time.RFC3339), m.Text)
// }
//
// logMessage := consumer.Contramap(extractText)(logString)
// logMessage(Message{
// Text: "Hello",
// Timestamp: time.Now(),
// }) // Logs: "[2024-01-20T10:00:00Z] Hello"
//
// Relationship to Local and Compose:
// - Contramap, Local, and Compose are identical in implementation
// - Contramap emphasizes the categorical/theoretical aspect
// - Local emphasizes the context transformation aspect
// - Compose emphasizes the function composition aspect
// - Use Contramap when working with category theory concepts
// - Use Local when adapting to different contexts
// - Use Compose when building functional pipelines
//
// Category Theory Background:
// - Consumer[A] forms a contravariant functor
// - The contravariant functor laws must hold:
// 1. contramap(id) = id
// 2. contramap(f ∘ g) = contramap(g) ∘ contramap(f)
// - This is dual to the covariant functor (map) operation
// - Consumers are contravariant because they consume rather than produce values
//
// Use Cases:
// - Working with contravariant functors in a categorical style
// - Adapting consumers to work with more specific types
// - Building type-safe consumer transformations
// - Implementing profunctor patterns (Consumer is a profunctor)
func Contramap[R1, R2 any](f func(R2) R1) Operator[R1, R2] {
return Local(f)
}

View File

@@ -381,3 +381,513 @@ func TestLocal(t *testing.T) {
assert.Equal(t, 42, captured)
})
}
func TestContramap(t *testing.T) {
t.Run("basic contravariant mapping", func(t *testing.T) {
var captured int
consumeInt := func(x int) {
captured = x
}
parseToInt := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
consumeString := Contramap(parseToInt)(consumeInt)
consumeString("42")
assert.Equal(t, 42, captured)
})
t.Run("contravariant identity law", func(t *testing.T) {
// contramap(identity) = identity
var captured int
consumeInt := func(x int) {
captured = x
}
identity := function.Identity[int]
consumeIdentity := Contramap(identity)(consumeInt)
consumeIdentity(42)
assert.Equal(t, 42, captured)
// Should behave identically to original consumer
consumeInt(100)
capturedDirect := captured
consumeIdentity(100)
capturedMapped := captured
assert.Equal(t, capturedDirect, capturedMapped)
})
t.Run("contravariant composition law", func(t *testing.T) {
// contramap(f . g) = contramap(g) . contramap(f)
var captured int
consumeInt := func(x int) {
captured = x
}
f := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
g := func(b bool) string {
if b {
return "1"
}
return "0"
}
// Compose f and g manually
fg := func(b bool) int {
return f(g(b))
}
// Method 1: contramap(f . g)
consumer1 := Contramap(fg)(consumeInt)
consumer1(true)
result1 := captured
// Method 2: contramap(g) . contramap(f)
consumer2 := Contramap(g)(Contramap(f)(consumeInt))
consumer2(true)
result2 := captured
assert.Equal(t, result1, result2)
assert.Equal(t, 1, result1)
})
t.Run("type hierarchy adaptation", func(t *testing.T) {
type Animal struct {
Name string
}
type Dog struct {
Animal Animal
Breed string
}
var capturedName string
consumeAnimal := func(a Animal) {
capturedName = a.Name
}
dogToAnimal := func(d Dog) Animal {
return d.Animal
}
consumeDog := Contramap(dogToAnimal)(consumeAnimal)
consumeDog(Dog{
Animal: Animal{Name: "Buddy"},
Breed: "Golden Retriever",
})
assert.Equal(t, "Buddy", capturedName)
})
t.Run("field extraction with contramap", func(t *testing.T) {
type Message struct {
Text string
Timestamp time.Time
}
var capturedText string
consumeString := func(s string) {
capturedText = s
}
extractText := func(m Message) string {
return m.Text
}
consumeMessage := Contramap(extractText)(consumeString)
consumeMessage(Message{
Text: "Hello",
Timestamp: time.Now(),
})
assert.Equal(t, "Hello", capturedText)
})
t.Run("multiple contramap applications", func(t *testing.T) {
type Level3 struct{ Value int }
type Level2 struct{ L3 Level3 }
type Level1 struct{ L2 Level2 }
var captured int
consumeInt := func(x int) {
captured = x
}
extract3 := func(l3 Level3) int { return l3.Value }
extract2 := func(l2 Level2) Level3 { return l2.L3 }
extract1 := func(l1 Level1) Level2 { return l1.L2 }
// Chain contramap operations
consumeLevel3 := Contramap(extract3)(consumeInt)
consumeLevel2 := Contramap(extract2)(consumeLevel3)
consumeLevel1 := Contramap(extract1)(consumeLevel2)
consumeLevel1(Level1{L2: Level2{L3: Level3{Value: 42}}})
assert.Equal(t, 42, captured)
})
t.Run("contramap with calculation", func(t *testing.T) {
type Rectangle struct {
Width int
Height int
}
var capturedArea int
consumeArea := func(area int) {
capturedArea = area
}
calculateArea := func(r Rectangle) int {
return r.Width * r.Height
}
consumeRectangle := Contramap(calculateArea)(consumeArea)
consumeRectangle(Rectangle{Width: 5, Height: 10})
assert.Equal(t, 50, capturedArea)
})
t.Run("contramap preserves side effects", func(t *testing.T) {
callCount := 0
consumer := func(x int) {
callCount++
}
transform := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
contramappedConsumer := Contramap(transform)(consumer)
contramappedConsumer("1")
contramappedConsumer("2")
contramappedConsumer("3")
assert.Equal(t, 3, callCount)
})
t.Run("contramap with pointer types", func(t *testing.T) {
var captured int
consumeInt := func(x int) {
captured = x
}
dereference := func(p *int) int {
if p == nil {
return 0
}
return *p
}
consumePointer := Contramap(dereference)(consumeInt)
value := 42
consumePointer(&value)
assert.Equal(t, 42, captured)
consumePointer(nil)
assert.Equal(t, 0, captured)
})
t.Run("contramap equivalence with Local", func(t *testing.T) {
var capturedLocal, capturedContramap int
consumeIntLocal := func(x int) {
capturedLocal = x
}
consumeIntContramap := func(x int) {
capturedContramap = x
}
transform := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
// Both should produce identical results
consumerLocal := Local(transform)(consumeIntLocal)
consumerContramap := Contramap(transform)(consumeIntContramap)
consumerLocal("42")
consumerContramap("42")
assert.Equal(t, capturedLocal, capturedContramap)
assert.Equal(t, 42, capturedLocal)
})
}
func TestCompose(t *testing.T) {
t.Run("basic composition", func(t *testing.T) {
var captured int
consumeInt := func(x int) {
captured = x
}
parseToInt := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
consumeString := Compose(parseToInt)(consumeInt)
consumeString("42")
assert.Equal(t, 42, captured)
})
t.Run("composing multiple transformations", func(t *testing.T) {
type Data struct {
Value string
}
type Wrapper struct {
Data Data
}
var captured string
consumeString := func(s string) {
captured = s
}
extractData := func(w Wrapper) Data { return w.Data }
extractValue := func(d Data) string { return d.Value }
// Compose step by step
consumeData := Compose(extractValue)(consumeString)
consumeWrapper := Compose(extractData)(consumeData)
consumeWrapper(Wrapper{Data: Data{Value: "Hello"}})
assert.Equal(t, "Hello", captured)
})
t.Run("function composition style", func(t *testing.T) {
type Request struct {
Body []byte
}
var captured string
processString := func(s string) {
captured = s
}
bytesToString := func(b []byte) string {
return string(b)
}
extractBody := func(r Request) []byte {
return r.Body
}
// Chain compositions
processBytes := Compose(bytesToString)(processString)
processRequest := Compose(extractBody)(processBytes)
processRequest(Request{Body: []byte("test")})
assert.Equal(t, "test", captured)
})
t.Run("compose with identity", func(t *testing.T) {
var captured int
consumeInt := func(x int) {
captured = x
}
identity := function.Identity[int]
composedConsumer := Compose(identity)(consumeInt)
composedConsumer(42)
assert.Equal(t, 42, captured)
})
t.Run("compose with field extraction", func(t *testing.T) {
type User struct {
Name string
Email string
Age int
}
var capturedName string
consumeName := func(name string) {
capturedName = name
}
extractName := func(u User) string {
return u.Name
}
consumeUser := Compose(extractName)(consumeName)
consumeUser(User{Name: "Alice", Email: "alice@example.com", Age: 30})
assert.Equal(t, "Alice", capturedName)
})
t.Run("compose with calculation", func(t *testing.T) {
type Circle struct {
Radius float64
}
var capturedArea float64
consumeArea := func(area float64) {
capturedArea = area
}
calculateArea := func(c Circle) float64 {
return 3.14159 * c.Radius * c.Radius
}
consumeCircle := Compose(calculateArea)(consumeArea)
consumeCircle(Circle{Radius: 5.0})
assert.InDelta(t, 78.53975, capturedArea, 0.00001)
})
t.Run("compose with slice operations", func(t *testing.T) {
var captured int
consumeLength := func(n int) {
captured = n
}
getLength := func(s []string) int {
return len(s)
}
consumeSlice := Compose(getLength)(consumeLength)
consumeSlice([]string{"a", "b", "c", "d"})
assert.Equal(t, 4, captured)
})
t.Run("compose with map operations", func(t *testing.T) {
var captured bool
consumeHasKey := func(has bool) {
captured = has
}
hasKey := func(m map[string]int) bool {
_, exists := m["key"]
return exists
}
consumeMap := Compose(hasKey)(consumeHasKey)
consumeMap(map[string]int{"key": 42})
assert.True(t, captured)
consumeMap(map[string]int{"other": 42})
assert.False(t, captured)
})
t.Run("compose preserves consumer behavior", func(t *testing.T) {
callCount := 0
consumer := func(x int) {
callCount++
}
transform := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
composedConsumer := Compose(transform)(consumer)
composedConsumer("1")
composedConsumer("2")
composedConsumer("3")
assert.Equal(t, 3, callCount)
})
t.Run("compose with error handling", func(t *testing.T) {
type Result struct {
Value int
Error error
}
var captured int
consumeInt := func(x int) {
captured = x
}
extractValue := func(r Result) int {
if r.Error != nil {
return -1
}
return r.Value
}
consumeResult := Compose(extractValue)(consumeInt)
consumeResult(Result{Value: 42, Error: nil})
assert.Equal(t, 42, captured)
consumeResult(Result{Value: 100, Error: assert.AnError})
assert.Equal(t, -1, captured)
})
t.Run("compose equivalence with Local", func(t *testing.T) {
var capturedLocal, capturedCompose int
consumeIntLocal := func(x int) {
capturedLocal = x
}
consumeIntCompose := func(x int) {
capturedCompose = x
}
transform := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
// Both should produce identical results
consumerLocal := Local(transform)(consumeIntLocal)
consumerCompose := Compose(transform)(consumeIntCompose)
consumerLocal("42")
consumerCompose("42")
assert.Equal(t, capturedLocal, capturedCompose)
assert.Equal(t, 42, capturedLocal)
})
t.Run("compose equivalence with Contramap", func(t *testing.T) {
var capturedCompose, capturedContramap int
consumeIntCompose := func(x int) {
capturedCompose = x
}
consumeIntContramap := func(x int) {
capturedContramap = x
}
transform := func(s string) int {
n, _ := strconv.Atoi(s)
return n
}
// All three should produce identical results
consumerCompose := Compose(transform)(consumeIntCompose)
consumerContramap := Contramap(transform)(consumeIntContramap)
consumerCompose("42")
consumerContramap("42")
assert.Equal(t, capturedCompose, capturedContramap)
assert.Equal(t, 42, capturedCompose)
})
}

View File

@@ -232,7 +232,7 @@ func TestContextPropagationThroughMonadTransforms(t *testing.T) {
var capturedCtx context.Context
computation := F.Pipe1(
Of[AppConfig](42),
Chain[AppConfig](func(n int) ReaderReaderIOResult[AppConfig, int] {
Chain(func(n int) ReaderReaderIOResult[AppConfig, int] {
return func(c AppConfig) ReaderIOResult[context.Context, int] {
return func(ctx context.Context) IOResult[int] {
return func() Result[int] {
@@ -405,7 +405,7 @@ func TestContextCancellationBetweenSteps(t *testing.T) {
}
}
},
Chain[AppConfig](func(n int) ReaderReaderIOResult[AppConfig, int] {
Chain(func(n int) ReaderReaderIOResult[AppConfig, int] {
return func(c AppConfig) ReaderIOResult[context.Context, int] {
return func(ctx context.Context) IOResult[int] {
return func() Result[int] {

View File

@@ -57,7 +57,7 @@ func TestSequence(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := Sequence[Config1, Config2, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -88,7 +88,7 @@ func TestSequence(t *testing.T) {
}
}
sequenced := Sequence[Config1, Config2, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -123,7 +123,7 @@ func TestSequence(t *testing.T) {
}
}
sequenced := Sequence[Config1, Config2, string](original)
sequenced := Sequence(original)
// Test with valid inputs
result1 := sequenced(Config1{value1: 42})(Config2{value2: "test"})(ctx)()
@@ -155,7 +155,7 @@ func TestSequence(t *testing.T) {
}
}
sequenced := Sequence[Config1, Config2, int](original)
sequenced := Sequence(original)
outcome := sequenced(Config1{value1: 0})(Config2{value2: ""})(ctx)()
assert.Equal(t, result.Of(0), outcome)
@@ -178,7 +178,7 @@ func TestSequence(t *testing.T) {
}
}
sequenced := Sequence[Config1, Config2, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 3}
cfg2 := Config2{value2: "test"}
@@ -207,7 +207,7 @@ func TestSequenceReader(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := SequenceReader[Config1, Config2, int](original)
sequenced := SequenceReader(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -239,7 +239,7 @@ func TestSequenceReader(t *testing.T) {
}
}
sequenced := SequenceReader[Config1, Config2, int](original)
sequenced := SequenceReader(original)
outcome := sequenced(Config1{value1: 10})(Config2{value2: "hello"})(ctx)()
assert.Equal(t, result.Left[int](testErr), outcome)
@@ -261,7 +261,7 @@ func TestSequenceReader(t *testing.T) {
}
}
sequenced := SequenceReader[Config1, Config2, string](original)
sequenced := SequenceReader(original)
// Test with valid inputs
result1 := sequenced(Config1{value1: 42})(Config2{value2: "test"})(ctx)()
@@ -285,7 +285,7 @@ func TestSequenceReader(t *testing.T) {
}
}
sequenced := SequenceReader[Config1, Config2, int](original)
sequenced := SequenceReader(original)
outcome := sequenced(Config1{value1: 0})(Config2{value2: ""})(ctx)()
assert.Equal(t, result.Of(0), outcome)
@@ -304,7 +304,7 @@ func TestSequenceReader(t *testing.T) {
}
}
sequenced := SequenceReader[Config1, Config2, int](original)
sequenced := SequenceReader(original)
cfg1 := Config1{value1: 3}
cfg2 := Config2{value2: "test"}
@@ -333,7 +333,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := SequenceReaderIO[Config1, Config2, int](original)
sequenced := SequenceReaderIO(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -365,7 +365,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
}
sequenced := SequenceReaderIO[Config1, Config2, int](original)
sequenced := SequenceReaderIO(original)
outcome := sequenced(Config1{value1: 10})(Config2{value2: "hello"})(ctx)()
assert.Equal(t, result.Left[int](testErr), outcome)
@@ -391,7 +391,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
}
sequenced := SequenceReaderIO[Config1, Config2, string](original)
sequenced := SequenceReaderIO(original)
// Test with valid inputs
sideEffect = 0
@@ -419,7 +419,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
}
sequenced := SequenceReaderIO[Config1, Config2, int](original)
sequenced := SequenceReaderIO(original)
outcome := sequenced(Config1{value1: 0})(Config2{value2: ""})(ctx)()
assert.Equal(t, result.Of(0), outcome)
@@ -442,7 +442,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
}
sequenced := SequenceReaderIO[Config1, Config2, int](original)
sequenced := SequenceReaderIO(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -478,7 +478,7 @@ func TestTraverse(t *testing.T) {
}
// Apply traverse to swap order and transform
traversed := Traverse[Config2, Config1, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
cfg1 := Config1{value1: 100}
cfg2 := Config2{value2: "test"}
@@ -496,7 +496,7 @@ func TestTraverse(t *testing.T) {
return Of[Config1](fmt.Sprintf("%d", n))
}
traversed := Traverse[Config2, Config1, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
outcome := traversed(Config1{value1: 100})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Left[string](testErr), outcome)
@@ -516,12 +516,12 @@ func TestTraverse(t *testing.T) {
// Test with negative value
originalNeg := Of[Config2](-1)
traversedNeg := Traverse[Config2, Config1, int, string](transform)(originalNeg)
traversedNeg := Traverse[Config2](transform)(originalNeg)
resultNeg := traversedNeg(Config1{value1: 100})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Left[string](testErr), resultNeg)
// Test with positive value
traversedPos := Traverse[Config2, Config1, int, string](transform)(original)
traversedPos := Traverse[Config2](transform)(original)
resultPos := traversedPos(Config1{value1: 100})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Of("42"), resultPos)
})
@@ -540,7 +540,7 @@ func TestTraverse(t *testing.T) {
}
}
traversed := Traverse[Config2, Config1, int, int](transform)(original)
traversed := Traverse[Config2](transform)(original)
outcome := traversed(Config1{value1: 5})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Of(50), outcome)
@@ -556,7 +556,7 @@ func TestTraverse(t *testing.T) {
outcome := F.Pipe2(
original,
Traverse[Config2, Config1, int, int](transform),
Traverse[Config2](transform),
func(k Kleisli[Config2, Config1, int]) ReaderReaderIOResult[Config2, int] {
return k(Config1{value1: 5})
},
@@ -582,7 +582,7 @@ func TestTraverseReader(t *testing.T) {
}
// Apply traverse to introduce Config1 and swap order
traversed := TraverseReader[Config2, Config1, int, string](formatWithConfig)(original)
traversed := TraverseReader[Config2](formatWithConfig)(original)
cfg1 := Config1{value1: 5}
cfg2 := Config2{value2: "test"}
@@ -600,7 +600,7 @@ func TestTraverseReader(t *testing.T) {
return reader.Of[Config1](fmt.Sprintf("%d", n))
}
traversed := TraverseReader[Config2, Config1, int, string](transform)(original)
traversed := TraverseReader[Config2](transform)(original)
outcome := traversed(Config1{value1: 5})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Left[string](testErr), outcome)
@@ -617,7 +617,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, int, int](double)(original)
traversed := TraverseReader[Config2](double)(original)
outcome := traversed(Config1{value1: 3})(Config2{value2: "test"})(ctx)()
assert.Equal(t, result.Of(126), outcome)
@@ -633,7 +633,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, int, int](transform)(original)
traversed := TraverseReader[Config2](transform)(original)
outcome := traversed(Config1{value1: 0})(Config2{value2: ""})(ctx)()
assert.Equal(t, result.Of(0), outcome)
@@ -649,7 +649,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, int, int](transform)(original)
traversed := TraverseReader[Config2](transform)(original)
cfg1 := Config1{value1: 5}
cfg2 := Config2{value2: "test"}
@@ -673,7 +673,7 @@ func TestTraverseReader(t *testing.T) {
outcome := F.Pipe2(
original,
TraverseReader[Config2, Config1, int, int](multiply),
TraverseReader[Config2](multiply),
func(k Kleisli[Config2, Config1, int]) ReaderReaderIOResult[Config2, int] {
return k(Config1{value1: 3})
},
@@ -698,7 +698,7 @@ func TestFlipIntegration(t *testing.T) {
}
// Sequence it
sequenced := Sequence[Config1, Config2, int](nested)
sequenced := Sequence(nested)
// Then traverse with a transformation
transform := func(n int) ReaderReaderIOResult[Config1, string] {
@@ -715,7 +715,7 @@ func TestFlipIntegration(t *testing.T) {
// Then apply traverse on a new computation
original := Of[Config2](5)
traversed := Traverse[Config2, Config1, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
outcome := traversed(cfg1)(cfg2)(ctx)()
assert.Equal(t, result.Of("length=5"), outcome)
})
@@ -734,7 +734,7 @@ func TestFlipIntegration(t *testing.T) {
}
}
}
seqResult := Sequence[Config1, Config2, int](seqErr)(cfg1)(cfg2)(ctx)()
seqResult := Sequence(seqErr)(cfg1)(cfg2)(ctx)()
assert.True(t, result.IsLeft(seqResult))
// Test SequenceReader with error
@@ -745,7 +745,7 @@ func TestFlipIntegration(t *testing.T) {
}
}
}
seqReaderResult := SequenceReader[Config1, Config2, int](seqReaderErr)(cfg1)(cfg2)(ctx)()
seqReaderResult := SequenceReader(seqReaderErr)(cfg1)(cfg2)(ctx)()
assert.True(t, result.IsLeft(seqReaderResult))
// Test SequenceReaderIO with error
@@ -756,7 +756,7 @@ func TestFlipIntegration(t *testing.T) {
}
}
}
seqReaderIOResult := SequenceReaderIO[Config1, Config2, int](seqReaderIOErr)(cfg1)(cfg2)(ctx)()
seqReaderIOResult := SequenceReaderIO(seqReaderIOErr)(cfg1)(cfg2)(ctx)()
assert.True(t, result.IsLeft(seqReaderIOResult))
// Test Traverse with error
@@ -764,7 +764,7 @@ func TestFlipIntegration(t *testing.T) {
travTransform := func(n int) ReaderReaderIOResult[Config1, string] {
return Of[Config1](fmt.Sprintf("%d", n))
}
travResult := Traverse[Config2, Config1, int, string](travTransform)(travErr)(cfg1)(cfg2)(ctx)()
travResult := Traverse[Config2](travTransform)(travErr)(cfg1)(cfg2)(ctx)()
assert.True(t, result.IsLeft(travResult))
// Test TraverseReader with error
@@ -772,7 +772,7 @@ func TestFlipIntegration(t *testing.T) {
travReaderTransform := func(n int) reader.Reader[Config1, string] {
return reader.Of[Config1](fmt.Sprintf("%d", n))
}
travReaderResult := TraverseReader[Config2, Config1, int, string](travReaderTransform)(travReaderErr)(cfg1)(cfg2)(ctx)()
travReaderResult := TraverseReader[Config2](travReaderTransform)(travReaderErr)(cfg1)(cfg2)(ctx)()
assert.True(t, result.IsLeft(travReaderResult))
})
}

View File

@@ -52,7 +52,7 @@ func FromReaderOption[R, A any](onNone Lazy[error]) Kleisli[R, ReaderOption[R, A
//
//go:inline
func FromReaderIOResult[R, A any](ma ReaderIOResult[R, A]) ReaderReaderIOResult[R, A] {
return RRIOE.FromReaderIOEither[context.Context, error](ma)
return RRIOE.FromReaderIOEither[context.Context](ma)
}
// FromReaderIO lifts a ReaderIO into a ReaderReaderIOResult.
@@ -734,7 +734,7 @@ func FromIO[R, A any](ma IO[A]) ReaderReaderIOResult[R, A] {
//
//go:inline
func FromIOEither[R, A any](ma IOEither[error, A]) ReaderReaderIOResult[R, A] {
return RRIOE.FromIOEither[R, context.Context, error](ma)
return RRIOE.FromIOEither[R, context.Context](ma)
}
// FromIOResult lifts an IOResult into a ReaderReaderIOResult.
@@ -742,14 +742,14 @@ func FromIOEither[R, A any](ma IOEither[error, A]) ReaderReaderIOResult[R, A] {
//
//go:inline
func FromIOResult[R, A any](ma IOResult[A]) ReaderReaderIOResult[R, A] {
return RRIOE.FromIOEither[R, context.Context, error](ma)
return RRIOE.FromIOEither[R, context.Context](ma)
}
// FromReaderEither lifts a ReaderEither into a ReaderReaderIOResult.
//
//go:inline
func FromReaderEither[R, A any](ma RE.ReaderEither[R, error, A]) ReaderReaderIOResult[R, A] {
return RRIOE.FromReaderEither[R, context.Context, error](ma)
return RRIOE.FromReaderEither[R, context.Context](ma)
}
// Ask retrieves the outer environment R.
@@ -782,7 +782,7 @@ func FromOption[R, A any](onNone Lazy[error]) func(Option[A]) ReaderReaderIOResu
//
//go:inline
func FromPredicate[R, A any](pred func(A) bool, onFalse func(A) error) Kleisli[R, A, A] {
return RRIOE.FromPredicate[R, context.Context, error](pred, onFalse)
return RRIOE.FromPredicate[R, context.Context](pred, onFalse)
}
// MonadAlt provides alternative/fallback behavior.
@@ -825,7 +825,7 @@ func Flap[R, B, A any](a A) Operator[R, func(A) B, B] {
//
//go:inline
func MonadMapLeft[R, A any](fa ReaderReaderIOResult[R, A], f Endmorphism[error]) ReaderReaderIOResult[R, A] {
return RRIOE.MonadMapLeft[R, context.Context](fa, f)
return RRIOE.MonadMapLeft(fa, f)
}
// MapLeft transforms the error value if the computation fails.
@@ -864,7 +864,7 @@ func ReadIOEither[A, R any](rio IOEither[error, R]) func(ReaderReaderIOResult[R,
//
//go:inline
func ReadIO[A, R any](rio IO[R]) func(ReaderReaderIOResult[R, A]) ReaderIOResult[context.Context, A] {
return RRIOE.ReadIO[context.Context, error, A, R](rio)
return RRIOE.ReadIO[context.Context, error, A](rio)
}
// MonadChainLeft handles errors by chaining a recovery computation.
@@ -873,7 +873,7 @@ func ReadIO[A, R any](rio IO[R]) func(ReaderReaderIOResult[R, A]) ReaderIOResult
//
//go:inline
func MonadChainLeft[R, A any](fa ReaderReaderIOResult[R, A], f Kleisli[R, error, A]) ReaderReaderIOResult[R, A] {
return RRIOE.MonadChainLeft[R, context.Context, error, error, A](fa, f)
return RRIOE.MonadChainLeft(fa, f)
}
// ChainLeft handles errors by chaining a recovery computation.
@@ -882,7 +882,7 @@ func MonadChainLeft[R, A any](fa ReaderReaderIOResult[R, A], f Kleisli[R, error,
//
//go:inline
func ChainLeft[R, A any](f Kleisli[R, error, A]) func(ReaderReaderIOResult[R, A]) ReaderReaderIOResult[R, A] {
return RRIOE.ChainLeft[R, context.Context, error, error, A](f)
return RRIOE.ChainLeft(f)
}
// Delay adds a time delay before executing the computation.

View File

@@ -101,7 +101,7 @@ func TestMonadChain(t *testing.T) {
func TestChain(t *testing.T) {
computation := F.Pipe1(
Of[AppConfig](21),
Chain[AppConfig](func(n int) ReaderReaderIOResult[AppConfig, int] {
Chain(func(n int) ReaderReaderIOResult[AppConfig, int] {
return Of[AppConfig](n * 2)
}),
)
@@ -127,7 +127,7 @@ func TestChainFirst(t *testing.T) {
sideEffect := 0
computation := F.Pipe1(
Of[AppConfig](42),
ChainFirst[AppConfig](func(n int) ReaderReaderIOResult[AppConfig, string] {
ChainFirst(func(n int) ReaderReaderIOResult[AppConfig, string] {
sideEffect = n
return Of[AppConfig]("ignored")
}),
@@ -141,7 +141,7 @@ func TestTap(t *testing.T) {
sideEffect := 0
computation := F.Pipe1(
Of[AppConfig](42),
Tap[AppConfig](func(n int) ReaderReaderIOResult[AppConfig, string] {
Tap(func(n int) ReaderReaderIOResult[AppConfig, string] {
sideEffect = n
return Of[AppConfig]("ignored")
}),
@@ -167,7 +167,7 @@ func TestFromEither(t *testing.T) {
t.Run("left", func(t *testing.T) {
err := errors.New("test error")
computation := FromEither[AppConfig, int](either.Left[int](err))
computation := FromEither[AppConfig](either.Left[int](err))
outcome := computation(defaultConfig)(t.Context())()
assert.True(t, result.IsLeft(outcome))
})
@@ -189,7 +189,7 @@ func TestFromResult(t *testing.T) {
}
func TestFromReader(t *testing.T) {
computation := FromReader[AppConfig](func(cfg AppConfig) int {
computation := FromReader(func(cfg AppConfig) int {
return len(cfg.DatabaseURL)
})
outcome := computation(defaultConfig)(t.Context())()
@@ -197,7 +197,7 @@ func TestFromReader(t *testing.T) {
}
func TestRightReader(t *testing.T) {
computation := RightReader[AppConfig](func(cfg AppConfig) int {
computation := RightReader(func(cfg AppConfig) int {
return len(cfg.LogLevel)
})
outcome := computation(defaultConfig)(t.Context())()
@@ -241,7 +241,7 @@ func TestFromIOEither(t *testing.T) {
t.Run("left", func(t *testing.T) {
err := errors.New("test error")
computation := FromIOEither[AppConfig, int](ioeither.Left[int](err))
computation := FromIOEither[AppConfig](ioeither.Left[int](err))
outcome := computation(defaultConfig)(t.Context())()
assert.True(t, result.IsLeft(outcome))
})
@@ -267,7 +267,7 @@ func TestFromIOResult(t *testing.T) {
}
func TestFromReaderIO(t *testing.T) {
computation := FromReaderIO[AppConfig](func(cfg AppConfig) io.IO[int] {
computation := FromReaderIO(func(cfg AppConfig) io.IO[int] {
return func() int { return len(cfg.DatabaseURL) }
})
outcome := computation(defaultConfig)(t.Context())()
@@ -275,7 +275,7 @@ func TestFromReaderIO(t *testing.T) {
}
func TestRightReaderIO(t *testing.T) {
computation := RightReaderIO[AppConfig](func(cfg AppConfig) io.IO[int] {
computation := RightReaderIO(func(cfg AppConfig) io.IO[int] {
return func() int { return len(cfg.LogLevel) }
})
outcome := computation(defaultConfig)(t.Context())()
@@ -293,7 +293,7 @@ func TestLeftReaderIO(t *testing.T) {
func TestFromReaderEither(t *testing.T) {
t.Run("right", func(t *testing.T) {
computation := FromReaderEither[AppConfig](func(cfg AppConfig) either.Either[error, int] {
computation := FromReaderEither(func(cfg AppConfig) either.Either[error, int] {
return either.Right[error](len(cfg.DatabaseURL))
})
outcome := computation(defaultConfig)(t.Context())()
@@ -302,7 +302,7 @@ func TestFromReaderEither(t *testing.T) {
t.Run("left", func(t *testing.T) {
err := errors.New("test error")
computation := FromReaderEither[AppConfig, int](func(cfg AppConfig) either.Either[error, int] {
computation := FromReaderEither(func(cfg AppConfig) either.Either[error, int] {
return either.Left[int](err)
})
outcome := computation(defaultConfig)(t.Context())()
@@ -396,7 +396,7 @@ func TestAlt(t *testing.T) {
computation := F.Pipe1(
Left[AppConfig, int](err),
Alt[AppConfig](func() ReaderReaderIOResult[AppConfig, int] {
Alt(func() ReaderReaderIOResult[AppConfig, int] {
return Of[AppConfig](99)
}),
)
@@ -461,7 +461,7 @@ func TestLocal(t *testing.T) {
Asks(func(cfg AppConfig) string {
return cfg.DatabaseURL
}),
Local[string, AppConfig, OtherConfig](func(other OtherConfig) AppConfig {
Local[string](func(other OtherConfig) AppConfig {
return AppConfig{DatabaseURL: other.URL, LogLevel: "debug"}
}),
)
@@ -518,7 +518,7 @@ func TestChainLeft(t *testing.T) {
err := errors.New("original error")
computation := F.Pipe1(
Left[AppConfig, int](err),
ChainLeft[AppConfig](func(e error) ReaderReaderIOResult[AppConfig, int] {
ChainLeft(func(e error) ReaderReaderIOResult[AppConfig, int] {
return Of[AppConfig](99)
}),
)
@@ -553,7 +553,7 @@ func TestChainEitherK(t *testing.T) {
func TestChainReaderK(t *testing.T) {
computation := F.Pipe1(
Of[AppConfig](10),
ChainReaderK[AppConfig](func(n int) reader.Reader[AppConfig, int] {
ChainReaderK(func(n int) reader.Reader[AppConfig, int] {
return func(cfg AppConfig) int {
return n + len(cfg.LogLevel)
}
@@ -566,7 +566,7 @@ func TestChainReaderK(t *testing.T) {
func TestChainReaderIOK(t *testing.T) {
computation := F.Pipe1(
Of[AppConfig](10),
ChainReaderIOK[AppConfig](func(n int) readerio.ReaderIO[AppConfig, int] {
ChainReaderIOK(func(n int) readerio.ReaderIO[AppConfig, int] {
return func(cfg AppConfig) io.IO[int] {
return func() int {
return n + len(cfg.DatabaseURL)
@@ -581,7 +581,7 @@ func TestChainReaderIOK(t *testing.T) {
func TestChainReaderEitherK(t *testing.T) {
computation := F.Pipe1(
Of[AppConfig](10),
ChainReaderEitherK[AppConfig](func(n int) RE.ReaderEither[AppConfig, error, int] {
ChainReaderEitherK(func(n int) RE.ReaderEither[AppConfig, error, int] {
return func(cfg AppConfig) either.Either[error, int] {
return either.Right[error](n + len(cfg.LogLevel))
}
@@ -670,7 +670,7 @@ func TestChainOptionK(t *testing.T) {
}
func TestFromReaderIOResult(t *testing.T) {
computation := FromReaderIOResult[AppConfig](func(cfg AppConfig) ioresult.IOResult[int] {
computation := FromReaderIOResult(func(cfg AppConfig) ioresult.IOResult[int] {
return func() result.Result[int] {
return result.Of(len(cfg.DatabaseURL))
}
@@ -711,7 +711,7 @@ func TestAp(t *testing.T) {
fa := Of[AppConfig](21)
computation := F.Pipe1(
Of[AppConfig](N.Mul(2)),
Ap[int, AppConfig](fa),
Ap[int](fa),
)
outcome := computation(defaultConfig)(t.Context())()
assert.Equal(t, result.Of(42), outcome)

View File

@@ -253,7 +253,7 @@ func Second[T1, T2 any](_ T1, t2 T2) T2 {
}
// Zero returns the zero value of the given type.
func Zero[A comparable]() A {
func Zero[A any]() A {
var zero A
return zero
}

View File

@@ -4,14 +4,11 @@ go 1.24
require (
github.com/stretchr/testify v1.11.1
github.com/urfave/cli/v2 v2.27.7
github.com/urfave/cli/v3 v3.6.2
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@@ -1,17 +1,11 @@
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@@ -204,7 +204,7 @@ func BenchmarkMonadChain_Left(b *testing.B) {
func BenchmarkChain_Right(b *testing.B) {
rioe := Right[benchConfig](42)
chainer := Chain[benchConfig](func(a int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](a * 2) })
chainer := Chain(func(a int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](a * 2) })
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
@@ -214,7 +214,7 @@ func BenchmarkChain_Right(b *testing.B) {
func BenchmarkChain_Left(b *testing.B) {
rioe := Left[benchConfig, int](benchErr)
chainer := Chain[benchConfig](func(a int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](a * 2) })
chainer := Chain(func(a int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](a * 2) })
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
@@ -224,7 +224,7 @@ func BenchmarkChain_Left(b *testing.B) {
func BenchmarkChainFirst_Right(b *testing.B) {
rioe := Right[benchConfig](42)
chainer := ChainFirst[benchConfig](func(a int) ReaderIOResult[benchConfig, string] { return Right[benchConfig]("logged") })
chainer := ChainFirst(func(a int) ReaderIOResult[benchConfig, string] { return Right[benchConfig]("logged") })
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
@@ -234,7 +234,7 @@ func BenchmarkChainFirst_Right(b *testing.B) {
func BenchmarkChainFirst_Left(b *testing.B) {
rioe := Left[benchConfig, int](benchErr)
chainer := ChainFirst[benchConfig](func(a int) ReaderIOResult[benchConfig, string] { return Right[benchConfig]("logged") })
chainer := ChainFirst(func(a int) ReaderIOResult[benchConfig, string] { return Right[benchConfig]("logged") })
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
@@ -443,7 +443,7 @@ func BenchmarkPipeline_Chain_Right(b *testing.B) {
for b.Loop() {
benchRIOE = F.Pipe1(
rioe,
Chain[benchConfig](func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x * 2) }),
Chain(func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x * 2) }),
)
}
}
@@ -455,7 +455,7 @@ func BenchmarkPipeline_Chain_Left(b *testing.B) {
for b.Loop() {
benchRIOE = F.Pipe1(
rioe,
Chain[benchConfig](func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x * 2) }),
Chain(func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x * 2) }),
)
}
}
@@ -468,7 +468,7 @@ func BenchmarkPipeline_Complex_Right(b *testing.B) {
benchRIOE = F.Pipe3(
rioe,
Map[benchConfig](N.Mul(2)),
Chain[benchConfig](func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Chain(func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Map[benchConfig](N.Mul(2)),
)
}
@@ -482,7 +482,7 @@ func BenchmarkPipeline_Complex_Left(b *testing.B) {
benchRIOE = F.Pipe3(
rioe,
Map[benchConfig](N.Mul(2)),
Chain[benchConfig](func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Chain(func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Map[benchConfig](N.Mul(2)),
)
}
@@ -492,7 +492,7 @@ func BenchmarkExecutePipeline_Complex_Right(b *testing.B) {
rioe := F.Pipe3(
Right[benchConfig](10),
Map[benchConfig](N.Mul(2)),
Chain[benchConfig](func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Chain(func(x int) ReaderIOResult[benchConfig, int] { return Right[benchConfig](x + 1) }),
Map[benchConfig](N.Mul(2)),
)
b.ResetTimer()

View File

@@ -0,0 +1,70 @@
package readert
import (
M "github.com/IBM/fp-go/v2/monoid"
S "github.com/IBM/fp-go/v2/semigroup"
)
// ApplySemigroup lifts a Semigroup[A] into a Semigroup[Reader[R, A]].
// This allows you to combine two Readers that produce semigroup values by combining
// their results using the semigroup's concat operation.
//
// The _map and _ap parameters are the Map and Ap operations for the Reader type,
// typically obtained from the reader package.
//
// Example:
//
// type Config struct { Multiplier int }
// // Using the additive semigroup for integers
// intSemigroup := semigroup.MakeSemigroup(func(a, b int) int { return a + b })
// readerSemigroup := reader.ApplySemigroup(
// reader.MonadMap[Config, int, func(int) int],
// reader.MonadAp[int, Config, int],
// intSemigroup,
// )
//
// r1 := reader.Of[Config](5)
// r2 := reader.Of[Config](3)
// combined := readerSemigroup.Concat(r1, r2)
// result := combined(Config{Multiplier: 1}) // 8
func ApplySemigroup[R, A any](
_map func(func(R) A, func(A) func(A) A) func(R, func(A) A),
_ap func(func(R, func(A) A), func(R) A) func(R) A,
s S.Semigroup[A],
) S.Semigroup[func(R) A] {
return S.ApplySemigroup(_map, _ap, s)
}
// ApplicativeMonoid lifts a Monoid[A] into a Monoid[Reader[R, A]].
// This allows you to combine Readers that produce monoid values, with an empty/identity Reader.
//
// The _of parameter is the Of operation (pure/return) for the Reader type.
// The _map and _ap parameters are the Map and Ap operations for the Reader type.
//
// Example:
//
// type Config struct { Prefix string }
// // Using the string concatenation monoid
// stringMonoid := monoid.MakeMonoid("", func(a, b string) string { return a + b })
// readerMonoid := reader.ApplicativeMonoid(
// reader.Of[Config, string],
// reader.MonadMap[Config, string, func(string) string],
// reader.MonadAp[string, Config, string],
// stringMonoid,
// )
//
// r1 := reader.Asks(func(c Config) string { return c.Prefix })
// r2 := reader.Of[Config]("hello")
// combined := readerMonoid.Concat(r1, r2)
// result := combined(Config{Prefix: ">> "}) // ">> hello"
// empty := readerMonoid.Empty()(Config{Prefix: "any"}) // ""
func ApplicativeMonoid[R, A any](
_of func(A) func(R) A,
_map func(func(R) A, func(A) func(A) A) func(R, func(A) A),
_ap func(func(R, func(A) A), func(R) A) func(R) A,
m M.Monoid[A],
) M.Monoid[func(R) A] {
return M.ApplicativeMonoid(_of, _map, _ap, m)
}

View File

@@ -35,7 +35,7 @@ import (
//
// safeOperation := io.WithLock(lock)(dangerousOperation)
// result := safeOperation()
func WithLock[A any](lock IO[context.CancelFunc]) func(fa IO[A]) IO[A] {
func WithLock[A any](lock IO[context.CancelFunc]) Operator[A, A] {
return func(fa IO[A]) IO[A] {
return func() A {
defer lock()()

View File

@@ -17,23 +17,28 @@
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"github.com/IBM/fp-go/v2/cli"
C "github.com/urfave/cli/v2"
C "github.com/urfave/cli/v3"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
app := &C.App{
app := &C.Command{
Name: "fp-go",
Usage: "Code generation for fp-go",
Commands: cli.Commands(),
}
if err := app.Run(os.Args); err != nil {
if err := app.Run(ctx, os.Args); err != nil {
log.Fatal(err)
}
}

View File

@@ -221,7 +221,7 @@ Lenses can be automatically generated using the `fp-go` CLI tool and a simple an
1. **Annotate your struct** with the `fp-go:Lens` comment:
```go
//go:generate go run github.com/IBM/fp-go/v2/main.go lens --dir . --filename gen_lens.go
//go:generate go run github.com/IBM/fp-go/v2 lens --dir . --filename gen_lens.go
// fp-go:Lens
type Person struct {
@@ -293,13 +293,23 @@ More specific optics can be converted to more general ones.
## 📦 Package Structure
### Core Optics
- **[optics/lens](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/lens)**: Lenses for product types (structs)
- **[optics/prism](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/prism)**: Prisms for sum types ([`Either`](https://pkg.go.dev/github.com/IBM/fp-go/v2/either), [`Result`](https://pkg.go.dev/github.com/IBM/fp-go/v2/result), etc.)
- **[optics/iso](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/iso)**: Isomorphisms for equivalent types
- **[optics/optional](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/optional)**: Optional optics for maybe values
- **[optics/traversal](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/traversal)**: Traversals for multiple values
Each package includes specialized sub-packages for common patterns:
### Utilities
- **[optics/builder](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/builder)**: Builder pattern for constructing complex optics
- **[optics/codec](https://pkg.go.dev/github.com/IBM/fp-go/v2/optics/codec)**: Type-safe encoding/decoding with validation
- Provides `Type[A, O, I]` for bidirectional transformations with validation
- Includes codecs for primitives (String, Int, Bool), collections (Array), and sum types (Either)
- Supports refinement types and codec composition via `Pipe`
- Integrates validation errors with context tracking
### Specialized Sub-packages
Each core optics package includes specialized sub-packages for common patterns:
- **array**: Optics for arrays/slices
- **either**: Optics for [`Either`](https://pkg.go.dev/github.com/IBM/fp-go/v2/either) types
- **option**: Optics for [`Option`](https://pkg.go.dev/github.com/IBM/fp-go/v2/option) types

View File

@@ -0,0 +1,31 @@
package builder
import (
"fmt"
F "github.com/IBM/fp-go/v2/function"
)
func MakeBuilder[S, A any](get func(S) Option[A], set func(A) Endomorphism[S], name string) Builder[S, A] {
return Builder[S, A]{
GetOption: get,
Set: set,
name: name,
}
}
func ComposeLensPrism[S, A, B any](r Prism[A, B]) func(Lens[S, A]) Builder[S, B] {
return func(l Lens[S, A]) Builder[S, B] {
return MakeBuilder(
F.Flow2(
l.Get,
r.GetOption,
),
F.Flow2(
r.ReverseGet,
l.Set,
),
fmt.Sprintf("Compose[%s -> %s]", l, r),
)
}
}

View File

@@ -0,0 +1,27 @@
package builder
import (
"github.com/IBM/fp-go/v2/endomorphism"
"github.com/IBM/fp-go/v2/optics/lens"
"github.com/IBM/fp-go/v2/optics/prism"
"github.com/IBM/fp-go/v2/option"
)
type (
Option[A any] = option.Option[A]
Lens[S, A any] = lens.Lens[S, A]
Prism[S, A any] = prism.Prism[S, A]
Endomorphism[A any] = endomorphism.Endomorphism[A]
Builder[S, A any] struct {
GetOption func(S) Option[A]
Set func(A) Endomorphism[S]
name string
}
Kleisli[S, A, B any] = func(A) Builder[S, B]
Operator[S, A, B any] = Kleisli[S, Builder[S, A], B]
)

View File

@@ -10,6 +10,7 @@ import (
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/either"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/lazy"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/pair"
"github.com/IBM/fp-go/v2/reader"
@@ -27,6 +28,8 @@ type typeImpl[A, O, I any] struct {
encode Encode[A, O]
}
var emptyContext = A.Empty[validation.ContextEntry]()
// MakeType creates a new Type with the given name, type checker, validator, and encoder.
//
// Parameters:
@@ -52,7 +55,7 @@ func MakeType[A, O, I any](
// Validate validates the input value in the context of a validation path.
// Returns a Reader that takes a Context and produces a Validation result.
func (t *typeImpl[A, O, I]) Validate(i I) Reader[Context, Validation[A]] {
func (t *typeImpl[A, O, I]) Validate(i I) Decode[Context, A] {
return t.validate(i)
}
@@ -138,16 +141,16 @@ func isTypedNil[A any](x any) Result[*A] {
return result.Left[*A](errors.New("expecting nil"))
}
func validateFromIs[A any](
is ReaderResult[any, A],
func validateFromIs[A, I any](
is ReaderResult[I, A],
msg string,
) Reader[any, Reader[Context, Validation[A]]] {
return func(u any) Reader[Context, Validation[A]] {
) Validate[I, A] {
return func(i I) Decode[Context, A] {
return F.Pipe2(
u,
i,
is,
result.Fold(
validation.FailureWithError[A](u, msg),
validation.FailureWithError[A](F.ToAny(i), msg),
F.Flow2(
validation.Success[A],
reader.Of[Context],
@@ -157,6 +160,17 @@ func validateFromIs[A any](
}
}
func isFromValidate[T, I any](val Validate[I, T]) ReaderResult[any, T] {
invalidType := result.Left[T](errors.New("invalid input type"))
return func(u any) Result[T] {
i, ok := u.(I)
if !ok {
return invalidType
}
return validation.ToResult(val(i)(emptyContext))
}
}
// MakeNilType creates a Type that validates nil values.
// It accepts any input and validates that it is nil, returning a typed nil pointer.
//
@@ -178,8 +192,7 @@ func Nil[A any]() Type[*A, *A, any] {
}
func MakeSimpleType[A any]() Type[A, A, any] {
var zero A
name := fmt.Sprintf("%T", zero)
name := fmt.Sprintf("%T", *new(A))
is := Is[A]()
return MakeType(
@@ -190,14 +203,53 @@ func MakeSimpleType[A any]() Type[A, A, any] {
)
}
// String creates a Type for string values.
// It validates that input is a string type and provides identity encoding/decoding.
// This is a simple type that accepts any input and validates it's a string.
//
// Returns:
// - A Type[string, string, any] that can validate, decode, and encode string values
//
// Example:
//
// stringType := codec.String()
// result := stringType.Decode("hello") // Success: Right("hello")
// result := stringType.Decode(123) // Failure: Left(validation errors)
// encoded := stringType.Encode("world") // Returns: "world"
func String() Type[string, string, any] {
return MakeSimpleType[string]()
}
// Int creates a Type for int values.
// It validates that input is an int type and provides identity encoding/decoding.
// This is a simple type that accepts any input and validates it's an int.
//
// Returns:
// - A Type[int, int, any] that can validate, decode, and encode int values
//
// Example:
//
// intType := codec.Int()
// result := intType.Decode(42) // Success: Right(42)
// result := intType.Decode("42") // Failure: Left(validation errors)
// encoded := intType.Encode(100) // Returns: 100
func Int() Type[int, int, any] {
return MakeSimpleType[int]()
}
// Bool creates a Type for bool values.
// It validates that input is a bool type and provides identity encoding/decoding.
// This is a simple type that accepts any input and validates it's a bool.
//
// Returns:
// - A Type[bool, bool, any] that can validate, decode, and encode bool values
//
// Example:
//
// boolType := codec.Bool()
// result := boolType.Decode(true) // Success: Right(true)
// result := boolType.Decode(1) // Failure: Left(validation errors)
// encoded := boolType.Encode(false) // Returns: false
func Bool() Type[bool, bool, any] {
return MakeSimpleType[bool]()
}
@@ -216,7 +268,7 @@ func pairToValidation[T any](p validationPair[T]) Validation[T] {
return either.Of[validation.Errors](value)
}
func validateArray[T any](item Type[T, T, any]) func(u any) Reader[Context, Validation[[]T]] {
func validateArrayFromArray[T, O, I any](item Type[T, O, I]) Validate[[]I, []T] {
appendErrors := F.Flow2(
A.Concat,
@@ -232,8 +284,48 @@ func validateArray[T any](item Type[T, T, any]) func(u any) Reader[Context, Vali
zero := pair.Zero[validation.Errors, []T]()
return func(u any) Reader[Context, Validation[[]T]] {
val := reflect.ValueOf(u)
return func(is []I) Decode[Context, []T] {
return func(c Context) Validation[[]T] {
return F.Pipe1(
A.MonadReduceWithIndex(is, func(i int, p validationPair[[]T], v I) validationPair[[]T] {
return either.MonadFold(
item.Validate(v)(appendContext(strconv.Itoa(i), itemName, v)(c)),
appendErrors,
appendValues,
)(p)
}, zero),
pairToValidation,
)
}
}
}
func validateArray[T, O any](item Type[T, O, any]) Validate[any, []T] {
appendErrors := F.Flow2(
A.Concat,
pair.MapHead[[]T, validation.Errors],
)
appendValues := F.Flow2(
A.Push,
pair.MapTail[validation.Errors, []T],
)
itemName := item.Name()
zero := pair.Zero[validation.Errors, []T]()
return func(i any) Decode[Context, []T] {
res, ok := i.([]T)
if ok {
return reader.Of[Context](validation.Success(res))
}
val := reflect.ValueOf(i)
if !val.IsValid() {
return validation.FailureWithMessage[[]T](val, "invalid value")
}
@@ -246,8 +338,9 @@ func validateArray[T any](item Type[T, T, any]) func(u any) Reader[Context, Vali
return F.Pipe1(
R.MonadReduceWithIndex(val, func(i int, p validationPair[[]T], v reflect.Value) validationPair[[]T] {
vIface := v.Interface()
return either.MonadFold(
item.Validate(v)(appendContext(strconv.Itoa(i), itemName, v)(c)),
item.Validate(vIface)(appendContext(strconv.Itoa(i), itemName, vIface)(c)),
appendErrors,
appendValues,
)(p)
@@ -260,3 +353,397 @@ func validateArray[T any](item Type[T, T, any]) func(u any) Reader[Context, Vali
}
}
}
// Array creates a Type for array/slice values with elements of type T.
// It validates that input is an array, slice, or string, and validates each element
// using the provided item Type. During encoding, it maps the encode function over all elements.
//
// Type Parameters:
// - T: The type of elements in the decoded array
// - O: The type of elements in the encoded array
//
// Parameters:
// - item: A Type[T, O, any] that defines how to validate/encode individual elements
//
// Returns:
// - A Type[[]T, []O, any] that can validate, decode, and encode array values
//
// The function handles:
// - Native Go slices of type []T (passed through directly)
// - reflect.Array, reflect.Slice, reflect.String (validated element by element)
// - Collects all validation errors from individual elements
// - Provides detailed context for each element's position in error messages
//
// Example:
//
// intArray := codec.Array(codec.Int())
// result := intArray.Decode([]int{1, 2, 3}) // Success: Right([1, 2, 3])
// result := intArray.Decode([]any{1, "2", 3}) // Failure: validation error at index 1
// encoded := intArray.Encode([]int{1, 2, 3}) // Returns: []int{1, 2, 3}
//
// stringArray := codec.Array(codec.String())
// result := stringArray.Decode([]string{"a", "b"}) // Success: Right(["a", "b"])
// result := stringArray.Decode("hello") // Success: Right(["h", "e", "l", "l", "o"])
func Array[T, O any](item Type[T, O, any]) Type[[]T, []O, any] {
validate := validateArray(item)
is := isFromValidate(validate)
name := fmt.Sprintf("Array[%s]", item.Name())
return MakeType(
name,
is,
validate,
A.Map(item.Encode),
)
}
// TranscodeArray creates a Type for array/slice values with strongly-typed input.
// Unlike Array which accepts any input type, TranscodeArray requires the input to be
// a slice of type []I, providing type safety at the input level.
//
// This function validates each element of the input slice using the provided item Type,
// transforming []I -> []T during decoding and []T -> []O during encoding.
//
// Type Parameters:
// - T: The type of elements in the decoded array
// - O: The type of elements in the encoded array
// - I: The type of elements in the input array (must be a slice)
//
// Parameters:
// - item: A Type[T, O, I] that defines how to validate/encode individual elements
//
// Returns:
// - A Type[[]T, []O, []I] that can validate, decode, and encode array values
//
// The function:
// - Requires input to be exactly []I (not any)
// - Validates each element using the item Type's validation logic
// - Collects all validation errors from individual elements
// - Provides detailed context for each element's position in error messages
// - Maps the encode function over all elements during encoding
//
// Example:
//
// // Create a codec that transforms string slices to int slices
// stringToInt := codec.MakeType[int, int, string](
// "StringToInt",
// func(s any) result.Result[int] { ... },
// func(s string) codec.Validate[int] { ... },
// func(i int) int { return i },
// )
// arrayCodec := codec.TranscodeArray(stringToInt)
//
// // Decode: []string -> []int
// result := arrayCodec.Decode([]string{"1", "2", "3"}) // Success: Right([1, 2, 3])
// result := arrayCodec.Decode([]string{"1", "x", "3"}) // Failure: validation error at index 1
//
// // Encode: []int -> []int
// encoded := arrayCodec.Encode([]int{1, 2, 3}) // Returns: []int{1, 2, 3}
//
// Use TranscodeArray when:
// - You need type-safe input validation ([]I instead of any)
// - You're transforming between different slice element types
// - You want compile-time guarantees about input types
//
// Use Array when:
// - You need to accept various input types (any, reflect.Value, etc.)
// - You're working with dynamic or unknown input types
func TranscodeArray[T, O, I any](item Type[T, O, I]) Type[[]T, []O, []I] {
validate := validateArrayFromArray(item)
is := isFromValidate(validate)
name := fmt.Sprintf("Array[%s]", item.Name())
return MakeType(
name,
is,
validate,
A.Map(item.Encode),
)
}
func validateEitherFromEither[L, R, OL, OR, IL, IR any](
leftItem Type[L, OL, IL],
rightItem Type[R, OR, IR],
) Validate[either.Either[IL, IR], either.Either[L, R]] {
// leftName := left.Name()
// rightName := right.Name()
return func(is either.Either[IL, IR]) Decode[Context, either.Either[L, R]] {
return either.MonadFold(
is,
F.Flow2(
leftItem.Validate,
readereither.Map[Context, validation.Errors](either.Left[R, L]),
),
F.Flow2(
rightItem.Validate,
readereither.Map[Context, validation.Errors](either.Right[L, R]),
),
)
}
}
// TranscodeEither creates a Type for Either values with strongly-typed left and right branches.
// It validates and transforms Either[IL, IR] to Either[L, R] during decoding, and
// Either[L, R] to Either[OL, OR] during encoding.
//
// This function is useful for handling sum types (discriminated unions) where a value can be
// one of two possible types. Each branch (Left and Right) is validated and transformed
// independently using its respective Type codec.
//
// Type Parameters:
// - L: The type of the decoded Left value
// - R: The type of the decoded Right value
// - OL: The type of the encoded Left value
// - OR: The type of the encoded Right value
// - IL: The type of the input Left value
// - IR: The type of the input Right value
//
// Parameters:
// - leftItem: A Type[L, OL, IL] that defines how to validate/encode Left values
// - rightItem: A Type[R, OR, IR] that defines how to validate/encode Right values
//
// Returns:
// - A Type[Either[L, R], Either[OL, OR], Either[IL, IR]] that can validate, decode, and encode Either values
//
// The function:
// - Validates Left values using leftItem's validation logic
// - Validates Right values using rightItem's validation logic
// - Preserves the Either structure (Left stays Left, Right stays Right)
// - Provides context-aware error messages indicating which branch failed
// - Transforms values through the respective codecs during encoding
//
// Example:
//
// // Create a codec for Either[string, int]
// stringCodec := codec.String()
// intCodec := codec.Int()
// eitherCodec := codec.TranscodeEither(stringCodec, intCodec)
//
// // Decode Left value
// leftResult := eitherCodec.Decode(either.Left[int]("error"))
// // Success: Right(Either.Left("error"))
//
// // Decode Right value
// rightResult := eitherCodec.Decode(either.Right[string](42))
// // Success: Right(Either.Right(42))
//
// // Encode Left value
// encodedLeft := eitherCodec.Encode(either.Left[int]("error"))
// // Returns: Either.Left("error")
//
// // Encode Right value
// encodedRight := eitherCodec.Encode(either.Right[string](42))
// // Returns: Either.Right(42)
//
// Use TranscodeEither when:
// - You need to handle sum types or discriminated unions
// - You want to validate and transform both branches of an Either independently
// - You're working with error handling patterns (Left for errors, Right for success)
// - You need type-safe transformations for both possible values
//
// Common patterns:
// - Error handling: Either[Error, Value]
// - Optional with reason: Either[Reason, Value]
// - Validation results: Either[ValidationError, ValidatedData]
func TranscodeEither[L, R, OL, OR, IL, IR any](leftItem Type[L, OL, IL], rightItem Type[R, OR, IR]) Type[either.Either[L, R], either.Either[OL, OR], either.Either[IL, IR]] {
validate := validateEitherFromEither(leftItem, rightItem)
is := isFromValidate(validate)
name := fmt.Sprintf("Either[%s, %s]", leftItem.Name(), rightItem.Name())
return MakeType(
name,
is,
validate,
either.Fold(F.Flow2(
leftItem.Encode,
either.Left[OR, OL],
), F.Flow2(
rightItem.Encode,
either.Right[OL, OR],
)),
)
}
func validateAlways[T any](is T) Decode[Context, T] {
return reader.Of[Context](validation.Success(is))
}
// Id creates an identity Type codec that performs no transformation or validation.
//
// An identity codec is a Type[T, T, T] where:
// - Decode: Always succeeds and returns the input value unchanged
// - Encode: Returns the input value unchanged (identity function)
// - Validation: Always succeeds without any checks
//
// This is useful as:
// - A building block for more complex codecs
// - A no-op codec when you need a Type but don't want any transformation
// - A starting point for codec composition
// - Testing and debugging codec pipelines
//
// Type Parameters:
// - T: The type that passes through unchanged
//
// Returns:
// - A Type[T, T, T] that performs identity operations on type T
//
// The codec:
// - Name: Uses the type's string representation (e.g., "int", "string")
// - Is: Checks if a value is of type T
// - Validate: Always succeeds and returns the input value
// - Encode: Identity function (returns input unchanged)
//
// Example:
//
// // Create an identity codec for strings
// stringId := codec.Id[string]()
//
// // Decode always succeeds
// result := stringId.Decode("hello") // Success: Right("hello")
//
// // Encode is identity
// encoded := stringId.Encode("world") // Returns: "world"
//
// // Use in composition
// arrayOfStrings := codec.TranscodeArray(stringId)
// result := arrayOfStrings.Decode([]string{"a", "b", "c"})
//
// Use cases:
// - When you need a Type but don't want any validation or transformation
// - As a placeholder in generic code that requires a Type parameter
// - Building blocks for TranscodeArray, TranscodeEither, etc.
// - Testing codec composition without side effects
//
// Note: Unlike MakeSimpleType which validates the type, Id always succeeds
// in validation. It only checks the type during the Is operation.
func Id[T any]() Type[T, T, T] {
return MakeType(
fmt.Sprintf("%T", *new(T)),
Is[T](),
validateAlways[T],
F.Identity[T],
)
}
func validateFromRefinement[A, B any](refinement Refinement[A, B]) Validate[A, B] {
return func(a A) Decode[Context, B] {
return func(ctx Context) Validation[B] {
return F.Pipe2(
a,
refinement.GetOption,
either.FromOption[B](func() validation.Errors {
return array.Of(&validation.ValidationError{
Value: a,
Context: ctx,
Messsage: fmt.Sprintf("type cannot be refined: %s", refinement),
})
}),
)
}
}
}
func isFromRefinement[A, B any](refinement Refinement[A, B]) ReaderResult[any, B] {
isA := Is[A]()
isB := Is[B]()
err := fmt.Errorf("type cannot be refined: %s", refinement)
isAtoB := F.Flow2(
isA,
result.ChainOptionK[A, B](lazy.Of(err))(refinement.GetOption),
)
return F.Pipe1(
isAtoB,
readereither.ChainLeft(reader.Of[error](isB)),
)
}
// FromRefinement creates a Type codec from a Refinement (Prism).
//
// A Refinement[A, B] represents the concept that B is a specialized/refined version of A.
// For example, PositiveInt is a refinement of int, or NonEmptyString is a refinement of string.
// This function converts a Prism[A, B] into a Type[B, A, A] codec that can validate and transform
// between the base type A and the refined type B.
//
// Type Parameters:
// - A: The base/broader type (e.g., int, string)
// - B: The refined/specialized type (e.g., PositiveInt, NonEmptyString)
//
// Parameters:
// - refinement: A Refinement[A, B] (which is a Prism[A, B]) that defines:
// - GetOption: A → Option[B] - attempts to refine A to B (may fail if refinement conditions aren't met)
// - ReverseGet: B → A - converts refined type back to base type (always succeeds)
//
// Returns:
// - A Type[B, A, A] codec where:
// - Decode: A → Validation[B] - validates that A satisfies refinement conditions and produces B
// - Encode: B → A - converts refined type back to base type using ReverseGet
// - Is: Checks if a value is of type B
// - Name: Descriptive name including the refinement's string representation
//
// The codec:
// - Uses the refinement's GetOption for validation during decoding
// - Returns validation errors if the refinement conditions are not met
// - Uses the refinement's ReverseGet for encoding (always succeeds)
// - Provides context-aware error messages indicating why refinement failed
//
// Example:
//
// // Define a refinement for positive integers
// positiveIntPrism := prism.MakePrismWithName(
// func(n int) option.Option[int] {
// if n > 0 {
// return option.Some(n)
// }
// return option.None[int]()
// },
// func(n int) int { return n },
// "PositiveInt",
// )
//
// // Create a codec from the refinement
// positiveIntCodec := codec.FromRefinement[int, int](positiveIntPrism)
//
// // Decode: validates the refinement condition
// result := positiveIntCodec.Decode(42) // Success: Right(42)
// result = positiveIntCodec.Decode(-5) // Failure: validation error
// result = positiveIntCodec.Decode(0) // Failure: validation error
//
// // Encode: converts back to base type
// encoded := positiveIntCodec.Encode(42) // Returns: 42
//
// Use cases:
// - Creating codecs for refined types (positive numbers, non-empty strings, etc.)
// - Validating that values meet specific constraints
// - Building type-safe APIs with refined types
// - Composing refinements with other codecs using Pipe
//
// Common refinement patterns:
// - Numeric constraints: PositiveInt, NonNegativeFloat, BoundedInt
// - String constraints: NonEmptyString, EmailAddress, URL
// - Collection constraints: NonEmptyArray, UniqueElements
// - Domain-specific constraints: ValidAge, ValidZipCode, ValidCreditCard
//
// Note: The refinement's GetOption returning None will result in a validation error
// with a message indicating the type cannot be refined. For more specific error messages,
// consider using MakeType directly with custom validation logic.
func FromRefinement[A, B any](refinement Refinement[A, B]) Type[B, A, A] {
return MakeType(
fmt.Sprintf("FromRefinement(%s)", refinement),
isFromRefinement(refinement),
validateFromRefinement(refinement),
refinement.ReverseGet,
)
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,129 @@
package decode
import (
"github.com/IBM/fp-go/v2/internal/readert"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/reader"
)
// Of creates a Decode that always succeeds with the given value.
// This is the pointed functor operation that lifts a pure value into the Decode context.
//
// Example:
//
// decoder := decode.Of[string](42)
// result := decoder("any input") // Always returns validation.Success(42)
func Of[I, A any](a A) Decode[I, A] {
return reader.Of[I](validation.Of(a))
}
// MonadChain sequences two decode operations, passing the result of the first to the second.
// This is the monadic bind operation that enables sequential composition of decoders.
//
// Example:
//
// decoder1 := decode.Of[string](42)
// decoder2 := decode.MonadChain(decoder1, func(n int) Decode[string, string] {
// return decode.Of[string](fmt.Sprintf("Number: %d", n))
// })
func MonadChain[I, A, B any](fa Decode[I, A], f Kleisli[I, A, B]) Decode[I, B] {
return readert.MonadChain(
validation.MonadChain,
fa,
f,
)
}
// Chain creates an operator that sequences decode operations.
// This is the curried version of MonadChain, useful for composition pipelines.
//
// Example:
//
// chainOp := decode.Chain(func(n int) Decode[string, string] {
// return decode.Of[string](fmt.Sprintf("Number: %d", n))
// })
// decoder := chainOp(decode.Of[string](42))
func Chain[I, A, B any](f Kleisli[I, A, B]) Operator[I, A, B] {
return readert.Chain[Decode[I, A]](
validation.Chain,
f,
)
}
// MonadMap transforms the decoded value using the provided function.
// This is the functor map operation that applies a transformation to successful decode results.
//
// Example:
//
// decoder := decode.Of[string](42)
// mapped := decode.MonadMap(decoder, func(n int) string {
// return fmt.Sprintf("Number: %d", n)
// })
func MonadMap[I, A, B any](fa Decode[I, A], f func(A) B) Decode[I, B] {
return readert.MonadMap[
Decode[I, A],
Decode[I, B]](
validation.MonadMap,
fa,
f,
)
}
// Map creates an operator that transforms decoded values.
// This is the curried version of MonadMap, useful for composition pipelines.
//
// Example:
//
// mapOp := decode.Map(func(n int) string {
// return fmt.Sprintf("Number: %d", n)
// })
// decoder := mapOp(decode.Of[string](42))
func Map[I, A, B any](f func(A) B) Operator[I, A, B] {
return readert.Map[
Decode[I, A],
Decode[I, B]](
validation.Map,
f,
)
}
// MonadAp applies a decoder containing a function to a decoder containing a value.
// This is the applicative apply operation that enables parallel composition of decoders.
//
// Example:
//
// decoderFn := decode.Of[string](func(n int) string {
// return fmt.Sprintf("Number: %d", n)
// })
// decoderVal := decode.Of[string](42)
// result := decode.MonadAp(decoderFn, decoderVal)
func MonadAp[B, I, A any](fab Decode[I, func(A) B], fa Decode[I, A]) Decode[I, B] {
return readert.MonadAp[
Decode[I, A],
Decode[I, B],
Decode[I, func(A) B], I, A](
validation.MonadAp[B, A],
fab,
fa,
)
}
// Ap creates an operator that applies a function decoder to a value decoder.
// This is the curried version of MonadAp, useful for composition pipelines.
//
// Example:
//
// apOp := decode.Ap[string](decode.Of[string](42))
// decoderFn := decode.Of[string](func(n int) string {
// return fmt.Sprintf("Number: %d", n)
// })
// result := apOp(decoderFn)
func Ap[B, I, A any](fa Decode[I, A]) Operator[I, func(A) B, B] {
return readert.Ap[
Decode[I, A],
Decode[I, B],
Decode[I, func(A) B], I, A](
validation.Ap[B, A],
fa,
)
}

View File

@@ -0,0 +1,384 @@
package decode
import (
"fmt"
"testing"
"github.com/IBM/fp-go/v2/either"
N "github.com/IBM/fp-go/v2/number"
"github.com/IBM/fp-go/v2/optics/codec/validation"
S "github.com/IBM/fp-go/v2/string"
"github.com/stretchr/testify/assert"
)
// TestOf tests the Of function
func TestOf(t *testing.T) {
t.Run("creates decoder that always succeeds", func(t *testing.T) {
decoder := Of[string](42)
res := decoder("any input")
assert.Equal(t, validation.Of(42), res)
})
t.Run("works with different input types", func(t *testing.T) {
decoder := Of[int]("hello")
res := decoder(123)
assert.Equal(t, validation.Of("hello"), res)
})
t.Run("works with complex types", func(t *testing.T) {
type Person struct {
Name string
Age int
}
person := Person{Name: "Alice", Age: 30}
decoder := Of[string](person)
res := decoder("input")
assert.Equal(t, validation.Of(person), res)
})
t.Run("ignores input value", func(t *testing.T) {
decoder := Of[string](100)
res1 := decoder("input1")
res2 := decoder("input2")
assert.Equal(t, res1, res2)
assert.Equal(t, validation.Of(100), res1)
})
}
// TestMonadChain tests the MonadChain function
func TestMonadChain(t *testing.T) {
t.Run("chains successful decoders", func(t *testing.T) {
decoder1 := Of[string](42)
decoder2 := MonadChain(decoder1, func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Number: %d", n))
})
res := decoder2("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("chains multiple operations", func(t *testing.T) {
decoder1 := Of[string](10)
decoder2 := MonadChain(decoder1, func(n int) Decode[string, int] {
return Of[string](n * 2)
})
decoder3 := MonadChain(decoder2, func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Result: %d", n))
})
res := decoder3("input")
assert.Equal(t, validation.Of("Result: 20"), res)
})
t.Run("propagates validation errors", func(t *testing.T) {
failingDecoder := func(input string) Validation[int] {
return either.Left[int](validation.Errors{
{Value: input, Messsage: "decode failed"},
})
}
decoder1 := failingDecoder
decoder2 := MonadChain(decoder1, func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Number: %d", n))
})
res := decoder2("input")
assert.True(t, either.IsLeft(res))
})
t.Run("short-circuits on first error", func(t *testing.T) {
failingDecoder := func(input string) Validation[int] {
return either.Left[int](validation.Errors{
{Value: input, Messsage: "first error"},
})
}
chainCalled := false
decoder := MonadChain(failingDecoder, func(n int) Decode[string, string] {
chainCalled = true
return Of[string]("should not be called")
})
res := decoder("input")
assert.True(t, either.IsLeft(res))
assert.False(t, chainCalled, "Chain function should not be called on error")
})
}
// TestChain tests the Chain function
func TestChain(t *testing.T) {
t.Run("creates chainable operator", func(t *testing.T) {
chainOp := Chain(func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Number: %d", n))
})
decoder := chainOp(Of[string](42))
res := decoder("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("can be composed", func(t *testing.T) {
double := Chain(func(n int) Decode[string, int] {
return Of[string](n * 2)
})
toString := Chain(func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Value: %d", n))
})
decoder := toString(double(Of[string](21)))
res := decoder("input")
assert.Equal(t, validation.Of("Value: 42"), res)
})
}
// TestMonadMap tests the MonadMap function
func TestMonadMap(t *testing.T) {
t.Run("maps successful decoder", func(t *testing.T) {
decoder := Of[string](42)
mapped := MonadMap(decoder, S.Format[int]("Number: %d"))
res := mapped("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("transforms value type", func(t *testing.T) {
decoder := Of[string]("hello")
mapped := MonadMap(decoder, S.Size)
res := mapped("input")
assert.Equal(t, validation.Of(5), res)
})
t.Run("preserves validation errors", func(t *testing.T) {
failingDecoder := func(input string) Validation[int] {
return either.Left[int](validation.Errors{
{Value: input, Messsage: "decode failed"},
})
}
mapped := MonadMap(failingDecoder, S.Format[int]("Number: %d"))
res := mapped("input")
assert.True(t, either.IsLeft(res))
})
t.Run("does not call function on error", func(t *testing.T) {
failingDecoder := func(input string) Validation[int] {
return either.Left[int](validation.Errors{
{Value: input, Messsage: "error"},
})
}
mapCalled := false
mapped := MonadMap(failingDecoder, func(n int) string {
mapCalled = true
return "should not be called"
})
res := mapped("input")
assert.True(t, either.IsLeft(res))
assert.False(t, mapCalled, "Map function should not be called on error")
})
t.Run("chains multiple maps", func(t *testing.T) {
decoder := Of[string](10)
mapped1 := MonadMap(decoder, N.Mul(2))
mapped2 := MonadMap(mapped1, N.Add(5))
mapped3 := MonadMap(mapped2, S.Format[int]("Result: %d"))
res := mapped3("input")
assert.Equal(t, validation.Of("Result: 25"), res)
})
}
// TestMap tests the Map function
func TestMap(t *testing.T) {
t.Run("creates mappable operator", func(t *testing.T) {
mapOp := Map[string](S.Format[int]("Number: %d"))
decoder := mapOp(Of[string](42))
res := decoder("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("can be composed", func(t *testing.T) {
double := Map[string](N.Mul(2))
toString := Map[string](S.Format[int]("Value: %d"))
decoder := toString(double(Of[string](21)))
res := decoder("input")
assert.Equal(t, validation.Of("Value: 42"), res)
})
}
// TestMonadAp tests the MonadAp function
func TestMonadAp(t *testing.T) {
t.Run("applies function decoder to value decoder", func(t *testing.T) {
decoderFn := Of[string](S.Format[int]("Number: %d"))
decoderVal := Of[string](42)
res := MonadAp(decoderFn, decoderVal)("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("works with different transformations", func(t *testing.T) {
decoderFn := Of[string](N.Mul(2))
decoderVal := Of[string](21)
res := MonadAp(decoderFn, decoderVal)("input")
assert.Equal(t, validation.Of(42), res)
})
t.Run("propagates function decoder error", func(t *testing.T) {
failingFnDecoder := func(input string) Validation[func(int) string] {
return either.Left[func(int) string](validation.Errors{
{Value: input, Messsage: "function decode failed"},
})
}
decoderVal := Of[string](42)
res := MonadAp(failingFnDecoder, decoderVal)("input")
assert.True(t, either.IsLeft(res))
})
t.Run("propagates value decoder error", func(t *testing.T) {
decoderFn := Of[string](S.Format[int]("Number: %d"))
failingValDecoder := func(input string) Validation[int] {
return either.Left[int](validation.Errors{
{Value: input, Messsage: "value decode failed"},
})
}
res := MonadAp(decoderFn, failingValDecoder)("input")
assert.True(t, either.IsLeft(res))
})
t.Run("combines multiple values", func(t *testing.T) {
// Create a function that takes two arguments
decoderFn := Of[string](N.Add[int])
decoderVal1 := Of[string](10)
decoderVal2 := Of[string](32)
// Apply first value
partial := MonadAp(decoderFn, decoderVal1)
// Apply second value
result := MonadAp(partial, decoderVal2)
res := result("input")
assert.Equal(t, validation.Of(42), res)
})
}
// TestAp tests the Ap function
func TestAp(t *testing.T) {
t.Run("creates applicable operator", func(t *testing.T) {
decoderVal := Of[string](42)
apOp := Ap[string](decoderVal)
decoderFn := Of[string](S.Format[int]("Number: %d"))
res := apOp(decoderFn)("input")
assert.Equal(t, validation.Of("Number: 42"), res)
})
t.Run("can be composed", func(t *testing.T) {
val1 := Of[string](10)
val2 := Of[string](32)
apOp1 := Ap[func(int) int](val1)
apOp2 := Ap[int](val2)
fnDecoder := Of[string](N.Add[int])
result := apOp2(apOp1(fnDecoder))
res := result("input")
assert.Equal(t, validation.Of(42), res)
})
}
// TestMonadLaws tests that the monad operations satisfy monad laws
func TestMonadLaws(t *testing.T) {
t.Run("left identity: Of(a) >>= f === f(a)", func(t *testing.T) {
a := 42
f := func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Number: %d", n))
}
left := MonadChain(Of[string](a), f)
right := f(a)
input := "test"
assert.Equal(t, right(input), left(input))
})
t.Run("right identity: m >>= Of === m", func(t *testing.T) {
m := Of[string](42)
left := MonadChain(m, func(a int) Decode[string, int] {
return Of[string](a)
})
input := "test"
assert.Equal(t, m(input), left(input))
})
t.Run("associativity: (m >>= f) >>= g === m >>= (\\x -> f(x) >>= g)", func(t *testing.T) {
m := Of[string](10)
f := func(n int) Decode[string, int] {
return Of[string](n * 2)
}
g := func(n int) Decode[string, string] {
return Of[string](fmt.Sprintf("Result: %d", n))
}
// (m >>= f) >>= g
left := MonadChain(MonadChain(m, f), g)
// m >>= (\x -> f(x) >>= g)
right := MonadChain(m, func(x int) Decode[string, string] {
return MonadChain(f(x), g)
})
input := "test"
assert.Equal(t, right(input), left(input))
})
}
// TestFunctorLaws tests that the functor operations satisfy functor laws
func TestFunctorLaws(t *testing.T) {
t.Run("identity: map(id) === id", func(t *testing.T) {
decoder := Of[string](42)
mapped := MonadMap(decoder, func(a int) int { return a })
input := "test"
assert.Equal(t, decoder(input), mapped(input))
})
t.Run("composition: map(f . g) === map(f) . map(g)", func(t *testing.T) {
decoder := Of[string](10)
f := N.Mul(2)
g := N.Add(5)
// map(f . g)
left := MonadMap(decoder, func(n int) int {
return f(g(n))
})
// map(f) . map(g)
right := MonadMap(MonadMap(decoder, g), f)
input := "test"
assert.Equal(t, right(input), left(input))
})
}

View File

@@ -0,0 +1,30 @@
package decode
import (
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/reader"
)
type (
// Validation represents the result of a validation operation that may contain
// validation errors or a successfully validated value of type A.
Validation[A any] = validation.Validation[A]
// Reader represents a computation that depends on an environment R and produces a value A.
Reader[R, A any] = reader.Reader[R, A]
// Decode is a function that decodes input I to type A with validation.
// It returns a Validation result directly.
Decode[I, A any] = Reader[I, Validation[A]]
// Kleisli represents a function from A to a decoded B given input type I.
// It's a Reader that takes an input A and produces a Decode[I, B] function.
// This enables composition of decoding operations in a functional style.
Kleisli[I, A, B any] = Reader[A, Decode[I, B]]
// Operator represents a decoding transformation that takes a decoded A and produces a decoded B.
// It's a specialized Kleisli arrow for composing decode operations where the input is already decoded.
// This allows chaining multiple decode transformations together.
Operator[I, A, B any] = Kleisli[I, Decode[I, A], B]
)

84
v2/optics/codec/format.go Normal file
View File

@@ -0,0 +1,84 @@
package codec
import (
"fmt"
"log/slog"
"github.com/IBM/fp-go/v2/internal/formatting"
)
// String implements the fmt.Stringer interface for typeImpl.
// It returns the name of the type, which is used for simple string representation.
//
// Example:
//
// stringType := codec.String()
// fmt.Println(stringType) // Output: "string"
func (t *typeImpl[A, O, I]) String() string {
return t.name
}
// Format implements the fmt.Formatter interface for typeImpl.
// It provides custom formatting based on the format verb:
// - %s, %v: Returns the type name
// - %q: Returns the type name in quotes
// - %#v: Returns a detailed Go-syntax representation
//
// Example:
//
// intType := codec.Int()
// fmt.Printf("%s\n", intType) // Output: int
// fmt.Printf("%q\n", intType) // Output: "int"
// fmt.Printf("%#v\n", intType) // Output: codec.Type[int, int, any]{name: "int"}
func (t *typeImpl[A, O, I]) Format(f fmt.State, verb rune) {
formatting.FmtString(t, f, verb)
}
// GoString implements the fmt.GoStringer interface for typeImpl.
// It returns a Go-syntax representation of the type that could be used
// to recreate the type (though not executable due to function values).
//
// This is called when using the %#v format verb with fmt.Printf.
//
// Example:
//
// stringType := codec.String()
// fmt.Printf("%#v\n", stringType)
// // Output: codec.Type[string, string, any]{name: "string"}
func (t *typeImpl[A, O, I]) GoString() string {
return fmt.Sprintf("codec.Type[%s, %s, %s]{name: %q}",
typeNameOf[A](), typeNameOf[O](), typeNameOf[I](), t.name)
}
// LogValue implements the slog.LogValuer interface for typeImpl.
// It provides structured logging representation of the codec type.
// Returns a slog.Value containing the type information as a group with
// the codec name and type parameters.
//
// This method is called automatically when logging a codec with slog.
//
// Example:
//
// stringType := codec.String()
// slog.Info("codec created", "codec", stringType)
// // Logs: codec={name=string type_a=string type_o=string type_i=interface {}}
func (t *typeImpl[A, O, I]) LogValue() slog.Value {
return slog.GroupValue(
slog.String("name", t.name),
slog.String("type_a", typeNameOf[A]()),
slog.String("type_o", typeNameOf[O]()),
slog.String("type_i", typeNameOf[I]()),
)
}
// typeNameOf returns a string representation of the type T.
// It handles the special case where T is 'any' (interface{}).
func typeNameOf[T any]() string {
var zero T
typeName := fmt.Sprintf("%T", zero)
// Handle the case where %T prints "<nil>" for interface{} types
if typeName == "<nil>" {
return "interface {}"
}
return typeName
}

View File

@@ -0,0 +1,216 @@
package codec
import (
"fmt"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
// TestTypeImplStringer tests the String() method implementation
func TestTypeImplStringer(t *testing.T) {
t.Run("String codec", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
result := codec.String()
assert.Equal(t, "string", result)
})
t.Run("Int codec", func(t *testing.T) {
codec := Int().(*typeImpl[int, int, any])
result := codec.String()
assert.Equal(t, "int", result)
})
t.Run("Bool codec", func(t *testing.T) {
codec := Bool().(*typeImpl[bool, bool, any])
result := codec.String()
assert.Equal(t, "bool", result)
})
}
// TestTypeImplFormat tests the Format() method implementation
func TestTypeImplFormat(t *testing.T) {
t.Run("String codec with %s", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
result := fmt.Sprintf("%s", codec)
assert.Equal(t, "string", result)
})
t.Run("String codec with %v", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
result := fmt.Sprintf("%v", codec)
assert.Equal(t, "string", result)
})
t.Run("String codec with %q", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
result := fmt.Sprintf("%q", codec)
assert.Equal(t, `"string"`, result)
})
t.Run("Int codec with %s", func(t *testing.T) {
codec := Int().(*typeImpl[int, int, any])
result := fmt.Sprintf("%s", codec)
assert.Equal(t, "int", result)
})
t.Run("Int codec with %#v", func(t *testing.T) {
codec := Int().(*typeImpl[int, int, any])
result := fmt.Sprintf("%#v", codec)
assert.Equal(t, `codec.Type[int, int, interface {}]{name: "int"}`, result)
})
}
// TestTypeImplGoString tests the GoString() method implementation
func TestTypeImplGoString(t *testing.T) {
t.Run("String codec", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
result := codec.GoString()
assert.Equal(t, `codec.Type[string, string, interface {}]{name: "string"}`, result)
})
t.Run("Int codec", func(t *testing.T) {
codec := Int().(*typeImpl[int, int, any])
result := codec.GoString()
assert.Equal(t, `codec.Type[int, int, interface {}]{name: "int"}`, result)
})
t.Run("Bool codec", func(t *testing.T) {
codec := Bool().(*typeImpl[bool, bool, any])
result := codec.GoString()
assert.Equal(t, `codec.Type[bool, bool, interface {}]{name: "bool"}`, result)
})
}
// TestTypeImplFormatWithPrintf tests that %#v uses GoString
func TestTypeImplFormatWithPrintf(t *testing.T) {
stringCodec := String().(*typeImpl[string, string, any])
// Test that %#v calls GoString
result := fmt.Sprintf("%#v", stringCodec)
assert.Equal(t, `codec.Type[string, string, interface {}]{name: "string"}`, result)
}
// TestComplexTypeFormatting tests formatting of more complex types
func TestComplexTypeFormatting(t *testing.T) {
// Create an array codec
arrayCodec := Array(Int()).(*typeImpl[[]int, []int, any])
// Test String()
name := arrayCodec.String()
assert.Equal(t, "Array[int]", name)
// Test Format with %s
formatted := fmt.Sprintf("%s", arrayCodec)
assert.Equal(t, "Array[int]", formatted)
// Test GoString
goString := arrayCodec.GoString()
// Just verify it's not empty
assert.NotEmpty(t, goString)
}
// TestFormatterInterface verifies that typeImpl implements fmt.Formatter
func TestFormatterInterface(t *testing.T) {
var _ fmt.Formatter = (*typeImpl[int, int, any])(nil)
}
// TestStringerInterface verifies that typeImpl implements fmt.Stringer
func TestStringerInterface(t *testing.T) {
var _ fmt.Stringer = (*typeImpl[int, int, any])(nil)
}
// TestGoStringerInterface verifies that typeImpl implements fmt.GoStringer
func TestGoStringerInterface(t *testing.T) {
var _ fmt.GoStringer = (*typeImpl[int, int, any])(nil)
}
// TestLogValuerInterface verifies that typeImpl implements slog.LogValuer
func TestLogValuerInterface(t *testing.T) {
var _ slog.LogValuer = (*typeImpl[int, int, any])(nil)
}
// TestTypeImplLogValue tests the LogValue() method implementation
func TestTypeImplLogValue(t *testing.T) {
t.Run("String codec", func(t *testing.T) {
codec := String().(*typeImpl[string, string, any])
logValue := codec.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
// Extract attributes from the group
attrs := logValue.Group()
assert.Len(t, attrs, 4)
// Check that we have the expected attributes
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "string", attrMap["name"])
assert.Equal(t, "string", attrMap["type_a"])
assert.Equal(t, "string", attrMap["type_o"])
assert.Contains(t, attrMap["type_i"], "interface")
})
t.Run("Int codec", func(t *testing.T) {
codec := Int().(*typeImpl[int, int, any])
logValue := codec.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
assert.Len(t, attrs, 4)
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "int", attrMap["name"])
assert.Equal(t, "int", attrMap["type_a"])
assert.Equal(t, "int", attrMap["type_o"])
})
t.Run("Bool codec", func(t *testing.T) {
codec := Bool().(*typeImpl[bool, bool, any])
logValue := codec.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
assert.Len(t, attrs, 4)
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "bool", attrMap["name"])
assert.Equal(t, "bool", attrMap["type_a"])
})
t.Run("Array codec", func(t *testing.T) {
codec := Array(Int()).(*typeImpl[[]int, []int, any])
logValue := codec.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
assert.Len(t, attrs, 4)
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "Array[int]", attrMap["name"])
})
}
// TestFormattableInterface verifies that typeImpl implements formatting.Formattable
func TestFormattableInterface(t *testing.T) {
var _ Formattable = (*typeImpl[int, int, any])(nil)
}

81
v2/optics/codec/prism.go Normal file
View File

@@ -0,0 +1,81 @@
package codec
import (
"github.com/IBM/fp-go/v2/either"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/optics/prism"
)
// TypeToPrism converts a Type codec into a Prism optic.
//
// A Type[A, S, S] represents a bidirectional codec that can decode S to A (with validation)
// and encode A back to S. A Prism[S, A] is an optic that can optionally extract an A from S
// and always construct an S from an A.
//
// This conversion bridges the codec and optics worlds, allowing you to use validation-based
// codecs as prisms for functional optics composition.
//
// Type Parameters:
// - S: The source/encoded type (both input and output)
// - A: The decoded/focus type
//
// Parameters:
// - t: A Type[A, S, S] codec where:
// - Decode: S → Validation[A] (may fail with validation errors)
// - Encode: A → S (always succeeds)
// - Name: Provides a descriptive name for the type
//
// Returns:
// - A Prism[S, A] where:
// - GetOption: S → Option[A] (Some if decode succeeds, None if validation fails)
// - ReverseGet: A → S (uses the codec's Encode function)
// - Name: Inherited from the Type's name
//
// The conversion works as follows:
// - GetOption: Decodes the value and converts validation result to Option
// (Right(a) → Some(a), Left(errors) → None)
// - ReverseGet: Directly uses the Type's Encode function
// - Name: Preserves the Type's descriptive name
//
// Example:
//
// // Create a codec for positive integers
// positiveInt := codec.MakeType[int, int, int](
// "PositiveInt",
// func(i any) result.Result[int] { ... },
// func(i int) codec.Validate[int] {
// if i <= 0 {
// return validation.FailureWithMessage(i, "must be positive")
// }
// return validation.Success(i)
// },
// func(i int) int { return i },
// )
//
// // Convert to prism
// prism := codec.TypeToPrism(positiveInt)
//
// // Use as prism
// value := prism.GetOption(42) // Some(42) - validation succeeds
// value = prism.GetOption(-5) // None - validation fails
// result := prism.ReverseGet(10) // 10 - encoding always succeeds
//
// Use cases:
// - Composing codecs with other optics (lenses, prisms, traversals)
// - Using validation logic in optics pipelines
// - Building complex data transformations with functional composition
// - Integrating type-safe parsing with optics-based data access
//
// Note: The prism's GetOption will return None for any validation failure,
// discarding the specific error details. If you need error information,
// use the Type's Decode method directly instead.
func TypeToPrism[S, A any](t Type[A, S, S]) Prism[S, A] {
return prism.MakePrismWithName(
F.Flow2(
t.Decode,
either.ToOption,
),
t.Encode,
t.Name(),
)
}

View File

@@ -0,0 +1,327 @@
package codec
import (
"testing"
"github.com/IBM/fp-go/v2/either"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/option"
"github.com/stretchr/testify/assert"
)
// TestTypeToPrismBasic tests basic TypeToPrism functionality
func TestTypeToPrismBasic(t *testing.T) {
// Create a simple string identity type
stringType := Id[string]()
prism := TypeToPrism(stringType)
t.Run("GetOption returns Some for valid value", func(t *testing.T) {
result := prism.GetOption("hello")
assert.True(t, option.IsSome(result), "Expected Some for valid string")
value := option.GetOrElse(F.Constant(""))(result)
assert.Equal(t, "hello", value)
})
t.Run("ReverseGet encodes value correctly", func(t *testing.T) {
encoded := prism.ReverseGet("world")
assert.Equal(t, "world", encoded)
})
t.Run("Name is preserved from Type", func(t *testing.T) {
assert.Equal(t, stringType.Name(), prism.String())
})
t.Run("Round trip preserves value", func(t *testing.T) {
original := "test value"
encoded := prism.ReverseGet(original)
decoded := prism.GetOption(encoded)
assert.True(t, option.IsSome(decoded))
value := option.GetOrElse(F.Constant(""))(decoded)
assert.Equal(t, original, value)
})
}
// TestTypeToPrismValidationLogic tests TypeToPrism with validation logic
func TestTypeToPrismValidationLogic(t *testing.T) {
// Create a type that validates positive integers
positiveIntType := MakeType(
"PositiveInt",
func(u any) either.Either[error, int] {
i, ok := u.(int)
if !ok || i <= 0 {
return either.Left[int](assert.AnError)
}
return either.Of[error](i)
},
func(i int) Decode[Context, int] {
return func(c Context) Validation[int] {
if i <= 0 {
return validation.FailureWithMessage[int](i, "must be positive")(c)
}
return validation.Success(i)
}
},
F.Identity[int],
)
prism := TypeToPrism(positiveIntType)
t.Run("GetOption returns Some for valid positive integer", func(t *testing.T) {
result := prism.GetOption(42)
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(0))(result)
assert.Equal(t, 42, value)
})
t.Run("GetOption returns None for negative integer", func(t *testing.T) {
result := prism.GetOption(-5)
assert.True(t, option.IsNone(result), "Expected None for negative integer")
})
t.Run("GetOption returns None for zero", func(t *testing.T) {
result := prism.GetOption(0)
assert.True(t, option.IsNone(result), "Expected None for zero")
})
t.Run("GetOption returns Some for boundary value", func(t *testing.T) {
result := prism.GetOption(1)
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(0))(result)
assert.Equal(t, 1, value)
})
t.Run("ReverseGet does not validate", func(t *testing.T) {
// ReverseGet should encode without validation
encoded := prism.ReverseGet(-10)
assert.Equal(t, -10, encoded, "ReverseGet should not validate")
})
t.Run("Name reflects validation purpose", func(t *testing.T) {
assert.Equal(t, "PositiveInt", prism.String())
})
}
// TestTypeToPrismWithComplexValidation tests more complex validation scenarios
func TestTypeToPrismWithComplexValidation(t *testing.T) {
// Create a type that validates strings with length constraints
boundedStringType := MakeType(
"BoundedString",
func(u any) either.Either[error, string] {
s, ok := u.(string)
if !ok {
return either.Left[string](assert.AnError)
}
return either.Of[error](s)
},
func(s string) Decode[Context, string] {
return func(c Context) Validation[string] {
if len(s) < 3 {
return validation.FailureWithMessage[string](s, "must be at least 3 characters")(c)
}
if len(s) > 10 {
return validation.FailureWithMessage[string](s, "must be at most 10 characters")(c)
}
return validation.Success(s)
}
},
F.Identity[string],
)
prism := TypeToPrism(boundedStringType)
t.Run("GetOption returns Some for valid length", func(t *testing.T) {
result := prism.GetOption("hello")
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(""))(result)
assert.Equal(t, "hello", value)
})
t.Run("GetOption returns None for too short string", func(t *testing.T) {
result := prism.GetOption("ab")
assert.True(t, option.IsNone(result))
})
t.Run("GetOption returns None for too long string", func(t *testing.T) {
result := prism.GetOption("this is way too long")
assert.True(t, option.IsNone(result))
})
t.Run("GetOption returns Some for minimum length", func(t *testing.T) {
result := prism.GetOption("abc")
assert.True(t, option.IsSome(result))
})
t.Run("GetOption returns Some for maximum length", func(t *testing.T) {
result := prism.GetOption("1234567890")
assert.True(t, option.IsSome(result))
})
}
// TestTypeToPrismWithNumericTypes tests TypeToPrism with different numeric types
func TestTypeToPrismWithNumericTypes(t *testing.T) {
t.Run("Float64 type", func(t *testing.T) {
floatType := Id[float64]()
prism := TypeToPrism(floatType)
result := prism.GetOption(3.14)
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(0.0))(result)
assert.Equal(t, 3.14, value)
})
t.Run("Int64 type", func(t *testing.T) {
int64Type := Id[int64]()
prism := TypeToPrism(int64Type)
result := prism.GetOption(int64(9223372036854775807))
assert.True(t, option.IsSome(result))
})
}
// TestTypeToPrismWithBooleanType tests TypeToPrism with boolean type
func TestTypeToPrismWithBooleanType(t *testing.T) {
boolType := Id[bool]()
prism := TypeToPrism(boolType)
t.Run("GetOption returns Some for true", func(t *testing.T) {
result := prism.GetOption(true)
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(false))(result)
assert.True(t, value)
})
t.Run("GetOption returns Some for false", func(t *testing.T) {
result := prism.GetOption(false)
assert.True(t, option.IsSome(result))
value := option.GetOrElse(F.Constant(true))(result)
assert.False(t, value)
})
t.Run("ReverseGet preserves boolean values", func(t *testing.T) {
assert.True(t, prism.ReverseGet(true))
assert.False(t, prism.ReverseGet(false))
})
}
// TestTypeToPrismEdgeCases tests edge cases and special scenarios
func TestTypeToPrismEdgeCases(t *testing.T) {
t.Run("Empty string validation", func(t *testing.T) {
nonEmptyStringType := MakeType(
"NonEmptyString",
func(u any) either.Either[error, string] {
s, ok := u.(string)
if !ok {
return either.Left[string](assert.AnError)
}
return either.Of[error](s)
},
func(s string) Decode[Context, string] {
return func(c Context) Validation[string] {
if s == "" {
return validation.FailureWithMessage[string](s, "must not be empty")(c)
}
return validation.Success(s)
}
},
F.Identity[string],
)
prism := TypeToPrism(nonEmptyStringType)
emptyResult := prism.GetOption("")
assert.True(t, option.IsNone(emptyResult), "Empty string should fail validation")
nonEmptyResult := prism.GetOption("a")
assert.True(t, option.IsSome(nonEmptyResult))
})
t.Run("Multiple validation failures", func(t *testing.T) {
strictIntType := MakeType(
"StrictInt",
func(u any) either.Either[error, int] {
i, ok := u.(int)
if !ok {
return either.Left[int](assert.AnError)
}
return either.Of[error](i)
},
func(i int) Decode[Context, int] {
return func(c Context) Validation[int] {
if i < 0 {
return validation.FailureWithMessage[int](i, "must be non-negative")(c)
}
if i > 100 {
return validation.FailureWithMessage[int](i, "must be at most 100")(c)
}
if i%2 != 0 {
return validation.FailureWithMessage[int](i, "must be even")(c)
}
return validation.Success(i)
}
},
F.Identity[int],
)
prism := TypeToPrism(strictIntType)
// Valid value
validResult := prism.GetOption(42)
assert.True(t, option.IsSome(validResult))
// Various invalid values
assert.True(t, option.IsNone(prism.GetOption(-1)), "Negative should fail")
assert.True(t, option.IsNone(prism.GetOption(101)), "Too large should fail")
assert.True(t, option.IsNone(prism.GetOption(43)), "Odd should fail")
})
}
// TestTypeToPrismNamePreservation tests that prism names are correctly preserved
func TestTypeToPrismNamePreservation(t *testing.T) {
testCases := []struct {
name string
typeName string
}{
{"Simple name", "SimpleType"},
{"Descriptive name", "PositiveIntegerValidator"},
{"With spaces", "Type With Spaces"},
{"With special chars", "Type_With-Special.Chars"},
{"Unicode name", "类型名称"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
stringType := MakeType(
tc.typeName,
func(u any) either.Either[error, string] {
s, ok := u.(string)
if !ok {
return either.Left[string](assert.AnError)
}
return either.Of[error](s)
},
func(s string) Decode[Context, string] {
return func(c Context) Validation[string] {
return validation.Success(s)
}
},
F.Identity[string],
)
prism := TypeToPrism(stringType)
assert.Equal(t, tc.typeName, prism.String())
})
}
}

View File

@@ -1,12 +1,15 @@
package codec
import (
"github.com/IBM/fp-go/v2/either"
"github.com/IBM/fp-go/v2/endomorphism"
"github.com/IBM/fp-go/v2/internal/formatting"
"github.com/IBM/fp-go/v2/lazy"
"github.com/IBM/fp-go/v2/optics/codec/decode"
"github.com/IBM/fp-go/v2/optics/codec/validate"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/optics/decoder"
"github.com/IBM/fp-go/v2/optics/encoder"
"github.com/IBM/fp-go/v2/optics/prism"
"github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/pair"
"github.com/IBM/fp-go/v2/reader"
@@ -15,6 +18,12 @@ import (
)
type (
// Formattable represents a type that can be formatted as a string representation.
// It provides a way to obtain a human-readable description of a type or value.
Formattable = formatting.Formattable
// ReaderResult represents a computation that depends on an environment R,
// produces a value A, and may fail with an error.
ReaderResult[R, A any] = readerresult.ReaderResult[R, A]
// Lazy represents a lazily evaluated value.
@@ -26,9 +35,6 @@ type (
// Option represents an optional value that may or may not be present.
Option[A any] = option.Option[A]
// Either represents a value that can be one of two types: Left (error) or Right (success).
Either[E, A any] = either.Either[E, A]
// Result represents a computation that may fail with an error.
Result[A any] = result.Result[A]
@@ -39,17 +45,21 @@ type (
Encode encoder.Encoder[O, A]
}
// Validation represents the result of a validation operation that may contain
// validation errors or a successfully validated value of type A.
Validation[A any] = validation.Validation[A]
// Context provides contextual information for validation operations,
// such as the current path in a nested structure.
Context = validation.Context
// Validate is a function that validates input I to produce type A.
// It takes an input and returns a Reader that depends on the validation Context.
Validate[I, A any] = Reader[I, Reader[Context, Validation[A]]]
Validate[I, A any] = validate.Validate[I, A]
// Decode is a function that decodes input I to type A with validation.
// It returns a Validation result directly.
Decode[I, A any] = Reader[I, Validation[A]]
Decode[I, A any] = decode.Decode[I, A]
// Encode is a function that encodes type A to output O.
Encode[A, O any] = Reader[A, O]
@@ -57,7 +67,7 @@ type (
// Decoder is an interface for types that can decode and validate input.
Decoder[I, A any] interface {
Name() string
Validate(I) Reader[Context, Validation[A]]
Validate(I) Decode[Context, A]
Decode(I) Validation[A]
}
@@ -70,6 +80,7 @@ type (
// and type checking capabilities. It represents a complete specification of
// how to work with a particular type.
Type[A, O, I any] interface {
Formattable
Decoder[I, A]
Encoder[A, O]
AsDecoder() Decoder[I, A]
@@ -77,7 +88,17 @@ type (
Is(any) Result[A]
}
// Endomorphism represents a function from type A to itself (A -> A).
// It forms a monoid under function composition.
Endomorphism[A any] = endomorphism.Endomorphism[A]
// Pair represents a tuple of two values of types L and R.
Pair[L, R any] = pair.Pair[L, R]
// Prism is an optic that focuses on a part of a sum type S that may or may not
// contain a value of type A. It provides a way to preview and review values.
Prism[S, A any] = prism.Prism[S, A]
// Refinement represents the concept that B is a specialized type of A
Refinement[A, B any] = Prism[A, B]
)

View File

@@ -0,0 +1,124 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"github.com/IBM/fp-go/v2/monoid"
)
// ApplicativeMonoid creates a Monoid instance for Validate[I, A] given a Monoid[A].
//
// This function lifts a monoid operation on values of type A to work with validators
// that produce values of type A. It uses the applicative functor structure of the
// nested Reader types to combine validators while preserving their validation context.
//
// The resulting monoid allows you to:
// - Combine multiple validators that produce monoidal values
// - Run validators in parallel and merge their results using the monoid operation
// - Build complex validators compositionally from simpler ones
//
// # Type Parameters
//
// - I: The input type that validators accept
// - A: The output type that validators produce (must have a Monoid instance)
//
// # Parameters
//
// - m: A Monoid[A] that defines how to combine values of type A
//
// # Returns
//
// A Monoid[Validate[I, A]] that can combine validators using the applicative structure.
//
// # How It Works
//
// The function composes three layers of applicative monoids:
// 1. The innermost layer uses validation.ApplicativeMonoid(m) to combine Validation[A] values
// 2. The middle layer wraps this in reader.ApplicativeMonoid for the Context dependency
// 3. The outer layer wraps everything in reader.ApplicativeMonoid for the input I dependency
//
// This creates a monoid that:
// - Takes the same input I for both validators
// - Threads the same Context through both validators
// - Combines successful results using the monoid operation on A
// - Accumulates validation errors from both validators if either fails
//
// # Example
//
// Combining string validators using string concatenation:
//
// import (
// "github.com/IBM/fp-go/v2/monoid"
// "github.com/IBM/fp-go/v2/string"
// "github.com/IBM/fp-go/v2/optics/codec/validate"
// "github.com/IBM/fp-go/v2/optics/codec/validation"
// )
//
// // Create a monoid for string validators
// stringMonoid := string.Monoid
// validatorMonoid := validate.ApplicativeMonoid[string, string](stringMonoid)
//
// // Define two validators that extract different parts
// validator1 := func(input string) validate.Reader[validation.Context, validation.Validation[string]] {
// return func(ctx validation.Context) validation.Validation[string] {
// return validation.Success("Hello ")
// }
// }
//
// validator2 := func(input string) validate.Reader[validation.Context, validation.Validation[string]] {
// return func(ctx validation.Context) validation.Validation[string] {
// return validation.Success("World")
// }
// }
//
// // Combine them - results will be concatenated
// combined := validatorMonoid.Concat(validator1, validator2)
// // When run, produces validation.Success("Hello World")
//
// Combining numeric validators using addition:
//
// import (
// "github.com/IBM/fp-go/v2/number"
// )
//
// // Create a monoid for int validators using addition
// intMonoid := number.MonoidSum[int]()
// validatorMonoid := validate.ApplicativeMonoid[string, int](intMonoid)
//
// // Validators that extract and validate different numeric fields
// // Results will be summed together
//
// # Notes
//
// - Both validators receive the same input value I
// - If either validator fails, all errors are accumulated
// - If both succeed, their results are combined using the monoid operation
// - The empty element of the monoid serves as the identity for the Concat operation
// - This follows the applicative functor laws for combining effectful computations
//
// # See Also
//
// - validation.ApplicativeMonoid: The underlying monoid for validation results
// - reader.ApplicativeMonoid: The monoid for reader computations
// - Monoid[A]: The monoid instance for the result type
func ApplicativeMonoid[I, A any](m Monoid[A]) Monoid[Validate[I, A]] {
return monoid.ApplicativeMonoid[A, Validate[I, A]](
Of,
MonadMap,
MonadAp,
m,
)
}

View File

@@ -0,0 +1,475 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"testing"
E "github.com/IBM/fp-go/v2/either"
N "github.com/IBM/fp-go/v2/number"
"github.com/IBM/fp-go/v2/optics/codec/validation"
S "github.com/IBM/fp-go/v2/string"
"github.com/stretchr/testify/assert"
)
var (
intAddMonoid = N.MonoidSum[int]()
strMonoid = S.Monoid
)
// Helper function to create a successful validator
func successValidator[I, A any](value A) Validate[I, A] {
return func(input I) Reader[validation.Context, validation.Validation[A]] {
return func(ctx validation.Context) validation.Validation[A] {
return validation.Success(value)
}
}
}
// Helper function to create a failing validator
func failureValidator[I, A any](message string) Validate[I, A] {
return func(input I) Reader[validation.Context, validation.Validation[A]] {
return validation.FailureWithMessage[A](input, message)
}
}
// Helper function to create a validator that uses the input
func inputDependentValidator[A any](f func(A) A) Validate[A, A] {
return func(input A) Reader[validation.Context, validation.Validation[A]] {
return func(ctx validation.Context) validation.Validation[A] {
return validation.Success(f(input))
}
}
}
// TestApplicativeMonoid_EmptyElement tests the empty element of the monoid
func TestApplicativeMonoid_EmptyElement(t *testing.T) {
t.Run("int addition monoid", func(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
empty := m.Empty()
result := empty("test")(nil)
assert.Equal(t, validation.Of(0), result)
})
t.Run("string concatenation monoid", func(t *testing.T) {
m := ApplicativeMonoid[int](strMonoid)
empty := m.Empty()
result := empty(42)(nil)
assert.Equal(t, validation.Of(""), result)
})
}
// TestApplicativeMonoid_ConcatSuccesses tests concatenating two successful validators
func TestApplicativeMonoid_ConcatSuccesses(t *testing.T) {
t.Run("int addition", func(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](5)
v2 := successValidator[string](3)
combined := m.Concat(v1, v2)
result := combined("input")(nil)
assert.Equal(t, validation.Of(8), result)
})
t.Run("string concatenation", func(t *testing.T) {
m := ApplicativeMonoid[int](strMonoid)
v1 := successValidator[int]("Hello")
v2 := successValidator[int](" World")
combined := m.Concat(v1, v2)
result := combined(42)(nil)
assert.Equal(t, validation.Of("Hello World"), result)
})
}
// TestApplicativeMonoid_ConcatWithFailure tests concatenating validators where one fails
func TestApplicativeMonoid_ConcatWithFailure(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
t.Run("left failure", func(t *testing.T) {
v1 := failureValidator[string, int]("left error")
v2 := successValidator[string](5)
combined := m.Concat(v1, v2)
result := combined("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "left error", errors[0].Messsage)
})
t.Run("right failure", func(t *testing.T) {
v1 := successValidator[string](5)
v2 := failureValidator[string, int]("right error")
combined := m.Concat(v1, v2)
result := combined("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "right error", errors[0].Messsage)
})
t.Run("both failures", func(t *testing.T) {
v1 := failureValidator[string, int]("left error")
v2 := failureValidator[string, int]("right error")
combined := m.Concat(v1, v2)
result := combined("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
// Note: The current implementation returns the first error encountered
assert.GreaterOrEqual(t, len(errors), 1)
// At least one of the errors should be present
hasError := false
for _, err := range errors {
if err.Messsage == "left error" || err.Messsage == "right error" {
hasError = true
break
}
}
assert.True(t, hasError, "Should contain at least one validation error")
})
}
// TestApplicativeMonoid_LeftIdentity tests the left identity law
func TestApplicativeMonoid_LeftIdentity(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v := successValidator[string](42)
// empty <> v == v
combined := m.Concat(m.Empty(), v)
resultCombined := combined("test")(nil)
resultOriginal := v("test")(nil)
assert.Equal(t, resultOriginal, resultCombined)
}
// TestApplicativeMonoid_RightIdentity tests the right identity law
func TestApplicativeMonoid_RightIdentity(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v := successValidator[string](42)
// v <> empty == v
combined := m.Concat(v, m.Empty())
resultCombined := combined("test")(nil)
resultOriginal := v("test")(nil)
assert.Equal(t, resultOriginal, resultCombined)
}
// TestApplicativeMonoid_Associativity tests the associativity law
func TestApplicativeMonoid_Associativity(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](1)
v2 := successValidator[string](2)
v3 := successValidator[string](3)
// (v1 <> v2) <> v3 == v1 <> (v2 <> v3)
left := m.Concat(m.Concat(v1, v2), v3)
right := m.Concat(v1, m.Concat(v2, v3))
resultLeft := left("test")(nil)
resultRight := right("test")(nil)
assert.Equal(t, resultRight, resultLeft)
// Both should equal 6
assert.Equal(t, validation.Of(6), resultLeft)
}
// TestApplicativeMonoid_AssociativityWithFailures tests associativity with failures
func TestApplicativeMonoid_AssociativityWithFailures(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](1)
v2 := failureValidator[string, int]("error 2")
v3 := successValidator[string](3)
// (v1 <> v2) <> v3 == v1 <> (v2 <> v3)
left := m.Concat(m.Concat(v1, v2), v3)
right := m.Concat(v1, m.Concat(v2, v3))
resultLeft := left("test")(nil)
resultRight := right("test")(nil)
// Both should fail with the same error
assert.True(t, E.IsLeft(resultLeft))
assert.True(t, E.IsLeft(resultRight))
_, errorsLeft := E.Unwrap(resultLeft)
_, errorsRight := E.Unwrap(resultRight)
assert.Len(t, errorsLeft, 1)
assert.Len(t, errorsRight, 1)
assert.Equal(t, "error 2", errorsLeft[0].Messsage)
assert.Equal(t, "error 2", errorsRight[0].Messsage)
}
// TestApplicativeMonoid_MultipleValidators tests combining multiple validators
func TestApplicativeMonoid_MultipleValidators(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](10)
v2 := successValidator[string](20)
v3 := successValidator[string](30)
v4 := successValidator[string](40)
// Chain multiple concat operations
combined := m.Concat(
m.Concat(
m.Concat(v1, v2),
v3,
),
v4,
)
result := combined("test")(nil)
assert.Equal(t, validation.Of(100), result)
}
// TestApplicativeMonoid_InputDependent tests validators that depend on input
func TestApplicativeMonoid_InputDependent(t *testing.T) {
m := ApplicativeMonoid[int](intAddMonoid)
// Validator that doubles the input
v1 := inputDependentValidator(N.Mul(2))
// Validator that adds 10 to the input
v2 := inputDependentValidator(N.Add(10))
combined := m.Concat(v1, v2)
result := combined(5)(nil)
// (5 * 2) + (5 + 10) = 10 + 15 = 25
assert.Equal(t, validation.Of(25), result)
}
// TestApplicativeMonoid_ContextPropagation tests that context is properly propagated
func TestApplicativeMonoid_ContextPropagation(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
// Create a validator that captures the context
var capturedContext validation.Context
v1 := func(input string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
capturedContext = ctx
return validation.Success(5)
}
}
v2 := successValidator[string](3)
combined := m.Concat(v1, v2)
// Create a context with some entries
ctx := validation.Context{
{Key: "field1", Type: "int"},
{Key: "field2", Type: "string"},
}
result := combined("test")(ctx)
assert.True(t, E.IsRight(result))
assert.Equal(t, ctx, capturedContext)
}
// TestApplicativeMonoid_ErrorAccumulation tests that errors are accumulated
func TestApplicativeMonoid_ErrorAccumulation(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := failureValidator[string, int]("error 1")
v2 := failureValidator[string, int]("error 2")
v3 := failureValidator[string, int]("error 3")
combined := m.Concat(m.Concat(v1, v2), v3)
result := combined("test")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
// Note: The current implementation returns the first error encountered
// At least one error should be present
assert.GreaterOrEqual(t, len(errors), 1)
hasError := false
for _, err := range errors {
if err.Messsage == "error 1" || err.Messsage == "error 2" || err.Messsage == "error 3" {
hasError = true
break
}
}
assert.True(t, hasError, "Should contain at least one validation error")
}
// TestApplicativeMonoid_MixedSuccessFailure tests mixing successes and failures
func TestApplicativeMonoid_MixedSuccessFailure(t *testing.T) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](10)
v2 := failureValidator[string, int]("error in v2")
v3 := successValidator[string](20)
v4 := failureValidator[string, int]("error in v4")
combined := m.Concat(
m.Concat(
m.Concat(v1, v2),
v3,
),
v4,
)
result := combined("test")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
// Note: The current implementation returns the first error encountered
// At least one error should be present
assert.GreaterOrEqual(t, len(errors), 1)
hasError := false
for _, err := range errors {
if err.Messsage == "error in v2" || err.Messsage == "error in v4" {
hasError = true
break
}
}
assert.True(t, hasError, "Should contain at least one validation error")
}
// TestApplicativeMonoid_DifferentInputTypes tests with different input types
func TestApplicativeMonoid_DifferentInputTypes(t *testing.T) {
t.Run("struct input", func(t *testing.T) {
type Config struct {
Port int
Timeout int
}
m := ApplicativeMonoid[Config](intAddMonoid)
v1 := func(cfg Config) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.Success(cfg.Port)
}
}
v2 := func(cfg Config) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.Success(cfg.Timeout)
}
}
combined := m.Concat(v1, v2)
result := combined(Config{Port: 8080, Timeout: 30})(nil)
assert.Equal(t, validation.Of(8110), result) // 8080 + 30
})
}
// TestApplicativeMonoid_StringConcatenation tests string concatenation scenarios
func TestApplicativeMonoid_StringConcatenation(t *testing.T) {
m := ApplicativeMonoid[string](strMonoid)
t.Run("build sentence", func(t *testing.T) {
v1 := successValidator[string]("The")
v2 := successValidator[string](" quick")
v3 := successValidator[string](" brown")
v4 := successValidator[string](" fox")
combined := m.Concat(
m.Concat(
m.Concat(v1, v2),
v3,
),
v4,
)
result := combined("input")(nil)
assert.Equal(t, validation.Of("The quick brown fox"), result)
})
t.Run("with empty strings", func(t *testing.T) {
v1 := successValidator[string]("Hello")
v2 := successValidator[string]("")
v3 := successValidator[string]("World")
combined := m.Concat(m.Concat(v1, v2), v3)
result := combined("input")(nil)
assert.Equal(t, validation.Of("HelloWorld"), result)
})
}
// Benchmark tests
func BenchmarkApplicativeMonoid_ConcatSuccesses(b *testing.B) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := successValidator[string](5)
v2 := successValidator[string](3)
combined := m.Concat(v1, v2)
b.ResetTimer()
for range b.N {
_ = combined("test")(nil)
}
}
func BenchmarkApplicativeMonoid_ConcatFailures(b *testing.B) {
m := ApplicativeMonoid[string](intAddMonoid)
v1 := failureValidator[string, int]("error 1")
v2 := failureValidator[string, int]("error 2")
combined := m.Concat(v1, v2)
b.ResetTimer()
for range b.N {
_ = combined("test")(nil)
}
}
func BenchmarkApplicativeMonoid_MultipleConcat(b *testing.B) {
m := ApplicativeMonoid[string](intAddMonoid)
validators := make([]Validate[string, int], 10)
for i := range validators {
validators[i] = successValidator[string](i)
}
// Chain all validators
combined := validators[0]
for i := 1; i < len(validators); i++ {
combined = m.Concat(combined, validators[i])
}
b.ResetTimer()
for range b.N {
_ = combined("test")(nil)
}
}

View File

@@ -0,0 +1,177 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"github.com/IBM/fp-go/v2/monoid"
"github.com/IBM/fp-go/v2/optics/codec/decode"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/reader"
)
type (
// Monoid represents an algebraic structure with an associative binary operation
// and an identity element. Used for combining values of type A.
//
// A Monoid[A] must satisfy:
// - Associativity: Concat(Concat(a, b), c) == Concat(a, Concat(b, c))
// - Identity: Concat(Empty(), a) == a == Concat(a, Empty())
//
// Common examples:
// - Numbers with addition (identity: 0)
// - Numbers with multiplication (identity: 1)
// - Strings with concatenation (identity: "")
// - Lists with concatenation (identity: [])
Monoid[A any] = monoid.Monoid[A]
// Reader represents a computation that depends on an environment R and produces a value A.
//
// Reader[R, A] is a function type: func(R) A
//
// The Reader pattern is used to:
// - Thread configuration or context through computations
// - Implement dependency injection in a functional way
// - Defer computation until the environment is available
// - Compose computations that share the same environment
//
// Example:
// type Config struct { Port int }
// getPort := func(cfg Config) int { return cfg.Port }
// // getPort is a Reader[Config, int]
Reader[R, A any] = reader.Reader[R, A]
// Validation represents the result of a validation operation that may contain
// validation errors or a successfully validated value of type A.
//
// Validation[A] is an Either[Errors, A], where:
// - Left(errors): Validation failed with one or more errors
// - Right(value): Validation succeeded with value of type A
//
// The Validation type supports:
// - Error accumulation: Multiple validation errors can be collected
// - Applicative composition: Parallel validations with error aggregation
// - Monadic composition: Sequential validations with short-circuiting
//
// Example:
// success := validation.Success(42) // Right(42)
// failure := validation.Failure[int](errors) // Left(errors)
Validation[A any] = validation.Validation[A]
// Context provides contextual information for validation operations,
// tracking the path through nested data structures.
//
// Context is a slice of ContextEntry values, where each entry represents
// a level in the nested structure being validated. This enables detailed
// error messages that show exactly where validation failed.
//
// Example context path for nested validation:
// Context{
// {Key: "user", Type: "User"},
// {Key: "address", Type: "Address"},
// {Key: "zipCode", Type: "string"},
// }
// // Represents: user.address.zipCode
//
// The context is used to generate error messages like:
// "at user.address.zipCode: expected string, got number"
Context = validation.Context
Decode[I, A any] = decode.Decode[I, A]
// Validate is a function that validates input I to produce type A with full context tracking.
//
// Type structure:
// Validate[I, A] = Reader[I, Decode[Context, A]]
//
// This means:
// 1. Takes an input of type I
// 2. Returns a Reader that depends on validation Context
// 3. That Reader produces a Validation[A] (Either[Errors, A])
//
// The layered structure enables:
// - Access to the input value being validated
// - Context tracking through nested structures
// - Error accumulation with detailed paths
// - Composition with other validators
//
// Example usage:
// validatePositive := func(n int) Reader[Context, Validation[int]] {
// return func(ctx Context) Validation[int] {
// if n > 0 {
// return validation.Success(n)
// }
// return validation.FailureWithMessage[int](n, "must be positive")(ctx)
// }
// }
// // validatePositive is a Validate[int, int]
//
// The Validate type forms:
// - A Functor: Can map over successful results
// - An Applicative: Can combine validators in parallel
// - A Monad: Can chain dependent validations
Validate[I, A any] = Reader[I, Decode[Context, A]]
// Errors is a collection of validation errors that occurred during validation.
//
// Each error in the collection contains:
// - The value that failed validation
// - The context path where the error occurred
// - A human-readable error message
// - An optional underlying cause error
//
// Errors can be accumulated from multiple validation failures, allowing
// all problems to be reported at once rather than failing fast.
Errors = validation.Errors
// Kleisli represents a Kleisli arrow for the Validate monad.
//
// A Kleisli arrow is a function from A to a monadic value Validate[I, B].
// It's used for composing computations that produce monadic results.
//
// Type: Kleisli[I, A, B] = func(A) Validate[I, B]
//
// Kleisli arrows can be composed using the Chain function, enabling
// sequential validation where later validators depend on earlier results.
//
// Example:
// parseString := func(s string) Validate[string, int] {
// // Parse string to int with validation
// }
// checkPositive := func(n int) Validate[string, int] {
// // Validate that int is positive
// }
// // Both are Kleisli arrows that can be composed
Kleisli[I, A, B any] = Reader[A, Validate[I, B]]
// Operator represents a transformation operator for validators.
//
// An Operator transforms a Validate[I, A] into a Validate[I, B].
// It's a specialized Kleisli arrow where the input is itself a validator.
//
// Type: Operator[I, A, B] = func(Validate[I, A]) Validate[I, B]
//
// Operators are used to:
// - Transform validation results (Map)
// - Chain dependent validations (Chain)
// - Apply function validators to value validators (Ap)
//
// Example:
// toUpper := Map[string, string, string](strings.ToUpper)
// // toUpper is an Operator[string, string, string]
// // It can be applied to any string validator to uppercase the result
Operator[I, A, B any] = Kleisli[I, Validate[I, A], B]
)

View File

@@ -0,0 +1,411 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package validate provides functional validation primitives for building composable validators.
//
// This package implements a validation framework based on functional programming principles,
// allowing you to build complex validators from simple, composable pieces. It uses the
// Reader monad pattern to thread validation context through nested structures.
//
// # Core Concepts
//
// The validate package is built around several key types:
//
// - Validate[I, A]: A validator that transforms input I to output A with validation context
// - Validation[A]: The result of validation, either errors or a valid value A
// - Context: Tracks the path through nested structures for detailed error messages
//
// # Type Structure
//
// A Validate[I, A] is defined as:
//
// Reader[I, Decode[A]]]
//
// This means:
// 1. It takes an input of type I
// 2. Returns a Reader that depends on validation Context
// 3. That Reader produces a Validation[A] (Either[Errors, A])
//
// This layered structure allows validators to:
// - Access the input value
// - Track validation context (path in nested structures)
// - Accumulate multiple validation errors
// - Compose with other validators
//
// # Validation Context
//
// The Context type tracks the path through nested data structures during validation.
// Each ContextEntry contains:
// - Key: The field name or map key
// - Type: The expected type name
// - Actual: The actual value being validated
//
// This provides detailed error messages like "at user.address.zipCode: expected string, got number".
//
// # Monoid Operations
//
// The package provides ApplicativeMonoid for combining validators using monoid operations.
// This allows you to:
// - Combine multiple validators that produce monoidal values
// - Accumulate results from parallel validations
// - Build complex validators from simpler ones
//
// # Example Usage
//
// Basic validation structure:
//
// import (
// "github.com/IBM/fp-go/v2/optics/codec/validate"
// "github.com/IBM/fp-go/v2/optics/codec/validation"
// )
//
// // A validator that checks if a string is non-empty
// func nonEmptyString(input string) validate.Reader[validation.Context, validation.Validation[string]] {
// if input == "" {
// return validation.FailureWithMessage[string](input, "string must not be empty")
// }
// return func(ctx validation.Context) validation.Validation[string] {
// return validation.Success(input)
// }
// }
//
// // Create a Validate function
// var validateNonEmpty validate.Validate[string, string] = func(input string) validate.Reader[validation.Context, validation.Validation[string]] {
// return nonEmptyString(input)
// }
//
// Combining validators with monoids:
//
// import (
// "github.com/IBM/fp-go/v2/monoid"
// "github.com/IBM/fp-go/v2/string"
// )
//
// // Combine string validators using string concatenation monoid
// stringMonoid := string.Monoid
// validatorMonoid := validate.ApplicativeMonoid[string, string](stringMonoid)
//
// // Now you can combine validators that produce strings
// combined := validatorMonoid.Concat(validator1, validator2)
//
// # Integration with Codec
//
// This package is designed to work with the optics/codec package for building
// type-safe encoders and decoders with validation. Validators can be composed
// into codecs that handle serialization, deserialization, and validation in a
// unified way.
//
// # Error Handling
//
// Validation errors are accumulated using the Either monad's applicative instance.
// This means:
// - Multiple validation errors can be collected in a single pass
// - Errors include full context path for debugging
// - Errors can be formatted for logging or user display
//
// See the validation package for error types and formatting options.
package validate
import (
"github.com/IBM/fp-go/v2/internal/readert"
"github.com/IBM/fp-go/v2/optics/codec/decode"
"github.com/IBM/fp-go/v2/reader"
)
// Of creates a Validate that always succeeds with the given value.
//
// This is the "pure" or "return" operation for the Validate monad. It lifts a plain
// value into the validation context without performing any actual validation.
//
// # Type Parameters
//
// - I: The input type (not used, but required for type consistency)
// - A: The type of the value to wrap
//
// # Parameters
//
// - a: The value to wrap in a successful validation
//
// # Returns
//
// A Validate[I, A] that ignores its input and always returns a successful validation
// containing the value a.
//
// # Example
//
// // Create a validator that always succeeds with value 42
// alwaysValid := validate.Of[string, int](42)
// result := alwaysValid("any input")(nil)
// // result is validation.Success(42)
//
// # Notes
//
// - This is useful for lifting pure values into the validation context
// - The input type I is ignored; the validator succeeds regardless of input
// - This satisfies the monad laws: Of is the left and right identity for Chain
func Of[I, A any](a A) Validate[I, A] {
return reader.Of[I](decode.Of[Context](a))
}
// MonadMap applies a function to the successful result of a validation.
//
// This is the functor map operation for Validate. It transforms the success value
// without affecting the validation logic or error handling. If the validation fails,
// the function is not applied and errors are preserved.
//
// # Type Parameters
//
// - I: The input type
// - A: The type of the current validation result
// - B: The type after applying the transformation
//
// # Parameters
//
// - fa: The validator to transform
// - f: The transformation function to apply to successful results
//
// # Returns
//
// A new Validate[I, B] that applies f to the result if validation succeeds.
//
// # Example
//
// // Transform a string validator to uppercase
// validateString := func(s string) validate.Reader[validation.Context, validation.Validation[string]] {
// return func(ctx validation.Context) validation.Validation[string] {
// return validation.Success(s)
// }
// }
//
// upperValidator := validate.MonadMap(validateString, strings.ToUpper)
// result := upperValidator("hello")(nil)
// // result is validation.Success("HELLO")
//
// # Notes
//
// - Preserves validation errors unchanged
// - Only applies the function to successful validations
// - Satisfies the functor laws: composition and identity
func MonadMap[I, A, B any](fa Validate[I, A], f func(A) B) Validate[I, B] {
return readert.MonadMap[
Validate[I, A],
Validate[I, B]](
decode.MonadMap,
fa,
f,
)
}
// Map creates an operator that transforms validation results.
//
// This is the curried version of MonadMap, returning a function that can be applied
// to validators. It's useful for creating reusable transformation pipelines.
//
// # Type Parameters
//
// - I: The input type
// - A: The type of the current validation result
// - B: The type after applying the transformation
//
// # Parameters
//
// - f: The transformation function to apply to successful results
//
// # Returns
//
// An Operator[I, A, B] that transforms Validate[I, A] to Validate[I, B].
//
// # Example
//
// // Create a reusable transformation
// toUpper := validate.Map[string, string, string](strings.ToUpper)
//
// // Apply it to different validators
// validator1 := toUpper(someStringValidator)
// validator2 := toUpper(anotherStringValidator)
//
// # Notes
//
// - This is the point-free style version of MonadMap
// - Useful for building transformation pipelines
// - Can be composed with other operators
func Map[I, A, B any](f func(A) B) Operator[I, A, B] {
return readert.Map[
Validate[I, A],
Validate[I, B]](
decode.Map,
f,
)
}
// Chain sequences two validators, where the second depends on the result of the first.
//
// This is the monadic bind operation for Validate. It allows you to create validators
// that depend on the results of previous validations, enabling complex validation logic
// that builds on earlier results.
//
// # Type Parameters
//
// - I: The input type
// - A: The type of the first validation result
// - B: The type of the second validation result
//
// # Parameters
//
// - f: A Kleisli arrow that takes a value of type A and returns a Validate[I, B]
//
// # Returns
//
// An Operator[I, A, B] that sequences the validations.
//
// # Example
//
// // First validate that a string is non-empty, then validate its length
// validateNonEmpty := func(s string) validate.Reader[validation.Context, validation.Validation[string]] {
// return func(ctx validation.Context) validation.Validation[string] {
// if s == "" {
// return validation.FailureWithMessage[string](s, "must not be empty")(ctx)
// }
// return validation.Success(s)
// }
// }
//
// validateLength := func(s string) validate.Validate[string, int] {
// return func(input string) validate.Reader[validation.Context, validation.Validation[int]] {
// return func(ctx validation.Context) validation.Validation[int] {
// if len(s) < 3 {
// return validation.FailureWithMessage[int](len(s), "too short")(ctx)
// }
// return validation.Success(len(s))
// }
// }
// }
//
// // Chain them together
// chained := validate.Chain(validateLength)(validateNonEmpty)
//
// # Notes
//
// - If the first validation fails, the second is not executed
// - Errors from the first validation are preserved
// - This enables dependent validation logic
// - Satisfies the monad laws: associativity and identity
func Chain[I, A, B any](f Kleisli[I, A, B]) Operator[I, A, B] {
return readert.Chain[Validate[I, A]](
decode.Chain,
f,
)
}
// MonadAp applies a validator containing a function to a validator containing a value.
//
// This is the applicative apply operation for Validate. It allows you to apply
// functions wrapped in validation context to values wrapped in validation context,
// accumulating errors from both if either fails.
//
// # Type Parameters
//
// - B: The result type after applying the function
// - I: The input type
// - A: The type of the value to which the function is applied
//
// # Parameters
//
// - fab: A validator that produces a function from A to B
// - fa: A validator that produces a value of type A
//
// # Returns
//
// A Validate[I, B] that applies the function to the value if both validations succeed.
//
// # Example
//
// // Create a validator that produces a function
// validateFunc := validate.Of[string, func(int) int](func(x int) int { return x * 2 })
//
// // Create a validator that produces a value
// validateValue := validate.Of[string, int](21)
//
// // Apply them
// result := validate.MonadAp(validateFunc, validateValue)
// // When run, produces validation.Success(42)
//
// # Notes
//
// - Both validators receive the same input
// - If either validation fails, all errors are accumulated
// - If both succeed, the function is applied to the value
// - This enables parallel validation with error accumulation
// - Satisfies the applicative functor laws
func MonadAp[B, I, A any](fab Validate[I, func(A) B], fa Validate[I, A]) Validate[I, B] {
return readert.MonadAp[
Validate[I, A],
Validate[I, B],
Validate[I, func(A) B], I, A](
decode.MonadAp[B, Context, A],
fab,
fa,
)
}
// Ap creates an operator that applies a function validator to a value validator.
//
// This is the curried version of MonadAp, returning a function that can be applied
// to function validators. It's useful for creating reusable applicative patterns.
//
// # Type Parameters
//
// - B: The result type after applying the function
// - I: The input type
// - A: The type of the value to which the function is applied
//
// # Parameters
//
// - fa: A validator that produces a value of type A
//
// # Returns
//
// An Operator[I, func(A) B, B] that applies function validators to the value validator.
//
// # Example
//
// // Create a value validator
// validateValue := validate.Of[string, int](21)
//
// // Create an applicative operator
// applyTo21 := validate.Ap[int, string, int](validateValue)
//
// // Create a function validator
// validateDouble := validate.Of[string, func(int) int](func(x int) int { return x * 2 })
//
// // Apply it
// result := applyTo21(validateDouble)
// // When run, produces validation.Success(42)
//
// # Notes
//
// - This is the point-free style version of MonadAp
// - Useful for building applicative pipelines
// - Enables parallel validation with error accumulation
// - Can be composed with other applicative operators
func Ap[B, I, A any](fa Validate[I, A]) Operator[I, func(A) B, B] {
return readert.Ap[
Validate[I, A],
Validate[I, B],
Validate[I, func(A) B], I, A](
decode.Ap[B, Context, A],
fa,
)
}

View File

@@ -0,0 +1,851 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package validate
import (
"testing"
E "github.com/IBM/fp-go/v2/either"
N "github.com/IBM/fp-go/v2/number"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/stretchr/testify/assert"
)
// TestValidateType tests the Validate type structure
func TestValidateType(t *testing.T) {
t.Run("basic validate function", func(t *testing.T) {
// Create a simple validator that checks if a number is positive
validatePositive := func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n > 0 {
return validation.Success(n)
}
return validation.FailureWithMessage[int](n, "must be positive")(ctx)
}
}
// Test with positive number
result := validatePositive(42)(nil)
assert.Equal(t, validation.Of(42), result)
// Test with negative number
result = validatePositive(-5)(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "must be positive", errors[0].Messsage)
})
t.Run("validate with context", func(t *testing.T) {
validateWithContext := func(s string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if s == "" {
return validation.FailureWithMessage[string](s, "empty string")(ctx)
}
return validation.Success(s)
}
}
ctx := validation.Context{
{Key: "username", Type: "string"},
}
result := validateWithContext("")(ctx)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, ctx, errors[0].Context)
})
}
// TestValidateComposition tests composing validators
func TestValidateComposition(t *testing.T) {
t.Run("sequential validation", func(t *testing.T) {
// First validator: check if string is not empty
validateNotEmpty := func(s string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if s == "" {
return validation.FailureWithMessage[string](s, "must not be empty")(ctx)
}
return validation.Success(s)
}
}
// Second validator: check if string has minimum length
validateMinLength := func(minLen int) func(string) Reader[validation.Context, validation.Validation[string]] {
return func(s string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if len(s) < minLen {
return validation.FailureWithMessage[string](s, "too short")(ctx)
}
return validation.Success(s)
}
}
}
// Test with valid input
input := "hello"
result1 := validateNotEmpty(input)(nil)
assert.Equal(t, validation.Of("hello"), result1)
result2 := validateMinLength(3)(input)(nil)
assert.Equal(t, validation.Of("hello"), result2)
// Test with invalid input
shortInput := "hi"
result3 := validateMinLength(5)(shortInput)(nil)
assert.True(t, E.IsLeft(result3))
})
}
// TestValidateWithDifferentTypes tests validators with various input/output types
func TestValidateWithDifferentTypes(t *testing.T) {
t.Run("string to int conversion", func(t *testing.T) {
// Validator that parses string to int
validateParseInt := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
// Simple parsing logic for testing
if s == "42" {
return validation.Success(42)
}
return validation.FailureWithMessage[int](s, "invalid integer")(ctx)
}
}
result := validateParseInt("42")(nil)
assert.Equal(t, validation.Of(42), result)
result = validateParseInt("abc")(nil)
assert.True(t, E.IsLeft(result))
})
t.Run("struct validation", func(t *testing.T) {
type User struct {
Name string
Age int
Email string
}
validateUser := func(u User) Reader[validation.Context, validation.Validation[User]] {
return func(ctx validation.Context) validation.Validation[User] {
if u.Name == "" {
return validation.FailureWithMessage[User](u, "name is required")(ctx)
}
if u.Age < 0 {
return validation.FailureWithMessage[User](u, "age must be non-negative")(ctx)
}
if u.Email == "" {
return validation.FailureWithMessage[User](u, "email is required")(ctx)
}
return validation.Success(u)
}
}
validUser := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
result := validateUser(validUser)(nil)
assert.Equal(t, validation.Of(validUser), result)
invalidUser := User{Name: "", Age: 30, Email: "alice@example.com"}
result = validateUser(invalidUser)(nil)
assert.True(t, E.IsLeft(result))
})
}
// TestValidateContextTracking tests context tracking through nested structures
func TestValidateContextTracking(t *testing.T) {
t.Run("nested context", func(t *testing.T) {
validateField := func(value string, fieldName string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
// Add field to context
newCtx := append(ctx, validation.ContextEntry{
Key: fieldName,
Type: "string",
})
if value == "" {
return validation.FailureWithMessage[string](value, "field is empty")(newCtx)
}
return validation.Success(value)
}
}
baseCtx := validation.Context{
{Key: "user", Type: "User"},
}
result := validateField("", "email")(baseCtx)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
// Check that context includes both user and email
assert.Len(t, errors[0].Context, 2)
assert.Equal(t, "user", errors[0].Context[0].Key)
assert.Equal(t, "email", errors[0].Context[1].Key)
})
}
// TestValidateErrorMessages tests error message generation
func TestValidateErrorMessages(t *testing.T) {
t.Run("custom error messages", func(t *testing.T) {
validateRange := func(min, max int) func(int) Reader[validation.Context, validation.Validation[int]] {
return func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n < min {
return validation.FailureWithMessage[int](n, "value too small")(ctx)
}
if n > max {
return validation.FailureWithMessage[int](n, "value too large")(ctx)
}
return validation.Success(n)
}
}
}
result := validateRange(0, 100)(150)(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Equal(t, "value too large", errors[0].Messsage)
result = validateRange(0, 100)(-10)(nil)
assert.True(t, E.IsLeft(result))
_, errors = E.Unwrap(result)
assert.Equal(t, "value too small", errors[0].Messsage)
})
}
// TestValidateTransformations tests validators that transform values
func TestValidateTransformations(t *testing.T) {
t.Run("normalize and validate", func(t *testing.T) {
// Validator that normalizes (trims) and validates
validateAndNormalize := func(s string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
// Simple trim simulation - trim all leading and trailing spaces
normalized := s
// Trim leading spaces
for len(normalized) > 0 && normalized[0] == ' ' {
normalized = normalized[1:]
}
// Trim trailing spaces
for len(normalized) > 0 && normalized[len(normalized)-1] == ' ' {
normalized = normalized[:len(normalized)-1]
}
if normalized == "" {
return validation.FailureWithMessage[string](s, "empty after normalization")(ctx)
}
return validation.Success(normalized)
}
}
result := validateAndNormalize(" hello ")(nil)
assert.Equal(t, validation.Of("hello"), result)
result = validateAndNormalize(" ")(nil)
assert.True(t, E.IsLeft(result))
})
}
// TestValidateChaining tests chaining multiple validators
func TestValidateChaining(t *testing.T) {
t.Run("chain validators manually", func(t *testing.T) {
// First validator
v1 := func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n < 0 {
return validation.FailureWithMessage[int](n, "must be non-negative")(ctx)
}
return validation.Success(n)
}
}
// Second validator (depends on first)
v2 := func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n > 100 {
return validation.FailureWithMessage[int](n, "must be <= 100")(ctx)
}
return validation.Success(n)
}
}
// Test valid value
input := 50
result1 := v1(input)(nil)
assert.Equal(t, validation.Of(50), result1)
result2 := v2(input)(nil)
assert.Equal(t, validation.Of(50), result2)
// Test invalid value (too large)
input = 150
result1 = v1(input)(nil)
assert.Equal(t, validation.Of(150), result1)
result2 = v2(input)(nil)
assert.True(t, E.IsLeft(result2))
})
}
// TestValidateComplexScenarios tests real-world validation scenarios
func TestValidateComplexScenarios(t *testing.T) {
t.Run("email validation", func(t *testing.T) {
validateEmail := func(email string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
// Simple email validation for testing
hasAt := false
hasDot := false
for _, c := range email {
if c == '@' {
hasAt = true
}
if c == '.' {
hasDot = true
}
}
if !hasAt || !hasDot {
return validation.FailureWithMessage[string](email, "invalid email format")(ctx)
}
return validation.Success(email)
}
}
result := validateEmail("user@example.com")(nil)
assert.Equal(t, validation.Of("user@example.com"), result)
result = validateEmail("invalid-email")(nil)
assert.True(t, E.IsLeft(result))
result = validateEmail("no-domain@")(nil)
assert.True(t, E.IsLeft(result))
})
t.Run("password strength validation", func(t *testing.T) {
validatePassword := func(pwd string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if len(pwd) < 8 {
return validation.FailureWithMessage[string](pwd, "password too short")(ctx)
}
hasUpper := false
hasLower := false
hasDigit := false
for _, c := range pwd {
if c >= 'A' && c <= 'Z' {
hasUpper = true
}
if c >= 'a' && c <= 'z' {
hasLower = true
}
if c >= '0' && c <= '9' {
hasDigit = true
}
}
if !hasUpper || !hasLower || !hasDigit {
return validation.FailureWithMessage[string](pwd, "password must contain upper, lower, and digit")(ctx)
}
return validation.Success(pwd)
}
}
result := validatePassword("StrongPass123")(nil)
assert.Equal(t, validation.Of("StrongPass123"), result)
result = validatePassword("weak")(nil)
assert.True(t, E.IsLeft(result))
result = validatePassword("nouppercase123")(nil)
assert.True(t, E.IsLeft(result))
})
}
// Benchmark tests
func BenchmarkValidate_Success(b *testing.B) {
validate := func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n > 0 {
return validation.Success(n)
}
return validation.FailureWithMessage[int](n, "must be positive")(ctx)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = validate(42)(nil)
}
}
func BenchmarkValidate_Failure(b *testing.B) {
validate := func(n int) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if n > 0 {
return validation.Success(n)
}
return validation.FailureWithMessage[int](n, "must be positive")(ctx)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = validate(-1)(nil)
}
}
func BenchmarkValidate_WithContext(b *testing.B) {
validate := func(s string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if s == "" {
return validation.FailureWithMessage[string](s, "empty string")(ctx)
}
return validation.Success(s)
}
}
ctx := validation.Context{
{Key: "field1", Type: "string"},
{Key: "field2", Type: "string"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = validate("test")(ctx)
}
}
// TestOf tests the Of function
func TestOf(t *testing.T) {
t.Run("creates successful validation with value", func(t *testing.T) {
validator := Of[string](42)
result := validator("any input")(nil)
assert.Equal(t, validation.Of(42), result)
})
t.Run("ignores input value", func(t *testing.T) {
validator := Of[string]("success")
result1 := validator("input1")(nil)
result2 := validator("input2")(nil)
result3 := validator("")(nil)
assert.Equal(t, validation.Of("success"), result1)
assert.Equal(t, validation.Of("success"), result2)
assert.Equal(t, validation.Of("success"), result3)
})
t.Run("works with different types", func(t *testing.T) {
type User struct {
Name string
Age int
}
user := User{Name: "Alice", Age: 30}
validator := Of[int](user)
result := validator(123)(nil)
assert.Equal(t, validation.Of(user), result)
})
}
// TestMonadMap tests the MonadMap function
func TestMonadMap(t *testing.T) {
t.Run("transforms successful validation", func(t *testing.T) {
validator := Of[string](21)
doubled := MonadMap(validator, N.Mul(2))
result := doubled("input")(nil)
assert.Equal(t, validation.Of(42), result)
})
t.Run("preserves validation errors", func(t *testing.T) {
failingValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "validation failed")(ctx)
}
}
mapped := MonadMap(failingValidator, N.Mul(2))
result := mapped("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "validation failed", errors[0].Messsage)
})
t.Run("chains multiple transformations", func(t *testing.T) {
validator := Of[string](10)
transformed := MonadMap(
MonadMap(
MonadMap(validator, N.Add(5)),
N.Mul(2),
),
N.Sub(10),
)
result := transformed("input")(nil)
assert.Equal(t, validation.Of(20), result) // (10 + 5) * 2 - 10 = 20
})
t.Run("transforms between different types", func(t *testing.T) {
validator := Of[string](42)
toString := MonadMap(validator, func(x int) string {
return "value: " + string(rune(x+'0'))
})
result := toString("input")(nil)
assert.True(t, E.IsRight(result))
if E.IsRight(result) {
value, _ := E.Unwrap(result)
assert.Contains(t, value, "value:")
}
})
}
// TestMap tests the Map function
func TestMap(t *testing.T) {
t.Run("creates reusable transformation", func(t *testing.T) {
double := Map[string](N.Mul(2))
validator1 := Of[string](21)
validator2 := Of[string](10)
result1 := double(validator1)("input")(nil)
result2 := double(validator2)("input")(nil)
assert.Equal(t, validation.Of(42), result1)
assert.Equal(t, validation.Of(20), result2)
})
t.Run("preserves errors in transformation", func(t *testing.T) {
increment := Map[string](func(x int) int { return x + 1 })
failingValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "error")(ctx)
}
}
result := increment(failingValidator)("input")(nil)
assert.True(t, E.IsLeft(result))
})
t.Run("composes with other operators", func(t *testing.T) {
addFive := Map[string](N.Add(5))
double := Map[string](N.Mul(2))
validator := Of[string](10)
composed := double(addFive(validator))
result := composed("input")(nil)
assert.Equal(t, validation.Of(30), result) // (10 + 5) * 2 = 30
})
}
// TestChain tests the Chain function
func TestChain(t *testing.T) {
t.Run("sequences dependent validations", func(t *testing.T) {
// First validator: parse string to int
parseValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
if s == "42" {
return validation.Success(42)
}
return validation.FailureWithMessage[int](s, "invalid number")(ctx)
}
}
// Second validator: check if number is positive
checkPositive := func(n int) Validate[string, string] {
return func(input string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
if n > 0 {
return validation.Success("positive")
}
return validation.FailureWithMessage[string](n, "not positive")(ctx)
}
}
}
chained := Chain(checkPositive)(parseValidator)
result := chained("42")(nil)
assert.Equal(t, validation.Of("positive"), result)
})
t.Run("stops on first validation failure", func(t *testing.T) {
failingValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "first failed")(ctx)
}
}
neverCalled := func(n int) Validate[string, string] {
return func(input string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
// This should never be reached
t.Error("Second validator should not be called")
return validation.Success("should not reach")
}
}
}
chained := Chain(neverCalled)(failingValidator)
result := chained("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Equal(t, "first failed", errors[0].Messsage)
})
t.Run("propagates second validation failure", func(t *testing.T) {
successValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.Success(42)
}
}
failingSecond := func(n int) Validate[string, string] {
return func(input string) Reader[validation.Context, validation.Validation[string]] {
return func(ctx validation.Context) validation.Validation[string] {
return validation.FailureWithMessage[string](n, "second failed")(ctx)
}
}
}
chained := Chain(failingSecond)(successValidator)
result := chained("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Equal(t, "second failed", errors[0].Messsage)
})
}
// TestMonadAp tests the MonadAp function
func TestMonadAp(t *testing.T) {
t.Run("applies function to value when both succeed", func(t *testing.T) {
funcValidator := Of[string](N.Mul(2))
valueValidator := Of[string](21)
result := MonadAp(funcValidator, valueValidator)("input")(nil)
assert.Equal(t, validation.Of(42), result)
})
t.Run("accumulates errors when function validator fails", func(t *testing.T) {
failingFunc := func(s string) Reader[validation.Context, validation.Validation[func(int) int]] {
return func(ctx validation.Context) validation.Validation[func(int) int] {
return validation.FailureWithMessage[func(int) int](s, "func failed")(ctx)
}
}
valueValidator := Of[string](21)
result := MonadAp(failingFunc, valueValidator)("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "func failed", errors[0].Messsage)
})
t.Run("accumulates errors when value validator fails", func(t *testing.T) {
funcValidator := Of[string](N.Mul(2))
failingValue := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "value failed")(ctx)
}
}
result := MonadAp(funcValidator, failingValue)("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Len(t, errors, 1)
assert.Equal(t, "value failed", errors[0].Messsage)
})
t.Run("returns error when both validators fail", func(t *testing.T) {
failingFunc := func(s string) Reader[validation.Context, validation.Validation[func(int) int]] {
return func(ctx validation.Context) validation.Validation[func(int) int] {
return validation.FailureWithMessage[func(int) int](s, "func failed")(ctx)
}
}
failingValue := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "value failed")(ctx)
}
}
result := MonadAp(failingFunc, failingValue)("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
// Note: The current implementation returns the first error encountered
assert.GreaterOrEqual(t, len(errors), 1)
// At least one of the errors should be present
hasError := false
for _, err := range errors {
if err.Messsage == "func failed" || err.Messsage == "value failed" {
hasError = true
break
}
}
assert.True(t, hasError, "Should contain at least one validation error")
})
}
// TestAp tests the Ap function
func TestAp(t *testing.T) {
t.Run("creates reusable applicative operator", func(t *testing.T) {
valueValidator := Of[string](21)
applyTo21 := Ap[int](valueValidator)
double := Of[string](N.Mul(2))
triple := Of[string](func(x int) int { return x * 3 })
result1 := applyTo21(double)("input")(nil)
result2 := applyTo21(triple)("input")(nil)
assert.Equal(t, validation.Of(42), result1)
assert.Equal(t, validation.Of(63), result2)
})
t.Run("preserves errors from value validator", func(t *testing.T) {
failingValue := func(s string) Reader[validation.Context, validation.Validation[int]] {
return func(ctx validation.Context) validation.Validation[int] {
return validation.FailureWithMessage[int](s, "value error")(ctx)
}
}
applyToFailing := Ap[int](failingValue)
funcValidator := Of[string](N.Mul(2))
result := applyToFailing(funcValidator)("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Equal(t, "value error", errors[0].Messsage)
})
t.Run("preserves errors from function validator", func(t *testing.T) {
valueValidator := Of[string](21)
applyTo21 := Ap[int](valueValidator)
failingFunc := func(s string) Reader[validation.Context, validation.Validation[func(int) int]] {
return func(ctx validation.Context) validation.Validation[func(int) int] {
return validation.FailureWithMessage[func(int) int](s, "func error")(ctx)
}
}
result := applyTo21(failingFunc)("input")(nil)
assert.True(t, E.IsLeft(result))
_, errors := E.Unwrap(result)
assert.Equal(t, "func error", errors[0].Messsage)
})
}
// TestMonadLaws tests that the monad laws hold for Validate
func TestMonadLaws(t *testing.T) {
t.Run("left identity: Of(a) >>= f === f(a)", func(t *testing.T) {
a := 42
f := func(x int) Validate[string, string] {
return Of[string]("value: " + string(rune(x+'0')))
}
// Of(a) >>= f
left := Chain(f)(Of[string](a))
// f(a)
right := f(a)
leftResult := left("input")(nil)
rightResult := right("input")(nil)
assert.Equal(t, E.IsRight(leftResult), E.IsRight(rightResult))
if E.IsRight(leftResult) {
leftVal, _ := E.Unwrap(leftResult)
rightVal, _ := E.Unwrap(rightResult)
assert.Equal(t, leftVal, rightVal)
}
})
t.Run("right identity: m >>= Of === m", func(t *testing.T) {
m := Of[string](42)
// m >>= Of
chained := Chain(func(x int) Validate[string, int] {
return Of[string](x)
})(m)
mResult := m("input")(nil)
chainedResult := chained("input")(nil)
assert.Equal(t, E.IsRight(mResult), E.IsRight(chainedResult))
if E.IsRight(mResult) {
mVal, _ := E.Unwrap(mResult)
chainedVal, _ := E.Unwrap(chainedResult)
assert.Equal(t, mVal, chainedVal)
}
})
}
// TestFunctorLaws tests that the functor laws hold for Validate
func TestFunctorLaws(t *testing.T) {
t.Run("identity: map(id) === id", func(t *testing.T) {
validator := Of[string](42)
identity := func(x int) int { return x }
mapped := MonadMap(validator, identity)
origResult := validator("input")(nil)
mappedResult := mapped("input")(nil)
assert.Equal(t, E.IsRight(origResult), E.IsRight(mappedResult))
if E.IsRight(origResult) {
origVal, _ := E.Unwrap(origResult)
mappedVal, _ := E.Unwrap(mappedResult)
assert.Equal(t, origVal, mappedVal)
}
})
t.Run("composition: map(f . g) === map(f) . map(g)", func(t *testing.T) {
validator := Of[string](10)
f := N.Mul(2)
g := N.Add(5)
// map(f . g)
composed := MonadMap(validator, func(x int) int { return f(g(x)) })
// map(f) . map(g)
separate := MonadMap(MonadMap(validator, g), f)
composedResult := composed("input")(nil)
separateResult := separate("input")(nil)
assert.Equal(t, E.IsRight(composedResult), E.IsRight(separateResult))
if E.IsRight(composedResult) {
composedVal, _ := E.Unwrap(composedResult)
separateVal, _ := E.Unwrap(separateResult)
assert.Equal(t, composedVal, separateVal)
}
})
}

View File

@@ -3,19 +3,18 @@ package codec
import (
"fmt"
"github.com/IBM/fp-go/v2/errors"
"github.com/IBM/fp-go/v2/internal/formatting"
"github.com/IBM/fp-go/v2/result"
)
func onTypeError(expType string) func(any) error {
return func(u any) error {
return fmt.Errorf("expecting type [%s] but got [%T]", expType, u)
}
return errors.OnSome[any](fmt.Sprintf("expecting type [%s] but got [%%T]", expType))
}
// Is checks if a value can be converted to type T.
// Returns Some(value) if the conversion succeeds, None otherwise.
// This is a type-safe cast operation.
func Is[T any]() func(any) Result[T] {
var zero T
return result.ToType[T](onTypeError(fmt.Sprintf("%T", zero)))
func Is[T any]() ReaderResult[any, T] {
return result.ToType[T](onTypeError(formatting.TypeInfo(*new(T))))
}

View File

@@ -31,6 +31,10 @@ func Ap[B, A any](fa Validation[A]) Operator[func(A) B, B] {
return either.ApV[B, A](ErrorsMonoid())(fa)
}
func MonadAp[B, A any](fab Validation[func(A) B], fa Validation[A]) Validation[B] {
return either.MonadApV[B, A](ErrorsMonoid())(fab, fa)
}
// Map transforms the value inside a successful validation using the provided function.
// If the validation is a failure, the errors are preserved unchanged.
// This is the functor map operation for Validation.
@@ -43,6 +47,18 @@ func Map[A, B any](f func(A) B) Operator[A, B] {
return either.Map[Errors](f)
}
func MonadMap[A, B any](fa Validation[A], f func(A) B) Validation[B] {
return either.MonadMap(fa, f)
}
func Chain[A, B any](f Kleisli[A, B]) Operator[A, B] {
return either.Chain(f)
}
func MonadChain[A, B any](fa Validation[A], f Kleisli[A, B]) Validation[B] {
return either.MonadChain(fa, f)
}
// Applicative creates an Applicative instance for Validation with error accumulation.
//
// This returns a lawful Applicative that accumulates validation errors using the Errors monoid.

View File

@@ -109,7 +109,7 @@ func TestAp(t *testing.T) {
funcValidation := Of(double)
valueValidation := Of(21)
result := Ap[int, int](valueValidation)(funcValidation)
result := Ap[int](valueValidation)(funcValidation)
assert.True(t, either.IsRight(result))
value := either.MonadFold(result,
@@ -126,7 +126,7 @@ func TestAp(t *testing.T) {
&ValidationError{Messsage: "value error"},
})
result := Ap[int, int](valueValidation)(funcValidation)
result := Ap[int](valueValidation)(funcValidation)
assert.True(t, either.IsLeft(result))
errors := either.MonadFold(result,
@@ -143,7 +143,7 @@ func TestAp(t *testing.T) {
})
valueValidation := Of(21)
result := Ap[int, int](valueValidation)(funcValidation)
result := Ap[int](valueValidation)(funcValidation)
assert.True(t, either.IsLeft(result))
errors := either.MonadFold(result,
@@ -162,7 +162,7 @@ func TestAp(t *testing.T) {
&ValidationError{Messsage: "value error"},
})
result := Ap[int, int](valueValidation)(funcValidation)
result := Ap[int](valueValidation)(funcValidation)
assert.True(t, either.IsLeft(result))
errors := either.MonadFold(result,
@@ -180,7 +180,7 @@ func TestAp(t *testing.T) {
funcValidation := Of(toUpper)
valueValidation := Of("hello")
result := Ap[string, string](valueValidation)(funcValidation)
result := Ap[string](valueValidation)(funcValidation)
assert.True(t, either.IsRight(result))
value := either.MonadFold(result,
@@ -199,7 +199,7 @@ func TestAp(t *testing.T) {
&ValidationError{Messsage: "value error 1"},
})
result := Ap[int, int](valueValidation)(funcValidation)
result := Ap[int](valueValidation)(funcValidation)
assert.True(t, either.IsLeft(result))
errors := either.MonadFold(result,
@@ -242,7 +242,7 @@ func TestMonadLaws(t *testing.T) {
t.Run("applicative identity law", func(t *testing.T) {
// Ap(v)(Of(id)) == v
v := Of(42)
result := Ap[int, int](v)(Of(F.Identity[int]))
result := Ap[int](v)(Of(F.Identity[int]))
assert.Equal(t, v, result)
})
@@ -252,7 +252,7 @@ func TestMonadLaws(t *testing.T) {
f := func(x int) int { return x * 2 }
x := 21
left := Ap[int, int](Of(x))(Of(f))
left := Ap[int](Of(x))(Of(f))
right := Of(f(x))
assert.Equal(t, left, right)
@@ -285,7 +285,7 @@ func TestMapWithOperator(t *testing.T) {
func TestApWithOperator(t *testing.T) {
t.Run("Ap returns an Operator", func(t *testing.T) {
valueValidation := Of(21)
operator := Ap[int, int](valueValidation)
operator := Ap[int](valueValidation)
// Operator can be applied to different function validations
double := func(x int) int { return x * 2 }

View File

@@ -4,9 +4,12 @@ import (
"github.com/IBM/fp-go/v2/either"
"github.com/IBM/fp-go/v2/monoid"
"github.com/IBM/fp-go/v2/reader"
"github.com/IBM/fp-go/v2/result"
)
type (
// Result represents a computation that may succeed with a value of type A or fail with an error.
Result[A any] = result.Result[A]
// Either represents a value that can be one of two types: Left (error) or Right (success).
Either[E, A any] = either.Either[E, A]
@@ -34,6 +37,13 @@ type (
// Errors is a collection of validation errors.
Errors = []*ValidationError
// validationErrors wraps a collection of validation errors with an optional root cause.
// It provides structured error information for validation failures.
validationErrors struct {
errors Errors
cause error
}
// Validation represents the result of a validation operation.
// Left contains validation errors, Right contains the successfully validated value.
Validation[A any] = Either[Errors, A]
@@ -41,9 +51,14 @@ type (
// Reader represents a computation that depends on an environment R and produces a value A.
Reader[R, A any] = reader.Reader[R, A]
// Kleisli represents a function from A to a validated B.
// It's a Reader that takes an input A and produces a Validation[B].
Kleisli[A, B any] = Reader[A, Validation[B]]
// Operator represents a validation transformation that takes a validated A and produces a validated B.
// It's a specialized Kleisli arrow for composing validation operations.
Operator[A, B any] = Kleisli[Validation[A], B]
// Monoid represents an algebraic structure with an associative binary operation and an identity element.
Monoid[A any] = monoid.Monoid[A]
)

View File

@@ -2,6 +2,7 @@ package validation
import (
"fmt"
"log/slog"
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/either"
@@ -73,6 +74,153 @@ func (v *ValidationError) Format(s fmt.State, verb rune) {
fmt.Fprint(s, result)
}
// LogValue implements the slog.LogValuer interface for ValidationError.
// It provides structured logging representation of the validation error.
// Returns a slog.Value containing the error details as a group with
// message, value, context path, and optional cause.
//
// This method is called automatically when logging a ValidationError with slog.
//
// Example:
//
// err := &ValidationError{Value: "abc", Messsage: "expected number"}
// slog.Error("validation failed", "error", err)
// // Logs: error={message="expected number" value="abc"}
func (v *ValidationError) LogValue() slog.Value {
attrs := []slog.Attr{
slog.String("message", v.Messsage),
slog.Any("value", v.Value),
}
// Add context path if available
if len(v.Context) > 0 {
path := ""
for i, entry := range v.Context {
if i > 0 {
path += "."
}
if entry.Key != "" {
path += entry.Key
} else {
path += entry.Type
}
}
attrs = append(attrs, slog.String("path", path))
}
// Add cause if present
if v.Cause != nil {
attrs = append(attrs, slog.Any("cause", v.Cause))
}
return slog.GroupValue(attrs...)
}
// Error implements the error interface for ValidationErrors.
// Returns a generic error message indicating validation errors occurred.
func (ve *validationErrors) Error() string {
if len(ve.errors) == 0 {
return "ValidationErrors: no errors"
}
if len(ve.errors) == 1 {
return "ValidationErrors: 1 error"
}
return fmt.Sprintf("ValidationErrors: %d errors", len(ve.errors))
}
// Unwrap returns the underlying cause error if present.
// This allows ValidationErrors to work with errors.Is and errors.As.
func (ve *validationErrors) Unwrap() error {
return ve.cause
}
// String returns a simple string representation of all validation errors.
// Each error is listed on a separate line with its index.
func (ve *validationErrors) String() string {
if len(ve.errors) == 0 {
return "ValidationErrors: no errors"
}
result := fmt.Sprintf("ValidationErrors (%d):\n", len(ve.errors))
for i, err := range ve.errors {
result += fmt.Sprintf(" [%d] %s\n", i, err.String())
}
if ve.cause != nil {
result += fmt.Sprintf(" caused by: %v\n", ve.cause)
}
return result
}
// Format implements fmt.Formatter for custom formatting of ValidationErrors.
// Supports verbs: %s, %v, %+v (with additional details)
// %s and %v: compact format with error count
// %+v: verbose format with all error details
func (ve *validationErrors) Format(s fmt.State, verb rune) {
if len(ve.errors) == 0 {
fmt.Fprint(s, "ValidationErrors: no errors")
return
}
// For simple format, just show the count
if verb == 's' || (verb == 'v' && !s.Flag('+')) {
if len(ve.errors) == 1 {
fmt.Fprint(s, "ValidationErrors: 1 error")
} else {
fmt.Fprintf(s, "ValidationErrors: %d errors", len(ve.errors))
}
return
}
// Verbose format with all details
if s.Flag('+') && verb == 'v' {
fmt.Fprintf(s, "ValidationErrors (%d):\n", len(ve.errors))
for i, err := range ve.errors {
fmt.Fprintf(s, " [%d] ", i)
err.Format(s, verb)
fmt.Fprint(s, "\n")
}
if ve.cause != nil {
fmt.Fprintf(s, " root cause: %+v\n", ve.cause)
}
}
}
// LogValue implements the slog.LogValuer interface for ValidationErrors.
// It provides structured logging representation of multiple validation errors.
// Returns a slog.Value containing the error count and individual errors as a group.
//
// This method is called automatically when logging ValidationErrors with slog.
//
// Example:
//
// errors := &ValidationErrors{Errors: []*ValidationError{{Messsage: "error1"}, {Messsage: "error2"}}}
// slog.Error("validation failed", "errors", errors)
// // Logs: errors={count=2 errors=[...]}
func (ve *validationErrors) LogValue() slog.Value {
attrs := []slog.Attr{
slog.Int("count", len(ve.errors)),
}
// Add individual errors as a group
if len(ve.errors) > 0 {
errorAttrs := make([]slog.Attr, len(ve.errors))
for i, err := range ve.errors {
errorAttrs[i] = slog.Any(fmt.Sprintf("error_%d", i), err)
}
attrs = append(attrs, slog.Any("errors", slog.GroupValue(errorAttrs...)))
}
// Add cause if present
if ve.cause != nil {
attrs = append(attrs, slog.Any("cause", ve.cause))
}
return slog.GroupValue(attrs...)
}
// Failures creates a validation failure from a collection of errors.
// Returns a Left Either containing the errors.
func Failures[T any](err Errors) Validation[T] {
@@ -123,3 +271,50 @@ func FailureWithError[T any](value any, message string) Reader[error, Reader[Con
func Success[T any](value T) Validation[T] {
return either.Of[Errors](value)
}
// MakeValidationErrors converts a collection of validation errors into a single error.
// It wraps the Errors slice in a ValidationErrors struct that implements the error interface.
// This is useful for converting validation failures into standard Go errors.
//
// Parameters:
// - errors: A slice of ValidationError pointers representing validation failures
//
// Returns:
// - An error that contains all the validation errors and can be used with standard error handling
//
// Example:
//
// errors := Errors{
// &ValidationError{Value: "abc", Messsage: "expected number"},
// &ValidationError{Value: nil, Messsage: "required field"},
// }
// err := MakeValidationErrors(errors)
// fmt.Println(err) // Output: ValidationErrors: 2 errors
func MakeValidationErrors(errors Errors) error {
return &validationErrors{errors: errors}
}
// ToResult converts a Validation[T] to a Result[T].
// It transforms the Left side (validation errors) into a standard error using MakeValidationErrors,
// while preserving the Right side (successful value) unchanged.
// This is useful for integrating validation results with code that expects Result types.
//
// Type Parameters:
// - T: The type of the successfully validated value
//
// Parameters:
// - val: A Validation[T] which is Either[Errors, T]
//
// Returns:
// - A Result[T] which is Either[error, T], with validation errors converted to a single error
//
// Example:
//
// validation := Success[int](42)
// result := ToResult(validation) // Result containing 42
//
// validation := Failures[int](Errors{&ValidationError{Messsage: "invalid"}})
// result := ToResult(validation) // Result containing ValidationErrors error
func ToResult[T any](val Validation[T]) Result[T] {
return either.MonadMapLeft(val, MakeValidationErrors)
}

View File

@@ -3,6 +3,7 @@ package validation
import (
"errors"
"fmt"
"log/slog"
"testing"
"github.com/IBM/fp-go/v2/either"
@@ -29,8 +30,8 @@ func TestValidationError_String(t *testing.T) {
expected := "ValidationError: invalid value"
assert.Equal(t, expected, err.String())
}
func TestValidationError_Unwrap(t *testing.T) {
t.Run("with cause", func(t *testing.T) {
cause := errors.New("underlying error")
err := &ValidationError{
@@ -417,3 +418,446 @@ func TestValidationError_FormatEdgeCases(t *testing.T) {
assert.Contains(t, result, "value: <nil>")
})
}
func TestMakeValidationErrors(t *testing.T) {
t.Run("creates error from single validation error", func(t *testing.T) {
errs := Errors{
&ValidationError{Value: "test", Messsage: "invalid value"},
}
err := MakeValidationErrors(errs)
require.NotNil(t, err)
assert.Equal(t, "ValidationErrors: 1 error", err.Error())
// Verify it's a ValidationErrors type
ve, ok := err.(*validationErrors)
require.True(t, ok)
assert.Len(t, ve.errors, 1)
assert.Equal(t, "invalid value", ve.errors[0].Messsage)
})
t.Run("creates error from multiple validation errors", func(t *testing.T) {
errs := Errors{
&ValidationError{Value: "test1", Messsage: "error 1"},
&ValidationError{Value: "test2", Messsage: "error 2"},
&ValidationError{Value: "test3", Messsage: "error 3"},
}
err := MakeValidationErrors(errs)
require.NotNil(t, err)
assert.Equal(t, "ValidationErrors: 3 errors", err.Error())
ve, ok := err.(*validationErrors)
require.True(t, ok)
assert.Len(t, ve.errors, 3)
})
t.Run("creates error from empty errors slice", func(t *testing.T) {
errs := Errors{}
err := MakeValidationErrors(errs)
require.NotNil(t, err)
assert.Equal(t, "ValidationErrors: no errors", err.Error())
ve, ok := err.(*validationErrors)
require.True(t, ok)
assert.Len(t, ve.errors, 0)
})
t.Run("preserves error details", func(t *testing.T) {
cause := errors.New("underlying cause")
errs := Errors{
&ValidationError{
Value: "abc",
Context: []ContextEntry{{Key: "field"}},
Messsage: "invalid format",
Cause: cause,
},
}
err := MakeValidationErrors(errs)
ve, ok := err.(*validationErrors)
require.True(t, ok)
require.Len(t, ve.errors, 1)
assert.Equal(t, "abc", ve.errors[0].Value)
assert.Equal(t, "invalid format", ve.errors[0].Messsage)
assert.Equal(t, cause, ve.errors[0].Cause)
assert.Len(t, ve.errors[0].Context, 1)
})
t.Run("error can be formatted", func(t *testing.T) {
errs := Errors{
&ValidationError{
Context: []ContextEntry{{Key: "user"}, {Key: "name"}},
Messsage: "required",
},
}
err := MakeValidationErrors(errs)
formatted := fmt.Sprintf("%+v", err)
assert.Contains(t, formatted, "ValidationErrors")
assert.Contains(t, formatted, "user.name")
assert.Contains(t, formatted, "required")
})
}
func TestToResult(t *testing.T) {
t.Run("converts successful validation to result", func(t *testing.T) {
validation := Success(42)
result := ToResult(validation)
assert.True(t, either.IsRight(result))
value := either.MonadFold(result,
func(error) int { return 0 },
F.Identity[int],
)
assert.Equal(t, 42, value)
})
t.Run("converts failed validation to result with error", func(t *testing.T) {
errs := Errors{
&ValidationError{Value: "abc", Messsage: "expected number"},
}
validation := Failures[int](errs)
result := ToResult(validation)
assert.True(t, either.IsLeft(result))
err := either.MonadFold(result,
F.Identity[error],
func(int) error { return nil },
)
require.NotNil(t, err)
assert.Equal(t, "ValidationErrors: 1 error", err.Error())
// Verify it's a ValidationErrors type
ve, ok := err.(*validationErrors)
require.True(t, ok)
assert.Len(t, ve.errors, 1)
assert.Equal(t, "expected number", ve.errors[0].Messsage)
})
t.Run("converts multiple validation errors to result", func(t *testing.T) {
errs := Errors{
&ValidationError{Value: "test1", Messsage: "error 1"},
&ValidationError{Value: "test2", Messsage: "error 2"},
}
validation := Failures[string](errs)
result := ToResult(validation)
assert.True(t, either.IsLeft(result))
err := either.MonadFold(result,
F.Identity[error],
func(string) error { return nil },
)
require.NotNil(t, err)
assert.Equal(t, "ValidationErrors: 2 errors", err.Error())
ve, ok := err.(*validationErrors)
require.True(t, ok)
assert.Len(t, ve.errors, 2)
})
t.Run("works with different types", func(t *testing.T) {
// String type
strValidation := Success("hello")
strResult := ToResult(strValidation)
assert.True(t, either.IsRight(strResult))
// Bool type
boolValidation := Success(true)
boolResult := ToResult(boolValidation)
assert.True(t, either.IsRight(boolResult))
// Struct type
type User struct{ Name string }
userValidation := Success(User{Name: "Alice"})
userResult := ToResult(userValidation)
assert.True(t, either.IsRight(userResult))
user := either.MonadFold(userResult,
func(error) User { return User{} },
F.Identity[User],
)
assert.Equal(t, "Alice", user.Name)
})
t.Run("preserves error context in result", func(t *testing.T) {
errs := Errors{
&ValidationError{
Value: nil,
Context: []ContextEntry{{Key: "user"}, {Key: "email"}},
Messsage: "required field",
},
}
validation := Failures[string](errs)
result := ToResult(validation)
err := either.MonadFold(result,
F.Identity[error],
func(string) error { return nil },
)
formatted := fmt.Sprintf("%+v", err)
assert.Contains(t, formatted, "user.email")
assert.Contains(t, formatted, "required field")
})
t.Run("preserves cause in result error", func(t *testing.T) {
cause := errors.New("parse error")
errs := Errors{
&ValidationError{
Value: "abc",
Messsage: "invalid number",
Cause: cause,
},
}
validation := Failures[int](errs)
result := ToResult(validation)
err := either.MonadFold(result,
F.Identity[error],
func(int) error { return nil },
)
ve, ok := err.(*validationErrors)
require.True(t, ok)
require.Len(t, ve.errors, 1)
assert.True(t, errors.Is(ve.errors[0], cause))
})
t.Run("result error implements error interface", func(t *testing.T) {
errs := Errors{
&ValidationError{Messsage: "test error"},
}
validation := Failures[int](errs)
result := ToResult(validation)
err := either.MonadFold(result,
F.Identity[error],
func(int) error { return nil },
)
// Should be usable as a standard error
var stdErr error = err
assert.NotNil(t, stdErr)
assert.Contains(t, stdErr.Error(), "ValidationErrors")
})
}
// TestValidationError_LogValue tests the LogValue() method implementation
func TestValidationError_LogValue(t *testing.T) {
t.Run("simple error without context", func(t *testing.T) {
err := &ValidationError{
Value: "test",
Messsage: "invalid value",
}
logValue := err.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
assert.GreaterOrEqual(t, len(attrs), 2)
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "invalid value", attrMap["message"])
assert.Contains(t, attrMap["value"], "test")
})
t.Run("error with context path", func(t *testing.T) {
err := &ValidationError{
Value: "test",
Context: []ContextEntry{{Key: "user"}, {Key: "name"}},
Messsage: "must not be empty",
}
logValue := err.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "must not be empty", attrMap["message"])
assert.Equal(t, "user.name", attrMap["path"])
})
t.Run("error with cause", func(t *testing.T) {
cause := errors.New("parse error")
err := &ValidationError{
Value: "abc",
Messsage: "invalid number",
Cause: cause,
}
logValue := err.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]any)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.Any()
}
assert.Equal(t, "invalid number", attrMap["message"])
assert.NotNil(t, attrMap["cause"])
})
t.Run("error with context using type", func(t *testing.T) {
err := &ValidationError{
Value: 123,
Context: []ContextEntry{{Type: "User"}, {Key: "age"}},
Messsage: "must be positive",
}
logValue := err.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "User.age", attrMap["path"])
})
t.Run("complex context path", func(t *testing.T) {
err := &ValidationError{
Value: "invalid",
Context: []ContextEntry{
{Key: "user"},
{Key: "address"},
{Key: "zipCode"},
},
Messsage: "invalid format",
}
logValue := err.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]string)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.String()
}
assert.Equal(t, "user.address.zipCode", attrMap["path"])
})
}
// TestValidationErrors_LogValue tests the LogValue() method implementation
func TestValidationErrors_LogValue(t *testing.T) {
t.Run("empty errors", func(t *testing.T) {
ve := &validationErrors{errors: Errors{}}
logValue := ve.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
attrMap := make(map[string]any)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.Any()
}
assert.Equal(t, int64(0), attrMap["count"])
})
t.Run("single error", func(t *testing.T) {
ve := &validationErrors{
errors: Errors{
&ValidationError{Value: "test", Messsage: "error 1"},
},
}
logValue := ve.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]any)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.Any()
}
assert.Equal(t, int64(1), attrMap["count"])
assert.NotNil(t, attrMap["errors"])
})
t.Run("multiple errors", func(t *testing.T) {
ve := &validationErrors{
errors: Errors{
&ValidationError{Value: "test1", Messsage: "error 1"},
&ValidationError{Value: "test2", Messsage: "error 2"},
&ValidationError{Value: "test3", Messsage: "error 3"},
},
}
logValue := ve.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]any)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.Any()
}
assert.Equal(t, int64(3), attrMap["count"])
assert.NotNil(t, attrMap["errors"])
})
t.Run("with cause", func(t *testing.T) {
cause := errors.New("underlying error")
ve := &validationErrors{
errors: Errors{
&ValidationError{Value: "test", Messsage: "error"},
},
cause: cause,
}
logValue := ve.LogValue()
attrs := logValue.Group()
attrMap := make(map[string]any)
for _, attr := range attrs {
attrMap[attr.Key] = attr.Value.Any()
}
assert.NotNil(t, attrMap["cause"])
})
t.Run("preserves error details", func(t *testing.T) {
ve := &validationErrors{
errors: Errors{
&ValidationError{
Value: "abc",
Context: []ContextEntry{{Key: "field"}},
Messsage: "invalid format",
},
},
}
logValue := ve.LogValue()
assert.Equal(t, slog.KindGroup, logValue.Kind())
attrs := logValue.Group()
assert.GreaterOrEqual(t, len(attrs), 2)
})
}
// TestLogValuerInterface verifies that ValidationError and ValidationErrors implement slog.LogValuer
func TestLogValuerInterface(t *testing.T) {
t.Run("ValidationError implements slog.LogValuer", func(t *testing.T) {
var _ slog.LogValuer = (*ValidationError)(nil)
})
t.Run("ValidationErrors implements slog.LogValuer", func(t *testing.T) {
var _ slog.LogValuer = (*validationErrors)(nil)
})
}

View File

@@ -0,0 +1,370 @@
package codec
import (
"testing"
"github.com/IBM/fp-go/v2/either"
R "github.com/IBM/fp-go/v2/result"
"github.com/stretchr/testify/assert"
)
// TestIsWithPrimitiveTypes tests the Is function with primitive types
func TestIsWithPrimitiveTypes(t *testing.T) {
t.Run("string type succeeds with string value", func(t *testing.T) {
isString := Is[string]()
res := isString("hello")
assert.Equal(t, R.Of("hello"), res)
})
t.Run("string type fails with int value", func(t *testing.T) {
isString := Is[string]()
res := isString(42)
assert.True(t, either.IsLeft(res), "Expected Left for invalid type")
})
t.Run("int type succeeds with int value", func(t *testing.T) {
isInt := Is[int]()
res := isInt(42)
assert.Equal(t, R.Of(42), res)
})
t.Run("int type fails with string value", func(t *testing.T) {
isInt := Is[int]()
res := isInt("42")
assert.True(t, either.IsLeft(res))
})
t.Run("bool type succeeds with bool value", func(t *testing.T) {
isBool := Is[bool]()
res := isBool(true)
assert.Equal(t, R.Of(true), res)
})
t.Run("bool type fails with int value", func(t *testing.T) {
isBool := Is[bool]()
res := isBool(1)
assert.True(t, either.IsLeft(res))
})
t.Run("float64 type succeeds with float64 value", func(t *testing.T) {
isFloat := Is[float64]()
res := isFloat(3.14)
assert.Equal(t, R.Of(3.14), res)
})
t.Run("float64 type fails with int value", func(t *testing.T) {
isFloat := Is[float64]()
res := isFloat(42)
assert.True(t, either.IsLeft(res))
})
}
// TestIsWithNumericTypes tests Is with different numeric types
func TestIsWithNumericTypes(t *testing.T) {
t.Run("int8 type", func(t *testing.T) {
isInt8 := Is[int8]()
res := isInt8(int8(127))
assert.Equal(t, R.Of(int8(127)), res)
// Fails with regular int
res = isInt8(127)
assert.True(t, either.IsLeft(res))
})
t.Run("int16 type", func(t *testing.T) {
isInt16 := Is[int16]()
res := isInt16(int16(32767))
assert.Equal(t, R.Of(int16(32767)), res)
})
t.Run("int32 type", func(t *testing.T) {
isInt32 := Is[int32]()
res := isInt32(int32(2147483647))
assert.Equal(t, R.Of(int32(2147483647)), res)
})
t.Run("int64 type", func(t *testing.T) {
isInt64 := Is[int64]()
res := isInt64(int64(9223372036854775807))
assert.Equal(t, R.Of(int64(9223372036854775807)), res)
})
t.Run("uint type", func(t *testing.T) {
isUint := Is[uint]()
res := isUint(uint(42))
assert.Equal(t, R.Of(uint(42)), res)
// Fails with int
res = isUint(42)
assert.True(t, either.IsLeft(res))
})
t.Run("float32 type", func(t *testing.T) {
isFloat32 := Is[float32]()
res := isFloat32(float32(3.14))
assert.Equal(t, R.Of(float32(3.14)), res)
// Fails with float64
res = isFloat32(3.14)
assert.True(t, either.IsLeft(res))
})
}
// TestIsWithComplexTypes tests Is with complex and composite types
func TestIsWithComplexTypes(t *testing.T) {
t.Run("slice type succeeds with slice", func(t *testing.T) {
isSlice := Is[[]int]()
res := isSlice([]int{1, 2, 3})
assert.Equal(t, R.Of([]int{1, 2, 3}), res)
})
t.Run("slice type fails with array", func(t *testing.T) {
isSlice := Is[[]int]()
res := isSlice([3]int{1, 2, 3})
assert.True(t, either.IsLeft(res))
})
t.Run("map type succeeds with map", func(t *testing.T) {
isMap := Is[map[string]int]()
testMap := map[string]int{"a": 1, "b": 2}
res := isMap(testMap)
assert.Equal(t, R.Of(testMap), res)
})
t.Run("map type fails with wrong key type", func(t *testing.T) {
isMap := Is[map[string]int]()
wrongMap := map[int]int{1: 1, 2: 2}
res := isMap(wrongMap)
assert.True(t, either.IsLeft(res))
})
t.Run("array type succeeds with array", func(t *testing.T) {
isArray := Is[[3]int]()
res := isArray([3]int{1, 2, 3})
assert.Equal(t, R.Of([3]int{1, 2, 3}), res)
})
t.Run("array type fails with different size", func(t *testing.T) {
isArray := Is[[3]int]()
res := isArray([4]int{1, 2, 3, 4})
assert.True(t, either.IsLeft(res))
})
}
// TestIsWithStructTypes tests Is with struct types
func TestIsWithStructTypes(t *testing.T) {
type Person struct {
Name string
Age int
}
type Employee struct {
Name string
Salary float64
}
t.Run("struct type succeeds with matching struct", func(t *testing.T) {
isPerson := Is[Person]()
person := Person{Name: "Alice", Age: 30}
res := isPerson(person)
assert.Equal(t, R.Of(person), res)
})
t.Run("struct type fails with different struct", func(t *testing.T) {
isPerson := Is[Person]()
employee := Employee{Name: "Bob", Salary: 50000}
res := isPerson(employee)
assert.True(t, either.IsLeft(res))
})
t.Run("struct type fails with primitive", func(t *testing.T) {
isPerson := Is[Person]()
res := isPerson("not a person")
assert.True(t, either.IsLeft(res))
})
}
// TestIsWithPointerTypes tests Is with pointer types
func TestIsWithPointerTypes(t *testing.T) {
t.Run("pointer type succeeds with pointer", func(t *testing.T) {
isStringPtr := Is[*string]()
str := "hello"
res := isStringPtr(&str)
assert.Equal(t, R.Of(&str), res)
})
t.Run("pointer type fails with non-pointer", func(t *testing.T) {
isStringPtr := Is[*string]()
res := isStringPtr("hello")
assert.True(t, either.IsLeft(res))
})
t.Run("pointer type succeeds with nil pointer", func(t *testing.T) {
isStringPtr := Is[*string]()
var nilPtr *string = nil
res := isStringPtr(nilPtr)
assert.Equal(t, R.Of(nilPtr), res)
})
t.Run("non-pointer type fails with pointer", func(t *testing.T) {
isString := Is[string]()
str := "hello"
res := isString(&str)
assert.True(t, either.IsLeft(res))
})
}
// TestIsWithEmptyValues tests Is with empty/zero values
func TestIsWithEmptyValues(t *testing.T) {
t.Run("empty string", func(t *testing.T) {
isString := Is[string]()
res := isString("")
assert.Equal(t, R.Of(""), res)
})
t.Run("zero int", func(t *testing.T) {
isInt := Is[int]()
res := isInt(0)
assert.Equal(t, R.Of(0), res)
})
t.Run("false bool", func(t *testing.T) {
isBool := Is[bool]()
res := isBool(false)
assert.Equal(t, R.Of(false), res)
})
t.Run("nil slice", func(t *testing.T) {
isSlice := Is[[]int]()
var nilSlice []int = nil
res := isSlice(nilSlice)
assert.Equal(t, R.Of(nilSlice), res)
})
t.Run("empty slice", func(t *testing.T) {
isSlice := Is[[]int]()
emptySlice := []int{}
res := isSlice(emptySlice)
assert.Equal(t, R.Of(emptySlice), res)
})
t.Run("nil map", func(t *testing.T) {
isMap := Is[map[string]int]()
var nilMap map[string]int = nil
res := isMap(nilMap)
assert.Equal(t, R.Of(nilMap), res)
})
}
// TestIsWithChannelTypes tests Is with channel types
func TestIsWithChannelTypes(t *testing.T) {
t.Run("channel type succeeds with channel", func(t *testing.T) {
isChan := Is[chan int]()
ch := make(chan int)
defer close(ch)
res := isChan(ch)
assert.Equal(t, R.Of(ch), res)
})
t.Run("channel type fails with wrong channel type", func(t *testing.T) {
isChan := Is[chan int]()
ch := make(chan string)
defer close(ch)
res := isChan(ch)
assert.True(t, either.IsLeft(res))
})
t.Run("bidirectional vs unidirectional channels", func(t *testing.T) {
isSendChan := Is[chan<- int]()
ch := make(chan int)
defer close(ch)
// Bidirectional channel can be used as send-only
sendCh := chan<- int(ch)
res := isSendChan(sendCh)
assert.Equal(t, R.Of(sendCh), res)
})
}
// TestIsWithFunctionTypes tests Is with function types
func TestIsWithFunctionTypes(t *testing.T) {
t.Run("function type succeeds with matching function", func(t *testing.T) {
isFunc := Is[func(int) int]()
fn := func(x int) int { return x * 2 }
res := isFunc(fn)
// Functions can't be compared for equality, so just check it's Right
assert.True(t, either.IsRight(res))
})
t.Run("function type fails with different signature", func(t *testing.T) {
isFunc := Is[func(int) int]()
fn := func(x string) string { return x }
res := isFunc(fn)
assert.True(t, either.IsLeft(res))
})
t.Run("function type fails with non-function", func(t *testing.T) {
isFunc := Is[func(int) int]()
res := isFunc(42)
assert.True(t, either.IsLeft(res))
})
}
// TestIsErrorMessages tests that Is produces appropriate error messages
func TestIsErrorMessages(t *testing.T) {
t.Run("error message for type mismatch", func(t *testing.T) {
isString := Is[string]()
res := isString(42)
assert.True(t, either.IsLeft(res), "Expected Left for type mismatch")
})
t.Run("error for struct type mismatch", func(t *testing.T) {
type CustomType struct {
Field string
}
isCustom := Is[CustomType]()
res := isCustom("not a custom type")
assert.True(t, either.IsLeft(res), "Expected Left for struct type mismatch")
})
}

View File

@@ -267,6 +267,11 @@ func MakeLensCurriedWithName[GET ~func(S) A, SET ~func(A) Endomorphism[S], S, A
return Lens[S, A]{Get: get, Set: set, name: name}
}
//go:inline
func MakeLensCurriedRefWithName[GET ~func(*S) A, SET ~func(A) Endomorphism[*S], S, A any](get GET, set SET, name string) Lens[*S, A] {
return Lens[*S, A]{Get: get, Set: setCopyCurried(set), name: name}
}
// MakeLensRef creates a [Lens] for pointer-based structures.
//
// Unlike [MakeLens], the setter does not need to create a copy manually. This function

View File

@@ -0,0 +1,252 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prism
import (
"fmt"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/lazy"
O "github.com/IBM/fp-go/v2/optics/optional"
"github.com/IBM/fp-go/v2/option"
)
func compose[S, A, B any](
creator func(get option.Kleisli[S, B], set func(B) Endomorphism[S], name string) Optional[S, B],
p Prism[A, B]) func(Lens[S, A]) Optional[S, B] {
return func(l Lens[S, A]) Optional[S, B] {
// GetOption: Lens.Get followed by Prism.GetOption
// This extracts A from S, then tries to extract B from A
getOption := F.Flow2(l.Get, p.GetOption)
// Set: Constructs a setter that respects the Optional laws
setOption := func(b B) func(S) S {
// Pre-compute the new A value by using Prism.ReverseGet
// This constructs an A from the given B
setl := l.Set(p.ReverseGet(b))
return func(s S) S {
// Check if the Prism matches the current value
return F.Pipe1(
getOption(s),
option.Fold(
// None case: Prism doesn't match, return s unchanged (no-op)
// This satisfies the GetSet law for Optional
lazy.Of(s),
// Some case: Prism matches, update the value
// This satisfies the SetGet law for Optional
func(_ B) S {
return setl(s)
},
),
)
}
}
return creator(
getOption,
setOption,
fmt.Sprintf("Compose[%s -> %s]", l, p),
)
}
}
// Compose composes a Lens with a Prism to create an Optional.
//
// This composition allows you to focus on a part of a structure (using a Lens)
// and then optionally extract a variant from that part (using a Prism). The result
// is an Optional because the Prism may not match the focused value.
//
// The composition follows the Optional laws (a relaxed form of lens laws):
//
// SetGet Law (GetSet for Optional):
// - If optional.GetOption(s) = Some(b), then optional.GetOption(optional.Set(b)(s)) = Some(b)
// - This ensures that setting a value and then getting it returns the same value
//
// GetSet Law (for Optional):
// - If optional.GetOption(s) = None, then optional.Set(b)(s) = s (no-op)
// - This ensures that setting a value when the optional doesn't match leaves the structure unchanged
//
// These laws are documented in the official fp-ts documentation:
// https://gcanti.github.io/monocle-ts/modules/Optional.ts.html
//
// Type Parameters:
// - S: The source/outer structure type
// - A: The intermediate type (focused by the Lens)
// - B: The target type (focused by the Prism within A)
//
// Parameters:
// - p: A Prism[A, B] that optionally extracts B from A
//
// Returns:
// - A function that takes a Lens[S, A] and returns an Optional[S, B]
//
// Behavior:
// - GetOption: First uses the Lens to get A from S, then uses the Prism to try to extract B from A.
// Returns Some(b) if both operations succeed, None otherwise.
// - Set: When setting a value b:
// - If GetOption(s) returns Some(_), it means the Prism matches, so we:
// 1. Use Prism.ReverseGet to construct an A from b
// 2. Use Lens.Set to update S with the new A
// - If GetOption(s) returns None, the Prism doesn't match, so we return s unchanged (no-op)
//
// Example:
//
// type Config struct {
// Database DatabaseConfig
// }
//
// type DatabaseConfig struct {
// Connection ConnectionType
// }
//
// type ConnectionType interface{ isConnection() }
// type PostgreSQL struct{ Host string }
// type MySQL struct{ Host string }
//
// // Lens to focus on Database field
// dbLens := lens.MakeLens(
// func(c Config) DatabaseConfig { return c.Database },
// func(c Config, db DatabaseConfig) Config { c.Database = db; return c },
// )
//
// // Prism to extract PostgreSQL from ConnectionType
// pgPrism := prism.MakePrism(
// func(ct ConnectionType) option.Option[PostgreSQL] {
// if pg, ok := ct.(PostgreSQL); ok {
// return option.Some(pg)
// }
// return option.None[PostgreSQL]()
// },
// func(pg PostgreSQL) ConnectionType { return pg },
// )
//
// // Compose to create Optional[Config, PostgreSQL]
// configPgOptional := Compose[Config, DatabaseConfig, PostgreSQL](pgPrism)(dbLens)
//
// config := Config{Database: DatabaseConfig{Connection: PostgreSQL{Host: "localhost"}}}
// host := configPgOptional.GetOption(config) // Some(PostgreSQL{Host: "localhost"})
//
// updated := configPgOptional.Set(PostgreSQL{Host: "remote"})(config)
// // updated.Database.Connection = PostgreSQL{Host: "remote"}
//
// configMySQL := Config{Database: DatabaseConfig{Connection: MySQL{Host: "localhost"}}}
// none := configPgOptional.GetOption(configMySQL) // None (Prism doesn't match)
// unchanged := configPgOptional.Set(PostgreSQL{Host: "remote"})(configMySQL)
// // unchanged == configMySQL (no-op because Prism doesn't match)
func Compose[S, A, B any](p Prism[A, B]) func(Lens[S, A]) Optional[S, B] {
return compose(O.MakeOptionalCurriedWithName[S, B], p)
}
// ComposeRef composes a Lens operating on pointer types with a Prism to create an Optional.
//
// This is the pointer-safe variant of Compose, designed for working with pointer types (*S).
// It automatically handles nil pointer cases and creates copies before modification to ensure
// immutability and prevent unintended side effects.
//
// The composition follows the same Optional laws as Compose:
//
// SetGet Law (GetSet for Optional):
// - If optional.GetOption(s) = Some(b), then optional.GetOption(optional.Set(b)(s)) = Some(b)
// - This ensures that setting a value and then getting it returns the same value
//
// GetSet Law (for Optional):
// - If optional.GetOption(s) = None, then optional.Set(b)(s) = s (no-op)
// - This ensures that setting a value when the optional doesn't match leaves the structure unchanged
//
// Nil Pointer Handling:
// - When s is nil and GetOption would return None, Set operations return nil (no-op)
// - When s is nil and GetOption would return Some (after creating default), Set creates a new instance
// - All Set operations create a shallow copy of *S before modification to preserve immutability
//
// These laws are documented in the official fp-ts documentation:
// https://gcanti.github.io/monocle-ts/modules/Optional.ts.html
//
// Type Parameters:
// - S: The source/outer structure type (used as *S in the lens)
// - A: The intermediate type (focused by the Lens)
// - B: The target type (focused by the Prism within A)
//
// Parameters:
// - p: A Prism[A, B] that optionally extracts B from A
//
// Returns:
// - A function that takes a Lens[*S, A] and returns an Optional[*S, B]
//
// Behavior:
// - GetOption: First uses the Lens to get A from *S, then uses the Prism to try to extract B from A.
// Returns Some(b) if both operations succeed, None otherwise.
// - Set: When setting a value b:
// - Creates a shallow copy of *S before any modification (nil-safe)
// - If GetOption(s) returns Some(_), it means the Prism matches, so we:
// 1. Use Prism.ReverseGet to construct an A from b
// 2. Use Lens.Set to update the copy of *S with the new A
// - If GetOption(s) returns None, the Prism doesn't match, so we return s unchanged (no-op)
//
// Example:
//
// type Config struct {
// Connection ConnectionType
// AppName string
// }
//
// type ConnectionType interface{ isConnection() }
// type PostgreSQL struct{ Host string }
// type MySQL struct{ Host string }
//
// // Lens to focus on Connection field (pointer-based)
// connLens := lens.MakeLensRef(
// func(c *Config) ConnectionType { return c.Connection },
// func(c *Config, ct ConnectionType) *Config { c.Connection = ct; return c },
// )
//
// // Prism to extract PostgreSQL from ConnectionType
// pgPrism := prism.MakePrism(
// func(ct ConnectionType) option.Option[PostgreSQL] {
// if pg, ok := ct.(PostgreSQL); ok {
// return option.Some(pg)
// }
// return option.None[PostgreSQL]()
// },
// func(pg PostgreSQL) ConnectionType { return pg },
// )
//
// // Compose to create Optional[*Config, PostgreSQL]
// configPgOptional := ComposeRef[Config, ConnectionType, PostgreSQL](pgPrism)(connLens)
//
// // Works with non-nil pointers
// config := &Config{Connection: PostgreSQL{Host: "localhost"}}
// host := configPgOptional.GetOption(config) // Some(PostgreSQL{Host: "localhost"})
// updated := configPgOptional.Set(PostgreSQL{Host: "remote"})(config)
// // updated is a new *Config with Connection = PostgreSQL{Host: "remote"}
// // original config is unchanged (immutability preserved)
//
// // Handles nil pointers safely
// var nilConfig *Config = nil
// none := configPgOptional.GetOption(nilConfig) // None (nil pointer)
// unchanged := configPgOptional.Set(PostgreSQL{Host: "remote"})(nilConfig)
// // unchanged == nil (no-op because source is nil)
//
// // Works with mismatched prisms
// configMySQL := &Config{Connection: MySQL{Host: "localhost"}}
// none = configPgOptional.GetOption(configMySQL) // None (Prism doesn't match)
// unchanged = configPgOptional.Set(PostgreSQL{Host: "remote"})(configMySQL)
// // unchanged == configMySQL (no-op because Prism doesn't match)
func ComposeRef[S, A, B any](p Prism[A, B]) func(Lens[*S, A]) Optional[*S, B] {
return compose(O.MakeOptionalRefCurriedWithName[S, B], p)
}

View File

@@ -0,0 +1,858 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prism
import (
"testing"
"github.com/IBM/fp-go/v2/assert"
F "github.com/IBM/fp-go/v2/function"
L "github.com/IBM/fp-go/v2/optics/lens"
P "github.com/IBM/fp-go/v2/optics/prism"
O "github.com/IBM/fp-go/v2/option"
)
// Test types for composition examples
// ConnectionType is a sum type representing different database connections
type ConnectionType interface {
isConnection()
}
type PostgreSQL struct {
Host string
Port int
}
func (PostgreSQL) isConnection() {}
type MySQL struct {
Host string
Port int
}
func (MySQL) isConnection() {}
type MongoDB struct {
Host string
Port int
}
func (MongoDB) isConnection() {}
// Config is the top-level configuration
type Config struct {
Connection ConnectionType
AppName string
}
// Helper functions to create prisms for each connection type
func postgresqlPrism() P.Prism[ConnectionType, PostgreSQL] {
return P.MakePrism(
func(ct ConnectionType) O.Option[PostgreSQL] {
if pg, ok := ct.(PostgreSQL); ok {
return O.Some(pg)
}
return O.None[PostgreSQL]()
},
func(pg PostgreSQL) ConnectionType { return pg },
)
}
func mysqlPrism() P.Prism[ConnectionType, MySQL] {
return P.MakePrism(
func(ct ConnectionType) O.Option[MySQL] {
if my, ok := ct.(MySQL); ok {
return O.Some(my)
}
return O.None[MySQL]()
},
func(my MySQL) ConnectionType { return my },
)
}
func mongodbPrism() P.Prism[ConnectionType, MongoDB] {
return P.MakePrism(
func(ct ConnectionType) O.Option[MongoDB] {
if mg, ok := ct.(MongoDB); ok {
return O.Some(mg)
}
return O.None[MongoDB]()
},
func(mg MongoDB) ConnectionType { return mg },
)
}
// Helper function to create connection lens
func connectionLens() L.Lens[Config, ConnectionType] {
return L.MakeLens(
func(c Config) ConnectionType { return c.Connection },
func(c Config, ct ConnectionType) Config {
c.Connection = ct
return c
},
)
}
// Helper function to create nil-safe connection lens for pointer types
func connectionLensRef() L.Lens[*Config, ConnectionType] {
return L.MakeLensRef(
func(c *Config) ConnectionType {
if c == nil {
return nil
}
return c.Connection
},
func(c *Config, ct ConnectionType) *Config {
if c == nil {
return &Config{Connection: ct}
}
c.Connection = ct
return c
},
)
}
// TestComposeBasicFunctionality tests basic composition behavior
func TestComposeBasicFunctionality(t *testing.T) {
t.Run("GetOption returns Some when Prism matches", func(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
// Compose connection lens with PostgreSQL prism
configPgOptional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
result := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("localhost")(pg.Host)(t)
assert.Equal(5432)(pg.Port)(t)
})
t.Run("GetOption returns None when Prism doesn't match", func(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
configPgOptional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
result := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(result))(t)
})
t.Run("Set updates value when Prism matches", func(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
configPgOptional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify the update
result := configPgOptional.GetOption(updated)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("remote.example.com")(pg.Host)(t)
assert.Equal(5433)(pg.Port)(t)
// Verify other fields are unchanged
assert.Equal("TestApp")(updated.AppName)(t)
})
t.Run("Set is no-op when Prism doesn't match", func(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
configPgOptional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify nothing changed (no-op)
assert.Equal(config)(updated)(t)
// Verify the connection is still MySQL
if my, ok := updated.Connection.(MySQL); ok {
assert.Equal("localhost")(my.Host)(t)
assert.Equal(3306)(my.Port)(t)
} else {
t.Fatal("Expected MySQL connection to remain unchanged")
}
})
}
// TestComposeOptionalLaws tests that the composition satisfies Optional laws
// Reference: https://gcanti.github.io/monocle-ts/modules/Optional.ts.html
func TestComposeOptionalLaws(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
configPgOptional := Compose[Config](pgPrism)(connLens)
t.Run("SetGet Law: GetOption(Set(b)(s)) = Some(b) when GetOption(s) = Some(_)", func(t *testing.T) {
// Start with a config that has PostgreSQL
config := Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
// Verify the prism matches
initial := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsSome(initial))(t)
// Set a new value
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Get the value back
result := configPgOptional.GetOption(updated)
// Verify SetGet law: we should get back what we set
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal(newPg.Host)(pg.Host)(t)
assert.Equal(newPg.Port)(pg.Port)(t)
})
t.Run("GetSet Law: Set(b)(s) = s when GetOption(s) = None (no-op)", func(t *testing.T) {
// Start with a config that has MySQL (not PostgreSQL)
config := Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
// Verify the prism doesn't match
initial := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(initial))(t)
// Try to set a PostgreSQL value
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify GetSet law: structure should be unchanged (no-op)
assert.Equal(config)(updated)(t)
})
t.Run("SetSet Law: Set(b2)(Set(b1)(s)) = Set(b2)(s)", func(t *testing.T) {
config := Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
pg1 := PostgreSQL{Host: "server1.example.com", Port: 5433}
pg2 := PostgreSQL{Host: "server2.example.com", Port: 5434}
// Set twice
setTwice := configPgOptional.Set(pg2)(configPgOptional.Set(pg1)(config))
// Set once with the final value
setOnce := configPgOptional.Set(pg2)(config)
// They should be equal
assert.Equal(setOnce)(setTwice)(t)
// Verify the final value
result := configPgOptional.GetOption(setTwice)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal(pg2.Host)(pg.Host)(t)
assert.Equal(pg2.Port)(pg.Port)(t)
})
}
// TestComposeMultipleVariants tests composition with different prism variants
func TestComposeMultipleVariants(t *testing.T) {
connLens := connectionLens()
t.Run("PostgreSQL variant", func(t *testing.T) {
pgPrism := postgresqlPrism()
optional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: PostgreSQL{Host: "pg.example.com", Port: 5432},
}
result := optional.GetOption(config)
assert.Equal(true)(O.IsSome(result))(t)
})
t.Run("MySQL variant", func(t *testing.T) {
myPrism := mysqlPrism()
optional := Compose[Config](myPrism)(connLens)
config := Config{
Connection: MySQL{Host: "mysql.example.com", Port: 3306},
}
result := optional.GetOption(config)
assert.Equal(true)(O.IsSome(result))(t)
})
t.Run("MongoDB variant", func(t *testing.T) {
mgPrism := mongodbPrism()
optional := Compose[Config](mgPrism)(connLens)
config := Config{
Connection: MongoDB{Host: "mongo.example.com", Port: 27017},
}
result := optional.GetOption(config)
assert.Equal(true)(O.IsSome(result))(t)
})
t.Run("Cross-variant no-op", func(t *testing.T) {
// Try to use PostgreSQL optional on MySQL config
pgPrism := postgresqlPrism()
optional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: MySQL{Host: "mysql.example.com", Port: 3306},
}
// GetOption should return None
result := optional.GetOption(config)
assert.Equal(true)(O.IsNone(result))(t)
// Set should be no-op
newPg := PostgreSQL{Host: "pg.example.com", Port: 5432}
updated := optional.Set(newPg)(config)
assert.Equal(config)(updated)(t)
})
}
// TestComposeEdgeCases tests edge cases and boundary conditions
func TestComposeEdgeCases(t *testing.T) {
t.Run("Identity lens with prism", func(t *testing.T) {
// Identity lens that doesn't transform the value
idLens := L.MakeLens(
func(ct ConnectionType) ConnectionType { return ct },
func(_ ConnectionType, ct ConnectionType) ConnectionType { return ct },
)
pgPrism := postgresqlPrism()
optional := Compose[ConnectionType](pgPrism)(idLens)
conn := ConnectionType(PostgreSQL{Host: "localhost", Port: 5432})
result := optional.GetOption(conn)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("localhost")(pg.Host)(t)
})
t.Run("Multiple sets preserve structure", func(t *testing.T) {
connLens := connectionLens()
pgPrism := postgresqlPrism()
optional := Compose[Config](pgPrism)(connLens)
config := Config{
Connection: PostgreSQL{Host: "host1", Port: 5432},
AppName: "TestApp",
}
// Apply multiple sets
pg2 := PostgreSQL{Host: "host2", Port: 5433}
pg3 := PostgreSQL{Host: "host3", Port: 5434}
pg4 := PostgreSQL{Host: "host4", Port: 5435}
updated := F.Pipe3(
config,
optional.Set(pg2),
optional.Set(pg3),
optional.Set(pg4),
)
// Verify final value
result := optional.GetOption(updated)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("host4")(pg.Host)(t)
assert.Equal(5435)(pg.Port)(t)
// Verify structure is preserved
assert.Equal("TestApp")(updated.AppName)(t)
})
}
// TestComposeDocumentationExample tests the example from the documentation
func TestComposeDocumentationExample(t *testing.T) {
// This test verifies the example code in the documentation works correctly
// Lens to focus on Connection field
connLens := L.MakeLens(
func(c Config) ConnectionType { return c.Connection },
func(c Config, ct ConnectionType) Config { c.Connection = ct; return c },
)
// Prism to extract PostgreSQL from ConnectionType
pgPrism := P.MakePrism(
func(ct ConnectionType) O.Option[PostgreSQL] {
if pg, ok := ct.(PostgreSQL); ok {
return O.Some(pg)
}
return O.None[PostgreSQL]()
},
func(pg PostgreSQL) ConnectionType { return pg },
)
// Compose to create Optional[Config, PostgreSQL]
configPgOptional := Compose[Config](pgPrism)(connLens)
config := Config{Connection: PostgreSQL{Host: "localhost"}}
host := configPgOptional.GetOption(config) // Some(PostgreSQL{Host: "localhost"})
assert.Equal(true)(O.IsSome(host))(t)
updated := configPgOptional.Set(PostgreSQL{Host: "remote"})(config)
// updated.Connection = PostgreSQL{Host: "remote"}
result := configPgOptional.GetOption(updated)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("remote")(pg.Host)(t)
configMySQL := Config{Connection: MySQL{Host: "localhost"}}
none := configPgOptional.GetOption(configMySQL) // None (Prism doesn't match)
assert.Equal(true)(O.IsNone(none))(t)
unchanged := configPgOptional.Set(PostgreSQL{Host: "remote"})(configMySQL)
// unchanged == configMySQL (no-op because Prism doesn't match)
assert.Equal(configMySQL)(unchanged)(t)
}
// TestComposeRefBasicFunctionality tests basic ComposeRef behavior with pointer types
func TestComposeRefBasicFunctionality(t *testing.T) {
t.Run("GetOption returns Some when Prism matches (non-nil pointer)", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
config := &Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
result := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("localhost")(pg.Host)(t)
assert.Equal(5432)(pg.Port)(t)
})
t.Run("GetOption returns None when pointer is nil", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
result := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(result))(t)
})
t.Run("GetOption returns None when Prism doesn't match", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
config := &Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
result := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(result))(t)
})
t.Run("Set updates value when Prism matches (creates copy)", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
original := &Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(original)
// Verify the update
result := configPgOptional.GetOption(updated)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("remote.example.com")(pg.Host)(t)
assert.Equal(5433)(pg.Port)(t)
// Verify immutability: original should be unchanged
if origPg, ok := original.Connection.(PostgreSQL); ok {
assert.Equal("localhost")(origPg.Host)(t)
assert.Equal(5432)(origPg.Port)(t)
} else {
t.Fatal("Original config should still have PostgreSQL connection")
}
// Verify they are different pointers
if original == updated {
t.Fatal("Set should create a new pointer, not modify in place")
}
})
t.Run("Set is no-op when pointer is nil", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify nothing changed (no-op for nil)
if updated != nil {
t.Fatalf("Expected nil, got %v", updated)
}
})
t.Run("Set is no-op when Prism doesn't match", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
original := &Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(original)
// Verify nothing changed (no-op)
assert.Equal(original)(updated)(t)
// Verify the connection is still MySQL
if my, ok := updated.Connection.(MySQL); ok {
assert.Equal("localhost")(my.Host)(t)
assert.Equal(3306)(my.Port)(t)
} else {
t.Fatal("Expected MySQL connection to remain unchanged")
}
})
}
// TestComposeRefOptionalLaws tests that ComposeRef satisfies Optional laws
func TestComposeRefOptionalLaws(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
t.Run("SetGet Law: GetOption(Set(b)(s)) = Some(b) when GetOption(s) = Some(_)", func(t *testing.T) {
// Start with a config that has PostgreSQL
config := &Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
// Verify the prism matches
initial := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsSome(initial))(t)
// Set a new value
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Get the value back
result := configPgOptional.GetOption(updated)
// Verify SetGet law: we should get back what we set
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal(newPg.Host)(pg.Host)(t)
assert.Equal(newPg.Port)(pg.Port)(t)
})
t.Run("GetSet Law: Set(b)(s) = s when GetOption(s) = None (no-op for nil)", func(t *testing.T) {
// Start with nil config
var config *Config = nil
// Verify the prism doesn't match
initial := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(initial))(t)
// Try to set a PostgreSQL value
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify GetSet law: structure should be unchanged (nil)
if updated != nil {
t.Fatalf("Expected nil, got %v", updated)
}
})
t.Run("GetSet Law: Set(b)(s) = s when GetOption(s) = None (no-op for mismatched prism)", func(t *testing.T) {
// Start with a config that has MySQL (not PostgreSQL)
config := &Config{
Connection: MySQL{Host: "localhost", Port: 3306},
AppName: "TestApp",
}
// Verify the prism doesn't match
initial := configPgOptional.GetOption(config)
assert.Equal(true)(O.IsNone(initial))(t)
// Try to set a PostgreSQL value
newPg := PostgreSQL{Host: "remote.example.com", Port: 5433}
updated := configPgOptional.Set(newPg)(config)
// Verify GetSet law: structure should be unchanged
assert.Equal(config)(updated)(t)
})
t.Run("SetSet Law: Set(b2)(Set(b1)(s)) = Set(b2)(s)", func(t *testing.T) {
config := &Config{
Connection: PostgreSQL{Host: "localhost", Port: 5432},
AppName: "TestApp",
}
pg1 := PostgreSQL{Host: "server1.example.com", Port: 5433}
pg2 := PostgreSQL{Host: "server2.example.com", Port: 5434}
// Set twice
setTwice := configPgOptional.Set(pg2)(configPgOptional.Set(pg1)(config))
// Set once with the final value
setOnce := configPgOptional.Set(pg2)(config)
// They should be equal in value (but different pointers due to immutability)
result1 := configPgOptional.GetOption(setTwice)
result2 := configPgOptional.GetOption(setOnce)
assert.Equal(true)(O.IsSome(result1))(t)
assert.Equal(true)(O.IsSome(result2))(t)
pg1Result := O.GetOrElse(F.Constant(PostgreSQL{}))(result1)
pg2Result := O.GetOrElse(F.Constant(PostgreSQL{}))(result2)
assert.Equal(pg2.Host)(pg1Result.Host)(t)
assert.Equal(pg2.Port)(pg1Result.Port)(t)
assert.Equal(pg2.Host)(pg2Result.Host)(t)
assert.Equal(pg2.Port)(pg2Result.Port)(t)
})
}
// TestComposeRefImmutability tests that ComposeRef preserves immutability
func TestComposeRefImmutability(t *testing.T) {
t.Run("Set creates a new pointer, doesn't modify original", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
optional := ComposeRef[Config](pgPrism)(connLens)
original := &Config{
Connection: PostgreSQL{Host: "original", Port: 5432},
AppName: "OriginalApp",
}
// Store original values
origPg := original.Connection.(PostgreSQL)
origAppName := original.AppName
// Perform multiple sets
pg1 := PostgreSQL{Host: "host1", Port: 5433}
pg2 := PostgreSQL{Host: "host2", Port: 5434}
pg3 := PostgreSQL{Host: "host3", Port: 5435}
updated1 := optional.Set(pg1)(original)
updated2 := optional.Set(pg2)(updated1)
updated3 := optional.Set(pg3)(updated2)
// Verify original is unchanged
currentPg := original.Connection.(PostgreSQL)
assert.Equal(origPg.Host)(currentPg.Host)(t)
assert.Equal(origPg.Port)(currentPg.Port)(t)
assert.Equal(origAppName)(original.AppName)(t)
// Verify final update has correct value
result := optional.GetOption(updated3)
assert.Equal(true)(O.IsSome(result))(t)
finalPg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("host3")(finalPg.Host)(t)
assert.Equal(5435)(finalPg.Port)(t)
// Verify all pointers are different
if original == updated1 || original == updated2 || original == updated3 {
t.Fatal("Set should create new pointers, not modify in place")
}
if updated1 == updated2 || updated2 == updated3 || updated1 == updated3 {
t.Fatal("Each Set should create a new pointer")
}
})
t.Run("Multiple operations on nil preserve nil", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
optional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
// Multiple sets on nil should all return nil
pg1 := PostgreSQL{Host: "host1", Port: 5433}
pg2 := PostgreSQL{Host: "host2", Port: 5434}
updated1 := optional.Set(pg1)(config)
updated2 := optional.Set(pg2)(updated1)
if updated1 != nil {
t.Fatalf("Expected nil after first set, got %v", updated1)
}
if updated2 != nil {
t.Fatalf("Expected nil after second set, got %v", updated2)
}
})
}
// TestComposeRefNilPointerEdgeCases tests edge cases with nil pointers
func TestComposeRefNilPointerEdgeCases(t *testing.T) {
t.Run("GetOption on nil returns None", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
optional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
result := optional.GetOption(config)
assert.Equal(true)(O.IsNone(result))(t)
})
t.Run("Set on nil with matching prism returns nil", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
optional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
newPg := PostgreSQL{Host: "remote", Port: 5432}
updated := optional.Set(newPg)(config)
if updated != nil {
t.Fatalf("Expected nil, got %v", updated)
}
})
t.Run("Chaining operations starting from nil", func(t *testing.T) {
connLens := connectionLensRef()
pgPrism := postgresqlPrism()
optional := ComposeRef[Config](pgPrism)(connLens)
var config *Config = nil
// Chain multiple operations
pg1 := PostgreSQL{Host: "host1", Port: 5433}
pg2 := PostgreSQL{Host: "host2", Port: 5434}
result := F.Pipe2(
config,
optional.Set(pg1),
optional.Set(pg2),
)
if result != nil {
t.Fatalf("Expected nil after chained operations, got %v", result)
}
})
}
// TestComposeRefDocumentationExample tests the example from the ComposeRef documentation
func TestComposeRefDocumentationExample(t *testing.T) {
// Lens to focus on Connection field (pointer-based)
connLens := connectionLensRef()
// Prism to extract PostgreSQL from ConnectionType
pgPrism := P.MakePrism(
func(ct ConnectionType) O.Option[PostgreSQL] {
if pg, ok := ct.(PostgreSQL); ok {
return O.Some(pg)
}
return O.None[PostgreSQL]()
},
func(pg PostgreSQL) ConnectionType { return pg },
)
// Compose to create Optional[*Config, PostgreSQL]
configPgOptional := ComposeRef[Config](pgPrism)(connLens)
// Works with non-nil pointers
config := &Config{Connection: PostgreSQL{Host: "localhost"}}
host := configPgOptional.GetOption(config) // Some(PostgreSQL{Host: "localhost"})
assert.Equal(true)(O.IsSome(host))(t)
updated := configPgOptional.Set(PostgreSQL{Host: "remote"})(config)
// updated is a new *Config with Connection = PostgreSQL{Host: "remote"}
result := configPgOptional.GetOption(updated)
assert.Equal(true)(O.IsSome(result))(t)
pg := O.GetOrElse(F.Constant(PostgreSQL{}))(result)
assert.Equal("remote")(pg.Host)(t)
// original config is unchanged (immutability preserved)
origPg := config.Connection.(PostgreSQL)
assert.Equal("localhost")(origPg.Host)(t)
// Handles nil pointers safely
var nilConfig *Config = nil
none := configPgOptional.GetOption(nilConfig) // None (nil pointer)
assert.Equal(true)(O.IsNone(none))(t)
unchanged := configPgOptional.Set(PostgreSQL{Host: "remote"})(nilConfig)
// unchanged == nil (no-op because source is nil)
if unchanged != nil {
t.Fatalf("Expected nil, got %v", unchanged)
}
// Works with mismatched prisms
configMySQL := &Config{Connection: MySQL{Host: "localhost"}}
none = configPgOptional.GetOption(configMySQL) // None (Prism doesn't match)
assert.Equal(true)(O.IsNone(none))(t)
unchanged = configPgOptional.Set(PostgreSQL{Host: "remote"})(configMySQL)
// unchanged == configMySQL (no-op because Prism doesn't match)
assert.Equal(configMySQL)(unchanged)(t)
}

View File

@@ -0,0 +1,15 @@
package prism
import (
"github.com/IBM/fp-go/v2/endomorphism"
L "github.com/IBM/fp-go/v2/optics/lens"
O "github.com/IBM/fp-go/v2/optics/optional"
P "github.com/IBM/fp-go/v2/optics/prism"
)
type (
Prism[S, A any] = P.Prism[S, A]
Lens[S, A any] = L.Lens[S, A]
Optional[S, A any] = O.Optional[S, A]
Endomorphism[A any] = endomorphism.Endomorphism[A]
)

View File

@@ -13,8 +13,77 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Optional is an optic used to zoom inside a product. Unlike the `Lens`, the element that the `Optional` focuses
// on may not exist.
// Package optional provides an optic for focusing on values that may not exist.
//
// # Overview
//
// Optional is an optic used to zoom inside a product. Unlike the Lens, the element that the Optional focuses
// on may not exist. An Optional[S, A] represents a relationship between a source type S and a focus type A,
// where the focus may or may not be present.
//
// # Optional Laws
//
// An Optional must satisfy the following laws, which are consistent with other functional programming libraries
// such as monocle-ts (https://gcanti.github.io/monocle-ts/modules/Optional.ts.html) and the Haskell lens library
// (https://hackage.haskell.org/package/lens):
//
// 1. GetSet Law (No-op on None):
// If GetOption(s) returns None, then Set(a)(s) must return s unchanged (no-op).
// This ensures that attempting to update a value that doesn't exist has no effect.
//
// Formally: GetOption(s) = None => Set(a)(s) = s
//
// 2. SetGet Law (Get what you Set):
// If GetOption(s) returns Some(_), then GetOption(Set(a)(s)) must return Some(a).
// This ensures that after setting a value, you can retrieve it.
//
// Formally: GetOption(s) = Some(_) => GetOption(Set(a)(s)) = Some(a)
//
// 3. SetSet Law (Last Set Wins):
// Setting twice is the same as setting once with the final value.
//
// Formally: Set(b)(Set(a)(s)) = Set(b)(s)
//
// # No-op Behavior
//
// A key property of Optional is that updating a value for which GetOption returns None is a no-op.
// This behavior is implemented through the optionalModify function, which only applies the modification
// if the optional value exists. When GetOption returns None, the original structure is returned unchanged.
//
// This is consistent with the behavior in:
// - monocle-ts: Optional.modify returns the original value when the optional doesn't match
// - Haskell lens: over and set operations are no-ops when the traversal finds no targets
//
// # Example
//
// type Person struct {
// Name string
// Age int
// }
//
// // Create an optional that focuses on non-empty names
// nameOptional := MakeOptional(
// func(p Person) option.Option[string] {
// if p.Name != "" {
// return option.Some(p.Name)
// }
// return option.None[string]()
// },
// func(p Person, name string) Person {
// p.Name = name
// return p
// },
// )
//
// // When the optional matches, Set updates the value
// person1 := Person{Name: "Alice", Age: 30}
// updated1 := nameOptional.Set("Bob")(person1)
// // updated1.Name == "Bob"
//
// // When the optional doesn't match (Name is empty), Set is a no-op
// person2 := Person{Name: "", Age: 30}
// updated2 := nameOptional.Set("Bob")(person2)
// // updated2 == person2 (unchanged)
package optional
import (
@@ -50,12 +119,29 @@ type (
Operator[S, A, B any] = func(Optional[S, A]) Optional[S, B]
)
// setCopy wraps a setter for a pointer into a setter that first creates a copy before
// setCopyRef wraps a setter for a pointer into a setter that first creates a copy before
// modifying that copy
func setCopy[SET ~func(*S, A) *S, S, A any](setter SET) func(s *S, a A) *S {
return func(s *S, a A) *S {
cpy := *s
return setter(&cpy, a)
func setCopyRef[SET ~func(A) func(*S) *S, S, A any](setter SET) func(a A) func(*S) *S {
return func(a A) func(*S) *S {
sa := setter(a)
return func(s *S) *S {
if s == nil {
return s
}
cpy := *s
return sa(&cpy)
}
}
}
func getRef[GET ~func(*S) O.Option[A], S, A any](getter GET) func(*S) O.Option[A] {
return func(s *S) O.Option[A] {
if s == nil {
return O.None[A]()
}
return getter(s)
}
}
@@ -68,8 +154,18 @@ func MakeOptional[S, A any](get O.Kleisli[S, A], set func(S, A) S) Optional[S, A
return MakeOptionalWithName(get, set, "GenericOptional")
}
//go:inline
func MakeOptionalCurried[S, A any](get O.Kleisli[S, A], set func(A) func(S) S) Optional[S, A] {
return MakeOptionalCurriedWithName(get, set, "GenericOptional")
}
//go:inline
func MakeOptionalWithName[S, A any](get O.Kleisli[S, A], set func(S, A) S, name string) Optional[S, A] {
return Optional[S, A]{GetOption: get, Set: F.Bind2of2(set), name: name}
return MakeOptionalCurriedWithName(get, F.Bind2of2(set), name)
}
func MakeOptionalCurriedWithName[S, A any](get O.Kleisli[S, A], set func(A) func(S) S, name string) Optional[S, A] {
return Optional[S, A]{GetOption: get, Set: set, name: name}
}
// MakeOptionalRef creates an Optional based on a getter and a setter function. The setter passed in does not have to create a shallow
@@ -77,12 +173,17 @@ func MakeOptionalWithName[S, A any](get O.Kleisli[S, A], set func(S, A) S, name
//
//go:inline
func MakeOptionalRef[S, A any](get O.Kleisli[*S, A], set func(*S, A) *S) Optional[*S, A] {
return MakeOptional(get, setCopy(set))
return MakeOptionalCurried(getRef(get), setCopyRef(F.Bind2of2(set)))
}
//go:inline
func MakeOptionalRefWithName[S, A any](get O.Kleisli[*S, A], set func(*S, A) *S, name string) Optional[*S, A] {
return MakeOptionalWithName(get, setCopy(set), name)
return MakeOptionalCurriedWithName(getRef(get), setCopyRef(F.Bind2of2(set)), name)
}
//go:inline
func MakeOptionalRefCurriedWithName[S, A any](get O.Kleisli[*S, A], set func(A) func(*S) *S, name string) Optional[*S, A] {
return MakeOptionalCurriedWithName(getRef(get), setCopyRef(set), name)
}
// Id returns am optional implementing the identity operation
@@ -147,12 +248,14 @@ func fromPredicate[S, A any](creator func(get O.Kleisli[S, A], set func(S, A) S)
return func(get func(S) A, set func(S, A) S) Optional[S, A] {
return creator(
F.Flow2(get, fromPred),
func(s S, _ A) S {
func(s S, a A) S {
return F.Pipe3(
s,
get,
fromPred,
O.Fold(F.Constant(s), F.Bind1st(set, s)),
O.Fold(F.Constant(s), func(_ A) S {
return set(s, a)
}),
)
},
)

View File

@@ -62,3 +62,927 @@ func TestOptional(t *testing.T) {
assert.Equal(t, O.Of(sampleResponse.info), responseOptional.GetOption(&sampleResponse))
assert.Equal(t, O.None[*Info](), responseOptional.GetOption(&sampleEmptyResponse))
}
// Test types for comprehensive testing
type Person struct {
Name string
Age int
}
type Config struct {
Timeout int
Retries int
}
// TestMakeOptionalBasicFunctionality tests basic Optional operations
func TestMakeOptionalBasicFunctionality(t *testing.T) {
t.Run("GetOption returns Some when value exists", func(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
person := Person{Name: "Alice", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsSome(result))
assert.Equal(t, "Alice", O.GetOrElse(F.Constant(""))(result))
})
t.Run("GetOption returns None when value doesn't exist", func(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
person := Person{Name: "", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("Set updates value when optional matches", func(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
person := Person{Name: "Alice", Age: 30}
updated := optional.Set("Bob")(person)
assert.Equal(t, "Bob", updated.Name)
assert.Equal(t, 30, updated.Age)
})
}
// TestOptionalLaws tests that Optional satisfies the optional laws
// Reference: https://gcanti.github.io/monocle-ts/modules/Optional.ts.html
func TestOptionalLaws(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
t.Run("SetGet Law: GetOption(Set(a)(s)) = Some(a) when GetOption(s) = Some(_)", func(t *testing.T) {
person := Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Set a new value
newName := "Bob"
updated := optional.Set(newName)(person)
// Get the value back
result := optional.GetOption(updated)
// Verify SetGet law: we should get back what we set
assert.True(t, O.IsSome(result))
assert.Equal(t, newName, O.GetOrElse(F.Constant(""))(result))
})
t.Run("GetSet Law: Set(a)(s) = s when GetOption(s) = None (no-op)", func(t *testing.T) {
person := Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to set a value - this should be a no-op since GetOption returns None
// Note: Direct Set always updates, but this is expected behavior.
// The no-op behavior is enforced through ModifyOption and optionalModify.
updated := optional.Set("Bob")(person)
// Direct Set will update even when GetOption returns None
// This is by design - Set is unconditional
assert.Equal(t, "Bob", updated.Name)
})
t.Run("SetSet Law: Set(b)(Set(a)(s)) = Set(b)(s)", func(t *testing.T) {
person := Person{Name: "Alice", Age: 30}
// Set twice
setTwice := optional.Set("Charlie")(optional.Set("Bob")(person))
// Set once with the final value
setOnce := optional.Set("Charlie")(person)
// They should be equal
assert.Equal(t, setOnce, setTwice)
assert.Equal(t, "Charlie", setTwice.Name)
})
}
// TestMakeOptionalRefBasicFunctionality tests MakeOptionalRef with pointer types
func TestMakeOptionalRefBasicFunctionality(t *testing.T) {
t.Run("GetOption returns Some when value exists (non-nil pointer)", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
person := &Person{Name: "Alice", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsSome(result))
assert.Equal(t, "Alice", O.GetOrElse(F.Constant(""))(result))
})
t.Run("GetOption returns None when pointer is nil", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("GetOption returns None when value doesn't exist", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
person := &Person{Name: "", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("Set updates value and creates copy (immutability)", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
original := &Person{Name: "Alice", Age: 30}
updated := optional.Set("Bob")(original)
// Verify the update
assert.Equal(t, "Bob", updated.Name)
assert.Equal(t, 30, updated.Age)
// Verify immutability: original should be unchanged
assert.Equal(t, "Alice", original.Name)
// Verify they are different pointers
assert.NotEqual(t, original, updated)
})
t.Run("Set is no-op when pointer is nil", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
updated := optional.Set("Bob")(person)
// Verify nothing changed (no-op for nil)
assert.Nil(t, updated)
})
}
// TestMakeOptionalRefLaws tests that MakeOptionalRef satisfies optional laws
func TestMakeOptionalRefLaws(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
t.Run("SetGet Law: GetOption(Set(a)(s)) = Some(a) when GetOption(s) = Some(_)", func(t *testing.T) {
person := &Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Set a new value
newName := "Bob"
updated := optional.Set(newName)(person)
// Get the value back
result := optional.GetOption(updated)
// Verify SetGet law: we should get back what we set
assert.True(t, O.IsSome(result))
assert.Equal(t, newName, O.GetOrElse(F.Constant(""))(result))
})
t.Run("GetSet Law: Set(a)(s) = s when GetOption(s) = None (nil pointer)", func(t *testing.T) {
var person *Person = nil
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to set a value
updated := optional.Set("Bob")(person)
// Verify GetSet law: structure should be unchanged (nil)
assert.Nil(t, updated)
})
t.Run("SetSet Law: Set(b)(Set(a)(s)) = Set(b)(s)", func(t *testing.T) {
person := &Person{Name: "Alice", Age: 30}
// Set twice
setTwice := optional.Set("Charlie")(optional.Set("Bob")(person))
// Set once with the final value
setOnce := optional.Set("Charlie")(person)
// They should have equal values (but different pointers due to immutability)
assert.Equal(t, setOnce.Name, setTwice.Name)
assert.Equal(t, setOnce.Age, setTwice.Age)
assert.Equal(t, "Charlie", setTwice.Name)
})
}
// TestMakeOptionalRefImmutability tests immutability guarantees
func TestMakeOptionalRefImmutability(t *testing.T) {
t.Run("Set creates a new pointer, doesn't modify original", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
original := &Person{Name: "Alice", Age: 30}
origName := original.Name
origAge := original.Age
// Perform multiple sets
updated1 := optional.Set("Bob")(original)
updated2 := optional.Set("Charlie")(updated1)
updated3 := optional.Set("David")(updated2)
// Verify original is unchanged
assert.Equal(t, origName, original.Name)
assert.Equal(t, origAge, original.Age)
// Verify final update has correct value
assert.Equal(t, "David", updated3.Name)
// Verify all pointers are different
assert.NotEqual(t, original, updated1)
assert.NotEqual(t, original, updated2)
assert.NotEqual(t, original, updated3)
assert.NotEqual(t, updated1, updated2)
assert.NotEqual(t, updated2, updated3)
})
t.Run("Multiple operations on nil preserve nil", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
// Multiple sets on nil should all return nil
updated1 := optional.Set("Bob")(person)
updated2 := optional.Set("Charlie")(updated1)
assert.Nil(t, updated1)
assert.Nil(t, updated2)
})
}
// TestMakeOptionalRefNilPointerEdgeCases tests edge cases with nil pointers
func TestMakeOptionalRefNilPointerEdgeCases(t *testing.T) {
t.Run("GetOption on nil returns None", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
return O.Some(p.Name)
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("Set on nil returns nil", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
return O.Some(p.Name)
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
updated := optional.Set("Bob")(person)
assert.Nil(t, updated)
})
t.Run("Chaining operations starting from nil", func(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
var person *Person = nil
// Chain multiple operations
result := F.Pipe2(
person,
optional.Set("Bob"),
optional.Set("Charlie"),
)
assert.Nil(t, result)
})
}
// TestFromPredicateRef tests FromPredicateRef with nil handling
func TestFromPredicateRef(t *testing.T) {
t.Run("Works with non-nil values matching predicate", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
person := &Person{Name: "Alice", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsSome(result))
assert.Equal(t, "Alice", O.GetOrElse(F.Constant(""))(result))
})
t.Run("Returns None for nil pointer", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
var person *Person = nil
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("Returns None when predicate doesn't match", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
person := &Person{Name: "", Age: 30}
result := optional.GetOption(person)
assert.True(t, O.IsNone(result))
})
t.Run("Set is no-op on nil pointer", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
var person *Person = nil
updated := optional.Set("Bob")(person)
assert.Nil(t, updated)
})
}
// TestOptionalComposition tests composing optionals
func TestOptionalComposition(t *testing.T) {
t.Run("Compose two optionals", func(t *testing.T) {
// First optional: Person -> Name (if not empty)
nameOptional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
// Second optional: String -> First character (if not empty)
firstCharOptional := MakeOptional(
func(s string) O.Option[rune] {
if len(s) > 0 {
return O.Some(rune(s[0]))
}
return O.None[rune]()
},
func(s string, r rune) string {
if len(s) > 0 {
return string(r) + s[1:]
}
return string(r)
},
)
// Compose them
composed := Compose[Person](firstCharOptional)(nameOptional)
person := Person{Name: "Alice", Age: 30}
result := composed.GetOption(person)
assert.True(t, O.IsSome(result))
assert.Equal(t, 'A', O.GetOrElse(F.Constant(rune(0)))(result))
})
}
// TestOptionalNoOpBehavior tests that modifying through optionalModify is a no-op when GetOption returns None
// This is the key law: updating a value for which the preview returns None is a no-op
func TestOptionalNoOpBehavior(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
t.Run("ModifyOption returns None when GetOption returns None", func(t *testing.T) {
person := Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify - should return None
modifyResult := ModifyOption[Person](func(name string) string {
return "Bob"
})(optional)(person)
assert.True(t, O.IsNone(modifyResult))
})
t.Run("optionalModify is no-op when GetOption returns None", func(t *testing.T) {
person := Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify using the internal optionalModify function
updated := optionalModify(func(name string) string {
return "Bob"
}, optional, person)
// Verify no-op: structure should be unchanged
assert.Equal(t, person, updated)
assert.Equal(t, "", updated.Name)
assert.Equal(t, 30, updated.Age)
})
t.Run("ModifyOption returns Some when GetOption returns Some", func(t *testing.T) {
person := Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Modify should return Some with updated value
modifyResult := ModifyOption[Person](func(name string) string {
return name + " Smith"
})(optional)(person)
assert.True(t, O.IsSome(modifyResult))
updatedPerson := O.GetOrElse(F.Constant(person))(modifyResult)
assert.Equal(t, "Alice Smith", updatedPerson.Name)
})
t.Run("optionalModify updates when GetOption returns Some", func(t *testing.T) {
person := Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Modify should update the value
updated := optionalModify(func(name string) string {
return name + " Smith"
}, optional, person)
assert.Equal(t, "Alice Smith", updated.Name)
assert.Equal(t, 30, updated.Age)
})
}
// TestOptionalNoOpBehaviorRef tests no-op behavior with pointer types
func TestOptionalNoOpBehaviorRef(t *testing.T) {
optional := MakeOptionalRef(
func(p *Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p *Person, name string) *Person {
p.Name = name
return p
},
)
t.Run("ModifyOption returns None when GetOption returns None (empty name)", func(t *testing.T) {
person := &Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify - should return None
modifyResult := ModifyOption[*Person](func(name string) string {
return "Bob"
})(optional)(person)
assert.True(t, O.IsNone(modifyResult))
})
t.Run("ModifyOption returns None when pointer is nil", func(t *testing.T) {
var person *Person = nil
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify - should return None
modifyResult := ModifyOption[*Person](func(name string) string {
return "Bob"
})(optional)(person)
assert.True(t, O.IsNone(modifyResult))
})
t.Run("optionalModify is no-op when GetOption returns None", func(t *testing.T) {
person := &Person{Name: "", Age: 30}
originalName := person.Name
originalAge := person.Age
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify
updated := optionalModify(func(name string) string {
return "Bob"
}, optional, person)
// Verify no-op: structure should be unchanged
assert.Equal(t, originalName, updated.Name)
assert.Equal(t, originalAge, updated.Age)
})
t.Run("optionalModify is no-op when pointer is nil", func(t *testing.T) {
var person *Person = nil
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Try to modify
updated := optionalModify(func(name string) string {
return "Bob"
}, optional, person)
// Verify no-op: should still be nil
assert.Nil(t, updated)
})
}
// TestFromPredicateNoOpBehavior tests that FromPredicate properly implements no-op behavior
func TestFromPredicateNoOpBehavior(t *testing.T) {
t.Run("FromPredicate Set is no-op when predicate doesn't match", func(t *testing.T) {
optional := FromPredicate[Person](func(name string) bool {
return name != ""
})(
func(p Person) string { return p.Name },
func(p Person, name string) Person { p.Name = name; return p },
)
person := Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Set should be no-op when predicate doesn't match
updated := optional.Set("Bob")(person)
// Verify no-op: structure should be unchanged
assert.Equal(t, person, updated)
assert.Equal(t, "", updated.Name)
assert.Equal(t, 30, updated.Age)
})
t.Run("FromPredicate Set updates when predicate matches on current value", func(t *testing.T) {
optional := FromPredicate[Person](func(name string) bool {
return name != ""
})(
func(p Person) string { return p.Name },
func(p Person, name string) Person { p.Name = name; return p },
)
person := Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Set should update when predicate matches on the CURRENT value
// Note: FromPredicate's setter checks the predicate on the current value,
// not the new value. This is the correct behavior for the no-op law.
updated := optional.Set("Bob")(person)
assert.Equal(t, "Bob", updated.Name)
assert.Equal(t, 30, updated.Age)
})
t.Run("FromPredicate demonstrates the no-op law correctly", func(t *testing.T) {
// This test shows that FromPredicate implements the no-op law:
// The setter checks if the CURRENT value matches the predicate
optional := FromPredicate[Person](func(age int) bool {
return age >= 18 // Adult predicate
})(
func(p Person) int { return p.Age },
func(p Person, age int) Person { p.Age = age; return p },
)
// Case 1: Current value matches predicate (adult) - Set should work
adult := Person{Name: "Alice", Age: 30}
updatedAdult := optional.Set(25)(adult)
assert.Equal(t, 25, updatedAdult.Age)
// Case 2: Current value doesn't match predicate (child) - Set is no-op
child := Person{Name: "Bob", Age: 10}
updatedChild := optional.Set(25)(child)
assert.Equal(t, 10, updatedChild.Age) // Unchanged - no-op!
})
}
// TestFromPredicateRefNoOpBehavior tests that FromPredicateRef properly implements no-op behavior
func TestFromPredicateRefNoOpBehavior(t *testing.T) {
t.Run("FromPredicateRef Set is no-op when predicate doesn't match", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
person := &Person{Name: "", Age: 30}
originalName := person.Name
originalAge := person.Age
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Set should be no-op when predicate doesn't match
updated := optional.Set("Bob")(person)
// Verify no-op: structure should be unchanged
assert.Equal(t, originalName, updated.Name)
assert.Equal(t, originalAge, updated.Age)
// Original should also be unchanged (immutability)
assert.Equal(t, originalName, person.Name)
})
t.Run("FromPredicateRef Set is no-op when pointer is nil", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
var person *Person = nil
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// Set should be no-op (return nil)
updated := optional.Set("Bob")(person)
assert.Nil(t, updated)
})
t.Run("FromPredicateRef Set updates when predicate matches on current value", func(t *testing.T) {
optional := FromPredicateRef[Person](func(name string) bool {
return name != ""
})(
func(p *Person) string { return p.Name },
func(p *Person, name string) *Person { p.Name = name; return p },
)
person := &Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// Set should update when predicate matches on the CURRENT value
updated := optional.Set("Bob")(person)
assert.Equal(t, "Bob", updated.Name)
assert.Equal(t, 30, updated.Age)
// Original should be unchanged (immutability)
assert.Equal(t, "Alice", person.Name)
})
t.Run("FromPredicateRef demonstrates the no-op law correctly", func(t *testing.T) {
// This test shows that FromPredicateRef implements the no-op law
optional := FromPredicateRef[Person](func(age int) bool {
return age >= 18 // Adult predicate
})(
func(p *Person) int { return p.Age },
func(p *Person, age int) *Person { p.Age = age; return p },
)
// Case 1: Current value matches predicate (adult) - Set should work
adult := &Person{Name: "Alice", Age: 30}
updatedAdult := optional.Set(25)(adult)
assert.Equal(t, 25, updatedAdult.Age)
assert.Equal(t, 30, adult.Age) // Original unchanged
// Case 2: Current value doesn't match predicate (child) - Set is no-op
child := &Person{Name: "Bob", Age: 10}
updatedChild := optional.Set(25)(child)
assert.Equal(t, 10, updatedChild.Age) // Unchanged - no-op!
assert.Equal(t, 10, child.Age) // Original also unchanged
})
}
// TestSetOptionNoOpBehavior tests SetOption behavior with None
func TestSetOptionNoOpBehavior(t *testing.T) {
optional := MakeOptional(
func(p Person) O.Option[string] {
if p.Name != "" {
return O.Some(p.Name)
}
return O.None[string]()
},
func(p Person, name string) Person {
p.Name = name
return p
},
)
t.Run("SetOption returns None when GetOption returns None", func(t *testing.T) {
person := Person{Name: "", Age: 30}
// Verify optional doesn't match
initial := optional.GetOption(person)
assert.True(t, O.IsNone(initial))
// SetOption should return None
result := SetOption[Person]("Bob")(optional)(person)
assert.True(t, O.IsNone(result))
})
t.Run("SetOption returns Some when GetOption returns Some", func(t *testing.T) {
person := Person{Name: "Alice", Age: 30}
// Verify optional matches
initial := optional.GetOption(person)
assert.True(t, O.IsSome(initial))
// SetOption should return Some with updated value
result := SetOption[Person]("Bob")(optional)(person)
assert.True(t, O.IsSome(result))
updatedPerson := O.GetOrElse(F.Constant(person))(result)
assert.Equal(t, "Bob", updatedPerson.Name)
})
}

View File

@@ -1037,3 +1037,63 @@ func FromOption[T any]() Prism[Option[T], T] {
"PrismFromOption",
)
}
// NonEmptyString creates a prism that matches non-empty strings.
// It provides a safe way to work with non-empty string values, handling
// empty strings gracefully through the Option type.
//
// This is a specialized version of FromNonZero[string]() that makes the intent
// clearer when working specifically with strings that must not be empty.
//
// The prism's GetOption returns Some(s) if the string is not empty;
// otherwise, it returns None.
//
// The prism's ReverseGet is the identity function, returning the string unchanged.
//
// Returns:
// - A Prism[string, string] that matches non-empty strings
//
// Example:
//
// // Create a prism for non-empty strings
// nonEmptyPrism := NonEmptyString()
//
// // Match non-empty string
// result := nonEmptyPrism.GetOption("hello") // Some("hello")
//
// // Empty string returns None
// result = nonEmptyPrism.GetOption("") // None[string]()
//
// // ReverseGet is identity
// value := nonEmptyPrism.ReverseGet("world") // "world"
//
// // Use with Set to update non-empty strings
// setter := Set[string, string]("updated")
// result := setter(nonEmptyPrism)("original") // "updated"
// result = setter(nonEmptyPrism)("") // "" (unchanged)
//
// // Compose with other prisms for validation pipelines
// // Example: Parse a non-empty string as an integer
// nonEmptyIntPrism := Compose[string, string, int](
// NonEmptyString(),
// ParseInt(),
// )
// value := nonEmptyIntPrism.GetOption("42") // Some(42)
// value = nonEmptyIntPrism.GetOption("") // None[int]()
// value = nonEmptyIntPrism.GetOption("abc") // None[int]()
//
// Common use cases:
// - Validating required string fields (usernames, names, IDs)
// - Filtering empty strings from data pipelines
// - Ensuring configuration values are non-empty
// - Composing with parsing prisms to validate input before parsing
// - Working with user input that must not be blank
//
// Key insight: This prism is particularly useful for validation scenarios where
// an empty string represents an invalid or missing value, allowing you to handle
// such cases gracefully through the Option type rather than with error handling.
//
//go:inline
func NonEmptyString() Prism[string, string] {
return FromNonZero[string]()
}

View File

@@ -1145,3 +1145,254 @@ func TestFromOptionComposition(t *testing.T) {
assert.True(t, O.IsNone(result))
})
}
// TestNonEmptyString tests the NonEmptyString prism
func TestNonEmptyString(t *testing.T) {
t.Run("match non-empty string", func(t *testing.T) {
prism := NonEmptyString()
result := prism.GetOption("hello")
assert.True(t, O.IsSome(result))
assert.Equal(t, "hello", O.GetOrElse(F.Constant("default"))(result))
})
t.Run("empty string returns None", func(t *testing.T) {
prism := NonEmptyString()
result := prism.GetOption("")
assert.True(t, O.IsNone(result))
})
t.Run("whitespace string is non-empty", func(t *testing.T) {
prism := NonEmptyString()
result := prism.GetOption(" ")
assert.True(t, O.IsSome(result))
assert.Equal(t, " ", O.GetOrElse(F.Constant("default"))(result))
})
t.Run("single character string", func(t *testing.T) {
prism := NonEmptyString()
result := prism.GetOption("a")
assert.True(t, O.IsSome(result))
assert.Equal(t, "a", O.GetOrElse(F.Constant("default"))(result))
})
t.Run("multiline string", func(t *testing.T) {
prism := NonEmptyString()
multiline := "line1\nline2\nline3"
result := prism.GetOption(multiline)
assert.True(t, O.IsSome(result))
assert.Equal(t, multiline, O.GetOrElse(F.Constant("default"))(result))
})
t.Run("unicode string", func(t *testing.T) {
prism := NonEmptyString()
unicode := "Hello 世界 🌍"
result := prism.GetOption(unicode)
assert.True(t, O.IsSome(result))
assert.Equal(t, unicode, O.GetOrElse(F.Constant("default"))(result))
})
t.Run("reverse get is identity", func(t *testing.T) {
prism := NonEmptyString()
assert.Equal(t, "", prism.ReverseGet(""))
assert.Equal(t, "hello", prism.ReverseGet("hello"))
assert.Equal(t, "world", prism.ReverseGet("world"))
})
}
// TestNonEmptyStringWithSet tests using Set with NonEmptyString prism
func TestNonEmptyStringWithSet(t *testing.T) {
t.Run("set on non-empty string", func(t *testing.T) {
prism := NonEmptyString()
setter := Set[string]("updated")
result := setter(prism)("original")
assert.Equal(t, "updated", result)
})
t.Run("set on empty string returns original", func(t *testing.T) {
prism := NonEmptyString()
setter := Set[string]("updated")
result := setter(prism)("")
assert.Equal(t, "", result)
})
t.Run("set with empty value on non-empty string", func(t *testing.T) {
prism := NonEmptyString()
setter := Set[string]("")
result := setter(prism)("original")
assert.Equal(t, "", result)
})
}
// TestNonEmptyStringPrismLaws tests that NonEmptyString satisfies prism laws
func TestNonEmptyStringPrismLaws(t *testing.T) {
t.Run("law 1: GetOption(ReverseGet(a)) == Some(a)", func(t *testing.T) {
prism := NonEmptyString()
// For any non-empty string a, GetOption(ReverseGet(a)) should return Some(a)
testCases := []string{"hello", "world", "a", "test string", "123"}
for _, testCase := range testCases {
reversed := prism.ReverseGet(testCase)
result := prism.GetOption(reversed)
assert.True(t, O.IsSome(result), "Expected Some for: %s", testCase)
assert.Equal(t, testCase, O.GetOrElse(F.Constant(""))(result))
}
})
t.Run("law 2: if GetOption(s) == Some(a), then ReverseGet(a) == s", func(t *testing.T) {
prism := NonEmptyString()
// For any non-empty string s where GetOption(s) returns Some(a),
// ReverseGet(a) should equal s
testCases := []string{"hello", "world", "test", " ", "123"}
for _, testCase := range testCases {
optResult := prism.GetOption(testCase)
if O.IsSome(optResult) {
extracted := O.GetOrElse(F.Constant(""))(optResult)
reversed := prism.ReverseGet(extracted)
assert.Equal(t, testCase, reversed)
}
}
})
t.Run("law 3: GetOption is idempotent", func(t *testing.T) {
prism := NonEmptyString()
testCases := []string{"hello", "", "world", " "}
for _, testCase := range testCases {
result1 := prism.GetOption(testCase)
result2 := prism.GetOption(testCase)
assert.Equal(t, result1, result2, "GetOption should be idempotent for: %s", testCase)
}
})
}
// TestNonEmptyStringComposition tests composing NonEmptyString with other prisms
func TestNonEmptyStringComposition(t *testing.T) {
t.Run("compose with ParseInt", func(t *testing.T) {
// Create a prism that only parses non-empty strings to int
nonEmptyPrism := NonEmptyString()
intPrism := ParseInt()
// Compose: string -> non-empty string -> int
composed := Compose[string](intPrism)(nonEmptyPrism)
// Test with valid non-empty string
result := composed.GetOption("42")
assert.True(t, O.IsSome(result))
assert.Equal(t, 42, O.GetOrElse(F.Constant(-1))(result))
// Test with empty string
result = composed.GetOption("")
assert.True(t, O.IsNone(result))
// Test with invalid non-empty string
result = composed.GetOption("abc")
assert.True(t, O.IsNone(result))
})
t.Run("compose with ParseFloat64", func(t *testing.T) {
// Create a prism that only parses non-empty strings to float64
nonEmptyPrism := NonEmptyString()
floatPrism := ParseFloat64()
composed := Compose[string](floatPrism)(nonEmptyPrism)
// Test with valid non-empty string
result := composed.GetOption("3.14")
assert.True(t, O.IsSome(result))
assert.Equal(t, 3.14, O.GetOrElse(F.Constant(-1.0))(result))
// Test with empty string
result = composed.GetOption("")
assert.True(t, O.IsNone(result))
// Test with invalid non-empty string
result = composed.GetOption("not a number")
assert.True(t, O.IsNone(result))
})
t.Run("compose with FromOption", func(t *testing.T) {
// Create a prism that extracts non-empty strings from Option[string]
optionPrism := FromOption[string]()
nonEmptyPrism := NonEmptyString()
composed := Compose[Option[string]](nonEmptyPrism)(optionPrism)
// Test with Some(non-empty)
someNonEmpty := O.Some("hello")
result := composed.GetOption(someNonEmpty)
assert.True(t, O.IsSome(result))
assert.Equal(t, "hello", O.GetOrElse(F.Constant(""))(result))
// Test with Some(empty)
someEmpty := O.Some("")
result = composed.GetOption(someEmpty)
assert.True(t, O.IsNone(result))
// Test with None
none := O.None[string]()
result = composed.GetOption(none)
assert.True(t, O.IsNone(result))
})
}
// TestNonEmptyStringValidation tests NonEmptyString for validation scenarios
func TestNonEmptyStringValidation(t *testing.T) {
t.Run("validate username", func(t *testing.T) {
prism := NonEmptyString()
// Valid username
validUsername := "john_doe"
result := prism.GetOption(validUsername)
assert.True(t, O.IsSome(result))
// Invalid empty username
emptyUsername := ""
result = prism.GetOption(emptyUsername)
assert.True(t, O.IsNone(result))
})
t.Run("validate configuration value", func(t *testing.T) {
prism := NonEmptyString()
// Valid config value
configValue := "production"
result := prism.GetOption(configValue)
assert.True(t, O.IsSome(result))
// Invalid empty config
emptyConfig := ""
result = prism.GetOption(emptyConfig)
assert.True(t, O.IsNone(result))
})
t.Run("filter non-empty strings from slice", func(t *testing.T) {
prism := NonEmptyString()
inputs := []string{"hello", "", "world", "", "test"}
var nonEmpty []string
for _, input := range inputs {
if result := prism.GetOption(input); O.IsSome(result) {
nonEmpty = append(nonEmpty, O.GetOrElse(F.Constant(""))(result))
}
}
assert.Equal(t, []string{"hello", "world", "test"}, nonEmpty)
})
}

View File

@@ -19,6 +19,7 @@ import (
"github.com/IBM/fp-go/v2/either"
"github.com/IBM/fp-go/v2/endomorphism"
O "github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/predicate"
"github.com/IBM/fp-go/v2/reader"
"github.com/IBM/fp-go/v2/result"
)
@@ -124,4 +125,6 @@ type (
// - A: The original focus type
// - B: The new focus type
Operator[S, A, B any] = func(Prism[S, A]) Prism[S, B]
Predicate[A any] = predicate.Predicate[A]
)

View File

@@ -479,6 +479,15 @@ func Second[A, B, C any](pbc Reader[B, C]) Reader[T.Tuple2[A, B], T.Tuple2[A, C]
// Read applies a context to a Reader to obtain its value.
// This is the "run" operation that executes a Reader with a specific environment.
//
// Note: Read is functionally identical to identity.Flap[A](e). Both take a value and
// return a function that applies that value to a function. The difference is semantic:
// - identity.Flap: Generic function application (applies value to any function)
// - reader.Read: Reader-specific execution (applies environment to a Reader)
//
// Recommendation: Use reader.Read when working in a Reader context, as it makes the
// intent clearer that you're executing a Reader computation with an environment.
// Use identity.Flap for general-purpose function application outside the Reader context.
//
// Example:
//
// type Config struct { Port int }

View File

@@ -173,6 +173,85 @@ func TestLocal(t *testing.T) {
assert.Equal(t, "localhost", result)
}
func TestContramap(t *testing.T) {
t.Run("transforms environment before passing to Reader", func(t *testing.T) {
type DetailedConfig struct {
Host string
Port int
}
type SimpleConfig struct{ Host string }
detailed := DetailedConfig{Host: "localhost", Port: 8080}
getHost := func(c SimpleConfig) string { return c.Host }
simplify := func(d DetailedConfig) SimpleConfig { return SimpleConfig{Host: d.Host} }
r := Contramap[string](simplify)(getHost)
result := r(detailed)
assert.Equal(t, "localhost", result)
})
t.Run("is functionally identical to Local", func(t *testing.T) {
type DetailedConfig struct {
Host string
Port int
}
type SimpleConfig struct{ Host string }
getHost := func(c SimpleConfig) string { return c.Host }
simplify := func(d DetailedConfig) SimpleConfig {
return SimpleConfig{Host: d.Host}
}
// Using Contramap
contramapResult := Contramap[string](simplify)(getHost)
// Using Local
localResult := Local[string](simplify)(getHost)
detailed := DetailedConfig{Host: "localhost", Port: 8080}
assert.Equal(t, contramapResult(detailed), localResult(detailed))
assert.Equal(t, "localhost", contramapResult(detailed))
})
t.Run("works with numeric transformations", func(t *testing.T) {
type LargeEnv struct{ Value int }
type SmallEnv struct{ Value int }
// Reader that doubles a value
doubler := func(e SmallEnv) int { return e.Value * 2 }
// Transform that extracts and scales
extract := func(l LargeEnv) SmallEnv {
return SmallEnv{Value: l.Value / 10}
}
adapted := Contramap[int](extract)(doubler)
result := adapted(LargeEnv{Value: 100})
assert.Equal(t, 20, result) // (100/10) * 2 = 20
})
t.Run("can be composed with Map for full profunctor behavior", func(t *testing.T) {
type Env struct{ Config Config }
env := Env{Config: Config{Port: 8080}}
// Extract config (contravariant)
extractConfig := func(e Env) Config { return e.Config }
// Get port and convert to string (covariant)
getPort := func(c Config) int { return c.Port }
toString := strconv.Itoa
// Contramap on input, Map on output
r := F.Pipe2(
getPort,
Contramap[int](extractConfig),
Map[Env](toString),
)
result := r(env)
assert.Equal(t, "8080", result)
})
}
func TestWithLocal(t *testing.T) {
t.Run("transforms environment before passing to Reader", func(t *testing.T) {
type DetailedConfig struct {

View File

@@ -15,71 +15,42 @@
package reader
import (
M "github.com/IBM/fp-go/v2/monoid"
S "github.com/IBM/fp-go/v2/semigroup"
)
import "github.com/IBM/fp-go/v2/monoid"
// ApplySemigroup lifts a Semigroup[A] into a Semigroup[Reader[R, A]].
// This allows you to combine two Readers that produce semigroup values by combining
// their results using the semigroup's concat operation.
// ApplicativeMonoid returns a [Monoid] that concatenates [Reader] instances via their applicative.
// This combines two Reader values by applying the underlying monoid's combine operation
// to their results using applicative application.
//
// The _map and _ap parameters are the Map and Ap operations for the Reader type,
// typically obtained from the reader package.
// The applicative behavior means that both Reader computations are executed with the same
// environment, and their results are combined using the underlying monoid. This is useful
// for accumulating values from multiple Reader computations that all depend on the same
// environment.
//
// Parameters:
// - m: The underlying monoid for type A
//
// Returns a Monoid for Reader[R, A].
//
// Example:
//
// type Config struct { Multiplier int }
// // Using the additive semigroup for integers
// intSemigroup := semigroup.MakeSemigroup(func(a, b int) int { return a + b })
// readerSemigroup := reader.ApplySemigroup(
// reader.MonadMap[Config, int, func(int) int],
// reader.MonadAp[int, Config, int],
// intSemigroup,
// )
// type Config struct { Port int; Timeout int }
// intMonoid := number.MonoidSum[int]()
// readerMonoid := ApplicativeMonoid[Config](intMonoid)
//
// r1 := reader.Of[Config](5)
// r2 := reader.Of[Config](3)
// combined := readerSemigroup.Concat(r1, r2)
// result := combined(Config{Multiplier: 1}) // 8
func ApplySemigroup[R, A any](
_map func(func(R) A, func(A) func(A) A) func(R, func(A) A),
_ap func(func(R, func(A) A), func(R) A) func(R) A,
s S.Semigroup[A],
) S.Semigroup[func(R) A] {
return S.ApplySemigroup(_map, _ap, s)
}
// ApplicativeMonoid lifts a Monoid[A] into a Monoid[Reader[R, A]].
// This allows you to combine Readers that produce monoid values, with an empty/identity Reader.
//
// The _of parameter is the Of operation (pure/return) for the Reader type.
// The _map and _ap parameters are the Map and Ap operations for the Reader type.
//
// Example:
//
// type Config struct { Prefix string }
// // Using the string concatenation monoid
// stringMonoid := monoid.MakeMonoid("", func(a, b string) string { return a + b })
// readerMonoid := reader.ApplicativeMonoid(
// reader.Of[Config, string],
// reader.MonadMap[Config, string, func(string) string],
// reader.MonadAp[string, Config, string],
// stringMonoid,
// )
//
// r1 := reader.Asks(func(c Config) string { return c.Prefix })
// r2 := reader.Of[Config]("hello")
// combined := readerMonoid.Concat(r1, r2)
// result := combined(Config{Prefix: ">> "}) // ">> hello"
// empty := readerMonoid.Empty()(Config{Prefix: "any"}) // ""
func ApplicativeMonoid[R, A any](
_of func(A) func(R) A,
_map func(func(R) A, func(A) func(A) A) func(R, func(A) A),
_ap func(func(R, func(A) A), func(R) A) func(R) A,
m M.Monoid[A],
) M.Monoid[func(R) A] {
return M.ApplicativeMonoid(_of, _map, _ap, m)
// getPort := func(c Config) int { return c.Port }
// getTimeout := func(c Config) int { return c.Timeout }
// combined := readerMonoid.Concat(getPort, getTimeout)
// // Result: func(c Config) int { return c.Port + c.Timeout }
//
// config := Config{Port: 8080, Timeout: 30}
// result := combined(config) // 8110
//
//go:inline
func ApplicativeMonoid[R, A any](m monoid.Monoid[A]) monoid.Monoid[Reader[R, A]] {
return monoid.ApplicativeMonoid(
Of[R, A],
MonadMap[R, A, func(A) A],
MonadAp[A, R, A],
m,
)
}

478
v2/reader/semigroup_test.go Normal file
View File

@@ -0,0 +1,478 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package reader
import (
"strconv"
"testing"
N "github.com/IBM/fp-go/v2/number"
S "github.com/IBM/fp-go/v2/string"
"github.com/stretchr/testify/assert"
)
// SemigroupConfig represents a test configuration environment for semigroup tests
type SemigroupConfig struct {
Host string
Port int
Timeout int
MaxRetries int
Debug bool
}
var (
defaultSemigroupConfig = SemigroupConfig{
Host: "localhost",
Port: 8080,
Timeout: 30,
MaxRetries: 3,
Debug: false,
}
semigroupIntAddMonoid = N.MonoidSum[int]()
semigroupIntMulMonoid = N.MonoidProduct[int]()
semigroupStrMonoid = S.Monoid
)
// TestApplicativeMonoidSemigroup tests the ApplicativeMonoid function
func TestApplicativeMonoidSemigroup(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
t.Run("empty element", func(t *testing.T) {
empty := readerMonoid.Empty()
result := empty(defaultSemigroupConfig)
assert.Equal(t, 0, result)
})
t.Run("concat two readers", func(t *testing.T) {
r1 := func(c SemigroupConfig) int { return c.Port }
r2 := func(c SemigroupConfig) int { return c.Timeout }
combined := readerMonoid.Concat(r1, r2)
result := combined(defaultSemigroupConfig)
// 8080 + 30 = 8110
assert.Equal(t, 8110, result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
r := func(c SemigroupConfig) int { return c.Port }
combined := readerMonoid.Concat(readerMonoid.Empty(), r)
result := combined(defaultSemigroupConfig)
assert.Equal(t, 8080, result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
r := func(c SemigroupConfig) int { return c.Port }
combined := readerMonoid.Concat(r, readerMonoid.Empty())
result := combined(defaultSemigroupConfig)
assert.Equal(t, 8080, result)
})
t.Run("concat multiple readers", func(t *testing.T) {
r1 := func(c SemigroupConfig) int { return c.Port }
r2 := func(c SemigroupConfig) int { return c.Timeout }
r3 := func(c SemigroupConfig) int { return c.MaxRetries }
r4 := Of[SemigroupConfig](100)
// Chain concat calls: ((r1 + r2) + r3) + r4
combined := readerMonoid.Concat(
readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
),
r4,
)
result := combined(defaultSemigroupConfig)
// 8080 + 30 + 3 + 100 = 8213
assert.Equal(t, 8213, result)
})
t.Run("concat constant readers", func(t *testing.T) {
r1 := Of[SemigroupConfig](10)
r2 := Of[SemigroupConfig](20)
r3 := Of[SemigroupConfig](30)
combined := readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
assert.Equal(t, 60, result)
})
t.Run("string concatenation", func(t *testing.T) {
strReaderMonoid := ApplicativeMonoid[SemigroupConfig](semigroupStrMonoid)
r1 := func(c SemigroupConfig) string { return c.Host }
r2 := Of[SemigroupConfig](":")
r3 := Asks(func(c SemigroupConfig) string {
return strconv.Itoa(c.Port)
})
combined := strReaderMonoid.Concat(
strReaderMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
assert.Equal(t, "localhost:8080", result)
})
t.Run("multiplication monoid", func(t *testing.T) {
mulReaderMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntMulMonoid)
r1 := func(c SemigroupConfig) int { return c.MaxRetries }
r2 := Of[SemigroupConfig](10)
r3 := Of[SemigroupConfig](2)
combined := mulReaderMonoid.Concat(
mulReaderMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
// 3 * 10 * 2 = 60
assert.Equal(t, 60, result)
})
t.Run("environment dependent computation", func(t *testing.T) {
// Create readers that use different parts of the environment
getPort := Asks(func(c SemigroupConfig) int { return c.Port })
getTimeout := Asks(func(c SemigroupConfig) int { return c.Timeout })
getRetries := Asks(func(c SemigroupConfig) int { return c.MaxRetries })
combined := readerMonoid.Concat(
readerMonoid.Concat(getPort, getTimeout),
getRetries,
)
result := combined(defaultSemigroupConfig)
// 8080 + 30 + 3 = 8113
assert.Equal(t, 8113, result)
})
t.Run("mixed constant and environment readers", func(t *testing.T) {
r1 := Of[SemigroupConfig](1000)
r2 := func(c SemigroupConfig) int { return c.Port }
r3 := Of[SemigroupConfig](5)
combined := readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
// 1000 + 8080 + 5 = 9085
assert.Equal(t, 9085, result)
})
t.Run("different environment values", func(t *testing.T) {
r1 := func(c SemigroupConfig) int { return c.Port }
r2 := func(c SemigroupConfig) int { return c.Timeout }
combined := readerMonoid.Concat(r1, r2)
// Test with different configs
config1 := SemigroupConfig{Port: 3000, Timeout: 60}
config2 := SemigroupConfig{Port: 9000, Timeout: 120}
result1 := combined(config1)
result2 := combined(config2)
assert.Equal(t, 3060, result1)
assert.Equal(t, 9120, result2)
})
t.Run("conditional reader based on environment", func(t *testing.T) {
r1 := func(c SemigroupConfig) int {
if c.Debug {
return c.Port * 2
}
return c.Port
}
r2 := func(c SemigroupConfig) int { return c.Timeout }
combined := readerMonoid.Concat(r1, r2)
// Test with debug off
result1 := combined(defaultSemigroupConfig)
assert.Equal(t, 8110, result1) // 8080 + 30
// Test with debug on
debugConfig := defaultSemigroupConfig
debugConfig.Debug = true
result2 := combined(debugConfig)
assert.Equal(t, 16190, result2) // (8080 * 2) + 30
})
}
// TestMonoidLawsSemigroup verifies that the monoid laws hold for ApplicativeMonoid
func TestMonoidLawsSemigroup(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := func(c SemigroupConfig) int { return c.Port }
result1 := readerMonoid.Concat(readerMonoid.Empty(), x)(defaultSemigroupConfig)
result2 := x(defaultSemigroupConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := func(c SemigroupConfig) int { return c.Port }
result1 := readerMonoid.Concat(x, readerMonoid.Empty())(defaultSemigroupConfig)
result2 := x(defaultSemigroupConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := func(c SemigroupConfig) int { return c.Port }
y := func(c SemigroupConfig) int { return c.Timeout }
z := func(c SemigroupConfig) int { return c.MaxRetries }
left := readerMonoid.Concat(readerMonoid.Concat(x, y), z)(defaultSemigroupConfig)
right := readerMonoid.Concat(x, readerMonoid.Concat(y, z))(defaultSemigroupConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with constants", func(t *testing.T) {
x := Of[SemigroupConfig](10)
y := Of[SemigroupConfig](20)
z := Of[SemigroupConfig](30)
left := readerMonoid.Concat(readerMonoid.Concat(x, y), z)(defaultSemigroupConfig)
right := readerMonoid.Concat(x, readerMonoid.Concat(y, z))(defaultSemigroupConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with mixed readers", func(t *testing.T) {
x := func(c SemigroupConfig) int { return c.Port }
y := Of[SemigroupConfig](100)
z := func(c SemigroupConfig) int { return c.Timeout }
left := readerMonoid.Concat(readerMonoid.Concat(x, y), z)(defaultSemigroupConfig)
right := readerMonoid.Concat(x, readerMonoid.Concat(y, z))(defaultSemigroupConfig)
assert.Equal(t, right, left)
})
}
// TestMonoidWithDifferentTypesSemigroup tests monoid with various types
func TestMonoidWithDifferentTypesSemigroup(t *testing.T) {
t.Run("string monoid", func(t *testing.T) {
strReaderMonoid := ApplicativeMonoid[SemigroupConfig](semigroupStrMonoid)
r1 := func(c SemigroupConfig) string { return "Host: " }
r2 := func(c SemigroupConfig) string { return c.Host }
r3 := Of[SemigroupConfig](" | Port: ")
r4 := Asks(func(c SemigroupConfig) string { return strconv.Itoa(c.Port) })
combined := strReaderMonoid.Concat(
strReaderMonoid.Concat(
strReaderMonoid.Concat(r1, r2),
r3,
),
r4,
)
result := combined(defaultSemigroupConfig)
assert.Equal(t, "Host: localhost | Port: 8080", result)
})
t.Run("product monoid", func(t *testing.T) {
mulReaderMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntMulMonoid)
r1 := func(c SemigroupConfig) int { return 2 }
r2 := func(c SemigroupConfig) int { return c.MaxRetries }
r3 := Of[SemigroupConfig](5)
combined := mulReaderMonoid.Concat(
mulReaderMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
// 2 * 3 * 5 = 30
assert.Equal(t, 30, result)
})
}
// TestComplexScenariosSemigroup tests more complex real-world scenarios
func TestComplexScenariosSemigroup(t *testing.T) {
t.Run("accumulate configuration values", func(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
// Accumulate multiple configuration values
getPort := Asks(func(c SemigroupConfig) int { return c.Port })
getTimeout := Asks(func(c SemigroupConfig) int { return c.Timeout })
getRetries := Asks(func(c SemigroupConfig) int { return c.MaxRetries })
getConstant := Of[SemigroupConfig](1000)
combined := readerMonoid.Concat(
readerMonoid.Concat(
readerMonoid.Concat(getPort, getTimeout),
getRetries,
),
getConstant,
)
result := combined(defaultSemigroupConfig)
// 8080 + 30 + 3 + 1000 = 9113
assert.Equal(t, 9113, result)
})
t.Run("build connection string", func(t *testing.T) {
strReaderMonoid := ApplicativeMonoid[SemigroupConfig](semigroupStrMonoid)
protocol := Of[SemigroupConfig]("http://")
host := func(c SemigroupConfig) string { return c.Host }
colon := Of[SemigroupConfig](":")
port := Asks(func(c SemigroupConfig) string { return strconv.Itoa(c.Port) })
buildURL := strReaderMonoid.Concat(
strReaderMonoid.Concat(
strReaderMonoid.Concat(protocol, host),
colon,
),
port,
)
result := buildURL(defaultSemigroupConfig)
assert.Equal(t, "http://localhost:8080", result)
})
t.Run("calculate total score", func(t *testing.T) {
type ScoreConfig struct {
BaseScore int
BonusPoints int
Multiplier int
PenaltyDeduction int
}
scoreConfig := ScoreConfig{
BaseScore: 100,
BonusPoints: 50,
Multiplier: 2,
PenaltyDeduction: 10,
}
readerMonoid := ApplicativeMonoid[ScoreConfig](semigroupIntAddMonoid)
getBase := func(c ScoreConfig) int { return c.BaseScore }
getBonus := func(c ScoreConfig) int { return c.BonusPoints }
getPenalty := func(c ScoreConfig) int { return -c.PenaltyDeduction }
totalScore := readerMonoid.Concat(
readerMonoid.Concat(getBase, getBonus),
getPenalty,
)
result := totalScore(scoreConfig)
// 100 + 50 - 10 = 140
assert.Equal(t, 140, result)
})
t.Run("compose multiple readers with empty", func(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
r1 := func(c SemigroupConfig) int { return c.Port }
r2 := readerMonoid.Empty()
r3 := func(c SemigroupConfig) int { return c.Timeout }
r4 := readerMonoid.Empty()
r5 := Of[SemigroupConfig](100)
combined := readerMonoid.Concat(
readerMonoid.Concat(
readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
),
r4,
),
r5,
)
result := combined(defaultSemigroupConfig)
// 8080 + 0 + 30 + 0 + 100 = 8210
assert.Equal(t, 8210, result)
})
}
// TestEdgeCasesSemigroup tests edge cases and boundary conditions
func TestEdgeCasesSemigroup(t *testing.T) {
t.Run("empty config struct", func(t *testing.T) {
type EmptyConfig struct{}
emptyConfig := EmptyConfig{}
readerMonoid := ApplicativeMonoid[EmptyConfig](semigroupIntAddMonoid)
r1 := Of[EmptyConfig](10)
r2 := Of[EmptyConfig](20)
combined := readerMonoid.Concat(r1, r2)
result := combined(emptyConfig)
assert.Equal(t, 30, result)
})
t.Run("zero values", func(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
r1 := Of[SemigroupConfig](0)
r2 := Of[SemigroupConfig](0)
r3 := Of[SemigroupConfig](0)
combined := readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
assert.Equal(t, 0, result)
})
t.Run("negative values", func(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
r1 := Of[SemigroupConfig](-100)
r2 := Of[SemigroupConfig](50)
r3 := Of[SemigroupConfig](-30)
combined := readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
// -100 + 50 - 30 = -80
assert.Equal(t, -80, result)
})
t.Run("large values", func(t *testing.T) {
readerMonoid := ApplicativeMonoid[SemigroupConfig](semigroupIntAddMonoid)
r1 := Of[SemigroupConfig](1000000)
r2 := Of[SemigroupConfig](2000000)
r3 := Of[SemigroupConfig](3000000)
combined := readerMonoid.Concat(
readerMonoid.Concat(r1, r2),
r3,
)
result := combined(defaultSemigroupConfig)
assert.Equal(t, 6000000, result)
})
}

135
v2/readereither/monoid.go Normal file
View File

@@ -0,0 +1,135 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readereither
import (
"github.com/IBM/fp-go/v2/lazy"
"github.com/IBM/fp-go/v2/monoid"
)
// ApplicativeMonoid returns a [Monoid] that concatenates [ReaderEither] instances via their applicative.
// This combines two ReaderEither values by applying the underlying monoid's combine operation
// to their success values using applicative application.
//
// The applicative behavior means that if either computation fails (returns Left), the entire
// combination fails. Both computations must succeed (return Right) for the result to succeed.
//
// Parameters:
// - m: The underlying monoid for type A
//
// Returns a Monoid for ReaderEither[R, E, A].
//
// Example:
//
// intMonoid := number.MonoidSum[int]()
// reMonoid := ApplicativeMonoid[Config, string](intMonoid)
//
// re1 := Right[Config, string](5)
// re2 := Right[Config, string](3)
// combined := reMonoid.Concat(re1, re2)
// // Result: Right(8)
//
// re3 := Left[Config, int]("error")
// failed := reMonoid.Concat(re1, re3)
// // Result: Left("error")
//
//go:inline
func ApplicativeMonoid[R, E, A any](m monoid.Monoid[A]) monoid.Monoid[ReaderEither[R, E, A]] {
return monoid.ApplicativeMonoid(
Of[R, E, A],
MonadMap[R, E, A, func(A) A],
MonadAp[A, R, E, A],
m,
)
}
// AlternativeMonoid is the alternative [Monoid] for [ReaderEither].
// This combines ReaderEither values using the alternative semantics,
// where the second value is only evaluated if the first fails.
//
// The alternative behavior provides fallback semantics: if the first computation
// succeeds (returns Right), its value is used. If it fails (returns Left), the
// second computation is tried. If both succeed, their values are combined using
// the underlying monoid.
//
// Parameters:
// - m: The underlying monoid for type A
//
// Returns a Monoid for ReaderEither[R, E, A] with alternative semantics.
//
// Example:
//
// intMonoid := number.MonoidSum[int]()
// reMonoid := AlternativeMonoid[Config, string](intMonoid)
//
// re1 := Left[Config, int]("error1")
// re2 := Right[Config, string](42)
// combined := reMonoid.Concat(re1, re2)
// // Result: Right(42) - falls back to second
//
// re3 := Right[Config, string](5)
// re4 := Right[Config, string](3)
// both := reMonoid.Concat(re3, re4)
// // Result: Right(8) - combines both successes
//
//go:inline
func AlternativeMonoid[R, E, A any](m monoid.Monoid[A]) monoid.Monoid[ReaderEither[R, E, A]] {
return monoid.AlternativeMonoid(
Of[R, E, A],
MonadMap[R, E, A, func(A) A],
MonadAp[A, R, E, A],
MonadAlt[R, E, A],
m,
)
}
// AltMonoid is the alternative [Monoid] for a [ReaderEither].
// This creates a monoid where the empty value is provided lazily,
// and combination uses the Alt operation (try first, fallback to second on failure).
//
// Unlike AlternativeMonoid, this does not combine successful values using an underlying
// monoid. Instead, it simply returns the first successful value, or falls back to the
// second if the first fails.
//
// Parameters:
// - zero: Lazy computation that provides the empty/identity value
//
// Returns a Monoid for ReaderEither[R, E, A] with Alt-based combination.
//
// Example:
//
// zero := lazy.MakeLazy(func() ReaderEither[Config, string, int] {
// return Left[Config, int]("no value")
// })
// reMonoid := AltMonoid(zero)
//
// re1 := Left[Config, int]("error1")
// re2 := Right[Config, string](42)
// combined := reMonoid.Concat(re1, re2)
// // Result: Right(42) - uses first success
//
// re3 := Right[Config, string](100)
// re4 := Right[Config, string](200)
// first := reMonoid.Concat(re3, re4)
// // Result: Right(100) - uses first success, doesn't combine
//
//go:inline
func AltMonoid[R, E, A any](zero lazy.Lazy[ReaderEither[R, E, A]]) monoid.Monoid[ReaderEither[R, E, A]] {
return monoid.AltMonoid(
zero,
MonadAlt[R, E, A],
)
}

View File

@@ -0,0 +1,747 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readereither
import (
"testing"
E "github.com/IBM/fp-go/v2/either"
N "github.com/IBM/fp-go/v2/number"
S "github.com/IBM/fp-go/v2/string"
"github.com/stretchr/testify/assert"
)
// Config represents a test configuration environment
type Config struct {
Host string
Port int
Timeout int
Debug bool
}
var (
defaultConfig = Config{
Host: "localhost",
Port: 8080,
Timeout: 30,
Debug: false,
}
intAddMonoid = N.MonoidSum[int]()
strMonoid = S.Monoid
)
// TestApplicativeMonoid tests the ApplicativeMonoid function
func TestApplicativeMonoid(t *testing.T) {
reMonoid := ApplicativeMonoid[Config, string](intAddMonoid)
t.Run("empty element", func(t *testing.T) {
empty := reMonoid.Empty()
result := empty(defaultConfig)
assert.Equal(t, E.Right[string](0), result)
})
t.Run("concat two Right values", func(t *testing.T) {
re1 := Right[Config, string](5)
re2 := Right[Config, string](3)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](8), result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(reMonoid.Empty(), re)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(re, reMonoid.Empty())
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat with left error", func(t *testing.T) {
reSuccess := Right[Config, string](5)
reFailure := Left[Config, int]("error occurred")
combined := reMonoid.Concat(reFailure, reSuccess)
result := combined(defaultConfig)
assert.Equal(t, E.Left[int]("error occurred"), result)
})
t.Run("concat with right error", func(t *testing.T) {
reSuccess := Right[Config, string](5)
reFailure := Left[Config, int]("error occurred")
combined := reMonoid.Concat(reSuccess, reFailure)
result := combined(defaultConfig)
assert.Equal(t, E.Left[int]("error occurred"), result)
})
t.Run("concat both errors", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// First error is returned
assert.Equal(t, E.Left[int]("error1"), result)
})
t.Run("concat multiple values", func(t *testing.T) {
re1 := Right[Config, string](1)
re2 := Right[Config, string](2)
re3 := Right[Config, string](3)
re4 := Right[Config, string](4)
// Chain concat calls: ((1 + 2) + 3) + 4
combined := reMonoid.Concat(
reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
),
re4,
)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](10), result)
})
t.Run("string concatenation", func(t *testing.T) {
strREMonoid := ApplicativeMonoid[Config, string](strMonoid)
re1 := Right[Config, string]("Hello")
re2 := Right[Config, string](" ")
re3 := Right[Config, string]("World")
combined := strREMonoid.Concat(
strREMonoid.Concat(re1, re2),
re3,
)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string]("Hello World"), result)
})
t.Run("environment dependent computation", func(t *testing.T) {
// Create computations that use the environment
re1 := Asks[string](func(cfg Config) int {
return cfg.Port
})
re2 := Right[Config, string](100)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// defaultConfig.Port is 8080, so 8080 + 100 = 8180
assert.Equal(t, E.Right[string](8180), result)
})
t.Run("environment dependent with error", func(t *testing.T) {
re1 := MonadChain(
Ask[Config, string](),
func(cfg Config) ReaderEither[Config, string, int] {
if cfg.Debug {
return Right[Config, string](cfg.Timeout)
}
return Left[Config, int]("debug mode disabled")
},
)
re2 := Right[Config, string](50)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// defaultConfig.Debug is false, so should fail
assert.Equal(t, E.Left[int]("debug mode disabled"), result)
})
}
// TestAlternativeMonoid tests the AlternativeMonoid function
func TestAlternativeMonoid(t *testing.T) {
reMonoid := AlternativeMonoid[Config, string](intAddMonoid)
t.Run("empty element", func(t *testing.T) {
empty := reMonoid.Empty()
result := empty(defaultConfig)
assert.Equal(t, E.Right[string](0), result)
})
t.Run("concat two Right values", func(t *testing.T) {
re1 := Right[Config, string](5)
re2 := Right[Config, string](3)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Alternative combines successful values
assert.Equal(t, E.Right[string](8), result)
})
t.Run("concat Left then Right - fallback behavior", func(t *testing.T) {
reFailure := Left[Config, int]("error")
reSuccess := Right[Config, string](42)
combined := reMonoid.Concat(reFailure, reSuccess)
result := combined(defaultConfig)
// Should fall back to second when first fails
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat Right then Left - uses first", func(t *testing.T) {
reSuccess := Right[Config, string](42)
reFailure := Left[Config, int]("error")
combined := reMonoid.Concat(reSuccess, reFailure)
result := combined(defaultConfig)
// Should use first successful value
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat both errors", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Second error is returned when both fail
assert.Equal(t, E.Left[int]("error2"), result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(reMonoid.Empty(), re)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(re, reMonoid.Empty())
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("multiple values with some failures", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Right[Config, string](5)
re3 := Left[Config, int]("error2")
re4 := Right[Config, string](10)
// Alternative should skip failures and accumulate successes
combined := reMonoid.Concat(
reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
),
re4,
)
result := combined(defaultConfig)
// Should accumulate successful values: 5 + 10 = 15
assert.Equal(t, E.Right[string](15), result)
})
t.Run("fallback chain", func(t *testing.T) {
// Simulate trying multiple sources until one succeeds
primary := Left[Config, string]("primary failed")
secondary := Left[Config, string]("secondary failed")
tertiary := Right[Config, string]("tertiary success")
strREMonoid := AlternativeMonoid[Config, string](strMonoid)
// Chain concat: try primary, then secondary, then tertiary
combined := strREMonoid.Concat(
strREMonoid.Concat(primary, secondary),
tertiary,
)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string]("tertiary success"), result)
})
t.Run("environment dependent with fallback", func(t *testing.T) {
// First computation fails
re1 := Left[Config, int]("error")
// Second computation uses environment
re2 := Asks[string](func(cfg Config) int {
return cfg.Timeout
})
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Should fall back to second computation
assert.Equal(t, E.Right[string](30), result)
})
t.Run("all failures in chain", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
re3 := Left[Config, int]("error3")
combined := reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
)
result := combined(defaultConfig)
// Last error is returned
assert.Equal(t, E.Left[int]("error3"), result)
})
}
// TestAltMonoid tests the AltMonoid function
func TestAltMonoid(t *testing.T) {
zero := func() ReaderEither[Config, string, int] {
return Left[Config, int]("no value")
}
reMonoid := AltMonoid(zero)
t.Run("empty element", func(t *testing.T) {
empty := reMonoid.Empty()
result := empty(defaultConfig)
assert.Equal(t, E.Left[int]("no value"), result)
})
t.Run("concat Left then Right - uses second", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Right[Config, string](42)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Should use first success
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat Right then Right - uses first", func(t *testing.T) {
re1 := Right[Config, string](100)
re2 := Right[Config, string](200)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Uses first success, doesn't combine
assert.Equal(t, E.Right[string](100), result)
})
t.Run("concat Right then Left - uses first", func(t *testing.T) {
re1 := Right[Config, string](42)
re2 := Left[Config, int]("error")
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat both errors", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// Second error is returned
assert.Equal(t, E.Left[int]("error2"), result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(reMonoid.Empty(), re)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
re := Right[Config, string](42)
combined := reMonoid.Concat(re, reMonoid.Empty())
result := combined(defaultConfig)
assert.Equal(t, E.Right[string](42), result)
})
t.Run("fallback chain with first success", func(t *testing.T) {
re1 := Right[Config, string](10)
re2 := Right[Config, string](20)
re3 := Right[Config, string](30)
combined := reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
)
result := combined(defaultConfig)
// First success is used
assert.Equal(t, E.Right[string](10), result)
})
t.Run("fallback chain with middle success", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Right[Config, string](20)
re3 := Right[Config, string](30)
combined := reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
)
result := combined(defaultConfig)
// First success (re2) is used
assert.Equal(t, E.Right[string](20), result)
})
t.Run("fallback chain with last success", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
re3 := Right[Config, string](30)
combined := reMonoid.Concat(
reMonoid.Concat(re1, re2),
re3,
)
result := combined(defaultConfig)
// Last success is used
assert.Equal(t, E.Right[string](30), result)
})
t.Run("environment dependent fallback", func(t *testing.T) {
re1 := MonadChain(
Ask[Config, string](),
func(cfg Config) ReaderEither[Config, string, int] {
if cfg.Debug {
return Right[Config, string](cfg.Port)
}
return Left[Config, int]("debug disabled")
},
)
re2 := Right[Config, string](9999)
combined := reMonoid.Concat(re1, re2)
result := combined(defaultConfig)
// First fails (debug is false), falls back to second
assert.Equal(t, E.Right[string](9999), result)
})
}
// TestMonoidLaws verifies that the monoid laws hold for ApplicativeMonoid
func TestApplicativeMonoidLaws(t *testing.T) {
reMonoid := ApplicativeMonoid[Config, string](intAddMonoid)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(reMonoid.Empty(), x)(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(x, reMonoid.Empty())(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := Right[Config, string](1)
y := Right[Config, string](2)
z := Right[Config, string](3)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with Left values", func(t *testing.T) {
// Verify associativity even with Left values
x := Right[Config, string](5)
y := Left[Config, int]("error")
z := Right[Config, string](10)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
}
// TestAlternativeMonoidLaws verifies that the monoid laws hold for AlternativeMonoid
func TestAlternativeMonoidLaws(t *testing.T) {
reMonoid := AlternativeMonoid[Config, string](intAddMonoid)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(reMonoid.Empty(), x)(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(x, reMonoid.Empty())(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := Right[Config, string](1)
y := Right[Config, string](2)
z := Right[Config, string](3)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with Left values", func(t *testing.T) {
// Verify associativity even with Left values
x := Left[Config, int]("error1")
y := Right[Config, string](5)
z := Right[Config, string](10)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
}
// TestAltMonoidLaws verifies that the monoid laws hold for AltMonoid
func TestAltMonoidLaws(t *testing.T) {
zero := func() ReaderEither[Config, string, int] {
return Left[Config, int]("no value")
}
reMonoid := AltMonoid(zero)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(reMonoid.Empty(), x)(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := Right[Config, string](42)
result1 := reMonoid.Concat(x, reMonoid.Empty())(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := Right[Config, string](1)
y := Right[Config, string](2)
z := Right[Config, string](3)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with Left values", func(t *testing.T) {
// Verify associativity even with Left values
x := Left[Config, int]("error1")
y := Left[Config, int]("error2")
z := Right[Config, string](10)
left := reMonoid.Concat(reMonoid.Concat(x, y), z)(defaultConfig)
right := reMonoid.Concat(x, reMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
}
// TestApplicativeVsAlternative compares the behavior of both monoids
func TestApplicativeVsAlternative(t *testing.T) {
applicativeMonoid := ApplicativeMonoid[Config, string](intAddMonoid)
alternativeMonoid := AlternativeMonoid[Config, string](intAddMonoid)
t.Run("both succeed - same result", func(t *testing.T) {
re1 := Right[Config, string](5)
re2 := Right[Config, string](3)
appResult := applicativeMonoid.Concat(re1, re2)(defaultConfig)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
assert.Equal(t, E.Right[string](8), appResult)
assert.Equal(t, E.Right[string](8), altResult)
assert.Equal(t, appResult, altResult)
})
t.Run("first fails - different behavior", func(t *testing.T) {
re1 := Left[Config, int]("error")
re2 := Right[Config, string](42)
appResult := applicativeMonoid.Concat(re1, re2)(defaultConfig)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
// Applicative fails if any fails
assert.Equal(t, E.Left[int]("error"), appResult)
// Alternative falls back to second
assert.Equal(t, E.Right[string](42), altResult)
})
t.Run("second fails - different behavior", func(t *testing.T) {
re1 := Right[Config, string](42)
re2 := Left[Config, int]("error")
appResult := applicativeMonoid.Concat(re1, re2)(defaultConfig)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
// Applicative fails if any fails
assert.Equal(t, E.Left[int]("error"), appResult)
// Alternative uses first success
assert.Equal(t, E.Right[string](42), altResult)
})
t.Run("both fail - different behavior", func(t *testing.T) {
re1 := Left[Config, int]("error1")
re2 := Left[Config, int]("error2")
appResult := applicativeMonoid.Concat(re1, re2)(defaultConfig)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
// Applicative returns first error
assert.Equal(t, E.Left[int]("error1"), appResult)
// Alternative returns second error
assert.Equal(t, E.Left[int]("error2"), altResult)
})
}
// TestAlternativeVsAlt compares AlternativeMonoid and AltMonoid
func TestAlternativeVsAlt(t *testing.T) {
alternativeMonoid := AlternativeMonoid[Config, string](intAddMonoid)
zero := func() ReaderEither[Config, string, int] {
return Left[Config, int]("no value")
}
altMonoid := AltMonoid(zero)
t.Run("both succeed - different behavior", func(t *testing.T) {
re1 := Right[Config, string](5)
re2 := Right[Config, string](3)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
altMonoidResult := altMonoid.Concat(re1, re2)(defaultConfig)
// Alternative combines values
assert.Equal(t, E.Right[string](8), altResult)
// AltMonoid uses first success
assert.Equal(t, E.Right[string](5), altMonoidResult)
})
t.Run("first fails - same behavior", func(t *testing.T) {
re1 := Left[Config, int]("error")
re2 := Right[Config, string](42)
altResult := alternativeMonoid.Concat(re1, re2)(defaultConfig)
altMonoidResult := altMonoid.Concat(re1, re2)(defaultConfig)
// Both fall back to second
assert.Equal(t, E.Right[string](42), altResult)
assert.Equal(t, E.Right[string](42), altMonoidResult)
})
}
// TestComplexScenarios tests more complex real-world scenarios
func TestComplexScenarios(t *testing.T) {
t.Run("accumulate configuration values", func(t *testing.T) {
reMonoid := ApplicativeMonoid[Config, string](intAddMonoid)
// Accumulate multiple configuration values
getPort := Asks[string](func(cfg Config) int {
return cfg.Port
})
getTimeout := Asks[string](func(cfg Config) int {
return cfg.Timeout
})
getConstant := Right[Config, string](100)
combined := reMonoid.Concat(
reMonoid.Concat(getPort, getTimeout),
getConstant,
)
result := combined(defaultConfig)
// 8080 + 30 + 100 = 8210
assert.Equal(t, E.Right[string](8210), result)
})
t.Run("fallback configuration loading", func(t *testing.T) {
reMonoid := AlternativeMonoid[Config, string](strMonoid)
// Simulate trying to load config from multiple sources
fromEnv := Left[Config, string]("env not set")
fromFile := Left[Config, string]("file not found")
fromDefault := Right[Config, string]("default-config")
combined := reMonoid.Concat(
reMonoid.Concat(fromEnv, fromFile),
fromDefault,
)
result := combined(defaultConfig)
assert.Equal(t, E.Right[string]("default-config"), result)
})
t.Run("partial success accumulation", func(t *testing.T) {
reMonoid := AlternativeMonoid[Config, string](intAddMonoid)
// Simulate collecting metrics where some may fail
metric1 := Right[Config, string](100)
metric2 := Left[Config, int]("metric2 failed") // Failed to collect
metric3 := Right[Config, string](200)
metric4 := Left[Config, int]("metric4 failed") // Failed to collect
metric5 := Right[Config, string](300)
combined := reMonoid.Concat(
reMonoid.Concat(
reMonoid.Concat(
reMonoid.Concat(metric1, metric2),
metric3,
),
metric4,
),
metric5,
)
result := combined(defaultConfig)
// Should accumulate only successful metrics: 100 + 200 + 300 = 600
assert.Equal(t, E.Right[string](600), result)
})
t.Run("cascading fallback with AltMonoid", func(t *testing.T) {
zero := func() ReaderEither[Config, string, string] {
return Left[Config, string]("all sources failed")
}
reMonoid := AltMonoid(zero)
// Try multiple data sources in order
primaryDB := Left[Config, string]("primary DB down")
secondaryDB := Left[Config, string]("secondary DB down")
cache := Right[Config, string]("cached-data")
fallback := Right[Config, string]("fallback-data")
combined := reMonoid.Concat(
reMonoid.Concat(
reMonoid.Concat(primaryDB, secondaryDB),
cache,
),
fallback,
)
result := combined(defaultConfig)
// Should use first successful source (cache)
assert.Equal(t, E.Right[string]("cached-data"), result)
})
}

View File

@@ -461,3 +461,24 @@ func TapLeft[A, R, EA, EB, B any](f Kleisli[R, EB, EA, B]) Operator[R, EA, A, A]
func MonadFold[E, L, A, B any](ma ReaderEither[E, L, A], onLeft func(L) Reader[E, B], onRight func(A) Reader[E, B]) Reader[E, B] {
return Fold(onLeft, onRight)(ma)
}
//go:inline
func MonadAlt[R, E, A any](first ReaderEither[R, E, A], second Lazy[ReaderEither[R, E, A]]) ReaderEither[R, E, A] {
return eithert.MonadAlt(
reader.Of[R, Either[E, A]],
reader.MonadChain[R, Either[E, A], Either[E, A]],
first,
second,
)
}
//go:inline
func Alt[R, E, A any](second Lazy[ReaderEither[R, E, A]]) Operator[R, E, A, A] {
return eithert.Alt(
reader.Of[R, Either[E, A]],
reader.Chain[R, Either[E, A], Either[E, A]],
second,
)
}

View File

@@ -285,7 +285,7 @@ func TestReadEither(t *testing.T) {
Map[Config, string](func(cfg Config) string {
return cfg.host + "/data"
}),
Chain[Config, string, string, int](func(url string) ReaderEither[Config, string, int] {
Chain(func(url string) ReaderEither[Config, string, int] {
return func(cfg Config) Either[string, int] {
if cfg.apiKey != "" {
return ET.Right[string](len(url))

View File

@@ -17,6 +17,7 @@ package readereither
import (
"github.com/IBM/fp-go/v2/either"
"github.com/IBM/fp-go/v2/lazy"
"github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/reader"
)
@@ -44,4 +45,6 @@ type (
// Operator represents a function that transforms one ReaderEither into another.
// It takes a ReaderEither[R, E, A] and produces a ReaderEither[R, E, B].
Operator[R, E, A, B any] = Kleisli[R, E, ReaderEither[R, E, A], B]
Lazy[A any] = lazy.Lazy[A]
)

View File

@@ -36,7 +36,7 @@ type (
//
// Returns a Monoid for ReaderIOResult[A].
func ApplicativeMonoid[R, A any](m monoid.Monoid[A]) Monoid[R, A] {
return RIOE.AlternativeMonoid[R, error](m)
return RIOE.ApplicativeMonoid[R, error](m)
}
// ApplicativeMonoidSeq returns a [Monoid] that concatenates [ReaderIOResult] instances via their applicative.

145
v2/readeroption/monoid.go Normal file
View File

@@ -0,0 +1,145 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readeroption
import "github.com/IBM/fp-go/v2/monoid"
// ApplicativeMonoid creates a Monoid for ReaderOption based on Applicative functor composition.
// The empty element is Of(m.Empty()), and concat combines two computations using the underlying monoid.
// Both computations must succeed (return Some) for the result to succeed.
//
// This is useful for accumulating results from multiple independent computations that all need
// to succeed. If any computation returns None, the entire result is None.
//
// The resulting monoid satisfies the monoid laws:
// - Left identity: Concat(Empty(), x) = x
// - Right identity: Concat(x, Empty()) = x
// - Associativity: Concat(Concat(x, y), z) = Concat(x, Concat(y, z))
//
// Parameters:
// - m: The underlying monoid for combining success values of type A
//
// Returns:
// - A Monoid[ReaderOption[R, A]] that combines ReaderOption computations
//
// Example:
//
// import (
// N "github.com/IBM/fp-go/v2/number"
// RO "github.com/IBM/fp-go/v2/readeroption"
// )
//
// // Create a monoid for integer addition
// intAdd := N.MonoidSum[int]()
// roMonoid := RO.ApplicativeMonoid[Config](intAdd)
//
// // Combine successful computations
// ro1 := RO.Of[Config](5)
// ro2 := RO.Of[Config](3)
// combined := roMonoid.Concat(ro1, ro2)
// // combined(cfg) returns option.Some(8)
//
// // If either fails, the whole computation fails
// ro3 := RO.None[Config, int]()
// failed := roMonoid.Concat(ro1, ro3)
// // failed(cfg) returns option.None[int]()
//
// // Empty element is the identity
// withEmpty := roMonoid.Concat(ro1, roMonoid.Empty())
// // withEmpty(cfg) returns option.Some(5)
//
//go:inline
func ApplicativeMonoid[R, A any](m monoid.Monoid[A]) monoid.Monoid[ReaderOption[R, A]] {
return monoid.ApplicativeMonoid(
Of[R, A],
MonadMap[R, A, func(A) A],
MonadAp[R, A, A],
m,
)
}
// AlternativeMonoid creates a Monoid for ReaderOption that combines both Alternative and Applicative behavior.
// It uses the provided monoid for the success values and falls back to alternative computations on failure.
//
// The empty element is Of(m.Empty()), and concat tries the first computation, falling back to the second
// if it fails (returns None), then combines successful values using the underlying monoid.
//
// This is particularly useful when you want to:
// - Try multiple computations and accumulate their results
// - Provide fallback behavior when computations fail
// - Combine results from computations that may or may not succeed
//
// The behavior differs from ApplicativeMonoid in that it provides fallback semantics:
// - If the first computation succeeds, use its value
// - If the first fails but the second succeeds, use the second's value
// - If both succeed, combine their values using the underlying monoid
// - If both fail, the result is None
//
// The resulting monoid satisfies the monoid laws:
// - Left identity: Concat(Empty(), x) = x
// - Right identity: Concat(x, Empty()) = x
// - Associativity: Concat(Concat(x, y), z) = Concat(x, Concat(y, z))
//
// Parameters:
// - m: The underlying monoid for combining success values of type A
//
// Returns:
// - A Monoid[ReaderOption[R, A]] that combines ReaderOption computations with fallback
//
// Example:
//
// import (
// N "github.com/IBM/fp-go/v2/number"
// RO "github.com/IBM/fp-go/v2/readeroption"
// )
//
// // Create a monoid for integer addition with alternative behavior
// intAdd := N.MonoidSum[int]()
// roMonoid := RO.AlternativeMonoid[Config](intAdd)
//
// // Combine successful computations
// ro1 := RO.Of[Config](5)
// ro2 := RO.Of[Config](3)
// combined := roMonoid.Concat(ro1, ro2)
// // combined(cfg) returns option.Some(8)
//
// // Fallback when first fails
// ro3 := RO.None[Config, int]()
// ro4 := RO.Of[Config](10)
// withFallback := roMonoid.Concat(ro3, ro4)
// // withFallback(cfg) returns option.Some(10)
//
// // Use first success when available
// withFirst := roMonoid.Concat(ro1, ro3)
// // withFirst(cfg) returns option.Some(5)
//
// // Accumulate multiple values with some failures
// result := roMonoid.Concat(
// roMonoid.Concat(ro3, ro1), // None + 5 = 5
// ro2, // 5 + 3 = 8
// )
// // result(cfg) returns option.Some(8)
//
//go:inline
func AlternativeMonoid[R, A any](m monoid.Monoid[A]) monoid.Monoid[ReaderOption[R, A]] {
return monoid.AlternativeMonoid(
Of[R, A],
MonadMap[R, A, func(A) A],
MonadAp[R, A, A],
MonadAlt[R, A],
m,
)
}

View File

@@ -0,0 +1,472 @@
// Copyright (c) 2023 - 2025 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package readeroption
import (
"testing"
N "github.com/IBM/fp-go/v2/number"
O "github.com/IBM/fp-go/v2/option"
S "github.com/IBM/fp-go/v2/string"
"github.com/stretchr/testify/assert"
)
var (
intAddMonoid = N.MonoidSum[int]()
strMonoid = S.Monoid
)
// TestApplicativeMonoid tests the ApplicativeMonoid function
func TestApplicativeMonoid(t *testing.T) {
roMonoid := ApplicativeMonoid[Config](intAddMonoid)
t.Run("empty element", func(t *testing.T) {
empty := roMonoid.Empty()
result := empty(defaultConfig)
assert.Equal(t, O.Some(0), result)
})
t.Run("concat two Some values", func(t *testing.T) {
ro1 := Of[Config](5)
ro2 := Of[Config](3)
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
assert.Equal(t, O.Some(8), result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
ro := Of[Config](42)
combined := roMonoid.Concat(roMonoid.Empty(), ro)
result := combined(defaultConfig)
assert.Equal(t, O.Some(42), result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
ro := Of[Config](42)
combined := roMonoid.Concat(ro, roMonoid.Empty())
result := combined(defaultConfig)
assert.Equal(t, O.Some(42), result)
})
t.Run("concat with left None", func(t *testing.T) {
roSuccess := Of[Config](5)
roFailure := None[Config, int]()
combined := roMonoid.Concat(roFailure, roSuccess)
result := combined(defaultConfig)
assert.Equal(t, O.None[int](), result)
})
t.Run("concat with right None", func(t *testing.T) {
roSuccess := Of[Config](5)
roFailure := None[Config, int]()
combined := roMonoid.Concat(roSuccess, roFailure)
result := combined(defaultConfig)
assert.Equal(t, O.None[int](), result)
})
t.Run("concat both None", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := None[Config, int]()
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
assert.Equal(t, O.None[int](), result)
})
t.Run("concat multiple values", func(t *testing.T) {
ro1 := Of[Config](1)
ro2 := Of[Config](2)
ro3 := Of[Config](3)
ro4 := Of[Config](4)
// Chain concat calls: ((1 + 2) + 3) + 4
combined := roMonoid.Concat(
roMonoid.Concat(
roMonoid.Concat(ro1, ro2),
ro3,
),
ro4,
)
result := combined(defaultConfig)
assert.Equal(t, O.Some(10), result)
})
t.Run("string concatenation", func(t *testing.T) {
strROMonoid := ApplicativeMonoid[Config](strMonoid)
ro1 := Of[Config]("Hello")
ro2 := Of[Config](" ")
ro3 := Of[Config]("World")
combined := strROMonoid.Concat(
strROMonoid.Concat(ro1, ro2),
ro3,
)
result := combined(defaultConfig)
assert.Equal(t, O.Some("Hello World"), result)
})
t.Run("environment dependent computation", func(t *testing.T) {
// Create computations that use the environment
ro1 := Asks(func(cfg Config) int {
return cfg.Port
})
ro2 := Of[Config](100)
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
// defaultConfig.Port is 8080, so 8080 + 100 = 8180
assert.Equal(t, O.Some(8180), result)
})
}
// TestAlternativeMonoid tests the AlternativeMonoid function
func TestAlternativeMonoid(t *testing.T) {
roMonoid := AlternativeMonoid[Config](intAddMonoid)
t.Run("empty element", func(t *testing.T) {
empty := roMonoid.Empty()
result := empty(defaultConfig)
assert.Equal(t, O.Some(0), result)
})
t.Run("concat two Some values", func(t *testing.T) {
ro1 := Of[Config](5)
ro2 := Of[Config](3)
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
// Alternative combines successful values
assert.Equal(t, O.Some(8), result)
})
t.Run("concat None then Some - fallback behavior", func(t *testing.T) {
roFailure := None[Config, int]()
roSuccess := Of[Config](42)
combined := roMonoid.Concat(roFailure, roSuccess)
result := combined(defaultConfig)
// Should fall back to second when first fails
assert.Equal(t, O.Some(42), result)
})
t.Run("concat Some then None - uses first", func(t *testing.T) {
roSuccess := Of[Config](42)
roFailure := None[Config, int]()
combined := roMonoid.Concat(roSuccess, roFailure)
result := combined(defaultConfig)
// Should use first successful value
assert.Equal(t, O.Some(42), result)
})
t.Run("concat both None", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := None[Config, int]()
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
assert.Equal(t, O.None[int](), result)
})
t.Run("concat with empty - left identity", func(t *testing.T) {
ro := Of[Config](42)
combined := roMonoid.Concat(roMonoid.Empty(), ro)
result := combined(defaultConfig)
assert.Equal(t, O.Some(42), result)
})
t.Run("concat with empty - right identity", func(t *testing.T) {
ro := Of[Config](42)
combined := roMonoid.Concat(ro, roMonoid.Empty())
result := combined(defaultConfig)
assert.Equal(t, O.Some(42), result)
})
t.Run("multiple values with some failures", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := Of[Config](5)
ro3 := None[Config, int]()
ro4 := Of[Config](10)
// Alternative should skip failures and accumulate successes
combined := roMonoid.Concat(
roMonoid.Concat(
roMonoid.Concat(ro1, ro2),
ro3,
),
ro4,
)
result := combined(defaultConfig)
// Should accumulate successful values: 5 + 10 = 15
assert.Equal(t, O.Some(15), result)
})
t.Run("fallback chain", func(t *testing.T) {
// Simulate trying multiple sources until one succeeds
primary := None[Config, string]()
secondary := None[Config, string]()
tertiary := Of[Config]("tertiary success")
strROMonoid := AlternativeMonoid[Config](strMonoid)
// Chain concat: try primary, then secondary, then tertiary
combined := strROMonoid.Concat(
strROMonoid.Concat(primary, secondary),
tertiary,
)
result := combined(defaultConfig)
assert.Equal(t, O.Some("tertiary success"), result)
})
t.Run("environment dependent with fallback", func(t *testing.T) {
// First computation fails
ro1 := None[Config, int]()
// Second computation uses environment
ro2 := Asks(func(cfg Config) int {
return cfg.Timeout
})
combined := roMonoid.Concat(ro1, ro2)
result := combined(defaultConfig)
// Should fall back to second computation
assert.Equal(t, O.Some(30), result)
})
t.Run("all failures in chain", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := None[Config, int]()
ro3 := None[Config, int]()
combined := roMonoid.Concat(
roMonoid.Concat(ro1, ro2),
ro3,
)
result := combined(defaultConfig)
assert.Equal(t, O.None[int](), result)
})
}
// TestMonoidLaws verifies that the monoid laws hold for ApplicativeMonoid
func TestMonoidLaws(t *testing.T) {
roMonoid := ApplicativeMonoid[Config](intAddMonoid)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := Of[Config](42)
result1 := roMonoid.Concat(roMonoid.Empty(), x)(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := Of[Config](42)
result1 := roMonoid.Concat(x, roMonoid.Empty())(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := Of[Config](1)
y := Of[Config](2)
z := Of[Config](3)
left := roMonoid.Concat(roMonoid.Concat(x, y), z)(defaultConfig)
right := roMonoid.Concat(x, roMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with None values", func(t *testing.T) {
// Verify associativity even with None values
x := Of[Config](5)
y := None[Config, int]()
z := Of[Config](10)
left := roMonoid.Concat(roMonoid.Concat(x, y), z)(defaultConfig)
right := roMonoid.Concat(x, roMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
}
// TestAlternativeMonoidLaws verifies that the monoid laws hold for AlternativeMonoid
func TestAlternativeMonoidLaws(t *testing.T) {
roMonoid := AlternativeMonoid[Config](intAddMonoid)
t.Run("left identity law", func(t *testing.T) {
// empty <> x == x
x := Of[Config](42)
result1 := roMonoid.Concat(roMonoid.Empty(), x)(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("right identity law", func(t *testing.T) {
// x <> empty == x
x := Of[Config](42)
result1 := roMonoid.Concat(x, roMonoid.Empty())(defaultConfig)
result2 := x(defaultConfig)
assert.Equal(t, result2, result1)
})
t.Run("associativity law", func(t *testing.T) {
// (x <> y) <> z == x <> (y <> z)
x := Of[Config](1)
y := Of[Config](2)
z := Of[Config](3)
left := roMonoid.Concat(roMonoid.Concat(x, y), z)(defaultConfig)
right := roMonoid.Concat(x, roMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
t.Run("associativity with None values", func(t *testing.T) {
// Verify associativity even with None values
x := None[Config, int]()
y := Of[Config](5)
z := Of[Config](10)
left := roMonoid.Concat(roMonoid.Concat(x, y), z)(defaultConfig)
right := roMonoid.Concat(x, roMonoid.Concat(y, z))(defaultConfig)
assert.Equal(t, right, left)
})
}
// TestApplicativeVsAlternative compares the behavior of both monoids
func TestApplicativeVsAlternative(t *testing.T) {
applicativeMonoid := ApplicativeMonoid[Config](intAddMonoid)
alternativeMonoid := AlternativeMonoid[Config](intAddMonoid)
t.Run("both succeed - same result", func(t *testing.T) {
ro1 := Of[Config](5)
ro2 := Of[Config](3)
appResult := applicativeMonoid.Concat(ro1, ro2)(defaultConfig)
altResult := alternativeMonoid.Concat(ro1, ro2)(defaultConfig)
assert.Equal(t, O.Some(8), appResult)
assert.Equal(t, O.Some(8), altResult)
assert.Equal(t, appResult, altResult)
})
t.Run("first fails - different behavior", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := Of[Config](42)
appResult := applicativeMonoid.Concat(ro1, ro2)(defaultConfig)
altResult := alternativeMonoid.Concat(ro1, ro2)(defaultConfig)
// Applicative fails if any fails
assert.Equal(t, O.None[int](), appResult)
// Alternative falls back to second
assert.Equal(t, O.Some(42), altResult)
})
t.Run("second fails - different behavior", func(t *testing.T) {
ro1 := Of[Config](42)
ro2 := None[Config, int]()
appResult := applicativeMonoid.Concat(ro1, ro2)(defaultConfig)
altResult := alternativeMonoid.Concat(ro1, ro2)(defaultConfig)
// Applicative fails if any fails
assert.Equal(t, O.None[int](), appResult)
// Alternative uses first success
assert.Equal(t, O.Some(42), altResult)
})
t.Run("both fail - same result", func(t *testing.T) {
ro1 := None[Config, int]()
ro2 := None[Config, int]()
appResult := applicativeMonoid.Concat(ro1, ro2)(defaultConfig)
altResult := alternativeMonoid.Concat(ro1, ro2)(defaultConfig)
assert.Equal(t, O.None[int](), appResult)
assert.Equal(t, O.None[int](), altResult)
assert.Equal(t, appResult, altResult)
})
}
// TestComplexScenarios tests more complex real-world scenarios
func TestComplexScenarios(t *testing.T) {
t.Run("accumulate configuration values", func(t *testing.T) {
roMonoid := ApplicativeMonoid[Config](intAddMonoid)
// Accumulate multiple configuration values
getPort := Asks(func(cfg Config) int { return cfg.Port })
getTimeout := Asks(func(cfg Config) int { return cfg.Timeout })
getConstant := Of[Config](100)
combined := roMonoid.Concat(
roMonoid.Concat(getPort, getTimeout),
getConstant,
)
result := combined(defaultConfig)
// 8080 + 30 + 100 = 8210
assert.Equal(t, O.Some(8210), result)
})
t.Run("fallback configuration loading", func(t *testing.T) {
roMonoid := AlternativeMonoid[Config](strMonoid)
// Simulate trying to load config from multiple sources
fromEnv := None[Config, string]()
fromFile := None[Config, string]()
fromDefault := Of[Config]("default-config")
combined := roMonoid.Concat(
roMonoid.Concat(fromEnv, fromFile),
fromDefault,
)
result := combined(defaultConfig)
assert.Equal(t, O.Some("default-config"), result)
})
t.Run("partial success accumulation", func(t *testing.T) {
roMonoid := AlternativeMonoid[Config](intAddMonoid)
// Simulate collecting metrics where some may fail
metric1 := Of[Config](100)
metric2 := None[Config, int]() // Failed to collect
metric3 := Of[Config](200)
metric4 := None[Config, int]() // Failed to collect
metric5 := Of[Config](300)
combined := roMonoid.Concat(
roMonoid.Concat(
roMonoid.Concat(
roMonoid.Concat(metric1, metric2),
metric3,
),
metric4,
),
metric5,
)
result := combined(defaultConfig)
// Should accumulate only successful metrics: 100 + 200 + 300 = 600
assert.Equal(t, O.Some(600), result)
})
}

View File

@@ -384,8 +384,13 @@ func Flap[E, B, A any](a A) Operator[E, func(A) B, B] {
// result := readeroption.MonadAlt(primary, fallback)
//
//go:inline
func MonadAlt[E, A any](fa, that ReaderOption[E, A]) ReaderOption[E, A] {
return MonadFold(fa, that, Of[E, A])
func MonadAlt[E, A any](first ReaderOption[E, A], second Lazy[ReaderOption[E, A]]) ReaderOption[E, A] {
return optiont.MonadAlt(
reader.Of[E, Option[A]],
reader.MonadChain[E, Option[A], Option[A]],
first,
second,
)
}
// Alt returns a function that provides an alternative ReaderOption if the first one returns None.
@@ -399,6 +404,10 @@ func MonadAlt[E, A any](fa, that ReaderOption[E, A]) ReaderOption[E, A] {
// )
//
//go:inline
func Alt[E, A any](that ReaderOption[E, A]) Operator[E, A, A] {
return Fold(that, Of[E, A])
func Alt[E, A any](second Lazy[ReaderOption[E, A]]) Operator[E, A, A] {
return optiont.Alt(
reader.Of[E, Option[A]],
reader.Chain[E, Option[A], Option[A]],
second,
)
}

View File

@@ -21,6 +21,7 @@ import (
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/internal/utils"
"github.com/IBM/fp-go/v2/lazy"
O "github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/reader"
"github.com/stretchr/testify/assert"
@@ -473,21 +474,21 @@ func TestMonadAlt(t *testing.T) {
t.Run("Alt with first Some", func(t *testing.T) {
primary := Of[Config](42)
fallback := Of[Config](99)
result := MonadAlt(primary, fallback)
result := MonadAlt(primary, lazy.Of(fallback))
assert.Equal(t, O.Some(42), result(defaultConfig))
})
t.Run("Alt with first None", func(t *testing.T) {
primary := None[Config, int]()
fallback := Of[Config](99)
result := MonadAlt(primary, fallback)
result := MonadAlt(primary, lazy.Of(fallback))
assert.Equal(t, O.Some(99), result(defaultConfig))
})
t.Run("Alt with both None", func(t *testing.T) {
primary := None[Config, int]()
fallback := None[Config, int]()
result := MonadAlt(primary, fallback)
result := MonadAlt(primary, lazy.Of(fallback))
assert.Equal(t, O.None[int](), result(defaultConfig))
})
}
@@ -497,7 +498,7 @@ func TestAlt(t *testing.T) {
t.Run("Alt with first Some", func(t *testing.T) {
result := F.Pipe1(
Of[Config](42),
Alt(Of[Config](99)),
Alt(lazy.Of(Of[Config](99))),
)
assert.Equal(t, O.Some(42), result(defaultConfig))
})
@@ -505,7 +506,7 @@ func TestAlt(t *testing.T) {
t.Run("Alt with first None", func(t *testing.T) {
result := F.Pipe1(
None[Config, int](),
Alt(Of[Config](99)),
Alt(lazy.Of(Of[Config](99))),
)
assert.Equal(t, O.Some(99), result(defaultConfig))
})

View File

@@ -150,7 +150,7 @@ func TestApS(t *testing.T) {
func TestBindIOEitherK(t *testing.T) {
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
BindIOEitherK[OuterCtx, InnerCtx, error](
BindIOEitherK[OuterCtx, InnerCtx](
utils.SetLastName,
func(s utils.Initial) IOE.IOEither[error, string] {
return IOE.Of[error]("Smith")
@@ -168,7 +168,7 @@ func TestBindIOEitherKError(t *testing.T) {
err := errors.New("io error")
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
BindIOEitherK[OuterCtx, InnerCtx, error](
BindIOEitherK[OuterCtx, InnerCtx](
utils.SetLastName,
func(s utils.Initial) IOE.IOEither[error, string] {
return IOE.Left[string](err)
@@ -244,7 +244,7 @@ func TestBindReaderIOK(t *testing.T) {
func TestBindEitherK(t *testing.T) {
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
BindEitherK[OuterCtx, InnerCtx, error](
BindEitherK[OuterCtx, InnerCtx](
utils.SetLastName,
func(s utils.Initial) E.Either[error, string] {
return E.Of[error]("Brown")
@@ -262,7 +262,7 @@ func TestBindEitherKError(t *testing.T) {
err := errors.New("either error")
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
BindEitherK[OuterCtx, InnerCtx, error](
BindEitherK[OuterCtx, InnerCtx](
utils.SetLastName,
func(s utils.Initial) E.Either[error, string] {
return E.Left[string](err)
@@ -279,7 +279,7 @@ func TestBindEitherKError(t *testing.T) {
func TestApIOEitherS(t *testing.T) {
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
ApIOEitherS[OuterCtx, InnerCtx, error](utils.SetLastName, IOE.Of[error]("Williams")),
ApIOEitherS[OuterCtx, InnerCtx](utils.SetLastName, IOE.Of[error]("Williams")),
Map[OuterCtx, InnerCtx, error](func(s utils.WithLastName) string {
return s.LastName
}),
@@ -335,7 +335,7 @@ func TestApReaderIOS(t *testing.T) {
func TestApEitherS(t *testing.T) {
res := F.Pipe2(
Do[OuterCtx, InnerCtx, error](utils.Empty),
ApEitherS[OuterCtx, InnerCtx, error](utils.SetLastName, E.Of[error]("Miller")),
ApEitherS[OuterCtx, InnerCtx](utils.SetLastName, E.Of[error]("Miller")),
Map[OuterCtx, InnerCtx, error](func(s utils.WithLastName) string {
return s.LastName
}),

View File

@@ -52,7 +52,7 @@ func TestSequence(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := Sequence[Config1, Config2, Context, error, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -79,7 +79,7 @@ func TestSequence(t *testing.T) {
return RIOE.Left[Context, ReaderReaderIOEither[Config1, Context, error, int]](testErr)
}
sequenced := Sequence[Config1, Config2, Context, error, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -105,7 +105,7 @@ func TestSequence(t *testing.T) {
})
}
sequenced := Sequence[Config1, Config2, Context, error, string](original)
sequenced := Sequence(original)
ctx := Context{contextID: "test"}
@@ -129,7 +129,7 @@ func TestSequence(t *testing.T) {
})
}
sequenced := Sequence[Config1, Config2, Context, error, int](original)
sequenced := Sequence(original)
result := sequenced(Config1{value1: 0})(Config2{value2: ""})(Context{contextID: ""})()
assert.Equal(t, E.Right[error](0), result)
@@ -142,7 +142,7 @@ func TestSequence(t *testing.T) {
})
}
sequenced := Sequence[Config1, Config2, Context, error, int](original)
sequenced := Sequence(original)
cfg1 := Config1{value1: 3}
cfg2 := Config2{value2: "test"}
@@ -166,7 +166,7 @@ func TestSequenceReader(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := SequenceReader[Config1, Config2, Context, error, int](original)
sequenced := SequenceReader(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -194,7 +194,7 @@ func TestSequenceReader(t *testing.T) {
return RIOE.Left[Context, R.Reader[Config1, int]](testErr)
}
sequenced := SequenceReader[Config1, Config2, Context, error, int](original)
sequenced := SequenceReader(original)
result := sequenced(Config1{value1: 10})(Config2{value2: "hello"})(Context{contextID: "test"})()
assert.Equal(t, E.Left[int](testErr), result)
@@ -210,7 +210,7 @@ func TestSequenceReader(t *testing.T) {
})
}
sequenced := SequenceReader[Config1, Config2, Context, error, string](original)
sequenced := SequenceReader(original)
ctx := Context{contextID: "test"}
@@ -230,7 +230,7 @@ func TestSequenceReader(t *testing.T) {
})
}
sequenced := SequenceReader[Config1, Config2, Context, error, int](original)
sequenced := SequenceReader(original)
result := sequenced(Config1{value1: 0})(Config2{value2: ""})(Context{contextID: ""})()
assert.Equal(t, E.Right[error](0), result)
@@ -243,7 +243,7 @@ func TestSequenceReader(t *testing.T) {
})
}
sequenced := SequenceReader[Config1, Config2, Context, error, int](original)
sequenced := SequenceReader(original)
cfg1 := Config1{value1: 3}
cfg2 := Config2{value2: "test"}
@@ -267,7 +267,7 @@ func TestSequenceReaderIO(t *testing.T) {
}
// Sequence swaps Config1 and Config2 order
sequenced := SequenceReaderIO[Config1, Config2, Context, error, int](original)
sequenced := SequenceReaderIO(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -295,7 +295,7 @@ func TestSequenceReaderIO(t *testing.T) {
return RIOE.Left[Context, readerio.ReaderIO[Config1, int]](testErr)
}
sequenced := SequenceReaderIO[Config1, Config2, Context, error, int](original)
sequenced := SequenceReaderIO(original)
result := sequenced(Config1{value1: 10})(Config2{value2: "hello"})(Context{contextID: "test"})()
assert.Equal(t, E.Left[int](testErr), result)
@@ -316,7 +316,7 @@ func TestSequenceReaderIO(t *testing.T) {
})
}
sequenced := SequenceReaderIO[Config1, Config2, Context, error, string](original)
sequenced := SequenceReaderIO(original)
ctx := Context{contextID: "test"}
@@ -340,7 +340,7 @@ func TestSequenceReaderIO(t *testing.T) {
})
}
sequenced := SequenceReaderIO[Config1, Config2, Context, error, int](original)
sequenced := SequenceReaderIO(original)
result := sequenced(Config1{value1: 0})(Config2{value2: ""})(Context{contextID: ""})()
assert.Equal(t, E.Right[error](0), result)
@@ -358,7 +358,7 @@ func TestSequenceReaderIO(t *testing.T) {
})
}
sequenced := SequenceReaderIO[Config1, Config2, Context, error, int](original)
sequenced := SequenceReaderIO(original)
cfg1 := Config1{value1: 10}
cfg2 := Config2{value2: "hello"}
@@ -389,7 +389,7 @@ func TestTraverse(t *testing.T) {
}
// Apply traverse to swap order and transform
traversed := Traverse[Config2, Config1, Context, error, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
cfg1 := Config1{value1: 100}
cfg2 := Config2{value2: "test"}
@@ -407,7 +407,7 @@ func TestTraverse(t *testing.T) {
return Of[Config1, Context, error](fmt.Sprintf("%d", n))
}
traversed := Traverse[Config2, Config1, Context, error, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
result := traversed(Config1{value1: 100})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Left[string](testErr), result)
@@ -426,12 +426,12 @@ func TestTraverse(t *testing.T) {
// Test with negative value
originalNeg := Of[Config2, Context, error](-1)
traversedNeg := Traverse[Config2, Config1, Context, error, int, string](transform)(originalNeg)
traversedNeg := Traverse[Config2](transform)(originalNeg)
resultNeg := traversedNeg(Config1{value1: 100})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Left[string](testErr), resultNeg)
// Test with positive value
traversedPos := Traverse[Config2, Config1, Context, error, int, string](transform)(original)
traversedPos := Traverse[Config2](transform)(original)
resultPos := traversedPos(Config1{value1: 100})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Right[error]("42"), resultPos)
})
@@ -447,7 +447,7 @@ func TestTraverse(t *testing.T) {
}
}
traversed := Traverse[Config2, Config1, Context, error, int, int](transform)(original)
traversed := Traverse[Config2](transform)(original)
result := traversed(Config1{value1: 5})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Right[error](50), result)
@@ -462,7 +462,7 @@ func TestTraverse(t *testing.T) {
result := F.Pipe2(
original,
Traverse[Config2, Config1, Context, error, int, int](transform),
Traverse[Config2](transform),
func(k Kleisli[Config2, Context, error, Config1, int]) ReaderReaderIOEither[Config2, Context, error, int] {
return k(Config1{value1: 5})
},
@@ -486,7 +486,7 @@ func TestTraverseReader(t *testing.T) {
}
// Apply traverse to introduce Config1 and swap order
traversed := TraverseReader[Config2, Config1, Context, error, int, string](formatWithConfig)(original)
traversed := TraverseReader[Config2, Config1, Context, error](formatWithConfig)(original)
cfg1 := Config1{value1: 5}
cfg2 := Config2{value2: "test"}
@@ -504,7 +504,7 @@ func TestTraverseReader(t *testing.T) {
return R.Of[Config1](fmt.Sprintf("%d", n))
}
traversed := TraverseReader[Config2, Config1, Context, error, int, string](transform)(original)
traversed := TraverseReader[Config2, Config1, Context, error](transform)(original)
result := traversed(Config1{value1: 5})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Left[string](testErr), result)
@@ -520,7 +520,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, Context, error, int, int](double)(original)
traversed := TraverseReader[Config2, Config1, Context, error](double)(original)
result := traversed(Config1{value1: 3})(Config2{value2: "test"})(Context{contextID: "test"})()
assert.Equal(t, E.Right[error](126), result)
@@ -535,7 +535,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, Context, error, int, int](transform)(original)
traversed := TraverseReader[Config2, Config1, Context, error](transform)(original)
result := traversed(Config1{value1: 0})(Config2{value2: ""})(Context{contextID: ""})()
assert.Equal(t, E.Right[error](0), result)
@@ -550,7 +550,7 @@ func TestTraverseReader(t *testing.T) {
}
}
traversed := TraverseReader[Config2, Config1, Context, error, int, int](transform)(original)
traversed := TraverseReader[Config2, Config1, Context, error](transform)(original)
cfg1 := Config1{value1: 5}
cfg2 := Config2{value2: "test"}
@@ -574,7 +574,7 @@ func TestTraverseReader(t *testing.T) {
result := F.Pipe2(
original,
TraverseReader[Config2, Config1, Context, error, int, int](multiply),
TraverseReader[Config2, Config1, Context, error](multiply),
func(k Kleisli[Config2, Context, error, Config1, int]) ReaderReaderIOEither[Config2, Context, error, int] {
return k(Config1{value1: 3})
},
@@ -593,7 +593,7 @@ func TestFlipIntegration(t *testing.T) {
}
// Sequence it
sequenced := Sequence[Config1, Config2, Context, error, int](nested)
sequenced := Sequence(nested)
// Then traverse with a transformation
transform := func(n int) ReaderReaderIOEither[Config1, Context, error, string] {
@@ -611,7 +611,7 @@ func TestFlipIntegration(t *testing.T) {
// Then apply traverse on a new computation
original := Of[Config2, Context, error](5)
traversed := Traverse[Config2, Config1, Context, error, int, string](transform)(original)
traversed := Traverse[Config2](transform)(original)
result := traversed(cfg1)(cfg2)(ctx)()
assert.Equal(t, E.Right[error]("length=5"), result)
})
@@ -626,21 +626,21 @@ func TestFlipIntegration(t *testing.T) {
seqErr := func(cfg2 Config2) RIOE.ReaderIOEither[Context, error, ReaderReaderIOEither[Config1, Context, error, int]] {
return RIOE.Left[Context, ReaderReaderIOEither[Config1, Context, error, int]](testErr)
}
seqResult := Sequence[Config1, Config2, Context, error, int](seqErr)(cfg1)(cfg2)(ctx)()
seqResult := Sequence(seqErr)(cfg1)(cfg2)(ctx)()
assert.True(t, E.IsLeft(seqResult))
// Test SequenceReader with error
seqReaderErr := func(cfg2 Config2) RIOE.ReaderIOEither[Context, error, R.Reader[Config1, int]] {
return RIOE.Left[Context, R.Reader[Config1, int]](testErr)
}
seqReaderResult := SequenceReader[Config1, Config2, Context, error, int](seqReaderErr)(cfg1)(cfg2)(ctx)()
seqReaderResult := SequenceReader(seqReaderErr)(cfg1)(cfg2)(ctx)()
assert.True(t, E.IsLeft(seqReaderResult))
// Test SequenceReaderIO with error
seqReaderIOErr := func(cfg2 Config2) RIOE.ReaderIOEither[Context, error, readerio.ReaderIO[Config1, int]] {
return RIOE.Left[Context, readerio.ReaderIO[Config1, int]](testErr)
}
seqReaderIOResult := SequenceReaderIO[Config1, Config2, Context, error, int](seqReaderIOErr)(cfg1)(cfg2)(ctx)()
seqReaderIOResult := SequenceReaderIO(seqReaderIOErr)(cfg1)(cfg2)(ctx)()
assert.True(t, E.IsLeft(seqReaderIOResult))
// Test Traverse with error
@@ -648,7 +648,7 @@ func TestFlipIntegration(t *testing.T) {
travTransform := func(n int) ReaderReaderIOEither[Config1, Context, error, string] {
return Of[Config1, Context, error](fmt.Sprintf("%d", n))
}
travResult := Traverse[Config2, Config1, Context, error, int, string](travTransform)(travErr)(cfg1)(cfg2)(ctx)()
travResult := Traverse[Config2](travTransform)(travErr)(cfg1)(cfg2)(ctx)()
assert.True(t, E.IsLeft(travResult))
// Test TraverseReader with error
@@ -656,7 +656,7 @@ func TestFlipIntegration(t *testing.T) {
travReaderTransform := func(n int) R.Reader[Config1, string] {
return R.Of[Config1](fmt.Sprintf("%d", n))
}
travReaderResult := TraverseReader[Config2, Config1, Context, error, int, string](travReaderTransform)(travReaderErr)(cfg1)(cfg2)(ctx)()
travReaderResult := TraverseReader[Config2, Config1, Context, error](travReaderTransform)(travReaderErr)(cfg1)(cfg2)(ctx)()
assert.True(t, E.IsLeft(travReaderResult))
})
}

View File

@@ -45,7 +45,7 @@ func FromReaderOption[R, C, A, E any](onNone Lazy[E]) Kleisli[R, C, E, ReaderOpt
//go:inline
func FromReaderIOEither[C, E, R, A any](ma ReaderIOEither[R, E, A]) ReaderReaderIOEither[R, C, E, A] {
return reader.MonadMap[R](ma, RIOE.FromIOEither[C])
return reader.MonadMap(ma, RIOE.FromIOEither[C])
}
//go:inline
@@ -55,12 +55,12 @@ func FromReaderIO[C, E, R, A any](ma ReaderIO[R, A]) ReaderReaderIOEither[R, C,
//go:inline
func RightReaderIO[C, E, R, A any](ma ReaderIO[R, A]) ReaderReaderIOEither[R, C, E, A] {
return reader.MonadMap[R](ma, RIOE.RightIO[C, E, A])
return reader.MonadMap(ma, RIOE.RightIO[C, E, A])
}
//go:inline
func LeftReaderIO[C, A, R, E any](me ReaderIO[R, E]) ReaderReaderIOEither[R, C, E, A] {
return reader.MonadMap[R](me, RIOE.LeftIO[C, A, E])
return reader.MonadMap(me, RIOE.LeftIO[C, A, E])
}
//go:inline
@@ -297,7 +297,7 @@ func ChainFirstReaderEitherK[C, E, R, A, B any](f RE.Kleisli[R, E, A, B]) Operat
//go:inline
func TapReaderEitherK[C, E, R, A, B any](f RE.Kleisli[R, E, A, B]) Operator[R, C, E, A, A] {
return ChainFirstReaderEitherK[C, E](f)
return ChainFirstReaderEitherK[C](f)
}
func ChainReaderOptionK[R, C, A, B, E any](onNone Lazy[E]) func(readeroption.Kleisli[R, A, B]) Operator[R, C, E, A, B] {
@@ -538,7 +538,7 @@ func FromIOEither[R, C, E, A any](ma IOEither[E, A]) ReaderReaderIOEither[R, C,
//go:inline
func FromReaderEither[R, C, E, A any](ma RE.ReaderEither[R, E, A]) ReaderReaderIOEither[R, C, E, A] {
return reader.MonadMap[R](ma, RIOE.FromEither[C])
return reader.MonadMap(ma, RIOE.FromEither[C])
}
//go:inline
@@ -587,12 +587,12 @@ func Flap[R, C, E, B, A any](a A) Operator[R, C, E, func(A) B, B] {
//go:inline
func MonadMapLeft[R, C, E1, E2, A any](fa ReaderReaderIOEither[R, C, E1, A], f func(E1) E2) ReaderReaderIOEither[R, C, E2, A] {
return reader.MonadMap[R](fa, RIOE.MapLeft[C, A, E1, E2](f))
return reader.MonadMap(fa, RIOE.MapLeft[C, A](f))
}
//go:inline
func MapLeft[R, C, A, E1, E2 any](f func(E1) E2) func(ReaderReaderIOEither[R, C, E1, A]) ReaderReaderIOEither[R, C, E2, A] {
return reader.Map[R](RIOE.MapLeft[C, A, E1, E2](f))
return reader.Map[R](RIOE.MapLeft[C, A](f))
}
//go:inline

View File

@@ -109,7 +109,7 @@ func TestMonadChain(t *testing.T) {
func TestChainFirst(t *testing.T) {
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
ChainFirst[OuterConfig, InnerConfig, error](func(v int) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
ChainFirst(func(v int) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
return Of[OuterConfig, InnerConfig, error](fmt.Sprintf("%d", v))
}),
)
@@ -128,7 +128,7 @@ func TestTap(t *testing.T) {
sideEffect := 0
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
Tap[OuterConfig, InnerConfig, error](func(v int) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
Tap(func(v int) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
sideEffect = v * 2
return Of[OuterConfig, InnerConfig, error]("ignored")
}),
@@ -187,7 +187,7 @@ func TestMonadApPar(t *testing.T) {
func TestFromEither(t *testing.T) {
t.Run("Right", func(t *testing.T) {
result := FromEither[OuterConfig, InnerConfig, error](E.Right[error](42))
result := FromEither[OuterConfig, InnerConfig](E.Right[error](42))
assert.Equal(t, E.Right[error](42), result(OuterConfig{})(InnerConfig{})())
})
@@ -246,7 +246,7 @@ func TestFromIOEither(t *testing.T) {
t.Run("Left", func(t *testing.T) {
err := errors.New("test error")
ioe := IOE.Left[int](err)
result := FromIOEither[OuterConfig, InnerConfig, error, int](ioe)
result := FromIOEither[OuterConfig, InnerConfig](ioe)
assert.Equal(t, E.Left[int](err), result(OuterConfig{})(InnerConfig{})())
})
}
@@ -273,14 +273,14 @@ func TestLeftReaderIO(t *testing.T) {
func TestFromReaderEither(t *testing.T) {
t.Run("Right", func(t *testing.T) {
re := RE.Right[OuterConfig, error](42)
result := FromReaderEither[OuterConfig, InnerConfig, error](re)
result := FromReaderEither[OuterConfig, InnerConfig](re)
assert.Equal(t, E.Right[error](42), result(OuterConfig{})(InnerConfig{})())
})
t.Run("Left", func(t *testing.T) {
err := errors.New("test error")
re := RE.Left[OuterConfig, int](err)
result := FromReaderEither[OuterConfig, InnerConfig, error, int](re)
result := FromReaderEither[OuterConfig, InnerConfig](re)
assert.Equal(t, E.Left[int](err), result(OuterConfig{})(InnerConfig{})())
})
}
@@ -288,14 +288,14 @@ func TestFromReaderEither(t *testing.T) {
func TestFromReaderIOEither(t *testing.T) {
t.Run("Right", func(t *testing.T) {
rioe := RIOE.Right[OuterConfig, error](42)
result := FromReaderIOEither[InnerConfig, error](rioe)
result := FromReaderIOEither[InnerConfig](rioe)
assert.Equal(t, E.Right[error](42), result(OuterConfig{})(InnerConfig{})())
})
t.Run("Left", func(t *testing.T) {
err := errors.New("test error")
rioe := RIOE.Left[OuterConfig, int](err)
result := FromReaderIOEither[InnerConfig, error, OuterConfig, int](rioe)
result := FromReaderIOEither[InnerConfig](rioe)
assert.Equal(t, E.Left[int](err), result(OuterConfig{})(InnerConfig{})())
})
}
@@ -339,7 +339,7 @@ func TestFromPredicate(t *testing.T) {
onFalse := func(n int) error { return fmt.Errorf("not positive: %d", n) }
t.Run("Predicate true", func(t *testing.T) {
result := FromPredicate[OuterConfig, InnerConfig, error](isPositive, onFalse)(5)
result := FromPredicate[OuterConfig, InnerConfig](isPositive, onFalse)(5)
assert.Equal(t, E.Right[error](5), result(OuterConfig{})(InnerConfig{})())
})
@@ -409,7 +409,7 @@ func TestMonadChainEitherK(t *testing.T) {
func TestChainFirstEitherK(t *testing.T) {
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
ChainFirstEitherK[OuterConfig, InnerConfig, error](func(v int) E.Either[error, string] {
ChainFirstEitherK[OuterConfig, InnerConfig](func(v int) E.Either[error, string] {
return E.Right[error](fmt.Sprintf("%d", v))
}),
)
@@ -428,7 +428,7 @@ func TestTapEitherK(t *testing.T) {
sideEffect := ""
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
TapEitherK[OuterConfig, InnerConfig, error](func(v int) E.Either[error, string] {
TapEitherK[OuterConfig, InnerConfig](func(v int) E.Either[error, string] {
sideEffect = fmt.Sprintf("%d", v)
return E.Right[error](sideEffect)
}),
@@ -577,7 +577,7 @@ func TestMonadTapReaderIOK(t *testing.T) {
func TestChainReaderEitherK(t *testing.T) {
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
ChainReaderEitherK[InnerConfig, error](func(v int) RE.ReaderEither[OuterConfig, error, string] {
ChainReaderEitherK[InnerConfig](func(v int) RE.ReaderEither[OuterConfig, error, string] {
return RE.Right[OuterConfig, error](fmt.Sprintf("%d", v))
}),
)
@@ -595,7 +595,7 @@ func TestMonadChainReaderEitherK(t *testing.T) {
func TestChainFirstReaderEitherK(t *testing.T) {
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
ChainFirstReaderEitherK[InnerConfig, error](func(v int) RE.ReaderEither[OuterConfig, error, string] {
ChainFirstReaderEitherK[InnerConfig](func(v int) RE.ReaderEither[OuterConfig, error, string] {
return RE.Right[OuterConfig, error](fmt.Sprintf("%d", v))
}),
)
@@ -614,7 +614,7 @@ func TestTapReaderEitherK(t *testing.T) {
sideEffect := ""
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
TapReaderEitherK[InnerConfig, error](func(v int) RE.ReaderEither[OuterConfig, error, string] {
TapReaderEitherK[InnerConfig](func(v int) RE.ReaderEither[OuterConfig, error, string] {
sideEffect = fmt.Sprintf("%d", v)
return RE.Right[OuterConfig, error](sideEffect)
}),
@@ -709,7 +709,7 @@ func TestTapReaderOptionK(t *testing.T) {
func TestChainIOEitherK(t *testing.T) {
g := F.Pipe1(
Of[OuterConfig, InnerConfig, error](1),
ChainIOEitherK[OuterConfig, InnerConfig, error](func(v int) IOE.IOEither[error, string] {
ChainIOEitherK[OuterConfig, InnerConfig](func(v int) IOE.IOEither[error, string] {
return IOE.Right[error](fmt.Sprintf("%d", v))
}),
)
@@ -850,7 +850,7 @@ func TestAlt(t *testing.T) {
}
g := F.Pipe1(
Right[OuterConfig, InnerConfig, error](42),
Alt[OuterConfig, InnerConfig, error, int](second),
Alt(second),
)
assert.Equal(t, E.Right[error](42), g(OuterConfig{})(InnerConfig{})())
})
@@ -862,7 +862,7 @@ func TestAlt(t *testing.T) {
}
g := F.Pipe1(
Left[OuterConfig, InnerConfig, int](err),
Alt[OuterConfig, InnerConfig, error, int](second),
Alt(second),
)
assert.Equal(t, E.Right[error](99), g(OuterConfig{})(InnerConfig{})())
})
@@ -939,7 +939,7 @@ func TestCompositionWithBothContexts(t *testing.T) {
Map[OuterConfig, InnerConfig, error](func(cfg OuterConfig) string {
return cfg.database
}),
Chain[OuterConfig, InnerConfig, error](func(db string) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
Chain(func(db string) ReaderReaderIOEither[OuterConfig, InnerConfig, error, string] {
return func(r OuterConfig) RIOE.ReaderIOEither[InnerConfig, error, string] {
return func(c InnerConfig) IOE.IOEither[error, string] {
return IOE.Right[error](fmt.Sprintf("%s:%s", db, c.apiKey))

View File

@@ -1,11 +1,25 @@
@echo off
setlocal enabledelayedexpansion
REM Get the directory to scan from parameter or use current directory
set "SCAN_DIR=%~1"
if "%SCAN_DIR%"=="" set "SCAN_DIR=."
REM Convert to absolute path
pushd "%SCAN_DIR%" 2>nul
if errorlevel 1 (
echo Error: Directory "%SCAN_DIR%" does not exist
exit /b 1
)
set "SCAN_DIR=%CD%"
popd
echo Finding and fixing unnecessary type arguments...
echo Scanning directory: %SCAN_DIR%
echo.
REM Find all Go files recursively
for /r %%f in (*.go) do (
REM Find all Go files recursively in the specified directory
for /r "%SCAN_DIR%" %%f in (*.go) do (
echo Checking %%f...
REM Run gopls check and capture output

View File

@@ -0,0 +1,219 @@
// Package builder demonstrates a functional builder pattern using fp-go.
// It shows how to construct and validate Person objects using lenses, prisms,
// and functional composition, separating the building phase (PartialPerson)
// from the validated result (Person).
package builder
import (
"github.com/IBM/fp-go/v2/array"
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/endomorphism"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/monoid"
"github.com/IBM/fp-go/v2/optics/prism"
"github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/reader"
"github.com/IBM/fp-go/v2/readeroption"
S "github.com/IBM/fp-go/v2/string"
)
var (
// partialPersonLenses provides lens accessors for PartialPerson fields.
// Generated by the fp-go:Lens directive in types.go.
partialPersonLenses = MakePartialPersonRefLenses()
// personLenses provides lens accessors for Person fields.
// Generated by the fp-go:Lens directive in types.go.
personLenses = MakePersonRefLenses()
// emptyPartialPerson is a zero-value PartialPerson used as a starting point for building.
emptyPartialPerson = &PartialPerson{}
// emptyPerson is a zero-value Person used as a starting point for validation.
emptyPerson = &Person{}
// monoidPartialPerson is a monoid for composing endomorphisms on PartialPerson.
// Allows combining multiple builder operations.
monoidPartialPerson = endomorphism.Monoid[*PartialPerson]()
// monoidPerson is a monoid for composing endomorphisms on Person.
monoidPerson = endomorphism.Monoid[*Person]()
// allOfPartialPerson combines multiple PartialPerson endomorphisms into one.
allOfPartialPerson = monoid.ConcatAll(monoidPartialPerson)
// foldPartialPersons folds an array of PartialPerson operations into a single ReaderOption.
foldPartialPersons = array.Fold(readeroption.ApplicativeMonoid[*PartialPerson](monoidPerson))
// foldPersons folds an array of Person operations into a single Reader.
foldPersons = array.Fold(reader.ApplicativeMonoid[*Person](monoidPartialPerson))
// namePrism is a prism that validates and converts between string and NonEmptyString.
// It ensures the name is not empty, returning None if validation fails.
//
// Forward direction: string -> Option[NonEmptyString] (validates non-empty)
// Reverse direction: NonEmptyString -> string (always succeeds)
namePrism = prism.MakePrismWithName(
func(s string) Option[NonEmptyString] {
if S.IsEmpty(s) {
return option.None[NonEmptyString]()
}
return option.Of(NonEmptyString(s))
},
func(ns NonEmptyString) string {
return string(ns)
},
"NonEmptyString",
)
// agePrism is a prism that validates and converts between int and AdultAge.
// It ensures the age is at least 18, returning None if validation fails.
//
// Forward direction: int -> Option[AdultAge] (validates >= 18)
// Reverse direction: AdultAge -> int (always succeeds)
agePrism = prism.MakePrismWithName(
func(a int) Option[AdultAge] {
if a < 18 {
return option.None[AdultAge]()
}
return option.Of(AdultAge(a))
},
func(aa AdultAge) int {
return int(aa)
},
"AdultAge",
)
// WithName is a builder function that sets the Name field of a PartialPerson.
// It returns an endomorphism that can be composed with other builder operations.
//
// Example:
// builder := WithName("Alice")
// person := builder(&PartialPerson{})
WithName = partialPersonLenses.Name.Set
// WithAge is a builder function that sets the Age field of a PartialPerson.
// It returns an endomorphism that can be composed with other builder operations.
//
// Example:
// builder := WithAge(25)
// person := builder(&PartialPerson{})
WithAge = partialPersonLenses.Age.Set
// PersonPrism is a prism that converts between a builder pattern (Endomorphism[*PartialPerson])
// and a validated Person in both directions.
//
// Forward direction (buildPerson): Endomorphism[*PartialPerson] -> Option[*Person]
// - Applies the builder to an empty PartialPerson
// - Validates all fields using namePrism and agePrism
// - Returns Some(*Person) if all validations pass, None otherwise
//
// Reverse direction (buildEndomorphism): *Person -> Endomorphism[*PartialPerson]
// - Extracts validated fields from Person
// - Converts them back to raw types
// - Returns a builder that reconstructs the PartialPerson
//
// This enables bidirectional conversion with validation in the forward direction.
PersonPrism = prism.MakePrismWithName(buildPerson(), buildEndomorphism(), "Person")
)
// MakePerson creates a builder (endomorphism) that sets both name and age fields
// on a PartialPerson. This is a convenience function that combines WithName and
// WithAge into a single builder operation.
//
// Parameters:
// - name: The name to set (will be validated later when converting to Person)
// - age: The age to set (will be validated later when converting to Person)
//
// Returns:
//
// An endomorphism that applies both field setters to a PartialPerson
//
// Example:
//
// builder := MakePerson("Alice", 25)
// partial := builder(&PartialPerson{})
// // partial now has Name="Alice" and Age=25
func MakePerson(name string, age int) Endomorphism[*PartialPerson] {
return F.Pipe1(
A.From(
WithName(name),
WithAge(age),
),
allOfPartialPerson)
}
// buildPerson constructs the forward direction of PersonPrism.
// It takes a builder (Endomorphism[*PartialPerson]) and attempts to create
// a validated Person by:
// 1. Applying the builder to an empty PartialPerson
// 2. Extracting and validating the Name field using namePrism
// 3. Extracting and validating the Age field using agePrism
// 4. Combining the validated fields into a Person
//
// Returns:
//
// A ReaderOption that produces Some(*Person) if all validations pass,
// or None if any validation fails.
func buildPerson() ReaderOption[Endomorphism[*PartialPerson], *Person] {
// maybeName extracts the name from PartialPerson, validates it,
// and creates a setter for the Person's Name field if valid
maybeName := F.Flow3(
partialPersonLenses.Name.Get,
namePrism.GetOption,
option.Map(personLenses.Name.Set),
)
// maybeAge extracts the age from PartialPerson, validates it,
// and creates a setter for the Person's Age field if valid
maybeAge := F.Flow3(
partialPersonLenses.Age.Get,
agePrism.GetOption,
option.Map(personLenses.Age.Set),
)
// Combine the field validators and apply them to build a Person
return F.Pipe2(
A.From(maybeName, maybeAge),
foldPartialPersons,
readeroption.Promap(reader.Read[*PartialPerson](emptyPartialPerson), reader.Read[*Person](emptyPerson)),
)
}
// buildEndomorphism constructs the reverse direction of PersonPrism.
// It takes a validated Person and creates a builder (Endomorphism[*PartialPerson])
// that can reconstruct the equivalent PartialPerson by:
// 1. Extracting the validated Name field and converting it back to string
// 2. Extracting the validated Age field and converting it back to int
// 3. Creating setters for the PartialPerson fields
//
// This reverse direction always succeeds because Person contains only valid data.
//
// Returns:
//
// A Reader that produces an endomorphism for reconstructing a PartialPerson
func buildEndomorphism() Reader[*Person, Endomorphism[*PartialPerson]] {
// name extracts the validated name, converts it to string,
// and creates a setter for PartialPerson's Name field
name := F.Flow3(
personLenses.Name.Get,
namePrism.ReverseGet,
partialPersonLenses.Name.Set,
)
// age extracts the validated age, converts it to int,
// and creates a setter for PartialPerson's Age field
age := F.Flow3(
personLenses.Age.Get,
agePrism.ReverseGet,
partialPersonLenses.Age.Set,
)
// Combine the field extractors into a single builder
return F.Pipe1(
A.From(name, age),
foldPersons,
)
}

View File

@@ -0,0 +1,30 @@
package builder
import (
"testing"
"github.com/IBM/fp-go/v2/endomorphism"
"github.com/IBM/fp-go/v2/option"
"github.com/stretchr/testify/assert"
)
func TestBuilderPrism(t *testing.T) {
b1 := MakePerson("Carsten", 55)
// this should be a valid person
p1, ok := option.Unwrap(PersonPrism.GetOption(b1))
assert.True(t, ok)
// convert back to a builder
b2 := PersonPrism.ReverseGet(p1)
// change the name
b3 := endomorphism.Chain(WithName("Jan"))(b1)
p2 := PersonPrism.GetOption(b2)
p3 := PersonPrism.GetOption(b3)
assert.Equal(t, p2, option.Of(p1))
assert.NotEqual(t, p3, option.Of(p1))
}

131
v2/samples/builder/codec.go Normal file
View File

@@ -0,0 +1,131 @@
// Package builder demonstrates codec-based validation and encoding/decoding
// for Person objects using fp-go's optics and validation framework.
//
// This file extends the builder pattern with codec functionality, enabling:
// - Bidirectional transformation between PartialPerson and Person
// - Validation with detailed error reporting
// - Type-safe encoding and decoding operations
package builder
import (
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/endomorphism"
F "github.com/IBM/fp-go/v2/function"
"github.com/IBM/fp-go/v2/identity"
"github.com/IBM/fp-go/v2/monoid"
"github.com/IBM/fp-go/v2/optics/codec"
"github.com/IBM/fp-go/v2/optics/codec/decode"
"github.com/IBM/fp-go/v2/optics/codec/validate"
"github.com/IBM/fp-go/v2/optics/codec/validation"
)
type (
// PersonCodec is a codec type that handles bidirectional transformation
// between Person and PartialPerson using endomorphisms.
//
// Type parameters:
// - A: *Person - The validated target type
// - O: Endomorphism[*PartialPerson] - The output encoding type (builder)
// - I: Endomorphism[*PartialPerson] - The input decoding type (builder)
//
// This codec enables:
// - Validation: Converting a PartialPerson builder to a validated Person
// - Encoding: Converting a Person back to a PartialPerson builder
PersonCodec = Type[*Person, Endomorphism[*PartialPerson], Endomorphism[*PartialPerson]]
)
var (
// nameCodec is a codec for validating and transforming name fields.
// It uses namePrism to ensure names are non-empty strings.
//
// Validation: string -> Result[NonEmptyString]
// Encoding: NonEmptyString -> string
nameCodec = codec.FromRefinement(namePrism)
// ageCodec is a codec for validating and transforming age fields.
// It uses agePrism to ensure ages meet adult criteria (>= 18).
//
// Validation: int -> Result[AdultAge]
// Encoding: AdultAge -> int
ageCodec = codec.FromRefinement(agePrism)
)
// makePersonValidate creates a validation function that transforms a PartialPerson
// builder (endomorphism) into a validated Person.
//
// The validation process:
// 1. Applies the builder endomorphism to an empty PartialPerson
// 2. Extracts and validates the Name field using nameCodec
// 3. Extracts and validates the Age field using ageCodec
// 4. Combines all validations using applicative composition
// 5. Returns either a validated Person or a collection of validation errors
//
// This function uses the Reader monad to thread validation context through
// the computation, and ReaderEither to accumulate validation errors.
//
// Returns:
//
// A Validate function that takes a PartialPerson builder and returns
// a Reader that produces a Validation result (either errors or a Person)
func makePersonValidate() Validate[Endomorphism[*PartialPerson], *Person] {
// Create a monoid for combining validation operations
// This allows multiple field validations to be composed together
rdrMonoid := validate.ApplicativeMonoid[*PartialPerson](endomorphism.Monoid[*Person]())
// allOfRdr combines an array of validation readers into a single reader
allOfRdr := monoid.ConcatAll(rdrMonoid)
// valName validates the Name field:
// 1. Extract name from PartialPerson
// 2. Validate using nameCodec (ensures non-empty)
// 3. Map to a Person name setter if valid
valName := F.Flow3(
partialPersonLenses.Name.Get,
nameCodec.Validate,
decode.Map[validation.Context](personLenses.Name.Set),
)
// valAge validates the Age field:
// 1. Extract age from PartialPerson
// 2. Validate using ageCodec (ensures >= 18)
// 3. Map to a Person age setter if valid
valAge := F.Flow3(
partialPersonLenses.Age.Get,
ageCodec.Validate,
decode.Map[validation.Context](personLenses.Age.Set),
)
// Collect all field validators
vals := A.From(valName, valAge)
// Combine all validations and apply to an empty Person
return F.Flow3(
identity.Flap[*PartialPerson](emptyPartialPerson),
allOfRdr(vals),
decode.Map[validation.Context](identity.Flap[*Person](emptyPerson)),
)
}
// makePersonCodec creates a complete codec for Person objects.
//
// The codec provides:
// - Type checking: Verifies if a value is a *Person
// - Validation: Converts PartialPerson builders to validated Person instances
// - Encoding: Converts Person instances back to PartialPerson builders
//
// This enables bidirectional transformation with validation:
// - Decode: Endomorphism[*PartialPerson] -> Validation[*Person]
// - Encode: *Person -> Endomorphism[*PartialPerson]
//
// Returns:
//
// A PersonCodec that can validate, encode, and decode Person objects
func makePersonCodec() PersonCodec {
return codec.MakeType(
"Person",
codec.Is[*Person](),
makePersonValidate(),
buildEndomorphism(),
)
}

View File

@@ -0,0 +1,331 @@
package builder
import (
"testing"
A "github.com/IBM/fp-go/v2/array"
"github.com/IBM/fp-go/v2/either"
"github.com/IBM/fp-go/v2/endomorphism"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestMakePersonValidate_ValidPerson tests validation of a valid person
func TestMakePersonValidate_ValidPerson(t *testing.T) {
// Arrange
validate := makePersonValidate()
builder := MakePerson("Alice", 25)
ctx := A.Of(validation.ContextEntry{Type: "Person", Actual: builder})
// Act
result := validate(builder)(ctx)
// Assert
assert.True(t, either.IsRight(result), "Expected validation to succeed")
person, _ := either.Unwrap(result)
require.NotNil(t, person, "Expected to unwrap person")
assert.Equal(t, NonEmptyString("Alice"), person.Name)
assert.Equal(t, AdultAge(25), person.Age)
}
// TestMakePersonValidate_EmptyName tests validation failure for empty name
func TestMakePersonValidate_EmptyName(t *testing.T) {
// Arrange
validate := makePersonValidate()
builder := MakePerson("", 25)
ctx := A.Of(validation.ContextEntry{Type: "Person", Actual: builder})
// Act
result := validate(builder)(ctx)
// Assert
assert.True(t, either.IsLeft(result), "Expected validation to fail for empty name")
_, errors := either.Unwrap(result)
assert.NotEmpty(t, errors, "Expected validation errors")
}
// TestMakePersonValidate_InvalidAge tests validation failure for age < 18
func TestMakePersonValidate_InvalidAge(t *testing.T) {
// Arrange
validate := makePersonValidate()
builder := MakePerson("Bob", 15)
ctx := A.Of(validation.ContextEntry{Type: "Person", Actual: builder})
// Act
result := validate(builder)(ctx)
// Assert
assert.True(t, either.IsLeft(result), "Expected validation to fail for age < 18")
_, errors := either.Unwrap(result)
assert.NotEmpty(t, errors, "Expected validation errors")
}
// TestMakePersonValidate_MultipleErrors tests validation with multiple errors
func TestMakePersonValidate_MultipleErrors(t *testing.T) {
// Arrange
validate := makePersonValidate()
builder := MakePerson("", 10) // Both empty name and invalid age
ctx := A.Of(validation.ContextEntry{Type: "Person", Actual: builder})
// Act
result := validate(builder)(ctx)
// Assert
assert.True(t, either.IsLeft(result), "Expected validation to fail")
_, errors := either.Unwrap(result)
assert.Len(t, errors, 2, "Expected two validation errors")
}
// TestMakePersonValidate_BoundaryAge tests validation at age boundary (18)
func TestMakePersonValidate_BoundaryAge(t *testing.T) {
// Arrange
validate := makePersonValidate()
builder := MakePerson("Charlie", 18)
ctx := A.Of(validation.ContextEntry{Type: "Person", Actual: builder})
// Act
result := validate(builder)(ctx)
// Assert
assert.True(t, either.IsRight(result), "Expected validation to succeed for age 18")
person, _ := either.Unwrap(result)
require.NotNil(t, person, "Expected to unwrap person")
assert.Equal(t, AdultAge(18), person.Age)
}
// TestMakePersonCodec_Decode tests the codec's Decode method
func TestMakePersonCodec_Decode(t *testing.T) {
// Arrange
codec := makePersonCodec()
builder := MakePerson("Diana", 30)
// Act
result := codec.Decode(builder)
// Assert
assert.True(t, either.IsRight(result), "Expected decode to succeed")
person, _ := either.Unwrap(result)
require.NotNil(t, person, "Expected to unwrap person")
assert.Equal(t, NonEmptyString("Diana"), person.Name)
assert.Equal(t, AdultAge(30), person.Age)
}
// TestMakePersonCodec_Decode_Invalid tests the codec's Decode method with invalid data
func TestMakePersonCodec_Decode_Invalid(t *testing.T) {
// Arrange
codec := makePersonCodec()
builder := MakePerson("", 10) // Invalid name and age
// Act
result := codec.Decode(builder)
// Assert
assert.True(t, either.IsLeft(result), "Expected decode to fail")
_, errors := either.Unwrap(result)
assert.Len(t, errors, 2, "Expected two validation errors")
}
// TestMakePersonCodec_Encode tests the codec's Encode method
func TestMakePersonCodec_Encode(t *testing.T) {
// Arrange
codec := makePersonCodec()
person := &Person{
Name: NonEmptyString("Eve"),
Age: AdultAge(28),
}
// Act
builder := codec.Encode(person)
// Apply the builder to get a PartialPerson
partial := builder(emptyPartialPerson)
// Assert
assert.Equal(t, "Eve", partial.Name)
assert.Equal(t, 28, partial.Age)
}
// TestMakePersonCodec_RoundTrip tests encoding and decoding round-trip
func TestMakePersonCodec_RoundTrip(t *testing.T) {
// Arrange
codec := makePersonCodec()
originalPerson := &Person{
Name: NonEmptyString("Frank"),
Age: AdultAge(35),
}
// Act - Encode to builder
builder := codec.Encode(originalPerson)
// Decode back to person
result := codec.Decode(builder)
// Assert
assert.True(t, either.IsRight(result), "Expected round-trip to succeed")
decodedPerson, _ := either.Unwrap(result)
require.NotNil(t, decodedPerson, "Expected to unwrap person")
assert.Equal(t, originalPerson.Name, decodedPerson.Name)
assert.Equal(t, originalPerson.Age, decodedPerson.Age)
}
// TestMakePersonCodec_Name tests the codec's Name method
func TestMakePersonCodec_Name(t *testing.T) {
// Arrange
codec := makePersonCodec()
// Act
name := codec.Name()
// Assert
assert.Equal(t, "Person", name)
}
// TestNameCodec_Validate tests the name codec validation
func TestNameCodec_Validate(t *testing.T) {
tests := []struct {
name string
input string
wantValid bool
}{
{
name: "valid name",
input: "Alice",
wantValid: true,
},
{
name: "empty name",
input: "",
wantValid: false,
},
{
name: "whitespace name",
input: " ",
wantValid: true, // Non-empty string, even if whitespace
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
ctx := A.Of(validation.ContextEntry{Type: "Name", Actual: tt.input})
// Act
result := nameCodec.Validate(tt.input)(ctx)
// Assert
if tt.wantValid {
assert.True(t, either.IsRight(result), "Expected validation to succeed")
} else {
assert.True(t, either.IsLeft(result), "Expected validation to fail")
}
})
}
}
// TestAgeCodec_Validate tests the age codec validation
func TestAgeCodec_Validate(t *testing.T) {
tests := []struct {
name string
input int
wantValid bool
}{
{
name: "valid adult age",
input: 25,
wantValid: true,
},
{
name: "boundary age 18",
input: 18,
wantValid: true,
},
{
name: "minor age",
input: 17,
wantValid: false,
},
{
name: "zero age",
input: 0,
wantValid: false,
},
{
name: "negative age",
input: -5,
wantValid: false,
},
{
name: "very old age",
input: 120,
wantValid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
ctx := A.Of(validation.ContextEntry{Type: "Age", Actual: tt.input})
// Act
result := ageCodec.Validate(tt.input)(ctx)
// Assert
if tt.wantValid {
assert.True(t, either.IsRight(result), "Expected validation to succeed")
} else {
assert.True(t, either.IsLeft(result), "Expected validation to fail")
}
})
}
}
// TestMakePersonCodec_WithComposedBuilders tests codec with composed builders
func TestMakePersonCodec_WithComposedBuilders(t *testing.T) {
// Arrange
codec := makePersonCodec()
// Create a builder by composing individual field setters
builder := endomorphism.Chain(
WithAge(40),
)(WithName("Grace"))
// Act
result := codec.Decode(builder)
// Assert
assert.True(t, either.IsRight(result), "Expected decode to succeed")
person, _ := either.Unwrap(result)
require.NotNil(t, person, "Expected to unwrap person")
assert.Equal(t, NonEmptyString("Grace"), person.Name)
assert.Equal(t, AdultAge(40), person.Age)
}
// TestMakePersonCodec_PartialBuilder tests codec with partial builder (missing fields)
func TestMakePersonCodec_PartialBuilder(t *testing.T) {
// Arrange
codec := makePersonCodec()
// Create a builder that only sets name
builder := WithName("Henry")
// Act
result := codec.Decode(builder)
// Assert
// Should fail because age is 0 (< 18)
assert.True(t, either.IsLeft(result), "Expected decode to fail for missing age")
}

View File

@@ -0,0 +1,223 @@
package builder
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2026-01-23 12:56:01.1431839 +0100 CET m=+0.004353901
import (
__lens "github.com/IBM/fp-go/v2/optics/lens"
__option "github.com/IBM/fp-go/v2/option"
__prism "github.com/IBM/fp-go/v2/optics/prism"
__lens_option "github.com/IBM/fp-go/v2/optics/lens/option"
__iso_option "github.com/IBM/fp-go/v2/optics/iso/option"
)
// PartialPersonLenses provides lenses for accessing fields of PartialPerson
type PartialPersonLenses struct {
// mandatory fields
Name __lens.Lens[PartialPerson, string]
Age __lens.Lens[PartialPerson, int]
// optional fields
NameO __lens_option.LensO[PartialPerson, string]
AgeO __lens_option.LensO[PartialPerson, int]
}
// PartialPersonRefLenses provides lenses for accessing fields of PartialPerson via a reference to PartialPerson
type PartialPersonRefLenses struct {
// mandatory fields
Name __lens.Lens[*PartialPerson, string]
Age __lens.Lens[*PartialPerson, int]
// optional fields
NameO __lens_option.LensO[*PartialPerson, string]
AgeO __lens_option.LensO[*PartialPerson, int]
// prisms
NameP __prism.Prism[*PartialPerson, string]
AgeP __prism.Prism[*PartialPerson, int]
}
// PartialPersonPrisms provides prisms for accessing fields of PartialPerson
type PartialPersonPrisms struct {
Name __prism.Prism[PartialPerson, string]
Age __prism.Prism[PartialPerson, int]
}
// MakePartialPersonLenses creates a new PartialPersonLenses with lenses for all fields
func MakePartialPersonLenses() PartialPersonLenses {
// mandatory lenses
lensName := __lens.MakeLensWithName(
func(s PartialPerson) string { return s.Name },
func(s PartialPerson, v string) PartialPerson { s.Name = v; return s },
"PartialPerson.Name",
)
lensAge := __lens.MakeLensWithName(
func(s PartialPerson) int { return s.Age },
func(s PartialPerson, v int) PartialPerson { s.Age = v; return s },
"PartialPerson.Age",
)
// optional lenses
lensNameO := __lens_option.FromIso[PartialPerson](__iso_option.FromZero[string]())(lensName)
lensAgeO := __lens_option.FromIso[PartialPerson](__iso_option.FromZero[int]())(lensAge)
return PartialPersonLenses{
// mandatory lenses
Name: lensName,
Age: lensAge,
// optional lenses
NameO: lensNameO,
AgeO: lensAgeO,
}
}
// MakePartialPersonRefLenses creates a new PartialPersonRefLenses with lenses for all fields
func MakePartialPersonRefLenses() PartialPersonRefLenses {
// mandatory lenses
lensName := __lens.MakeLensStrictWithName(
func(s *PartialPerson) string { return s.Name },
func(s *PartialPerson, v string) *PartialPerson { s.Name = v; return s },
"(*PartialPerson).Name",
)
lensAge := __lens.MakeLensStrictWithName(
func(s *PartialPerson) int { return s.Age },
func(s *PartialPerson, v int) *PartialPerson { s.Age = v; return s },
"(*PartialPerson).Age",
)
// optional lenses
lensNameO := __lens_option.FromIso[*PartialPerson](__iso_option.FromZero[string]())(lensName)
lensAgeO := __lens_option.FromIso[*PartialPerson](__iso_option.FromZero[int]())(lensAge)
return PartialPersonRefLenses{
// mandatory lenses
Name: lensName,
Age: lensAge,
// optional lenses
NameO: lensNameO,
AgeO: lensAgeO,
}
}
// MakePartialPersonPrisms creates a new PartialPersonPrisms with prisms for all fields
func MakePartialPersonPrisms() PartialPersonPrisms {
_fromNonZeroName := __option.FromNonZero[string]()
_prismName := __prism.MakePrismWithName(
func(s PartialPerson) __option.Option[string] { return _fromNonZeroName(s.Name) },
func(v string) PartialPerson {
return PartialPerson{ Name: v }
},
"PartialPerson.Name",
)
_fromNonZeroAge := __option.FromNonZero[int]()
_prismAge := __prism.MakePrismWithName(
func(s PartialPerson) __option.Option[int] { return _fromNonZeroAge(s.Age) },
func(v int) PartialPerson {
return PartialPerson{ Age: v }
},
"PartialPerson.Age",
)
return PartialPersonPrisms {
Name: _prismName,
Age: _prismAge,
}
}
// PersonLenses provides lenses for accessing fields of Person
type PersonLenses struct {
// mandatory fields
Name __lens.Lens[Person, NonEmptyString]
Age __lens.Lens[Person, AdultAge]
// optional fields
NameO __lens_option.LensO[Person, NonEmptyString]
AgeO __lens_option.LensO[Person, AdultAge]
}
// PersonRefLenses provides lenses for accessing fields of Person via a reference to Person
type PersonRefLenses struct {
// mandatory fields
Name __lens.Lens[*Person, NonEmptyString]
Age __lens.Lens[*Person, AdultAge]
// optional fields
NameO __lens_option.LensO[*Person, NonEmptyString]
AgeO __lens_option.LensO[*Person, AdultAge]
// prisms
NameP __prism.Prism[*Person, NonEmptyString]
AgeP __prism.Prism[*Person, AdultAge]
}
// PersonPrisms provides prisms for accessing fields of Person
type PersonPrisms struct {
Name __prism.Prism[Person, NonEmptyString]
Age __prism.Prism[Person, AdultAge]
}
// MakePersonLenses creates a new PersonLenses with lenses for all fields
func MakePersonLenses() PersonLenses {
// mandatory lenses
lensName := __lens.MakeLensWithName(
func(s Person) NonEmptyString { return s.Name },
func(s Person, v NonEmptyString) Person { s.Name = v; return s },
"Person.Name",
)
lensAge := __lens.MakeLensWithName(
func(s Person) AdultAge { return s.Age },
func(s Person, v AdultAge) Person { s.Age = v; return s },
"Person.Age",
)
// optional lenses
lensNameO := __lens_option.FromIso[Person](__iso_option.FromZero[NonEmptyString]())(lensName)
lensAgeO := __lens_option.FromIso[Person](__iso_option.FromZero[AdultAge]())(lensAge)
return PersonLenses{
// mandatory lenses
Name: lensName,
Age: lensAge,
// optional lenses
NameO: lensNameO,
AgeO: lensAgeO,
}
}
// MakePersonRefLenses creates a new PersonRefLenses with lenses for all fields
func MakePersonRefLenses() PersonRefLenses {
// mandatory lenses
lensName := __lens.MakeLensStrictWithName(
func(s *Person) NonEmptyString { return s.Name },
func(s *Person, v NonEmptyString) *Person { s.Name = v; return s },
"(*Person).Name",
)
lensAge := __lens.MakeLensStrictWithName(
func(s *Person) AdultAge { return s.Age },
func(s *Person, v AdultAge) *Person { s.Age = v; return s },
"(*Person).Age",
)
// optional lenses
lensNameO := __lens_option.FromIso[*Person](__iso_option.FromZero[NonEmptyString]())(lensName)
lensAgeO := __lens_option.FromIso[*Person](__iso_option.FromZero[AdultAge]())(lensAge)
return PersonRefLenses{
// mandatory lenses
Name: lensName,
Age: lensAge,
// optional lenses
NameO: lensNameO,
AgeO: lensAgeO,
}
}
// MakePersonPrisms creates a new PersonPrisms with prisms for all fields
func MakePersonPrisms() PersonPrisms {
_fromNonZeroName := __option.FromNonZero[NonEmptyString]()
_prismName := __prism.MakePrismWithName(
func(s Person) __option.Option[NonEmptyString] { return _fromNonZeroName(s.Name) },
func(v NonEmptyString) Person {
return Person{ Name: v }
},
"Person.Name",
)
_fromNonZeroAge := __option.FromNonZero[AdultAge]()
_prismAge := __prism.MakePrismWithName(
func(s Person) __option.Option[AdultAge] { return _fromNonZeroAge(s.Age) },
func(v AdultAge) Person {
return Person{ Age: v }
},
"Person.Age",
)
return PersonPrisms {
Name: _prismName,
Age: _prismAge,
}
}

View File

@@ -0,0 +1,88 @@
// Package builder demonstrates the builder pattern using functional programming concepts
// from fp-go, including validation and transformation of data structures.
package builder
import (
"github.com/IBM/fp-go/v2/endomorphism"
"github.com/IBM/fp-go/v2/optics/codec"
"github.com/IBM/fp-go/v2/optics/codec/validate"
"github.com/IBM/fp-go/v2/optics/codec/validation"
"github.com/IBM/fp-go/v2/optics/lens"
"github.com/IBM/fp-go/v2/optics/prism"
"github.com/IBM/fp-go/v2/option"
"github.com/IBM/fp-go/v2/reader"
"github.com/IBM/fp-go/v2/readeroption"
"github.com/IBM/fp-go/v2/result"
)
//go:generate go run ../../main.go lens --dir . --filename gen_lens.go
type (
// Endomorphism represents a function from type A to type A.
// It is an alias for endomorphism.Endomorphism[A].
Endomorphism[A any] = endomorphism.Endomorphism[A]
// Result represents a computation that may succeed with a value of type A or fail with an error.
// It is an alias for result.Result[A].
Result[A any] = result.Result[A]
// Option represents an optional value of type A that may or may not be present.
// It is an alias for option.Option[A].
Option[A any] = option.Option[A]
// ReaderOption represents a computation that depends on an environment R and produces
// an optional value of type A. It is an alias for readeroption.ReaderOption[R, A].
ReaderOption[R, A any] = readeroption.ReaderOption[R, A]
// Reader represents a computation that depends on an environment R and produces
// a value of type A. It is an alias for reader.Reader[R, A].
Reader[R, A any] = reader.Reader[R, A]
Prism[S, A any] = prism.Prism[S, A]
Lens[S, A any] = lens.Lens[S, A]
Type[A, O, I any] = codec.Type[A, O, I]
Validate[I, A any] = validate.Validate[I, A]
Validation[A any] = validation.Validation[A]
Encode[A, O any] = codec.Encode[A, O]
// NonEmptyString is a string type that represents a validated non-empty string.
// It is used to ensure that string fields contain meaningful data.
NonEmptyString string
// AdultAge is an unsigned integer type that represents a validated age
// that meets adult criteria (typically >= 18).
AdultAge uint
)
// PartialPerson represents a person record with unvalidated fields.
// This type is typically used as an intermediate representation before
// validation is applied to create a Person instance.
//
// The fp-go:Lens directive generates lens functions for accessing and
// modifying the fields of this struct in a functional way.
//
// fp-go:Lens
type PartialPerson struct {
// Name is the person's name as a raw string, which may be empty or invalid.
Name string
// Age is the person's age as a raw integer, which may be negative or otherwise invalid.
Age int
}
// Person represents a person record with validated fields.
// All fields in this type have been validated and are guaranteed to meet
// specific business rules (non-empty name, adult age).
//
// The fp-go:Lens directive generates lens functions for accessing and
// modifying the fields of this struct in a functional way.
//
// fp-go:Lens
type Person struct {
// Name is the person's validated name, guaranteed to be non-empty.
Name NonEmptyString
// Age is the person's validated age, guaranteed to meet adult criteria.
Age AdultAge
}