mirror of
https://github.com/IBM/fp-go.git
synced 2026-02-28 13:12:03 +02:00
Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2d111e8ec | ||
|
|
ae141c85c6 | ||
|
|
1230b4581b | ||
|
|
70c831c8f9 | ||
|
|
cc0c14c7cf | ||
|
|
19159ad49e | ||
|
|
b9c8fb4ff1 | ||
|
|
4529e720bc | ||
|
|
ef8b2ea65d | ||
|
|
5bd7caafdd | ||
|
|
47ebcd79b1 | ||
|
|
dbad94806e | ||
|
|
c4cac1cb3e | ||
|
|
a3fdb03df4 | ||
|
|
47727fd514 | ||
|
|
ece7d088ea | ||
|
|
13d25eca32 | ||
|
|
a68e32308d | ||
|
|
61b948425b | ||
|
|
a276f3acff | ||
|
|
8c656a4297 | ||
|
|
bd9a642e93 | ||
|
|
3b55cae265 | ||
|
|
1472fa5a50 | ||
|
|
49deb57d24 | ||
|
|
abb55ddbd0 | ||
|
|
f6b01dffdc | ||
|
|
43b666edbb | ||
|
|
e42d765852 | ||
|
|
d2da8a32b4 | ||
|
|
7484af664b | ||
|
|
ae38e3f8f4 | ||
|
|
e0f854bda3 | ||
|
|
34786c3cd8 | ||
|
|
a7aa7e3560 | ||
|
|
ff2a4299b2 | ||
|
|
edd66d63e6 | ||
|
|
909aec8eba | ||
|
|
da0344f9bd | ||
|
|
cd79dd56b9 | ||
|
|
df07599a9e | ||
|
|
30ad0e4dd8 | ||
|
|
2374d7f1e4 | ||
|
|
eafc008798 | ||
|
|
46bf065e34 | ||
|
|
b4e303423b |
13
.github/workflows/build.yml
vendored
13
.github/workflows/build.yml
vendored
@@ -24,11 +24,11 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: ['1.20.x', '1.21.x', '1.22.x', '1.23.x', '1.24.x', '1.25.x']
|
||||
go-version: ['1.20.x', '1.21.x', '1.22.x', '1.23.x', '1.24.x', '1.25.x', '1.26.x']
|
||||
fail-fast: false # Continue with other versions if one fails
|
||||
steps:
|
||||
# full checkout for semantic-release
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up Go ${{ matrix.go-version }}
|
||||
@@ -42,6 +42,7 @@ jobs:
|
||||
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||
|
||||
- name: Upload coverage to Coveralls
|
||||
continue-on-error: true
|
||||
uses: coverallsapp/github-action@v2
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -64,9 +65,9 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: ['1.24.x', '1.25.x']
|
||||
go-version: ['1.24.x', '1.25.x', '1.26.x']
|
||||
steps:
|
||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up Go ${{ matrix.go-version }}
|
||||
@@ -81,6 +82,7 @@ jobs:
|
||||
go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||
|
||||
- name: Upload coverage to Coveralls
|
||||
continue-on-error: true
|
||||
uses: coverallsapp/github-action@v2
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -106,6 +108,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Coveralls Finished
|
||||
continue-on-error: true
|
||||
uses: coverallsapp/github-action@v2
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -126,7 +129,7 @@ jobs:
|
||||
steps:
|
||||
# full checkout for semantic-release
|
||||
- name: Full checkout
|
||||
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
|
||||
226
v2/AGENTS.md
Normal file
226
v2/AGENTS.md
Normal file
@@ -0,0 +1,226 @@
|
||||
# Agent Guidelines for fp-go/v2
|
||||
|
||||
This document provides guidelines for AI agents working on the fp-go/v2 project.
|
||||
|
||||
## Documentation Standards
|
||||
|
||||
### Go Doc Comments
|
||||
|
||||
1. **Use Standard Go Doc Format**
|
||||
- Do NOT use markdown-style links like `[text](url)`
|
||||
- Use simple type references: `ReaderResult`, `Validate[I, A]`, `validation.Success`
|
||||
- Go's documentation system will automatically create links
|
||||
|
||||
2. **Structure**
|
||||
```go
|
||||
// FunctionName does something useful.
|
||||
//
|
||||
// Longer description explaining the purpose and behavior.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - T: Description of type parameter
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - param: Description of parameter
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - ReturnType: Description of return value
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// code example here
|
||||
//
|
||||
// # See Also
|
||||
//
|
||||
// - RelatedFunction: Brief description
|
||||
func FunctionName[T any](param T) ReturnType {
|
||||
```
|
||||
|
||||
3. **Code Examples**
|
||||
- Use idiomatic Go patterns
|
||||
- Prefer `result.Eitherize1(strconv.Atoi)` over manual error handling
|
||||
- Show realistic, runnable examples
|
||||
|
||||
### File Headers
|
||||
|
||||
Always include the Apache 2.0 license header:
|
||||
|
||||
```go
|
||||
// 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.
|
||||
```
|
||||
|
||||
## Testing Standards
|
||||
|
||||
### Test Structure
|
||||
|
||||
1. **Organize Tests by Category**
|
||||
```go
|
||||
func TestFunctionName_Success(t *testing.T) {
|
||||
t.Run("specific success case", func(t *testing.T) {
|
||||
// test code
|
||||
})
|
||||
}
|
||||
|
||||
func TestFunctionName_Failure(t *testing.T) {
|
||||
t.Run("specific failure case", func(t *testing.T) {
|
||||
// test code
|
||||
})
|
||||
}
|
||||
|
||||
func TestFunctionName_EdgeCases(t *testing.T) {
|
||||
// edge case tests
|
||||
}
|
||||
|
||||
func TestFunctionName_Integration(t *testing.T) {
|
||||
// integration tests
|
||||
}
|
||||
```
|
||||
|
||||
2. **Use Direct Assertions**
|
||||
- Prefer: `assert.Equal(t, validation.Success(expected), actual)`
|
||||
- Avoid: Verbose `either.MonadFold` patterns unless necessary
|
||||
- Exception: When you need to verify pointer is not nil or extract specific fields
|
||||
|
||||
3. **Use Idiomatic Patterns**
|
||||
- Use `result.Eitherize1` for converting `(T, error)` functions
|
||||
- Use `result.Of` for success values
|
||||
- Use `result.Left` for error values
|
||||
|
||||
### Test Coverage
|
||||
|
||||
Include tests for:
|
||||
- **Success cases**: Normal operation with various input types
|
||||
- **Failure cases**: Error handling and error preservation
|
||||
- **Edge cases**: Nil, empty, zero values, boundary conditions
|
||||
- **Integration**: Composition with other functions
|
||||
- **Type safety**: Verify type parameters work correctly
|
||||
- **Benchmarks**: Performance-critical paths
|
||||
|
||||
### Example Test Pattern
|
||||
|
||||
```go
|
||||
func TestFromReaderResult_Success(t *testing.T) {
|
||||
t.Run("converts successful ReaderResult", func(t *testing.T) {
|
||||
// Arrange
|
||||
parseIntRR := result.Eitherize1(strconv.Atoi)
|
||||
validator := FromReaderResult[string, int](parseIntRR)
|
||||
|
||||
// Act
|
||||
result := validator("42")(nil)
|
||||
|
||||
// Assert
|
||||
assert.Equal(t, validation.Success(42), result)
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## Code Style
|
||||
|
||||
### Functional Patterns
|
||||
|
||||
1. **Prefer Composition**
|
||||
```go
|
||||
validator := F.Pipe1(
|
||||
FromReaderResult[string, int](parseIntRR),
|
||||
Chain(validatePositive),
|
||||
)
|
||||
```
|
||||
|
||||
2. **Use Type-Safe Helpers**
|
||||
- `result.Eitherize1` for `func(T) (R, error)`
|
||||
- `result.Of` for wrapping success values
|
||||
- `result.Left` for wrapping errors
|
||||
|
||||
3. **Avoid Verbose Patterns**
|
||||
- Don't manually handle `(value, error)` tuples when helpers exist
|
||||
- Don't use `either.MonadFold` in tests unless necessary
|
||||
|
||||
### Error Handling
|
||||
|
||||
1. **In Production Code**
|
||||
- Use `validation.Success` for successful validations
|
||||
- Use `validation.FailureWithMessage` for simple failures
|
||||
- Use `validation.FailureWithError` to preserve error causes
|
||||
|
||||
2. **In Tests**
|
||||
- Verify error messages and causes
|
||||
- Check error context is preserved
|
||||
- Test error accumulation when applicable
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Converting Error-Based Functions
|
||||
|
||||
```go
|
||||
// Good: Use Eitherize1
|
||||
parseIntRR := result.Eitherize1(strconv.Atoi)
|
||||
|
||||
// Avoid: Manual error handling
|
||||
parseIntRR := func(input string) result.Result[int] {
|
||||
val, err := strconv.Atoi(input)
|
||||
if err != nil {
|
||||
return result.Left[int](err)
|
||||
}
|
||||
return result.Of(val)
|
||||
}
|
||||
```
|
||||
|
||||
### Testing Validation Results
|
||||
|
||||
```go
|
||||
// Good: Direct comparison
|
||||
assert.Equal(t, validation.Success(42), result)
|
||||
|
||||
// Avoid: Verbose extraction (unless you need to verify specific fields)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) int { return 0 },
|
||||
F.Identity[int],
|
||||
)
|
||||
assert.Equal(t, 42, value)
|
||||
```
|
||||
|
||||
### Documentation Examples
|
||||
|
||||
```go
|
||||
// Good: Concise and idiomatic
|
||||
// parseIntRR := result.Eitherize1(strconv.Atoi)
|
||||
// validator := FromReaderResult[string, int](parseIntRR)
|
||||
|
||||
// Avoid: Verbose manual patterns
|
||||
// parseIntRR := func(input string) result.Result[int] {
|
||||
// val, err := strconv.Atoi(input)
|
||||
// if err != nil {
|
||||
// return result.Left[int](err)
|
||||
// }
|
||||
// return result.Of(val)
|
||||
// }
|
||||
```
|
||||
|
||||
## Checklist for New Code
|
||||
|
||||
- [ ] Apache 2.0 license header included
|
||||
- [ ] Go doc comments use standard format (no markdown links)
|
||||
- [ ] Code examples are idiomatic and concise
|
||||
- [ ] Tests cover success, failure, edge cases, and integration
|
||||
- [ ] Tests use direct assertions where possible
|
||||
- [ ] Benchmarks included for performance-critical code
|
||||
- [ ] All tests pass
|
||||
- [ ] Code uses functional composition patterns
|
||||
- [ ] Error handling preserves context and causes
|
||||
@@ -460,12 +460,15 @@ func process() IOResult[string] {
|
||||
- **Either** - Type-safe error handling with left/right values
|
||||
- **Result** - Simplified Either with error as left type (recommended for error handling)
|
||||
- **IO** - Lazy evaluation and side effect management
|
||||
- **IOOption** - Combine IO with Option for optional values with side effects
|
||||
- **IOResult** - Combine IO with Result for error handling (recommended over IOEither)
|
||||
- **Reader** - Dependency injection pattern
|
||||
- **ReaderOption** - Combine Reader with Option for optional values with dependency injection
|
||||
- **ReaderIOOption** - Combine Reader, IO, and Option for optional values with dependency injection and side effects
|
||||
- **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
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/internal/array"
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// From constructs an array from a set of variadic arguments
|
||||
@@ -163,11 +163,11 @@ func FilterMapWithIndex[A, B any](f func(int, A) Option[B]) Operator[A, B] {
|
||||
return G.FilterMapWithIndex[[]A, []B](f)
|
||||
}
|
||||
|
||||
// FilterChain maps an array with an iterating function that returns an [Option] of an array. It keeps only the Some values discarding the Nones and then flattens the result.
|
||||
// ChainOptionK maps an array with an iterating function that returns an [Option] of an array. It keeps only the Some values discarding the Nones and then flattens the result.
|
||||
//
|
||||
//go:inline
|
||||
func FilterChain[A, B any](f option.Kleisli[A, []B]) Operator[A, B] {
|
||||
return G.FilterChain[[]A](f)
|
||||
func ChainOptionK[A, B any](f option.Kleisli[A, []B]) Operator[A, B] {
|
||||
return G.ChainOptionK[[]A](f)
|
||||
}
|
||||
|
||||
// FilterMapRef filters an array using a predicate on pointers and maps the matching elements using a function on pointers.
|
||||
@@ -239,6 +239,16 @@ func ReduceRef[A, B any](f func(B, *A) B, initial B) func([]A) B {
|
||||
}
|
||||
|
||||
// Append adds an element to the end of an array, returning a new array.
|
||||
// This is a non-curried version that takes both the array and element as parameters.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// arr := []int{1, 2, 3}
|
||||
// result := array.Append(arr, 4)
|
||||
// // result: []int{1, 2, 3, 4}
|
||||
// // arr: []int{1, 2, 3} (unchanged)
|
||||
//
|
||||
// For a curried version, see Push.
|
||||
//
|
||||
//go:inline
|
||||
func Append[A any](as []A, a A) []A {
|
||||
@@ -443,7 +453,7 @@ func Size[A any](as []A) int {
|
||||
// the second contains elements for which it returns true.
|
||||
//
|
||||
//go:inline
|
||||
func MonadPartition[A any](as []A, pred func(A) bool) tuple.Tuple2[[]A, []A] {
|
||||
func MonadPartition[A any](as []A, pred func(A) bool) pair.Pair[[]A, []A] {
|
||||
return G.MonadPartition(as, pred)
|
||||
}
|
||||
|
||||
@@ -451,7 +461,7 @@ func MonadPartition[A any](as []A, pred func(A) bool) tuple.Tuple2[[]A, []A] {
|
||||
// for which the predicate returns false, the right one those for which the predicate returns true
|
||||
//
|
||||
//go:inline
|
||||
func Partition[A any](pred func(A) bool) func([]A) tuple.Tuple2[[]A, []A] {
|
||||
func Partition[A any](pred func(A) bool) func([]A) pair.Pair[[]A, []A] {
|
||||
return G.Partition[[]A](pred)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,308 +16,88 @@
|
||||
package array
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestReplicate(t *testing.T) {
|
||||
result := Replicate(3, "a")
|
||||
assert.Equal(t, []string{"a", "a", "a"}, result)
|
||||
|
||||
empty := Replicate(0, 42)
|
||||
assert.Equal(t, []int{}, empty)
|
||||
}
|
||||
|
||||
func TestMonadMap(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
result := MonadMap(src, N.Mul(2))
|
||||
assert.Equal(t, []int{2, 4, 6}, result)
|
||||
}
|
||||
|
||||
func TestMonadMapRef(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
result := MonadMapRef(src, func(x *int) int { return *x * 2 })
|
||||
assert.Equal(t, []int{2, 4, 6}, result)
|
||||
}
|
||||
|
||||
func TestMapWithIndex(t *testing.T) {
|
||||
src := []string{"a", "b", "c"}
|
||||
mapper := MapWithIndex(func(i int, s string) string {
|
||||
return fmt.Sprintf("%d:%s", i, s)
|
||||
})
|
||||
result := mapper(src)
|
||||
assert.Equal(t, []string{"0:a", "1:b", "2:c"}, result)
|
||||
}
|
||||
|
||||
func TestMapRef(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
mapper := MapRef(func(x *int) int { return *x * 2 })
|
||||
result := mapper(src)
|
||||
assert.Equal(t, []int{2, 4, 6}, result)
|
||||
}
|
||||
|
||||
func TestFilterWithIndex(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
filter := FilterWithIndex(func(i, x int) bool {
|
||||
return i%2 == 0 && x > 2
|
||||
})
|
||||
result := filter(src)
|
||||
assert.Equal(t, []int{3, 5}, result)
|
||||
}
|
||||
|
||||
func TestFilterRef(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
filter := FilterRef(func(x *int) bool { return *x > 2 })
|
||||
result := filter(src)
|
||||
assert.Equal(t, []int{3, 4, 5}, result)
|
||||
}
|
||||
|
||||
func TestMonadFilterMap(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4}
|
||||
result := MonadFilterMap(src, func(x int) O.Option[string] {
|
||||
if x%2 == 0 {
|
||||
return O.Some(fmt.Sprintf("even:%d", x))
|
||||
}
|
||||
return O.None[string]()
|
||||
})
|
||||
assert.Equal(t, []string{"even:2", "even:4"}, result)
|
||||
}
|
||||
|
||||
func TestMonadFilterMapWithIndex(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4}
|
||||
result := MonadFilterMapWithIndex(src, func(i, x int) O.Option[string] {
|
||||
if i%2 == 0 {
|
||||
return O.Some(fmt.Sprintf("%d:%d", i, x))
|
||||
}
|
||||
return O.None[string]()
|
||||
})
|
||||
assert.Equal(t, []string{"0:1", "2:3"}, result)
|
||||
}
|
||||
|
||||
func TestFilterMapWithIndex(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4}
|
||||
filter := FilterMapWithIndex(func(i, x int) O.Option[string] {
|
||||
if i%2 == 0 {
|
||||
return O.Some(fmt.Sprintf("%d:%d", i, x))
|
||||
}
|
||||
return O.None[string]()
|
||||
})
|
||||
result := filter(src)
|
||||
assert.Equal(t, []string{"0:1", "2:3"}, result)
|
||||
}
|
||||
|
||||
func TestFilterMapRef(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
filter := FilterMapRef(
|
||||
func(x *int) bool { return *x > 2 },
|
||||
func(x *int) string { return fmt.Sprintf("val:%d", *x) },
|
||||
)
|
||||
result := filter(src)
|
||||
assert.Equal(t, []string{"val:3", "val:4", "val:5"}, result)
|
||||
}
|
||||
|
||||
func TestReduceWithIndex(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
reducer := ReduceWithIndex(func(i, acc, x int) int {
|
||||
return acc + i + x
|
||||
// TestMonadReduceWithIndex tests the MonadReduceWithIndex function
|
||||
func TestMonadReduceWithIndex(t *testing.T) {
|
||||
// Test with integers - sum with index multiplication
|
||||
numbers := []int{1, 2, 3, 4, 5}
|
||||
result := MonadReduceWithIndex(numbers, func(idx, acc, val int) int {
|
||||
return acc + (val * idx)
|
||||
}, 0)
|
||||
result := reducer(src)
|
||||
assert.Equal(t, 9, result) // 0 + (0+1) + (1+2) + (2+3) = 9
|
||||
}
|
||||
// Expected: 0*1 + 1*2 + 2*3 + 3*4 + 4*5 = 0 + 2 + 6 + 12 + 20 = 40
|
||||
assert.Equal(t, 40, result)
|
||||
|
||||
func TestReduceRightWithIndex(t *testing.T) {
|
||||
src := []string{"a", "b", "c"}
|
||||
reducer := ReduceRightWithIndex(func(i int, x, acc string) string {
|
||||
return fmt.Sprintf("%s%d:%s", acc, i, x)
|
||||
// Test with empty array
|
||||
empty := []int{}
|
||||
result2 := MonadReduceWithIndex(empty, func(idx, acc, val int) int {
|
||||
return acc + val
|
||||
}, 10)
|
||||
assert.Equal(t, 10, result2)
|
||||
|
||||
// Test with strings - concatenate with index
|
||||
words := []string{"a", "b", "c"}
|
||||
result3 := MonadReduceWithIndex(words, func(idx int, acc, val string) string {
|
||||
return acc + val + string(rune('0'+idx))
|
||||
}, "")
|
||||
result := reducer(src)
|
||||
assert.Equal(t, "2:c1:b0:a", result)
|
||||
assert.Equal(t, "a0b1c2", result3)
|
||||
}
|
||||
|
||||
func TestReduceRef(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
reducer := ReduceRef(func(acc int, x *int) int {
|
||||
return acc + *x
|
||||
}, 0)
|
||||
result := reducer(src)
|
||||
assert.Equal(t, 6, result)
|
||||
}
|
||||
|
||||
func TestZero(t *testing.T) {
|
||||
result := Zero[int]()
|
||||
assert.Equal(t, []int{}, result)
|
||||
assert.True(t, IsEmpty(result))
|
||||
}
|
||||
|
||||
func TestMonadChain(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
result := MonadChain(src, func(x int) []int {
|
||||
return []int{x, x * 10}
|
||||
})
|
||||
assert.Equal(t, []int{1, 10, 2, 20, 3, 30}, result)
|
||||
}
|
||||
|
||||
func TestChain(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
chain := Chain(func(x int) []int {
|
||||
return []int{x, x * 10}
|
||||
})
|
||||
result := chain(src)
|
||||
assert.Equal(t, []int{1, 10, 2, 20, 3, 30}, result)
|
||||
}
|
||||
|
||||
func TestMonadAp(t *testing.T) {
|
||||
fns := []func(int) int{
|
||||
N.Mul(2),
|
||||
N.Add(10),
|
||||
}
|
||||
values := []int{1, 2}
|
||||
result := MonadAp(fns, values)
|
||||
assert.Equal(t, []int{2, 4, 11, 12}, result)
|
||||
}
|
||||
|
||||
func TestMatchLeft(t *testing.T) {
|
||||
matcher := MatchLeft(
|
||||
func() string { return "empty" },
|
||||
func(head int, tail []int) string {
|
||||
return fmt.Sprintf("head:%d,tail:%v", head, tail)
|
||||
},
|
||||
)
|
||||
|
||||
assert.Equal(t, "empty", matcher([]int{}))
|
||||
assert.Equal(t, "head:1,tail:[2 3]", matcher([]int{1, 2, 3}))
|
||||
}
|
||||
|
||||
func TestTail(t *testing.T) {
|
||||
assert.Equal(t, O.None[[]int](), Tail([]int{}))
|
||||
assert.Equal(t, O.Some([]int{2, 3}), Tail([]int{1, 2, 3}))
|
||||
assert.Equal(t, O.Some([]int{}), Tail([]int{1}))
|
||||
}
|
||||
|
||||
func TestFirst(t *testing.T) {
|
||||
assert.Equal(t, O.None[int](), First([]int{}))
|
||||
assert.Equal(t, O.Some(1), First([]int{1, 2, 3}))
|
||||
}
|
||||
|
||||
func TestLast(t *testing.T) {
|
||||
assert.Equal(t, O.None[int](), Last([]int{}))
|
||||
assert.Equal(t, O.Some(3), Last([]int{1, 2, 3}))
|
||||
assert.Equal(t, O.Some(1), Last([]int{1}))
|
||||
}
|
||||
|
||||
func TestUpsertAt(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
upsert := UpsertAt(99)
|
||||
|
||||
result1 := upsert(src)
|
||||
assert.Equal(t, []int{1, 2, 3, 99}, result1)
|
||||
}
|
||||
|
||||
func TestSize(t *testing.T) {
|
||||
assert.Equal(t, 0, Size([]int{}))
|
||||
assert.Equal(t, 3, Size([]int{1, 2, 3}))
|
||||
}
|
||||
|
||||
func TestMonadPartition(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
result := MonadPartition(src, func(x int) bool { return x > 2 })
|
||||
assert.Equal(t, []int{1, 2}, result.F1)
|
||||
assert.Equal(t, []int{3, 4, 5}, result.F2)
|
||||
}
|
||||
|
||||
func TestIsNil(t *testing.T) {
|
||||
var nilSlice []int
|
||||
assert.True(t, IsNil(nilSlice))
|
||||
assert.False(t, IsNil([]int{}))
|
||||
assert.False(t, IsNil([]int{1}))
|
||||
}
|
||||
|
||||
func TestIsNonNil(t *testing.T) {
|
||||
var nilSlice []int
|
||||
assert.False(t, IsNonNil(nilSlice))
|
||||
assert.True(t, IsNonNil([]int{}))
|
||||
assert.True(t, IsNonNil([]int{1}))
|
||||
}
|
||||
|
||||
func TestConstNil(t *testing.T) {
|
||||
result := ConstNil[int]()
|
||||
assert.True(t, IsNil(result))
|
||||
}
|
||||
|
||||
func TestSliceRight(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
slicer := SliceRight[int](2)
|
||||
result := slicer(src)
|
||||
assert.Equal(t, []int{3, 4, 5}, result)
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
copied := Copy(src)
|
||||
assert.Equal(t, src, copied)
|
||||
// Verify it's a different slice
|
||||
copied[0] = 99
|
||||
assert.Equal(t, 1, src[0])
|
||||
assert.Equal(t, 99, copied[0])
|
||||
}
|
||||
|
||||
func TestClone(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
cloner := Clone(N.Mul(2))
|
||||
result := cloner(src)
|
||||
assert.Equal(t, []int{2, 4, 6}, result)
|
||||
}
|
||||
|
||||
func TestFoldMapWithIndex(t *testing.T) {
|
||||
src := []string{"a", "b", "c"}
|
||||
folder := FoldMapWithIndex[string](S.Monoid)(func(i int, s string) string {
|
||||
return fmt.Sprintf("%d:%s", i, s)
|
||||
})
|
||||
result := folder(src)
|
||||
assert.Equal(t, "0:a1:b2:c", result)
|
||||
}
|
||||
|
||||
func TestFold(t *testing.T) {
|
||||
src := []int{1, 2, 3, 4, 5}
|
||||
folder := Fold(N.MonoidSum[int]())
|
||||
result := folder(src)
|
||||
assert.Equal(t, 15, result)
|
||||
}
|
||||
|
||||
func TestPush(t *testing.T) {
|
||||
src := []int{1, 2, 3}
|
||||
pusher := Push(4)
|
||||
result := pusher(src)
|
||||
// TestAppend tests the Append function
|
||||
func TestAppend(t *testing.T) {
|
||||
// Test appending to non-empty array
|
||||
arr := []int{1, 2, 3}
|
||||
result := Append(arr, 4)
|
||||
assert.Equal(t, []int{1, 2, 3, 4}, result)
|
||||
// Verify original array is unchanged
|
||||
assert.Equal(t, []int{1, 2, 3}, arr)
|
||||
|
||||
// Test appending to empty array
|
||||
empty := []int{}
|
||||
result2 := Append(empty, 1)
|
||||
assert.Equal(t, []int{1}, result2)
|
||||
|
||||
// Test appending strings
|
||||
words := []string{"hello", "world"}
|
||||
result3 := Append(words, "!")
|
||||
assert.Equal(t, []string{"hello", "world", "!"}, result3)
|
||||
|
||||
// Test appending to nil array
|
||||
var nilArr []int
|
||||
result4 := Append(nilArr, 42)
|
||||
assert.Equal(t, []int{42}, result4)
|
||||
}
|
||||
|
||||
func TestMonadFlap(t *testing.T) {
|
||||
fns := []func(int) string{
|
||||
func(x int) string { return fmt.Sprintf("a%d", x) },
|
||||
func(x int) string { return fmt.Sprintf("b%d", x) },
|
||||
}
|
||||
result := MonadFlap(fns, 5)
|
||||
assert.Equal(t, []string{"a5", "b5"}, result)
|
||||
}
|
||||
// TestStrictEquals tests the StrictEquals function
|
||||
func TestStrictEquals(t *testing.T) {
|
||||
eq := StrictEquals[int]()
|
||||
|
||||
func TestFlap(t *testing.T) {
|
||||
fns := []func(int) string{
|
||||
func(x int) string { return fmt.Sprintf("a%d", x) },
|
||||
func(x int) string { return fmt.Sprintf("b%d", x) },
|
||||
}
|
||||
flapper := Flap[string](5)
|
||||
result := flapper(fns)
|
||||
assert.Equal(t, []string{"a5", "b5"}, result)
|
||||
}
|
||||
// Test equal arrays
|
||||
arr1 := []int{1, 2, 3}
|
||||
arr2 := []int{1, 2, 3}
|
||||
assert.True(t, eq.Equals(arr1, arr2))
|
||||
|
||||
func TestPrepend(t *testing.T) {
|
||||
src := []int{2, 3, 4}
|
||||
prepender := Prepend(1)
|
||||
result := prepender(src)
|
||||
assert.Equal(t, []int{1, 2, 3, 4}, result)
|
||||
// Test different arrays
|
||||
arr3 := []int{1, 2, 4}
|
||||
assert.False(t, eq.Equals(arr1, arr3))
|
||||
|
||||
// Test different lengths
|
||||
arr4 := []int{1, 2}
|
||||
assert.False(t, eq.Equals(arr1, arr4))
|
||||
|
||||
// Test empty arrays
|
||||
empty1 := []int{}
|
||||
empty2 := []int{}
|
||||
assert.True(t, eq.Equals(empty1, empty2))
|
||||
|
||||
// Test with strings
|
||||
strEq := StrictEquals[string]()
|
||||
words1 := []string{"hello", "world"}
|
||||
words2 := []string{"hello", "world"}
|
||||
words3 := []string{"hello", "there"}
|
||||
assert.True(t, strEq.Equals(words1, words2))
|
||||
assert.False(t, strEq.Equals(words1, words3))
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ import (
|
||||
"github.com/IBM/fp-go/v2/internal/utils"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
T "github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -163,11 +163,11 @@ func TestPartition(t *testing.T) {
|
||||
return n > 2
|
||||
}
|
||||
|
||||
assert.Equal(t, T.MakeTuple2(Empty[int](), Empty[int]()), Partition(pred)(Empty[int]()))
|
||||
assert.Equal(t, T.MakeTuple2(From(1), From(3)), Partition(pred)(From(1, 3)))
|
||||
assert.Equal(t, pair.MakePair(Empty[int](), Empty[int]()), Partition(pred)(Empty[int]()))
|
||||
assert.Equal(t, pair.MakePair(From(1), From(3)), Partition(pred)(From(1, 3)))
|
||||
}
|
||||
|
||||
func TestFilterChain(t *testing.T) {
|
||||
func TestChainOptionK(t *testing.T) {
|
||||
src := From(1, 2, 3)
|
||||
|
||||
f := func(i int) O.Option[[]string] {
|
||||
@@ -177,7 +177,7 @@ func TestFilterChain(t *testing.T) {
|
||||
return O.None[[]string]()
|
||||
}
|
||||
|
||||
res := FilterChain(f)(src)
|
||||
res := ChainOptionK(f)(src)
|
||||
|
||||
assert.Equal(t, From("a1", "b1", "a3", "b3"), res)
|
||||
}
|
||||
|
||||
@@ -63,17 +63,26 @@ func Bind[S1, S2, T any](
|
||||
|
||||
// Let attaches the result of a pure computation to a context S1 to produce a context S2.
|
||||
// Unlike Bind, the computation function returns a plain value T rather than []T.
|
||||
// This is useful when you need to compute a derived value from the current context
|
||||
// without introducing additional array elements.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := array.Let(
|
||||
// func(sum int) func(s struct{ X int }) struct{ X, Sum int } {
|
||||
// return func(s struct{ X int }) struct{ X, Sum int } {
|
||||
// return struct{ X, Sum int }{s.X, sum}
|
||||
// }
|
||||
// },
|
||||
// func(s struct{ X int }) int { return s.X * 2 },
|
||||
// type State1 struct{ X int }
|
||||
// type State2 struct{ X, Double int }
|
||||
//
|
||||
// result := F.Pipe2(
|
||||
// []State1{{X: 5}, {X: 10}},
|
||||
// array.Let(
|
||||
// func(double int) func(s State1) State2 {
|
||||
// return func(s State1) State2 {
|
||||
// return State2{X: s.X, Double: double}
|
||||
// }
|
||||
// },
|
||||
// func(s State1) int { return s.X * 2 },
|
||||
// ),
|
||||
// )
|
||||
// // result: []State2{{X: 5, Double: 10}, {X: 10, Double: 20}}
|
||||
//
|
||||
//go:inline
|
||||
func Let[S1, S2, T any](
|
||||
@@ -84,18 +93,25 @@ func Let[S1, S2, T any](
|
||||
}
|
||||
|
||||
// LetTo attaches a constant value to a context S1 to produce a context S2.
|
||||
// This is useful for adding constant values to the context.
|
||||
// This is useful for adding constant values to the context without computation.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := array.LetTo(
|
||||
// func(name string) func(s struct{ X int }) struct{ X int; Name string } {
|
||||
// return func(s struct{ X int }) struct{ X int; Name string } {
|
||||
// return struct{ X int; Name string }{s.X, name}
|
||||
// }
|
||||
// },
|
||||
// "constant",
|
||||
// type State1 struct{ X int }
|
||||
// type State2 struct{ X int; Name string }
|
||||
//
|
||||
// result := F.Pipe2(
|
||||
// []State1{{X: 1}, {X: 2}},
|
||||
// array.LetTo(
|
||||
// func(name string) func(s State1) State2 {
|
||||
// return func(s State1) State2 {
|
||||
// return State2{X: s.X, Name: name}
|
||||
// }
|
||||
// },
|
||||
// "constant",
|
||||
// ),
|
||||
// )
|
||||
// // result: []State2{{X: 1, Name: "constant"}, {X: 2, Name: "constant"}}
|
||||
//
|
||||
//go:inline
|
||||
func LetTo[S1, S2, T any](
|
||||
@@ -107,15 +123,19 @@ func LetTo[S1, S2, T any](
|
||||
|
||||
// BindTo initializes a new state S1 from a value T.
|
||||
// This is typically the first operation after Do to start building the context.
|
||||
// It transforms each element of type T into a state of type S1.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct{ X int }
|
||||
//
|
||||
// result := F.Pipe2(
|
||||
// []int{1, 2, 3},
|
||||
// array.BindTo(func(x int) struct{ X int } {
|
||||
// return struct{ X int }{x}
|
||||
// array.BindTo(func(x int) State {
|
||||
// return State{X: x}
|
||||
// }),
|
||||
// )
|
||||
// // result: []State{{X: 1}, {X: 2}, {X: 3}}
|
||||
//
|
||||
//go:inline
|
||||
func BindTo[S1, T any](
|
||||
|
||||
@@ -22,57 +22,176 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type TestState1 struct {
|
||||
X int
|
||||
}
|
||||
|
||||
type TestState2 struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
// TestLet tests the Let function
|
||||
func TestLet(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(TestState1{}),
|
||||
type State1 struct {
|
||||
X int
|
||||
}
|
||||
type State2 struct {
|
||||
X int
|
||||
Double int
|
||||
}
|
||||
|
||||
// Test Let with pure computation
|
||||
result := F.Pipe1(
|
||||
[]State1{{X: 5}, {X: 10}},
|
||||
Let(
|
||||
func(y int) func(s TestState1) TestState2 {
|
||||
return func(s TestState1) TestState2 {
|
||||
return TestState2{X: s.X, Y: y}
|
||||
func(double int) func(s State1) State2 {
|
||||
return func(s State1) State2 {
|
||||
return State2{X: s.X, Double: double}
|
||||
}
|
||||
},
|
||||
func(s TestState1) int { return s.X * 2 },
|
||||
func(s State1) int { return s.X * 2 },
|
||||
),
|
||||
Map(func(s TestState2) int { return s.X + s.Y }),
|
||||
)
|
||||
|
||||
assert.Equal(t, []int{0}, result)
|
||||
expected := []State2{{X: 5, Double: 10}, {X: 10, Double: 20}}
|
||||
assert.Equal(t, expected, result)
|
||||
|
||||
// Test Let with empty array
|
||||
empty := []State1{}
|
||||
result2 := F.Pipe1(
|
||||
empty,
|
||||
Let(
|
||||
func(double int) func(s State1) State2 {
|
||||
return func(s State1) State2 {
|
||||
return State2{X: s.X, Double: double}
|
||||
}
|
||||
},
|
||||
func(s State1) int { return s.X * 2 },
|
||||
),
|
||||
)
|
||||
assert.Equal(t, []State2{}, result2)
|
||||
}
|
||||
|
||||
// TestLetTo tests the LetTo function
|
||||
func TestLetTo(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(TestState1{X: 5}),
|
||||
type State1 struct {
|
||||
X int
|
||||
}
|
||||
type State2 struct {
|
||||
X int
|
||||
Name string
|
||||
}
|
||||
|
||||
// Test LetTo with constant value
|
||||
result := F.Pipe1(
|
||||
[]State1{{X: 1}, {X: 2}},
|
||||
LetTo(
|
||||
func(y int) func(s TestState1) TestState2 {
|
||||
return func(s TestState1) TestState2 {
|
||||
return TestState2{X: s.X, Y: y}
|
||||
func(name string) func(s State1) State2 {
|
||||
return func(s State1) State2 {
|
||||
return State2{X: s.X, Name: name}
|
||||
}
|
||||
},
|
||||
42,
|
||||
"constant",
|
||||
),
|
||||
Map(func(s TestState2) int { return s.X + s.Y }),
|
||||
)
|
||||
|
||||
assert.Equal(t, []int{47}, result)
|
||||
expected := []State2{{X: 1, Name: "constant"}, {X: 2, Name: "constant"}}
|
||||
assert.Equal(t, expected, result)
|
||||
|
||||
// Test LetTo with different constant
|
||||
result2 := F.Pipe1(
|
||||
[]State1{{X: 10}},
|
||||
LetTo(
|
||||
func(name string) func(s State1) State2 {
|
||||
return func(s State1) State2 {
|
||||
return State2{X: s.X, Name: name}
|
||||
}
|
||||
},
|
||||
"test",
|
||||
),
|
||||
)
|
||||
|
||||
expected2 := []State2{{X: 10, Name: "test"}}
|
||||
assert.Equal(t, expected2, result2)
|
||||
}
|
||||
|
||||
// TestBindTo tests the BindTo function
|
||||
func TestBindTo(t *testing.T) {
|
||||
type State struct {
|
||||
X int
|
||||
}
|
||||
|
||||
// Test BindTo with integers
|
||||
result := F.Pipe1(
|
||||
[]int{1, 2, 3},
|
||||
BindTo(func(x int) TestState1 {
|
||||
return TestState1{X: x}
|
||||
BindTo(func(x int) State {
|
||||
return State{X: x}
|
||||
}),
|
||||
)
|
||||
|
||||
expected := []TestState1{{X: 1}, {X: 2}, {X: 3}}
|
||||
expected := []State{{X: 1}, {X: 2}, {X: 3}}
|
||||
assert.Equal(t, expected, result)
|
||||
|
||||
// Test BindTo with strings
|
||||
type StringState struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
result2 := F.Pipe1(
|
||||
[]string{"hello", "world"},
|
||||
BindTo(func(s string) StringState {
|
||||
return StringState{Value: s}
|
||||
}),
|
||||
)
|
||||
|
||||
expected2 := []StringState{{Value: "hello"}, {Value: "world"}}
|
||||
assert.Equal(t, expected2, result2)
|
||||
|
||||
// Test BindTo with empty array
|
||||
empty := []int{}
|
||||
result3 := F.Pipe1(
|
||||
empty,
|
||||
BindTo(func(x int) State {
|
||||
return State{X: x}
|
||||
}),
|
||||
)
|
||||
assert.Equal(t, []State{}, result3)
|
||||
}
|
||||
|
||||
// TestDoWithLetAndBindTo tests combining Do, Let, LetTo, and BindTo
|
||||
func TestDoWithLetAndBindTo(t *testing.T) {
|
||||
type State1 struct {
|
||||
X int
|
||||
}
|
||||
type State2 struct {
|
||||
X int
|
||||
Double int
|
||||
}
|
||||
type State3 struct {
|
||||
X int
|
||||
Double int
|
||||
Name string
|
||||
}
|
||||
|
||||
// Test complex pipeline
|
||||
result := F.Pipe3(
|
||||
[]int{5, 10},
|
||||
BindTo(func(x int) State1 {
|
||||
return State1{X: x}
|
||||
}),
|
||||
Let(
|
||||
func(double int) func(s State1) State2 {
|
||||
return func(s State1) State2 {
|
||||
return State2{X: s.X, Double: double}
|
||||
}
|
||||
},
|
||||
func(s State1) int { return s.X * 2 },
|
||||
),
|
||||
LetTo(
|
||||
func(name string) func(s State2) State3 {
|
||||
return func(s State2) State3 {
|
||||
return State3{X: s.X, Double: s.Double, Name: name}
|
||||
}
|
||||
},
|
||||
"result",
|
||||
),
|
||||
)
|
||||
|
||||
expected := []State3{
|
||||
{X: 5, Double: 10, Name: "result"},
|
||||
{X: 10, Double: 20, Name: "result"},
|
||||
}
|
||||
assert.Equal(t, expected, result)
|
||||
}
|
||||
|
||||
137
v2/array/coverage.out
Normal file
137
v2/array/coverage.out
Normal file
@@ -0,0 +1,137 @@
|
||||
mode: set
|
||||
github.com/IBM/fp-go/v2/array/any.go:34.65,36.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/any.go:48.51,50.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:30.33,32.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:37.52,39.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:44.39,46.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:52.50,54.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:58.54,61.23 3 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:61.23,63.3 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:64.2,64.11 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:70.62,72.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:83.48,85.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:89.52,91.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:93.55,96.23 3 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:96.23,98.14 2 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:98.14,100.4 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:102.2,102.15 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:105.75,108.23 3 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:108.23,110.14 2 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:110.14,112.4 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:114.2,114.15 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:120.54,122.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:127.68,129.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:132.58,134.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:140.67,142.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:148.78,150.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:155.65,157.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:162.76,164.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:169.69,171.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:174.80,175.26 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:175.26,177.3 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:180.64,182.25 2 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:182.25,184.3 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:185.2,185.16 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:189.65,191.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:194.79,196.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:206.62,208.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:214.76,216.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:221.67,223.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:229.81,231.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:235.66,236.24 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:236.24,238.3 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:254.37,256.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:261.34,263.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:266.37,268.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:273.25,275.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:280.24,282.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:287.25,289.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:295.56,297.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:308.54,310.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:316.53,318.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:324.50,326.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:331.76,333.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:338.83,340.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:346.38,348.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:354.36,356.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:362.37,364.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:370.36,372.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:375.49,376.26 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:376.26,380.35 4 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:380.35,385.4 4 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:386.3,386.16 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:395.50,397.26 2 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:397.26,398.18 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:398.18,400.4 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:401.3,401.25 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:406.60,407.36 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:407.36,409.3 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:419.36,421.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:424.49,426.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:432.49,434.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:440.42,442.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:447.30,449.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:456.78,458.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:464.75,466.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:469.32,471.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:474.35,476.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:479.28,481.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:486.50,488.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:493.29,495.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:500.47,502.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:507.67,509.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:514.81,516.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:521.45,523.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:528.38,530.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:605.43,607.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:613.52,615.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:621.49,623.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:628.44,630.2 1 0
|
||||
github.com/IBM/fp-go/v2/array/array.go:714.33,716.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:780.53,781.26 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:781.26,782.47 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:782.47,782.67 1 1
|
||||
github.com/IBM/fp-go/v2/array/array.go:839.31,841.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:36.7,38.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:60.20,62.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:91.20,93.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:120.20,122.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:143.19,145.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/bind.go:166.20,168.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/eq.go:35.37,37.49 2 1
|
||||
github.com/IBM/fp-go/v2/array/eq.go:37.49,39.3 1 1
|
||||
github.com/IBM/fp-go/v2/array/eq.go:43.45,45.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:33.65,35.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:48.79,50.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:68.78,70.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:76.89,78.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:89.64,91.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:97.78,99.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:105.77,107.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/find.go:113.88,115.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/magma.go:38.50,40.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/monad.go:39.65,41.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:35.36,37.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:48.42,50.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:52.45,54.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:68.45,73.48 3 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:73.48,75.3 1 1
|
||||
github.com/IBM/fp-go/v2/array/monoid.go:77.2,77.12 1 1
|
||||
github.com/IBM/fp-go/v2/array/sequence.go:27.19,29.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/sequence.go:69.22,71.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/sequence.go:92.53,98.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/sort.go:35.47,37.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/sort.go:65.68,67.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/sort.go:96.51,98.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:66.34,68.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:83.24,86.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:94.39,96.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:105.29,108.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:110.142,117.46 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:117.46,118.54 1 1
|
||||
github.com/IBM/fp-go/v2/array/traverse.go:118.54,125.4 1 1
|
||||
github.com/IBM/fp-go/v2/array/uniq.go:20.43,22.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/uniq.go:49.60,51.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/zip.go:38.73,40.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/zip.go:58.55,60.2 1 1
|
||||
github.com/IBM/fp-go/v2/array/zip.go:81.62,83.2 1 1
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
FC "github.com/IBM/fp-go/v2/internal/functor"
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// Of constructs a single element array
|
||||
@@ -215,7 +215,7 @@ func Filter[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) AS {
|
||||
return FilterWithIndex[AS](F.Ignore1of2[int](pred))
|
||||
}
|
||||
|
||||
func FilterChain[GA ~[]A, GB ~[]B, A, B any](f func(a A) O.Option[GB]) func(GA) GB {
|
||||
func ChainOptionK[GA ~[]A, GB ~[]B, A, B any](f func(a A) O.Option[GB]) func(GA) GB {
|
||||
return F.Flow2(
|
||||
FilterMap[GA, []GB](f),
|
||||
Flatten[[]GB],
|
||||
@@ -234,7 +234,7 @@ func FilterMapWithIndex[GA ~[]A, GB ~[]B, A, B any](f func(int, A) O.Option[B])
|
||||
return F.Bind2nd(MonadFilterMapWithIndex[GA, GB, A, B], f)
|
||||
}
|
||||
|
||||
func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, GA] {
|
||||
func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) pair.Pair[GA, GA] {
|
||||
left := Empty[GA]()
|
||||
right := Empty[GA]()
|
||||
array.Reduce(as, func(c bool, a A) bool {
|
||||
@@ -246,10 +246,10 @@ func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, G
|
||||
return c
|
||||
}, true)
|
||||
// returns the partition
|
||||
return tuple.MakeTuple2(left, right)
|
||||
return pair.MakePair(left, right)
|
||||
}
|
||||
|
||||
func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) tuple.Tuple2[GA, GA] {
|
||||
func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) pair.Pair[GA, GA] {
|
||||
return F.Bind2nd(MonadPartition[GA, A], pred)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package generic
|
||||
import (
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
T "github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// ZipWith applies a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one
|
||||
@@ -34,19 +34,19 @@ func ZipWith[AS ~[]A, BS ~[]B, CS ~[]C, FCT ~func(A, B) C, A, B, C any](fa AS, f
|
||||
|
||||
// Zip takes two arrays and returns an array of corresponding pairs. If one input array is short, excess elements of the
|
||||
// longer array are discarded
|
||||
func Zip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](fb BS) func(AS) CS {
|
||||
return F.Bind23of3(ZipWith[AS, BS, CS, func(A, B) T.Tuple2[A, B]])(fb, T.MakeTuple2[A, B])
|
||||
func Zip[AS ~[]A, BS ~[]B, CS ~[]pair.Pair[A, B], A, B any](fb BS) func(AS) CS {
|
||||
return F.Bind23of3(ZipWith[AS, BS, CS, func(A, B) pair.Pair[A, B]])(fb, pair.MakePair[A, B])
|
||||
}
|
||||
|
||||
// Unzip is the function is reverse of [Zip]. Takes an array of pairs and return two corresponding arrays
|
||||
func Unzip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](cs CS) T.Tuple2[AS, BS] {
|
||||
func Unzip[AS ~[]A, BS ~[]B, CS ~[]pair.Pair[A, B], A, B any](cs CS) pair.Pair[AS, BS] {
|
||||
l := len(cs)
|
||||
as := make(AS, l)
|
||||
bs := make(BS, l)
|
||||
for i := range l {
|
||||
t := cs[i]
|
||||
as[i] = t.F1
|
||||
bs[i] = t.F2
|
||||
as[i] = pair.Head(t)
|
||||
bs[i] = pair.Tail(t)
|
||||
}
|
||||
return T.MakeTuple2(as, bs)
|
||||
return pair.MakePair(as, bs)
|
||||
}
|
||||
|
||||
78
v2/array/sequence_extended_test.go
Normal file
78
v2/array/sequence_extended_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// 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 array
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestSequenceWithOption tests the generic Sequence function with Option monad
|
||||
func TestSequenceWithOption(t *testing.T) {
|
||||
// Test with Option monad - all Some values
|
||||
opts := From(
|
||||
O.Some(1),
|
||||
O.Some(2),
|
||||
O.Some(3),
|
||||
)
|
||||
|
||||
// Use the Sequence function with Option's applicative monoid
|
||||
monoid := O.ApplicativeMonoid(Monoid[int]())
|
||||
seq := Sequence(O.Map(Of[int]), monoid)
|
||||
result := seq(opts)
|
||||
|
||||
assert.Equal(t, O.Of(From(1, 2, 3)), result)
|
||||
|
||||
// Test with Option monad - contains None
|
||||
optsWithNone := From(
|
||||
O.Some(1),
|
||||
O.None[int](),
|
||||
O.Some(3),
|
||||
)
|
||||
|
||||
result2 := seq(optsWithNone)
|
||||
assert.True(t, O.IsNone(result2))
|
||||
|
||||
// Test with empty array
|
||||
empty := Empty[Option[int]]()
|
||||
result3 := seq(empty)
|
||||
assert.Equal(t, O.Some(Empty[int]()), result3)
|
||||
}
|
||||
|
||||
// TestMonadSequence tests the MonadSequence function
|
||||
func TestMonadSequence(t *testing.T) {
|
||||
// Test with Option monad
|
||||
opts := From(
|
||||
O.Some("hello"),
|
||||
O.Some("world"),
|
||||
)
|
||||
|
||||
monoid := O.ApplicativeMonoid(Monoid[string]())
|
||||
result := MonadSequence(O.Map(Of[string]), monoid, opts)
|
||||
|
||||
assert.Equal(t, O.Of(From("hello", "world")), result)
|
||||
|
||||
// Test with None in the array
|
||||
optsWithNone := From(
|
||||
O.Some("hello"),
|
||||
O.None[string](),
|
||||
)
|
||||
|
||||
result2 := MonadSequence(O.Map(Of[string]), monoid, optsWithNone)
|
||||
assert.Equal(t, O.None[[]string](), result2)
|
||||
}
|
||||
164
v2/array/traverse_extended_test.go
Normal file
164
v2/array/traverse_extended_test.go
Normal file
@@ -0,0 +1,164 @@
|
||||
// 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 array
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestMonadTraverse tests the MonadTraverse function
|
||||
func TestMonadTraverse(t *testing.T) {
|
||||
// Test converting integers to strings via Option
|
||||
numbers := []int{1, 2, 3}
|
||||
|
||||
result := MonadTraverse(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
numbers,
|
||||
func(n int) O.Option[string] {
|
||||
return O.Some(strconv.Itoa(n))
|
||||
},
|
||||
)
|
||||
|
||||
assert.True(t, O.IsSome(result))
|
||||
assert.Equal(t, []string{"1", "2", "3"}, O.GetOrElse(func() []string { return []string{} })(result))
|
||||
|
||||
// Test with a function that can return None
|
||||
result2 := MonadTraverse(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
numbers,
|
||||
func(n int) O.Option[string] {
|
||||
if n == 2 {
|
||||
return O.None[string]()
|
||||
}
|
||||
return O.Some(strconv.Itoa(n))
|
||||
},
|
||||
)
|
||||
|
||||
assert.True(t, O.IsNone(result2))
|
||||
|
||||
// Test with empty array
|
||||
empty := []int{}
|
||||
result3 := MonadTraverse(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
empty,
|
||||
func(n int) O.Option[string] {
|
||||
return O.Some(strconv.Itoa(n))
|
||||
},
|
||||
)
|
||||
|
||||
assert.True(t, O.IsSome(result3))
|
||||
assert.Equal(t, []string{}, O.GetOrElse(func() []string { return nil })(result3))
|
||||
}
|
||||
|
||||
// TestTraverseWithIndex tests the TraverseWithIndex function
|
||||
func TestTraverseWithIndex(t *testing.T) {
|
||||
// Test with index-aware transformation
|
||||
words := []string{"a", "b", "c"}
|
||||
|
||||
traverser := TraverseWithIndex(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
func(idx int, s string) O.Option[string] {
|
||||
return O.Some(s + strconv.Itoa(idx))
|
||||
},
|
||||
)
|
||||
|
||||
result := traverser(words)
|
||||
assert.True(t, O.IsSome(result))
|
||||
assert.Equal(t, []string{"a0", "b1", "c2"}, O.GetOrElse(func() []string { return []string{} })(result))
|
||||
|
||||
// Test with conditional None based on index
|
||||
traverser2 := TraverseWithIndex(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
func(idx int, s string) O.Option[string] {
|
||||
if idx == 1 {
|
||||
return O.None[string]()
|
||||
}
|
||||
return O.Some(s)
|
||||
},
|
||||
)
|
||||
|
||||
result2 := traverser2(words)
|
||||
assert.True(t, O.IsNone(result2))
|
||||
}
|
||||
|
||||
// TestMonadTraverseWithIndex tests the MonadTraverseWithIndex function
|
||||
func TestMonadTraverseWithIndex(t *testing.T) {
|
||||
// Test with index-aware transformation
|
||||
numbers := []int{10, 20, 30}
|
||||
|
||||
result := MonadTraverseWithIndex(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
numbers,
|
||||
func(idx, n int) O.Option[string] {
|
||||
return O.Some(strconv.Itoa(n * idx))
|
||||
},
|
||||
)
|
||||
|
||||
assert.True(t, O.IsSome(result))
|
||||
// Expected: [10*0, 20*1, 30*2] = ["0", "20", "60"]
|
||||
assert.Equal(t, []string{"0", "20", "60"}, O.GetOrElse(func() []string { return []string{} })(result))
|
||||
|
||||
// Test with None at specific index
|
||||
result2 := MonadTraverseWithIndex(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
numbers,
|
||||
func(idx, n int) O.Option[string] {
|
||||
if idx == 2 {
|
||||
return O.None[string]()
|
||||
}
|
||||
return O.Some(strconv.Itoa(n))
|
||||
},
|
||||
)
|
||||
|
||||
assert.True(t, O.IsNone(result2))
|
||||
}
|
||||
|
||||
// TestMakeTraverseType tests the MakeTraverseType function
|
||||
func TestMakeTraverseType(t *testing.T) {
|
||||
// Create a traverse type for Option
|
||||
traverseType := MakeTraverseType[int, string, O.Option[string], O.Option[[]string], O.Option[func(string) []string]]()
|
||||
|
||||
// Use it to traverse an array
|
||||
numbers := []int{1, 2, 3}
|
||||
result := traverseType(
|
||||
O.Of[[]string],
|
||||
O.Map[[]string, func(string) []string],
|
||||
O.Ap[[]string, string],
|
||||
)(func(n int) O.Option[string] {
|
||||
return O.Some(strconv.Itoa(n * 2))
|
||||
})(numbers)
|
||||
|
||||
assert.True(t, O.IsSome(result))
|
||||
assert.Equal(t, []string{"2", "4", "6"}, O.GetOrElse(func() []string { return []string{} })(result))
|
||||
}
|
||||
@@ -17,7 +17,7 @@ package array
|
||||
|
||||
import (
|
||||
G "github.com/IBM/fp-go/v2/array/generic"
|
||||
T "github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// ZipWith applies a function to pairs of elements at the same index in two arrays,
|
||||
@@ -55,8 +55,8 @@ func ZipWith[FCT ~func(A, B) C, A, B, C any](fa []A, fb []B, f FCT) []C {
|
||||
// // Result: [(a, 1), (b, 2)]
|
||||
//
|
||||
//go:inline
|
||||
func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B] {
|
||||
return G.Zip[[]A, []B, []T.Tuple2[A, B]](fb)
|
||||
func Zip[A, B any](fb []B) func([]A) []pair.Pair[A, B] {
|
||||
return G.Zip[[]A, []B, []pair.Pair[A, B]](fb)
|
||||
}
|
||||
|
||||
// Unzip is the reverse of Zip. It takes an array of pairs (tuples) and returns
|
||||
@@ -78,6 +78,6 @@ func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B] {
|
||||
// ages := result.Tail // [30, 25, 35]
|
||||
//
|
||||
//go:inline
|
||||
func Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B] {
|
||||
func Unzip[A, B any](cs []pair.Pair[A, B]) pair.Pair[[]A, []B] {
|
||||
return G.Unzip[[]A, []B](cs)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
T "github.com/IBM/fp-go/v2/tuple"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestZip(t *testing.T) {
|
||||
|
||||
res := Zip[string](left)(right)
|
||||
|
||||
assert.Equal(t, From(T.MakeTuple2("a", 1), T.MakeTuple2("b", 2), T.MakeTuple2("c", 3)), res)
|
||||
assert.Equal(t, From(pair.MakePair("a", 1), pair.MakePair("b", 2), pair.MakePair("c", 3)), res)
|
||||
}
|
||||
|
||||
func TestUnzip(t *testing.T) {
|
||||
@@ -51,6 +51,6 @@ func TestUnzip(t *testing.T) {
|
||||
|
||||
unzipped := Unzip(zipped)
|
||||
|
||||
assert.Equal(t, right, unzipped.F1)
|
||||
assert.Equal(t, left, unzipped.F2)
|
||||
assert.Equal(t, right, pair.Head(unzipped))
|
||||
assert.Equal(t, left, pair.Tail(unzipped))
|
||||
}
|
||||
|
||||
@@ -194,6 +194,25 @@ func ArrayNotEmpty[T any](arr []T) Reader {
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayEmpty checks if an array is empty.
|
||||
//
|
||||
// This is the complement of ArrayNotEmpty, asserting that a slice has no elements.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestArrayEmpty(t *testing.T) {
|
||||
// empty := []int{}
|
||||
// assert.ArrayEmpty(empty)(t) // Passes
|
||||
//
|
||||
// numbers := []int{1, 2, 3}
|
||||
// assert.ArrayEmpty(numbers)(t) // Fails
|
||||
// }
|
||||
func ArrayEmpty[T any](arr []T) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return assert.Empty(t, arr)
|
||||
}
|
||||
}
|
||||
|
||||
// RecordNotEmpty checks if a map is not empty.
|
||||
//
|
||||
// Example:
|
||||
@@ -211,6 +230,25 @@ func RecordNotEmpty[K comparable, T any](mp map[K]T) Reader {
|
||||
}
|
||||
}
|
||||
|
||||
// RecordEmpty checks if a map is empty.
|
||||
//
|
||||
// This is the complement of RecordNotEmpty, asserting that a map has no key-value pairs.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestRecordEmpty(t *testing.T) {
|
||||
// empty := map[string]int{}
|
||||
// assert.RecordEmpty(empty)(t) // Passes
|
||||
//
|
||||
// config := map[string]int{"timeout": 30}
|
||||
// assert.RecordEmpty(config)(t) // Fails
|
||||
// }
|
||||
func RecordEmpty[K comparable, T any](mp map[K]T) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return assert.Empty(t, mp)
|
||||
}
|
||||
}
|
||||
|
||||
// StringNotEmpty checks if a string is not empty.
|
||||
//
|
||||
// Example:
|
||||
@@ -504,15 +542,7 @@ func AllOf(readers []Reader) Reader {
|
||||
//
|
||||
//go:inline
|
||||
func RunAll(testcases map[string]Reader) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
current := true
|
||||
for k, r := range testcases {
|
||||
current = current && t.Run(k, func(t1 *testing.T) {
|
||||
r(t1)
|
||||
})
|
||||
}
|
||||
return current
|
||||
}
|
||||
return SequenceRecord(testcases)
|
||||
}
|
||||
|
||||
// Local transforms a Reader that works on type R1 into a Reader that works on type R2,
|
||||
|
||||
@@ -85,6 +85,33 @@ func TestArrayNotEmpty(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestArrayEmpty(t *testing.T) {
|
||||
t.Run("should pass for empty array", func(t *testing.T) {
|
||||
arr := []int{}
|
||||
result := ArrayEmpty(arr)(t)
|
||||
if !result {
|
||||
t.Error("Expected ArrayEmpty to pass for empty array")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail for non-empty array", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
arr := []int{1, 2, 3}
|
||||
result := ArrayEmpty(arr)(mockT)
|
||||
if result {
|
||||
t.Error("Expected ArrayEmpty to fail for non-empty array")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with different types", func(t *testing.T) {
|
||||
strArr := []string{}
|
||||
result := ArrayEmpty(strArr)(t)
|
||||
if !result {
|
||||
t.Error("Expected ArrayEmpty to pass for empty string array")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRecordNotEmpty(t *testing.T) {
|
||||
t.Run("should pass for non-empty map", func(t *testing.T) {
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
@@ -131,6 +158,33 @@ func TestArrayLength(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRecordEmpty(t *testing.T) {
|
||||
t.Run("should pass for empty map", func(t *testing.T) {
|
||||
mp := map[string]int{}
|
||||
result := RecordEmpty(mp)(t)
|
||||
if !result {
|
||||
t.Error("Expected RecordEmpty to pass for empty map")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail for non-empty map", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
result := RecordEmpty(mp)(mockT)
|
||||
if result {
|
||||
t.Error("Expected RecordEmpty to fail for non-empty map")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with different key-value types", func(t *testing.T) {
|
||||
mp := map[int]string{}
|
||||
result := RecordEmpty(mp)(t)
|
||||
if !result {
|
||||
t.Error("Expected RecordEmpty to pass for empty map with int keys")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRecordLength(t *testing.T) {
|
||||
t.Run("should pass when map length matches", func(t *testing.T) {
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
@@ -150,6 +204,33 @@ func TestRecordLength(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringNotEmpty(t *testing.T) {
|
||||
t.Run("should pass for non-empty string", func(t *testing.T) {
|
||||
str := "Hello, World!"
|
||||
result := StringNotEmpty(str)(t)
|
||||
if !result {
|
||||
t.Error("Expected StringNotEmpty to pass for non-empty string")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail for empty string", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
str := ""
|
||||
result := StringNotEmpty(str)(mockT)
|
||||
if result {
|
||||
t.Error("Expected StringNotEmpty to fail for empty string")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should pass for string with whitespace", func(t *testing.T) {
|
||||
str := " "
|
||||
result := StringNotEmpty(str)(t)
|
||||
if !result {
|
||||
t.Error("Expected StringNotEmpty to pass for string with whitespace")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringLength(t *testing.T) {
|
||||
t.Run("should pass when string length matches", func(t *testing.T) {
|
||||
str := "hello"
|
||||
|
||||
122
v2/assert/from.go
Normal file
122
v2/assert/from.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// FromReaderIOResult converts a ReaderIOResult[Reader] into a Reader.
|
||||
//
|
||||
// This function bridges the gap between context-aware, IO-based computations that may fail
|
||||
// (ReaderIOResult) and the simpler Reader type used for test assertions. It executes the
|
||||
// ReaderIOResult computation using the test's context, handles any potential errors by
|
||||
// converting them to test failures via NoError, and returns the resulting Reader.
|
||||
//
|
||||
// The conversion process:
|
||||
// 1. Executes the ReaderIOResult with the test context (t.Context())
|
||||
// 2. Runs the resulting IO operation ()
|
||||
// 3. Extracts the Result, converting errors to test failures using NoError
|
||||
// 4. Returns a Reader that can be applied to *testing.T
|
||||
//
|
||||
// This is particularly useful when you have test assertions that need to:
|
||||
// - Access context for cancellation or deadlines
|
||||
// - Perform IO operations (file access, network calls, etc.)
|
||||
// - Handle potential errors gracefully in tests
|
||||
//
|
||||
// Parameters:
|
||||
// - ri: A ReaderIOResult that produces a Reader when given a context and executed
|
||||
//
|
||||
// Returns:
|
||||
// - A Reader that can be directly applied to *testing.T for assertion
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestWithContext(t *testing.T) {
|
||||
// // Create a ReaderIOResult that performs an IO operation
|
||||
// checkDatabase := func(ctx context.Context) func() result.Result[assert.Reader] {
|
||||
// return func() result.Result[assert.Reader] {
|
||||
// // Simulate database check
|
||||
// if err := db.PingContext(ctx); err != nil {
|
||||
// return result.Error[assert.Reader](err)
|
||||
// }
|
||||
// return result.Of[assert.Reader](assert.NoError(nil))
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Convert to Reader and execute
|
||||
// assertion := assert.FromReaderIOResult(checkDatabase)
|
||||
// assertion(t)
|
||||
// }
|
||||
func FromReaderIOResult(ri ReaderIOResult[Reader]) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return F.Pipe1(
|
||||
ri(t.Context())(),
|
||||
result.GetOrElse(NoError),
|
||||
)(t)
|
||||
}
|
||||
}
|
||||
|
||||
// FromReaderIO converts a ReaderIO[Reader] into a Reader.
|
||||
//
|
||||
// This function bridges the gap between context-aware, IO-based computations (ReaderIO)
|
||||
// and the simpler Reader type used for test assertions. It executes the ReaderIO
|
||||
// computation using the test's context and returns the resulting Reader.
|
||||
//
|
||||
// Unlike FromReaderIOResult, this function does not handle errors explicitly - it assumes
|
||||
// the IO operation will succeed or that any errors are handled within the ReaderIO itself.
|
||||
//
|
||||
// The conversion process:
|
||||
// 1. Executes the ReaderIO with the test context (t.Context())
|
||||
// 2. Runs the resulting IO operation ()
|
||||
// 3. Returns a Reader that can be applied to *testing.T
|
||||
//
|
||||
// This is particularly useful when you have test assertions that need to:
|
||||
// - Access context for cancellation or deadlines
|
||||
// - Perform IO operations that don't fail (or handle failures internally)
|
||||
// - Integrate with context-aware testing utilities
|
||||
//
|
||||
// Parameters:
|
||||
// - ri: A ReaderIO that produces a Reader when given a context and executed
|
||||
//
|
||||
// Returns:
|
||||
// - A Reader that can be directly applied to *testing.T for assertion
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestWithIO(t *testing.T) {
|
||||
// // Create a ReaderIO that performs an IO operation
|
||||
// logAndCheck := func(ctx context.Context) func() assert.Reader {
|
||||
// return func() assert.Reader {
|
||||
// // Log something using context
|
||||
// logger.InfoContext(ctx, "Running test")
|
||||
// // Return an assertion
|
||||
// return assert.Equal(42)(computeValue())
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Convert to Reader and execute
|
||||
// assertion := assert.FromReaderIO(logAndCheck)
|
||||
// assertion(t)
|
||||
// }
|
||||
func FromReaderIO(ri ReaderIO[Reader]) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return ri(t.Context())()(t)
|
||||
}
|
||||
}
|
||||
383
v2/assert/from_test.go
Normal file
383
v2/assert/from_test.go
Normal file
@@ -0,0 +1,383 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
func TestFromReaderIOResult(t *testing.T) {
|
||||
t.Run("should pass when ReaderIOResult returns success with passing assertion", func(t *testing.T) {
|
||||
// Create a ReaderIOResult that returns a successful Reader
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
// Return a Reader that always passes
|
||||
return result.Of(func(t *testing.T) bool {
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIOResult to pass when ReaderIOResult returns success")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should pass when ReaderIOResult returns success with Equal assertion", func(t *testing.T) {
|
||||
// Create a ReaderIOResult that returns a successful Equal assertion
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
return result.Of(Equal(42)(42))
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIOResult to pass with Equal assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail when ReaderIOResult returns error", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
// Create a ReaderIOResult that returns an error
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
return result.Left[Reader](errors.New("test error"))
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(mockT)
|
||||
if res {
|
||||
t.Error("Expected FromReaderIOResult to fail when ReaderIOResult returns error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail when ReaderIOResult returns success but assertion fails", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
// Create a ReaderIOResult that returns a failing assertion
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
return result.Of(Equal(42)(43))
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(mockT)
|
||||
if res {
|
||||
t.Error("Expected FromReaderIOResult to fail when assertion fails")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should use test context", func(t *testing.T) {
|
||||
contextUsed := false
|
||||
|
||||
// Create a ReaderIOResult that checks if context is provided
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
if ctx != nil {
|
||||
contextUsed = true
|
||||
}
|
||||
return func() result.Result[Reader] {
|
||||
return result.Of(func(t *testing.T) bool {
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
reader(t)
|
||||
|
||||
if !contextUsed {
|
||||
t.Error("Expected FromReaderIOResult to use test context")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with NoError assertion", func(t *testing.T) {
|
||||
// Create a ReaderIOResult that returns NoError assertion
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
return result.Of(NoError(nil))
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIOResult to pass with NoError assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with complex assertions", func(t *testing.T) {
|
||||
// Create a ReaderIOResult with multiple composed assertions
|
||||
ri := func(ctx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
arr := []int{1, 2, 3}
|
||||
assertions := AllOf([]Reader{
|
||||
ArrayNotEmpty(arr),
|
||||
ArrayLength[int](3)(arr),
|
||||
ArrayContains(2)(arr),
|
||||
})
|
||||
return result.Of(assertions)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIOResult to pass with complex assertions")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromReaderIO(t *testing.T) {
|
||||
t.Run("should pass when ReaderIO returns passing assertion", func(t *testing.T) {
|
||||
// Create a ReaderIO that returns a Reader that always passes
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass when ReaderIO returns passing assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should pass when ReaderIO returns Equal assertion", func(t *testing.T) {
|
||||
// Create a ReaderIO that returns an Equal assertion
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
return Equal(42)(42)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with Equal assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should fail when ReaderIO returns failing assertion", func(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
// Create a ReaderIO that returns a failing assertion
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
return Equal(42)(43)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(mockT)
|
||||
if res {
|
||||
t.Error("Expected FromReaderIO to fail when assertion fails")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should use test context", func(t *testing.T) {
|
||||
contextUsed := false
|
||||
|
||||
// Create a ReaderIO that checks if context is provided
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
if ctx != nil {
|
||||
contextUsed = true
|
||||
}
|
||||
return func() Reader {
|
||||
return func(t *testing.T) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
reader(t)
|
||||
|
||||
if !contextUsed {
|
||||
t.Error("Expected FromReaderIO to use test context")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with NoError assertion", func(t *testing.T) {
|
||||
// Create a ReaderIO that returns NoError assertion
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
return NoError(nil)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with NoError assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with Error assertion", func(t *testing.T) {
|
||||
// Create a ReaderIO that returns Error assertion
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
return Error(errors.New("expected error"))
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with Error assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with complex assertions", func(t *testing.T) {
|
||||
// Create a ReaderIO with multiple composed assertions
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
return AllOf([]Reader{
|
||||
RecordNotEmpty(mp),
|
||||
RecordLength[string, int](2)(mp),
|
||||
ContainsKey[int]("a")(mp),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with complex assertions")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with string assertions", func(t *testing.T) {
|
||||
// Create a ReaderIO with string assertions
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
str := "hello world"
|
||||
return AllOf([]Reader{
|
||||
StringNotEmpty(str),
|
||||
StringLength[any, any](11)(str),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with string assertions")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with Result assertions", func(t *testing.T) {
|
||||
// Create a ReaderIO with Result assertions
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
successResult := result.Of(42)
|
||||
return Success(successResult)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with Success assertion")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should work with Failure assertion", func(t *testing.T) {
|
||||
// Create a ReaderIO with Failure assertion
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
failureResult := result.Left[int](errors.New("test error"))
|
||||
return Failure(failureResult)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected FromReaderIO to pass with Failure assertion")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestFromReaderIOResultIntegration tests integration scenarios
|
||||
func TestFromReaderIOResultIntegration(t *testing.T) {
|
||||
t.Run("should work in a realistic scenario with context cancellation", func(t *testing.T) {
|
||||
// Create a ReaderIOResult that uses the context
|
||||
ri := func(testCtx context.Context) func() result.Result[Reader] {
|
||||
return func() result.Result[Reader] {
|
||||
// Check if context is valid
|
||||
if testCtx == nil {
|
||||
return result.Left[Reader](errors.New("context is nil"))
|
||||
}
|
||||
|
||||
// Return a successful assertion
|
||||
return result.Of(Equal("test")("test"))
|
||||
}
|
||||
}
|
||||
|
||||
// Use the actual testing.T from the subtest
|
||||
reader := FromReaderIOResult(ri)
|
||||
res := reader(t)
|
||||
if !res {
|
||||
t.Error("Expected integration test to pass")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestFromReaderIOIntegration tests integration scenarios
|
||||
func TestFromReaderIOIntegration(t *testing.T) {
|
||||
t.Run("should work in a realistic scenario with logging", func(t *testing.T) {
|
||||
logCalled := false
|
||||
|
||||
// Create a ReaderIO that simulates logging
|
||||
ri := func(ctx context.Context) func() Reader {
|
||||
return func() Reader {
|
||||
// Simulate logging with context
|
||||
if ctx != nil {
|
||||
logCalled = true
|
||||
}
|
||||
|
||||
// Return an assertion
|
||||
return Equal(100)(100)
|
||||
}
|
||||
}
|
||||
|
||||
reader := FromReaderIO(ri)
|
||||
res := reader(t)
|
||||
|
||||
if !res {
|
||||
t.Error("Expected integration test to pass")
|
||||
}
|
||||
|
||||
if !logCalled {
|
||||
t.Error("Expected logging to be called")
|
||||
}
|
||||
})
|
||||
}
|
||||
207
v2/assert/logger.go
Normal file
207
v2/assert/logger.go
Normal file
@@ -0,0 +1,207 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
)
|
||||
|
||||
// Logf creates a logging function that outputs formatted test messages using Go's testing.T.Logf.
|
||||
//
|
||||
// This function provides a functional programming approach to test logging, returning a
|
||||
// [ReaderIO] that can be composed with other test operations. It's particularly useful
|
||||
// for debugging tests, tracing execution flow, or documenting test behavior without
|
||||
// affecting test outcomes.
|
||||
//
|
||||
// The function uses a curried design pattern:
|
||||
// 1. First, you provide a format string (prefix) with format verbs (like %v, %d, %s)
|
||||
// 2. This returns a function that takes a value of type T
|
||||
// 3. That function returns a ReaderIO that performs the logging when executed
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - prefix: A format string compatible with fmt.Printf (e.g., "Value: %v", "Count: %d")
|
||||
// The format string should contain exactly one format verb that matches type T
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A function that takes a value of type T and returns a [ReaderIO][*testing.T, Void]
|
||||
// When executed, this ReaderIO logs the formatted message to the test output
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - T: The type of value to be logged. Can be any type that can be formatted by fmt
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Debugging test execution by logging intermediate values
|
||||
// - Tracing the flow of complex test scenarios
|
||||
// - Documenting test behavior in the test output
|
||||
// - Logging values in functional pipelines without breaking the chain
|
||||
// - Creating reusable logging operations for specific types
|
||||
//
|
||||
// # Example - Basic Logging
|
||||
//
|
||||
// func TestBasicLogging(t *testing.T) {
|
||||
// // Create a logger for integers
|
||||
// logInt := assert.Logf[int]("Processing value: %d")
|
||||
//
|
||||
// // Use it to log a value
|
||||
// value := 42
|
||||
// logInt(value)(t)() // Outputs: "Processing value: 42"
|
||||
// }
|
||||
//
|
||||
// # Example - Logging in Test Pipeline
|
||||
//
|
||||
// func TestPipelineWithLogging(t *testing.T) {
|
||||
// type User struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// }
|
||||
//
|
||||
// user := User{Name: "Alice", Age: 30}
|
||||
//
|
||||
// // Create a logger for User
|
||||
// logUser := assert.Logf[User]("Testing user: %+v")
|
||||
//
|
||||
// // Log the user being tested
|
||||
// logUser(user)(t)()
|
||||
//
|
||||
// // Continue with assertions
|
||||
// assert.StringNotEmpty(user.Name)(t)
|
||||
// assert.That(func(age int) bool { return age > 0 })(user.Age)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Multiple Loggers for Different Types
|
||||
//
|
||||
// func TestMultipleLoggers(t *testing.T) {
|
||||
// // Create type-specific loggers
|
||||
// logString := assert.Logf[string]("String value: %s")
|
||||
// logInt := assert.Logf[int]("Integer value: %d")
|
||||
// logFloat := assert.Logf[float64]("Float value: %.2f")
|
||||
//
|
||||
// // Use them throughout the test
|
||||
// logString("hello")(t)() // Outputs: "String value: hello"
|
||||
// logInt(42)(t)() // Outputs: "Integer value: 42"
|
||||
// logFloat(3.14159)(t)() // Outputs: "Float value: 3.14"
|
||||
// }
|
||||
//
|
||||
// # Example - Logging Complex Structures
|
||||
//
|
||||
// func TestComplexStructureLogging(t *testing.T) {
|
||||
// type Config struct {
|
||||
// Host string
|
||||
// Port int
|
||||
// Timeout int
|
||||
// }
|
||||
//
|
||||
// config := Config{Host: "localhost", Port: 8080, Timeout: 30}
|
||||
//
|
||||
// // Use %+v to include field names
|
||||
// logConfig := assert.Logf[Config]("Configuration: %+v")
|
||||
// logConfig(config)(t)()
|
||||
// // Outputs: "Configuration: {Host:localhost Port:8080 Timeout:30}"
|
||||
//
|
||||
// // Or use %#v for Go-syntax representation
|
||||
// logConfigGo := assert.Logf[Config]("Config (Go syntax): %#v")
|
||||
// logConfigGo(config)(t)()
|
||||
// // Outputs: "Config (Go syntax): assert.Config{Host:"localhost", Port:8080, Timeout:30}"
|
||||
// }
|
||||
//
|
||||
// # Example - Debugging Test Failures
|
||||
//
|
||||
// func TestWithDebugLogging(t *testing.T) {
|
||||
// numbers := []int{1, 2, 3, 4, 5}
|
||||
// logSlice := assert.Logf[[]int]("Testing slice: %v")
|
||||
//
|
||||
// // Log the input data
|
||||
// logSlice(numbers)(t)()
|
||||
//
|
||||
// // Perform assertions
|
||||
// assert.ArrayNotEmpty(numbers)(t)
|
||||
// assert.ArrayLength[int](5)(numbers)(t)
|
||||
//
|
||||
// // Log intermediate results
|
||||
// sum := 0
|
||||
// for _, n := range numbers {
|
||||
// sum += n
|
||||
// }
|
||||
// logInt := assert.Logf[int]("Sum: %d")
|
||||
// logInt(sum)(t)()
|
||||
//
|
||||
// assert.Equal(15)(sum)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Conditional Logging
|
||||
//
|
||||
// func TestConditionalLogging(t *testing.T) {
|
||||
// logDebug := assert.Logf[string]("DEBUG: %s")
|
||||
//
|
||||
// values := []int{1, 2, 3, 4, 5}
|
||||
// for _, v := range values {
|
||||
// if v%2 == 0 {
|
||||
// logDebug(fmt.Sprintf("Found even number: %d", v))(t)()
|
||||
// }
|
||||
// }
|
||||
// // Outputs:
|
||||
// // DEBUG: Found even number: 2
|
||||
// // DEBUG: Found even number: 4
|
||||
// }
|
||||
//
|
||||
// # Format Verbs
|
||||
//
|
||||
// Common format verbs you can use in the prefix string:
|
||||
// - %v: Default format
|
||||
// - %+v: Default format with field names for structs
|
||||
// - %#v: Go-syntax representation
|
||||
// - %T: Type of the value
|
||||
// - %d: Integer in base 10
|
||||
// - %s: String
|
||||
// - %f: Floating point number
|
||||
// - %t: Boolean (true/false)
|
||||
// - %p: Pointer address
|
||||
//
|
||||
// See the fmt package documentation for a complete list of format verbs.
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - Logging does not affect test pass/fail status
|
||||
// - Log output appears in test results when running with -v flag or when tests fail
|
||||
// - The function returns Void, indicating it's used for side effects only
|
||||
// - The ReaderIO pattern allows logging to be composed with other operations
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [FromReaderIO]: Converts ReaderIO operations into test assertions
|
||||
// - testing.T.Logf: The underlying Go testing log function
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Go testing package: https://pkg.go.dev/testing
|
||||
// - fmt package format verbs: https://pkg.go.dev/fmt
|
||||
// - ReaderIO pattern: Combines Reader (context dependency) with IO (side effects)
|
||||
func Logf[T any](prefix string) func(T) readerio.ReaderIO[*testing.T, Void] {
|
||||
return func(a T) readerio.ReaderIO[*testing.T, Void] {
|
||||
return func(t *testing.T) IO[Void] {
|
||||
return io.FromImpure(func() {
|
||||
t.Logf(prefix, a)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
406
v2/assert/logger_test.go
Normal file
406
v2/assert/logger_test.go
Normal file
@@ -0,0 +1,406 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLogf_BasicInteger tests basic integer logging
|
||||
func TestLogf_BasicInteger(t *testing.T) {
|
||||
logInt := Logf[int]("Processing value: %d")
|
||||
|
||||
// This should not panic and should log the value
|
||||
logInt(42)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_BasicString tests basic string logging
|
||||
func TestLogf_BasicString(t *testing.T) {
|
||||
logString := Logf[string]("String value: %s")
|
||||
|
||||
logString("hello world")(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_BasicFloat tests basic float logging
|
||||
func TestLogf_BasicFloat(t *testing.T) {
|
||||
logFloat := Logf[float64]("Float value: %.2f")
|
||||
|
||||
logFloat(3.14159)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_BasicBoolean tests basic boolean logging
|
||||
func TestLogf_BasicBoolean(t *testing.T) {
|
||||
logBool := Logf[bool]("Boolean value: %t")
|
||||
|
||||
logBool(true)(t)()
|
||||
logBool(false)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_ComplexStruct tests logging of complex structures
|
||||
func TestLogf_ComplexStruct(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
logUser := Logf[User]("User: %+v")
|
||||
|
||||
user := User{Name: "Alice", Age: 30}
|
||||
logUser(user)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_Slice tests logging of slices
|
||||
func TestLogf_Slice(t *testing.T) {
|
||||
logSlice := Logf[[]int]("Slice: %v")
|
||||
|
||||
numbers := []int{1, 2, 3, 4, 5}
|
||||
logSlice(numbers)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_Map tests logging of maps
|
||||
func TestLogf_Map(t *testing.T) {
|
||||
logMap := Logf[map[string]int]("Map: %v")
|
||||
|
||||
data := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||
logMap(data)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_Pointer tests logging of pointers
|
||||
func TestLogf_Pointer(t *testing.T) {
|
||||
logPtr := Logf[*int]("Pointer: %p")
|
||||
|
||||
value := 42
|
||||
logPtr(&value)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_NilPointer tests logging of nil pointers
|
||||
func TestLogf_NilPointer(t *testing.T) {
|
||||
logPtr := Logf[*int]("Pointer: %v")
|
||||
|
||||
var nilPtr *int
|
||||
logPtr(nilPtr)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_EmptyString tests logging of empty strings
|
||||
func TestLogf_EmptyString(t *testing.T) {
|
||||
logString := Logf[string]("String: '%s'")
|
||||
|
||||
logString("")(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_EmptySlice tests logging of empty slices
|
||||
func TestLogf_EmptySlice(t *testing.T) {
|
||||
logSlice := Logf[[]int]("Slice: %v")
|
||||
|
||||
logSlice([]int{})(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_EmptyMap tests logging of empty maps
|
||||
func TestLogf_EmptyMap(t *testing.T) {
|
||||
logMap := Logf[map[string]int]("Map: %v")
|
||||
|
||||
logMap(map[string]int{})(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_MultipleTypes tests using multiple loggers for different types
|
||||
func TestLogf_MultipleTypes(t *testing.T) {
|
||||
logString := Logf[string]("String: %s")
|
||||
logInt := Logf[int]("Integer: %d")
|
||||
logFloat := Logf[float64]("Float: %.2f")
|
||||
|
||||
logString("test")(t)()
|
||||
logInt(42)(t)()
|
||||
logFloat(3.14)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_WithinTestPipeline tests logging within a test pipeline
|
||||
func TestLogf_WithinTestPipeline(t *testing.T) {
|
||||
type Config struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
config := Config{Host: "localhost", Port: 8080}
|
||||
|
||||
logConfig := Logf[Config]("Testing config: %+v")
|
||||
logConfig(config)(t)()
|
||||
|
||||
// Continue with assertions
|
||||
StringNotEmpty(config.Host)(t)
|
||||
That(func(port int) bool { return port > 0 })(config.Port)(t)
|
||||
|
||||
// Test passes if no panic occurs and assertions pass
|
||||
}
|
||||
|
||||
// TestLogf_NestedStructures tests logging of nested structures
|
||||
func TestLogf_NestedStructures(t *testing.T) {
|
||||
type Address struct {
|
||||
Street string
|
||||
City string
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Address Address
|
||||
}
|
||||
|
||||
logPerson := Logf[Person]("Person: %+v")
|
||||
|
||||
person := Person{
|
||||
Name: "Bob",
|
||||
Address: Address{
|
||||
Street: "123 Main St",
|
||||
City: "Springfield",
|
||||
},
|
||||
}
|
||||
|
||||
logPerson(person)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_Interface tests logging of interface values
|
||||
func TestLogf_Interface(t *testing.T) {
|
||||
logAny := Logf[any]("Value: %v")
|
||||
|
||||
logAny(42)(t)()
|
||||
logAny("string")(t)()
|
||||
logAny([]int{1, 2, 3})(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_GoSyntaxFormat tests logging with Go-syntax format
|
||||
func TestLogf_GoSyntaxFormat(t *testing.T) {
|
||||
type Point struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
logPoint := Logf[Point]("Point: %#v")
|
||||
|
||||
point := Point{X: 10, Y: 20}
|
||||
logPoint(point)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_TypeFormat tests logging with type format
|
||||
func TestLogf_TypeFormat(t *testing.T) {
|
||||
logType := Logf[any]("Type: %T, Value: %v")
|
||||
|
||||
logType(42)(t)()
|
||||
logType("string")(t)()
|
||||
logType(3.14)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_LargeNumbers tests logging of large numbers
|
||||
func TestLogf_LargeNumbers(t *testing.T) {
|
||||
logInt := Logf[int64]("Large number: %d")
|
||||
|
||||
logInt(9223372036854775807)(t)() // Max int64
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_NegativeNumbers tests logging of negative numbers
|
||||
func TestLogf_NegativeNumbers(t *testing.T) {
|
||||
logInt := Logf[int]("Number: %d")
|
||||
|
||||
logInt(-42)(t)()
|
||||
logInt(-100)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_SpecialFloats tests logging of special float values
|
||||
func TestLogf_SpecialFloats(t *testing.T) {
|
||||
logFloat := Logf[float64]("Float: %v")
|
||||
|
||||
logFloat(0.0)(t)()
|
||||
logFloat(-0.0)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_UnicodeStrings tests logging of unicode strings
|
||||
func TestLogf_UnicodeStrings(t *testing.T) {
|
||||
logString := Logf[string]("Unicode: %s")
|
||||
|
||||
logString("Hello, 世界")(t)()
|
||||
logString("Emoji: 🎉🎊")(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_MultilineStrings tests logging of multiline strings
|
||||
func TestLogf_MultilineStrings(t *testing.T) {
|
||||
logString := Logf[string]("Multiline:\n%s")
|
||||
|
||||
multiline := `Line 1
|
||||
Line 2
|
||||
Line 3`
|
||||
|
||||
logString(multiline)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_ReuseLogger tests reusing the same logger multiple times
|
||||
func TestLogf_ReuseLogger(t *testing.T) {
|
||||
logInt := Logf[int]("Value: %d")
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
logInt(i)(t)()
|
||||
}
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_ConditionalLogging tests conditional logging based on values
|
||||
func TestLogf_ConditionalLogging(t *testing.T) {
|
||||
logDebug := Logf[string]("DEBUG: %s")
|
||||
|
||||
values := []int{1, 2, 3, 4, 5}
|
||||
for _, v := range values {
|
||||
if v%2 == 0 {
|
||||
logDebug(fmt.Sprintf("Found even number: %d", v))(t)()
|
||||
}
|
||||
}
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_WithAssertions tests combining logging with assertions
|
||||
func TestLogf_WithAssertions(t *testing.T) {
|
||||
logInt := Logf[int]("Testing value: %d")
|
||||
|
||||
value := 42
|
||||
logInt(value)(t)()
|
||||
|
||||
// Perform assertion after logging
|
||||
Equal(42)(value)(t)
|
||||
|
||||
// Test passes if assertion passes
|
||||
}
|
||||
|
||||
// TestLogf_DebuggingFailures demonstrates using logging to debug test failures
|
||||
func TestLogf_DebuggingFailures(t *testing.T) {
|
||||
logSlice := Logf[[]int]("Input slice: %v")
|
||||
logInt := Logf[int]("Computed sum: %d")
|
||||
|
||||
numbers := []int{1, 2, 3, 4, 5}
|
||||
logSlice(numbers)(t)()
|
||||
|
||||
sum := 0
|
||||
for _, n := range numbers {
|
||||
sum += n
|
||||
}
|
||||
logInt(sum)(t)()
|
||||
|
||||
Equal(15)(sum)(t)
|
||||
|
||||
// Test passes if assertion passes
|
||||
}
|
||||
|
||||
// TestLogf_ComplexDataStructures tests logging of complex nested data
|
||||
func TestLogf_ComplexDataStructures(t *testing.T) {
|
||||
type Metadata struct {
|
||||
Version string
|
||||
Tags []string
|
||||
}
|
||||
|
||||
type Document struct {
|
||||
ID int
|
||||
Title string
|
||||
Metadata Metadata
|
||||
}
|
||||
|
||||
logDoc := Logf[Document]("Document: %+v")
|
||||
|
||||
doc := Document{
|
||||
ID: 1,
|
||||
Title: "Test Document",
|
||||
Metadata: Metadata{
|
||||
Version: "1.0",
|
||||
Tags: []string{"test", "example"},
|
||||
},
|
||||
}
|
||||
|
||||
logDoc(doc)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_ArrayTypes tests logging of array types
|
||||
func TestLogf_ArrayTypes(t *testing.T) {
|
||||
logArray := Logf[[5]int]("Array: %v")
|
||||
|
||||
arr := [5]int{1, 2, 3, 4, 5}
|
||||
logArray(arr)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_ChannelTypes tests logging of channel types
|
||||
func TestLogf_ChannelTypes(t *testing.T) {
|
||||
logChan := Logf[chan int]("Channel: %v")
|
||||
|
||||
ch := make(chan int, 1)
|
||||
logChan(ch)(t)()
|
||||
close(ch)
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
|
||||
// TestLogf_FunctionTypes tests logging of function types
|
||||
func TestLogf_FunctionTypes(t *testing.T) {
|
||||
logFunc := Logf[func() int]("Function: %v")
|
||||
|
||||
fn := func() int { return 42 }
|
||||
logFunc(fn)(t)()
|
||||
|
||||
// Test passes if no panic occurs
|
||||
}
|
||||
152
v2/assert/monoid.go
Normal file
152
v2/assert/monoid.go
Normal file
@@ -0,0 +1,152 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/boolean"
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// ApplicativeMonoid returns a [monoid.Monoid] for combining test assertion [Reader]s.
|
||||
//
|
||||
// This monoid combines multiple test assertions using logical AND (conjunction) semantics,
|
||||
// meaning all assertions must pass for the combined assertion to pass. It leverages the
|
||||
// applicative structure of Reader to execute multiple assertions with the same testing.T
|
||||
// context and combines their boolean results using boolean.MonoidAll (logical AND).
|
||||
//
|
||||
// The monoid provides:
|
||||
// - Concat: Combines two assertions such that both must pass (logical AND)
|
||||
// - Empty: Returns an assertion that always passes (identity element)
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Composing multiple test assertions into a single assertion
|
||||
// - Building complex test conditions from simpler ones
|
||||
// - Creating reusable assertion combinators
|
||||
// - Implementing test assertion DSLs
|
||||
//
|
||||
// # Monoid Laws
|
||||
//
|
||||
// The returned monoid satisfies the standard monoid laws:
|
||||
//
|
||||
// 1. Associativity:
|
||||
// Concat(Concat(a1, a2), a3) ≡ Concat(a1, Concat(a2, a3))
|
||||
//
|
||||
// 2. Left Identity:
|
||||
// Concat(Empty(), a) ≡ a
|
||||
//
|
||||
// 3. Right Identity:
|
||||
// Concat(a, Empty()) ≡ a
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A [monoid.Monoid][Reader] that combines assertions using logical AND
|
||||
//
|
||||
// # Example - Basic Usage
|
||||
//
|
||||
// func TestUserValidation(t *testing.T) {
|
||||
// user := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
|
||||
// m := assert.ApplicativeMonoid()
|
||||
//
|
||||
// // Combine multiple assertions
|
||||
// assertion := m.Concat(
|
||||
// assert.Equal("Alice")(user.Name),
|
||||
// m.Concat(
|
||||
// assert.Equal(30)(user.Age),
|
||||
// assert.StringNotEmpty(user.Email),
|
||||
// ),
|
||||
// )
|
||||
//
|
||||
// // Execute combined assertion
|
||||
// assertion(t) // All three assertions must pass
|
||||
// }
|
||||
//
|
||||
// # Example - Building Reusable Validators
|
||||
//
|
||||
// func TestWithReusableValidators(t *testing.T) {
|
||||
// m := assert.ApplicativeMonoid()
|
||||
//
|
||||
// // Create a reusable validator
|
||||
// validateUser := func(u User) assert.Reader {
|
||||
// return m.Concat(
|
||||
// assert.StringNotEmpty(u.Name),
|
||||
// m.Concat(
|
||||
// assert.True(u.Age > 0),
|
||||
// assert.StringContains("@")(u.Email),
|
||||
// ),
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// user := User{Name: "Bob", Age: 25, Email: "bob@test.com"}
|
||||
// validateUser(user)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Using Empty for Identity
|
||||
//
|
||||
// func TestEmptyIdentity(t *testing.T) {
|
||||
// m := assert.ApplicativeMonoid()
|
||||
// assertion := assert.Equal(42)(42)
|
||||
//
|
||||
// // Empty is the identity - these are equivalent
|
||||
// result1 := m.Concat(m.Empty(), assertion)(t)
|
||||
// result2 := m.Concat(assertion, m.Empty())(t)
|
||||
// result3 := assertion(t)
|
||||
// // All three produce the same result
|
||||
// }
|
||||
//
|
||||
// # Example - Combining with AllOf
|
||||
//
|
||||
// func TestCombiningWithAllOf(t *testing.T) {
|
||||
// // ApplicativeMonoid provides the underlying mechanism for AllOf
|
||||
// arr := []int{1, 2, 3, 4, 5}
|
||||
//
|
||||
// // These are conceptually equivalent:
|
||||
// m := assert.ApplicativeMonoid()
|
||||
// manual := m.Concat(
|
||||
// assert.ArrayNotEmpty(arr),
|
||||
// m.Concat(
|
||||
// assert.ArrayLength[int](5)(arr),
|
||||
// assert.ArrayContains(3)(arr),
|
||||
// ),
|
||||
// )
|
||||
//
|
||||
// // AllOf uses ApplicativeMonoid internally
|
||||
// convenient := assert.AllOf([]assert.Reader{
|
||||
// assert.ArrayNotEmpty(arr),
|
||||
// assert.ArrayLength[int](5)(arr),
|
||||
// assert.ArrayContains(3)(arr),
|
||||
// })
|
||||
//
|
||||
// manual(t)
|
||||
// convenient(t)
|
||||
// }
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [AllOf]: Convenient wrapper for combining multiple assertions using this monoid
|
||||
// - [boolean.MonoidAll]: The underlying boolean monoid (logical AND with true as identity)
|
||||
// - [reader.ApplicativeMonoid]: Generic applicative monoid for Reader types
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Haskell Monoid: https://hackage.haskell.org/package/base/docs/Data-Monoid.html
|
||||
// - Applicative Functors: https://hackage.haskell.org/package/base/docs/Control-Applicative.html
|
||||
// - Boolean Monoid (All): https://hackage.haskell.org/package/base/docs/Data-Monoid.html#t:All
|
||||
func ApplicativeMonoid() monoid.Monoid[Reader] {
|
||||
return reader.ApplicativeMonoid[*testing.T](boolean.MonoidAll)
|
||||
}
|
||||
454
v2/assert/monoid_test.go
Normal file
454
v2/assert/monoid_test.go
Normal file
@@ -0,0 +1,454 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestApplicativeMonoid_Empty tests that Empty returns an assertion that always passes
|
||||
func TestApplicativeMonoid_Empty(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
empty := m.Empty()
|
||||
|
||||
result := empty(t)
|
||||
if !result {
|
||||
t.Error("Expected Empty() to return an assertion that always passes")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_Concat_BothPass tests that Concat returns true when both assertions pass
|
||||
func TestApplicativeMonoid_Concat_BothPass(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion1 := Equal(42)(42)
|
||||
assertion2 := Equal("hello")("hello")
|
||||
|
||||
combined := m.Concat(assertion1, assertion2)
|
||||
result := combined(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected Concat to pass when both assertions pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_Concat_FirstFails tests that Concat returns false when first assertion fails
|
||||
func TestApplicativeMonoid_Concat_FirstFails(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion1 := Equal(42)(43) // This will fail
|
||||
assertion2 := Equal("hello")("hello")
|
||||
|
||||
combined := m.Concat(assertion1, assertion2)
|
||||
result := combined(mockT)
|
||||
|
||||
if result {
|
||||
t.Error("Expected Concat to fail when first assertion fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_Concat_SecondFails tests that Concat returns false when second assertion fails
|
||||
func TestApplicativeMonoid_Concat_SecondFails(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion1 := Equal(42)(42)
|
||||
assertion2 := Equal("hello")("world") // This will fail
|
||||
|
||||
combined := m.Concat(assertion1, assertion2)
|
||||
result := combined(mockT)
|
||||
|
||||
if result {
|
||||
t.Error("Expected Concat to fail when second assertion fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_Concat_BothFail tests that Concat returns false when both assertions fail
|
||||
func TestApplicativeMonoid_Concat_BothFail(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion1 := Equal(42)(43) // This will fail
|
||||
assertion2 := Equal("hello")("world") // This will fail
|
||||
|
||||
combined := m.Concat(assertion1, assertion2)
|
||||
result := combined(mockT)
|
||||
|
||||
if result {
|
||||
t.Error("Expected Concat to fail when both assertions fail")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_LeftIdentity tests the left identity law: Concat(Empty(), a) = a
|
||||
func TestApplicativeMonoid_LeftIdentity(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion := Equal(42)(42)
|
||||
|
||||
// Concat(Empty(), assertion) should behave the same as assertion
|
||||
combined := m.Concat(m.Empty(), assertion)
|
||||
|
||||
result1 := assertion(t)
|
||||
result2 := combined(t)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Left identity law violated: Concat(Empty(), a) should equal a")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_RightIdentity tests the right identity law: Concat(a, Empty()) = a
|
||||
func TestApplicativeMonoid_RightIdentity(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion := Equal(42)(42)
|
||||
|
||||
// Concat(assertion, Empty()) should behave the same as assertion
|
||||
combined := m.Concat(assertion, m.Empty())
|
||||
|
||||
result1 := assertion(t)
|
||||
result2 := combined(t)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Right identity law violated: Concat(a, Empty()) should equal a")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_Associativity tests the associativity law: Concat(Concat(a, b), c) = Concat(a, Concat(b, c))
|
||||
func TestApplicativeMonoid_Associativity(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
a1 := Equal(1)(1)
|
||||
a2 := Equal(2)(2)
|
||||
a3 := Equal(3)(3)
|
||||
|
||||
// Concat(Concat(a1, a2), a3)
|
||||
left := m.Concat(m.Concat(a1, a2), a3)
|
||||
|
||||
// Concat(a1, Concat(a2, a3))
|
||||
right := m.Concat(a1, m.Concat(a2, a3))
|
||||
|
||||
result1 := left(t)
|
||||
result2 := right(t)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Associativity law violated: Concat(Concat(a, b), c) should equal Concat(a, Concat(b, c))")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_AssociativityWithFailure tests associativity when assertions fail
|
||||
func TestApplicativeMonoid_AssociativityWithFailure(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
a1 := Equal(1)(1)
|
||||
a2 := Equal(2)(3) // This will fail
|
||||
a3 := Equal(3)(3)
|
||||
|
||||
// Concat(Concat(a1, a2), a3)
|
||||
left := m.Concat(m.Concat(a1, a2), a3)
|
||||
|
||||
// Concat(a1, Concat(a2, a3))
|
||||
right := m.Concat(a1, m.Concat(a2, a3))
|
||||
|
||||
result1 := left(mockT)
|
||||
result2 := right(mockT)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Associativity law violated even with failures")
|
||||
}
|
||||
|
||||
if result1 || result2 {
|
||||
t.Error("Expected both to fail when one assertion fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_ComplexAssertions tests combining complex assertions
|
||||
func TestApplicativeMonoid_ComplexAssertions(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
arr := []int{1, 2, 3, 4, 5}
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
|
||||
arrayAssertions := m.Concat(
|
||||
ArrayNotEmpty(arr),
|
||||
m.Concat(
|
||||
ArrayLength[int](5)(arr),
|
||||
ArrayContains(3)(arr),
|
||||
),
|
||||
)
|
||||
|
||||
mapAssertions := m.Concat(
|
||||
RecordNotEmpty(mp),
|
||||
RecordLength[string, int](2)(mp),
|
||||
)
|
||||
|
||||
combined := m.Concat(arrayAssertions, mapAssertions)
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected complex combined assertions to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_ComplexAssertionsWithFailure tests complex assertions when one fails
|
||||
func TestApplicativeMonoid_ComplexAssertionsWithFailure(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
arr := []int{1, 2, 3}
|
||||
mp := map[string]int{"a": 1, "b": 2}
|
||||
|
||||
arrayAssertions := m.Concat(
|
||||
ArrayNotEmpty(arr),
|
||||
m.Concat(
|
||||
ArrayLength[int](5)(arr), // This will fail - array has 3 elements, not 5
|
||||
ArrayContains(3)(arr),
|
||||
),
|
||||
)
|
||||
|
||||
mapAssertions := m.Concat(
|
||||
RecordNotEmpty(mp),
|
||||
RecordLength[string, int](2)(mp),
|
||||
)
|
||||
|
||||
combined := m.Concat(arrayAssertions, mapAssertions)
|
||||
|
||||
result := combined(mockT)
|
||||
if result {
|
||||
t.Error("Expected complex combined assertions to fail when one assertion fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_MultipleConcat tests chaining multiple Concat operations
|
||||
func TestApplicativeMonoid_MultipleConcat(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
a1 := Equal(1)(1)
|
||||
a2 := Equal(2)(2)
|
||||
a3 := Equal(3)(3)
|
||||
a4 := Equal(4)(4)
|
||||
|
||||
combined := m.Concat(
|
||||
m.Concat(a1, a2),
|
||||
m.Concat(a3, a4),
|
||||
)
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected multiple Concat operations to pass when all assertions pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_WithStringAssertions tests combining string assertions
|
||||
func TestApplicativeMonoid_WithStringAssertions(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
str := "hello world"
|
||||
|
||||
combined := m.Concat(
|
||||
StringNotEmpty(str),
|
||||
StringLength[any, any](11)(str),
|
||||
)
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected string assertions to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_WithBooleanAssertions tests combining boolean assertions
|
||||
func TestApplicativeMonoid_WithBooleanAssertions(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
combined := m.Concat(
|
||||
Equal(true)(true),
|
||||
m.Concat(
|
||||
Equal(false)(false),
|
||||
Equal(true)(true),
|
||||
),
|
||||
)
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected boolean assertions to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_WithErrorAssertions tests combining error assertions
|
||||
func TestApplicativeMonoid_WithErrorAssertions(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
combined := m.Concat(
|
||||
NoError(nil),
|
||||
Equal("test")("test"),
|
||||
)
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected error assertions to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_EmptyWithMultipleConcat tests Empty with multiple Concat operations
|
||||
func TestApplicativeMonoid_EmptyWithMultipleConcat(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
assertion := Equal(42)(42)
|
||||
|
||||
// Multiple Empty values should still act as identity
|
||||
combined := m.Concat(
|
||||
m.Empty(),
|
||||
m.Concat(
|
||||
assertion,
|
||||
m.Empty(),
|
||||
),
|
||||
)
|
||||
|
||||
result1 := assertion(t)
|
||||
result2 := combined(t)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Multiple Empty values should still act as identity")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_OnlyEmpty tests using only Empty values
|
||||
func TestApplicativeMonoid_OnlyEmpty(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
// Concat of Empty values should still be Empty (identity)
|
||||
combined := m.Concat(m.Empty(), m.Empty())
|
||||
|
||||
result := combined(t)
|
||||
if !result {
|
||||
t.Error("Expected Concat of Empty values to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_RealWorldExample tests a realistic use case
|
||||
func TestApplicativeMonoid_RealWorldExample(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
validateUser := func(u User) Reader {
|
||||
return m.Concat(
|
||||
StringNotEmpty(u.Name),
|
||||
m.Concat(
|
||||
That(func(age int) bool { return age > 0 })(u.Age),
|
||||
m.Concat(
|
||||
That(func(age int) bool { return age < 150 })(u.Age),
|
||||
That(func(email string) bool {
|
||||
for _, ch := range email {
|
||||
if ch == '@' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})(u.Email),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
validUser := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
|
||||
result := validateUser(validUser)(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected valid user to pass all validations")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_RealWorldExampleWithFailure tests a realistic use case with failure
|
||||
func TestApplicativeMonoid_RealWorldExampleWithFailure(t *testing.T) {
|
||||
mockT := &testing.T{}
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
m := ApplicativeMonoid()
|
||||
|
||||
validateUser := func(u User) Reader {
|
||||
return m.Concat(
|
||||
StringNotEmpty(u.Name),
|
||||
m.Concat(
|
||||
That(func(age int) bool { return age > 0 })(u.Age),
|
||||
m.Concat(
|
||||
That(func(age int) bool { return age < 150 })(u.Age),
|
||||
That(func(email string) bool {
|
||||
for _, ch := range email {
|
||||
if ch == '@' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})(u.Email),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
invalidUser := User{Name: "Bob", Age: 200, Email: "bob@test.com"} // Age > 150
|
||||
result := validateUser(invalidUser)(mockT)
|
||||
|
||||
if result {
|
||||
t.Error("Expected invalid user to fail validation")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplicativeMonoid_IntegrationWithAllOf demonstrates relationship with AllOf
|
||||
func TestApplicativeMonoid_IntegrationWithAllOf(t *testing.T) {
|
||||
m := ApplicativeMonoid()
|
||||
arr := []int{1, 2, 3, 4, 5}
|
||||
|
||||
// Using ApplicativeMonoid directly
|
||||
manualCombination := m.Concat(
|
||||
ArrayNotEmpty(arr),
|
||||
m.Concat(
|
||||
ArrayLength[int](5)(arr),
|
||||
ArrayContains(3)(arr),
|
||||
),
|
||||
)
|
||||
|
||||
// Using AllOf (which uses ApplicativeMonoid internally)
|
||||
allOfCombination := AllOf([]Reader{
|
||||
ArrayNotEmpty(arr),
|
||||
ArrayLength[int](5)(arr),
|
||||
ArrayContains(3)(arr),
|
||||
})
|
||||
|
||||
result1 := manualCombination(t)
|
||||
result2 := allOfCombination(t)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Error("Expected manual combination and AllOf to produce same result")
|
||||
}
|
||||
|
||||
if !result1 || !result2 {
|
||||
t.Error("Expected both combinations to pass")
|
||||
}
|
||||
}
|
||||
650
v2/assert/traverse.go
Normal file
650
v2/assert/traverse.go
Normal file
@@ -0,0 +1,650 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
// TraverseArray transforms an array of values into a test suite by applying a function
|
||||
// that generates named test cases for each element.
|
||||
//
|
||||
// This function enables data-driven testing where you have a collection of test inputs
|
||||
// and want to run a named subtest for each one. It follows the functional programming
|
||||
// pattern of "traverse" - transforming a collection while preserving structure and
|
||||
// accumulating effects (in this case, test execution).
|
||||
//
|
||||
// The function takes each element of the array, applies the provided function to generate
|
||||
// a [Pair] of (test name, test assertion), and runs each as a separate subtest using
|
||||
// Go's t.Run. All subtests must pass for the overall test to pass.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes a value of type T and returns a [Pair] containing:
|
||||
// - Head: The test name (string) for the subtest
|
||||
// - Tail: The test assertion ([Reader]) to execute
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A [Kleisli] function that takes an array of T and returns a [Reader] that:
|
||||
// - Executes each element as a named subtest
|
||||
// - Returns true only if all subtests pass
|
||||
// - Provides proper test isolation and reporting via t.Run
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Data-driven testing with multiple test cases
|
||||
// - Parameterized tests where each parameter gets its own subtest
|
||||
// - Testing collections where each element needs validation
|
||||
// - Property-based testing with generated test data
|
||||
//
|
||||
// # Example - Basic Data-Driven Testing
|
||||
//
|
||||
// func TestMathOperations(t *testing.T) {
|
||||
// type TestCase struct {
|
||||
// Input int
|
||||
// Expected int
|
||||
// }
|
||||
//
|
||||
// testCases := []TestCase{
|
||||
// {Input: 2, Expected: 4},
|
||||
// {Input: 3, Expected: 9},
|
||||
// {Input: 4, Expected: 16},
|
||||
// }
|
||||
//
|
||||
// square := func(n int) int { return n * n }
|
||||
//
|
||||
// traverse := assert.TraverseArray(func(tc TestCase) assert.Pair[string, assert.Reader] {
|
||||
// name := fmt.Sprintf("square(%d)=%d", tc.Input, tc.Expected)
|
||||
// assertion := assert.Equal(tc.Expected)(square(tc.Input))
|
||||
// return pair.MakePair(name, assertion)
|
||||
// })
|
||||
//
|
||||
// traverse(testCases)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - String Validation
|
||||
//
|
||||
// func TestStringValidation(t *testing.T) {
|
||||
// inputs := []string{"hello", "world", "test"}
|
||||
//
|
||||
// traverse := assert.TraverseArray(func(s string) assert.Pair[string, assert.Reader] {
|
||||
// return pair.MakePair(
|
||||
// fmt.Sprintf("validate_%s", s),
|
||||
// assert.AllOf([]assert.Reader{
|
||||
// assert.StringNotEmpty(s),
|
||||
// assert.That(func(str string) bool { return len(str) > 0 })(s),
|
||||
// }),
|
||||
// )
|
||||
// })
|
||||
//
|
||||
// traverse(inputs)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Complex Object Testing
|
||||
//
|
||||
// func TestUsers(t *testing.T) {
|
||||
// type User struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// Email string
|
||||
// }
|
||||
//
|
||||
// users := []User{
|
||||
// {Name: "Alice", Age: 30, Email: "alice@example.com"},
|
||||
// {Name: "Bob", Age: 25, Email: "bob@example.com"},
|
||||
// }
|
||||
//
|
||||
// traverse := assert.TraverseArray(func(u User) assert.Pair[string, assert.Reader] {
|
||||
// return pair.MakePair(
|
||||
// fmt.Sprintf("user_%s", u.Name),
|
||||
// assert.AllOf([]assert.Reader{
|
||||
// assert.StringNotEmpty(u.Name),
|
||||
// assert.That(func(age int) bool { return age > 0 })(u.Age),
|
||||
// assert.That(func(email string) bool {
|
||||
// return len(email) > 0 && strings.Contains(email, "@")
|
||||
// })(u.Email),
|
||||
// }),
|
||||
// )
|
||||
// })
|
||||
//
|
||||
// traverse(users)(t)
|
||||
// }
|
||||
//
|
||||
// # Comparison with RunAll
|
||||
//
|
||||
// TraverseArray and [RunAll] serve similar purposes but differ in their approach:
|
||||
//
|
||||
// - TraverseArray: Generates test cases from an array of data
|
||||
//
|
||||
// - Input: Array of values + function to generate test cases
|
||||
//
|
||||
// - Use when: You have test data and need to generate test cases from it
|
||||
//
|
||||
// - RunAll: Executes pre-defined named test cases
|
||||
//
|
||||
// - Input: Map of test names to assertions
|
||||
//
|
||||
// - Use when: You have already defined test cases with names
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [SequenceSeq2]: Similar but works with Go iterators (Seq2) instead of arrays
|
||||
// - [RunAll]: Executes a map of named test cases
|
||||
// - [AllOf]: Combines multiple assertions without subtests
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Haskell traverse: https://hackage.haskell.org/package/base/docs/Data-Traversable.html#v:traverse
|
||||
// - Go subtests: https://go.dev/blog/subtests
|
||||
func TraverseArray[T any](f func(T) Pair[string, Reader]) Kleisli[[]T] {
|
||||
return func(ts []T) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
ok := true
|
||||
for _, src := range ts {
|
||||
test := f(src)
|
||||
res := t.Run(pair.Head(test), func(t *testing.T) {
|
||||
pair.Tail(test)(t)
|
||||
})
|
||||
ok = ok && res
|
||||
}
|
||||
return ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceSeq2 executes a sequence of named test cases provided as a Go iterator.
|
||||
//
|
||||
// This function takes a [Seq2] iterator that yields (name, assertion) pairs and
|
||||
// executes each as a separate subtest using Go's t.Run. It's similar to [TraverseArray]
|
||||
// but works directly with Go's iterator protocol (introduced in Go 1.23) rather than
|
||||
// requiring an array.
|
||||
//
|
||||
// The function iterates through all test cases, running each as a named subtest.
|
||||
// All subtests must pass for the overall test to pass. This provides proper test
|
||||
// isolation and clear reporting of which specific test cases fail.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - s: A [Seq2] iterator that yields pairs of:
|
||||
// - Key: Test name (string) for the subtest
|
||||
// - Value: Test assertion ([Reader]) to execute
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A [Reader] that:
|
||||
// - Executes each test case as a named subtest
|
||||
// - Returns true only if all subtests pass
|
||||
// - Provides proper test isolation via t.Run
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Working with iterator-based test data
|
||||
// - Lazy evaluation of test cases
|
||||
// - Integration with Go 1.23+ iterator patterns
|
||||
// - Memory-efficient testing of large test suites
|
||||
//
|
||||
// # Example - Basic Usage with Iterator
|
||||
//
|
||||
// func TestWithIterator(t *testing.T) {
|
||||
// // Create an iterator of test cases
|
||||
// testCases := func(yield func(string, assert.Reader) bool) {
|
||||
// if !yield("test_addition", assert.Equal(4)(2+2)) {
|
||||
// return
|
||||
// }
|
||||
// if !yield("test_subtraction", assert.Equal(1)(3-2)) {
|
||||
// return
|
||||
// }
|
||||
// if !yield("test_multiplication", assert.Equal(6)(2*3)) {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// assert.SequenceSeq2(testCases)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Generated Test Cases
|
||||
//
|
||||
// func TestGeneratedCases(t *testing.T) {
|
||||
// // Generate test cases on the fly
|
||||
// generateTests := func(yield func(string, assert.Reader) bool) {
|
||||
// for i := 1; i <= 5; i++ {
|
||||
// name := fmt.Sprintf("test_%d", i)
|
||||
// assertion := assert.Equal(i*i)(i * i)
|
||||
// if !yield(name, assertion) {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// assert.SequenceSeq2(generateTests)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Filtering Test Cases
|
||||
//
|
||||
// func TestFilteredCases(t *testing.T) {
|
||||
// type TestCase struct {
|
||||
// Name string
|
||||
// Input int
|
||||
// Expected int
|
||||
// Skip bool
|
||||
// }
|
||||
//
|
||||
// allCases := []TestCase{
|
||||
// {Name: "test1", Input: 2, Expected: 4, Skip: false},
|
||||
// {Name: "test2", Input: 3, Expected: 9, Skip: true},
|
||||
// {Name: "test3", Input: 4, Expected: 16, Skip: false},
|
||||
// }
|
||||
//
|
||||
// // Create iterator that filters out skipped tests
|
||||
// activeTests := func(yield func(string, assert.Reader) bool) {
|
||||
// for _, tc := range allCases {
|
||||
// if !tc.Skip {
|
||||
// assertion := assert.Equal(tc.Expected)(tc.Input * tc.Input)
|
||||
// if !yield(tc.Name, assertion) {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// assert.SequenceSeq2(activeTests)(t)
|
||||
// }
|
||||
//
|
||||
// # Comparison with TraverseArray
|
||||
//
|
||||
// SequenceSeq2 and [TraverseArray] serve similar purposes but differ in their input:
|
||||
//
|
||||
// - SequenceSeq2: Works with iterators (Seq2)
|
||||
//
|
||||
// - Input: Iterator yielding (name, assertion) pairs
|
||||
//
|
||||
// - Use when: Working with Go 1.23+ iterators or lazy evaluation
|
||||
//
|
||||
// - Memory: More efficient for large test suites (lazy evaluation)
|
||||
//
|
||||
// - TraverseArray: Works with arrays
|
||||
//
|
||||
// - Input: Array of values + transformation function
|
||||
//
|
||||
// - Use when: You have an array of test data
|
||||
//
|
||||
// - Memory: All test data must be in memory
|
||||
//
|
||||
// # Comparison with RunAll
|
||||
//
|
||||
// SequenceSeq2 and [RunAll] are very similar:
|
||||
//
|
||||
// - SequenceSeq2: Takes an iterator (Seq2)
|
||||
// - RunAll: Takes a map[string]Reader
|
||||
//
|
||||
// Both execute named test cases as subtests. Choose based on your data structure:
|
||||
// use SequenceSeq2 for iterators, RunAll for maps.
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [TraverseArray]: Similar but works with arrays instead of iterators
|
||||
// - [RunAll]: Executes a map of named test cases
|
||||
// - [AllOf]: Combines multiple assertions without subtests
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Go iterators: https://go.dev/blog/range-functions
|
||||
// - Go subtests: https://go.dev/blog/subtests
|
||||
// - Haskell sequence: https://hackage.haskell.org/package/base/docs/Data-Traversable.html#v:sequence
|
||||
func SequenceSeq2[T any](s Seq2[string, Reader]) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
ok := true
|
||||
for name, test := range s {
|
||||
res := t.Run(name, func(t *testing.T) {
|
||||
test(t)
|
||||
})
|
||||
ok = ok && res
|
||||
}
|
||||
return ok
|
||||
}
|
||||
}
|
||||
|
||||
// TraverseRecord transforms a map of values into a test suite by applying a function
|
||||
// that generates test assertions for each map entry.
|
||||
//
|
||||
// This function enables data-driven testing where you have a map of test data and want
|
||||
// to run a named subtest for each entry. The map keys become test names, and the function
|
||||
// transforms each value into a test assertion. It follows the functional programming
|
||||
// pattern of "traverse" - transforming a collection while preserving structure and
|
||||
// accumulating effects (in this case, test execution).
|
||||
//
|
||||
// The function takes each key-value pair from the map, applies the provided function to
|
||||
// generate a [Reader] assertion, and runs each as a separate subtest using Go's t.Run.
|
||||
// All subtests must pass for the overall test to pass.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A [Kleisli] function that takes a value of type T and returns a [Reader] assertion
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A [Kleisli] function that takes a map[string]T and returns a [Reader] that:
|
||||
// - Executes each map entry as a named subtest (using the key as the test name)
|
||||
// - Returns true only if all subtests pass
|
||||
// - Provides proper test isolation and reporting via t.Run
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Data-driven testing with named test cases in a map
|
||||
// - Testing configuration maps where keys are meaningful names
|
||||
// - Validating collections where natural keys exist
|
||||
// - Property-based testing with named scenarios
|
||||
//
|
||||
// # Example - Basic Configuration Testing
|
||||
//
|
||||
// func TestConfigurations(t *testing.T) {
|
||||
// configs := map[string]int{
|
||||
// "timeout": 30,
|
||||
// "maxRetries": 3,
|
||||
// "bufferSize": 1024,
|
||||
// }
|
||||
//
|
||||
// validatePositive := assert.That(func(n int) bool { return n > 0 })
|
||||
//
|
||||
// traverse := assert.TraverseRecord(validatePositive)
|
||||
// traverse(configs)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - User Validation
|
||||
//
|
||||
// func TestUserMap(t *testing.T) {
|
||||
// type User struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// }
|
||||
//
|
||||
// users := map[string]User{
|
||||
// "alice": {Name: "Alice", Age: 30},
|
||||
// "bob": {Name: "Bob", Age: 25},
|
||||
// "carol": {Name: "Carol", Age: 35},
|
||||
// }
|
||||
//
|
||||
// validateUser := func(u User) assert.Reader {
|
||||
// return assert.AllOf([]assert.Reader{
|
||||
// assert.StringNotEmpty(u.Name),
|
||||
// assert.That(func(age int) bool { return age > 0 && age < 150 })(u.Age),
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// traverse := assert.TraverseRecord(validateUser)
|
||||
// traverse(users)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - API Endpoint Testing
|
||||
//
|
||||
// func TestEndpoints(t *testing.T) {
|
||||
// type Endpoint struct {
|
||||
// Path string
|
||||
// Method string
|
||||
// }
|
||||
//
|
||||
// endpoints := map[string]Endpoint{
|
||||
// "get_users": {Path: "/api/users", Method: "GET"},
|
||||
// "create_user": {Path: "/api/users", Method: "POST"},
|
||||
// "delete_user": {Path: "/api/users/:id", Method: "DELETE"},
|
||||
// }
|
||||
//
|
||||
// validateEndpoint := func(e Endpoint) assert.Reader {
|
||||
// return assert.AllOf([]assert.Reader{
|
||||
// assert.StringNotEmpty(e.Path),
|
||||
// assert.That(func(path string) bool {
|
||||
// return strings.HasPrefix(path, "/api/")
|
||||
// })(e.Path),
|
||||
// assert.That(func(method string) bool {
|
||||
// return method == "GET" || method == "POST" ||
|
||||
// method == "PUT" || method == "DELETE"
|
||||
// })(e.Method),
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// traverse := assert.TraverseRecord(validateEndpoint)
|
||||
// traverse(endpoints)(t)
|
||||
// }
|
||||
//
|
||||
// # Comparison with TraverseArray
|
||||
//
|
||||
// TraverseRecord and [TraverseArray] serve similar purposes but differ in their input:
|
||||
//
|
||||
// - TraverseRecord: Works with maps (records)
|
||||
//
|
||||
// - Input: Map with string keys + transformation function
|
||||
//
|
||||
// - Use when: You have named test data in a map
|
||||
//
|
||||
// - Test names: Derived from map keys
|
||||
//
|
||||
// - TraverseArray: Works with arrays
|
||||
//
|
||||
// - Input: Array of values + function that generates names and assertions
|
||||
//
|
||||
// - Use when: You have sequential test data
|
||||
//
|
||||
// - Test names: Generated by the transformation function
|
||||
//
|
||||
// # Comparison with SequenceRecord
|
||||
//
|
||||
// TraverseRecord and [SequenceRecord] are closely related:
|
||||
//
|
||||
// - TraverseRecord: Transforms values into assertions
|
||||
//
|
||||
// - Input: map[string]T + function T -> Reader
|
||||
//
|
||||
// - Use when: You need to transform data before asserting
|
||||
//
|
||||
// - SequenceRecord: Executes pre-defined assertions
|
||||
//
|
||||
// - Input: map[string]Reader
|
||||
//
|
||||
// - Use when: Assertions are already defined
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [SequenceRecord]: Similar but takes pre-defined assertions
|
||||
// - [TraverseArray]: Similar but works with arrays
|
||||
// - [RunAll]: Alias for SequenceRecord
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Haskell traverse: https://hackage.haskell.org/package/base/docs/Data-Traversable.html#v:traverse
|
||||
// - Go subtests: https://go.dev/blog/subtests
|
||||
func TraverseRecord[T any](f Kleisli[T]) Kleisli[map[string]T] {
|
||||
return func(m map[string]T) Reader {
|
||||
return func(t *testing.T) bool {
|
||||
ok := true
|
||||
for name, src := range m {
|
||||
res := t.Run(name, func(t *testing.T) {
|
||||
f(src)(t)
|
||||
})
|
||||
ok = ok && res
|
||||
}
|
||||
return ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SequenceRecord executes a map of named test cases as subtests.
|
||||
//
|
||||
// This function takes a map where keys are test names and values are test assertions
|
||||
// ([Reader]), and executes each as a separate subtest using Go's t.Run. It's the
|
||||
// record (map) equivalent of [SequenceSeq2] and is actually aliased as [RunAll] for
|
||||
// convenience.
|
||||
//
|
||||
// The function iterates through all map entries, running each as a named subtest.
|
||||
// All subtests must pass for the overall test to pass. This provides proper test
|
||||
// isolation and clear reporting of which specific test cases fail.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - m: A map[string]Reader where:
|
||||
// - Keys: Test names (strings) for the subtests
|
||||
// - Values: Test assertions ([Reader]) to execute
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A [Reader] that:
|
||||
// - Executes each map entry as a named subtest
|
||||
// - Returns true only if all subtests pass
|
||||
// - Provides proper test isolation via t.Run
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Executing a collection of pre-defined named test cases
|
||||
// - Organizing related tests in a map structure
|
||||
// - Running multiple assertions with descriptive names
|
||||
// - Building test suites programmatically
|
||||
//
|
||||
// # Example - Basic Named Tests
|
||||
//
|
||||
// func TestMathOperations(t *testing.T) {
|
||||
// tests := map[string]assert.Reader{
|
||||
// "addition": assert.Equal(4)(2 + 2),
|
||||
// "subtraction": assert.Equal(1)(3 - 2),
|
||||
// "multiplication": assert.Equal(6)(2 * 3),
|
||||
// "division": assert.Equal(2)(6 / 3),
|
||||
// }
|
||||
//
|
||||
// assert.SequenceRecord(tests)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - String Validation Suite
|
||||
//
|
||||
// func TestStringValidations(t *testing.T) {
|
||||
// testString := "hello world"
|
||||
//
|
||||
// tests := map[string]assert.Reader{
|
||||
// "not_empty": assert.StringNotEmpty(testString),
|
||||
// "correct_length": assert.StringLength[any, any](11)(testString),
|
||||
// "has_space": assert.That(func(s string) bool {
|
||||
// return strings.Contains(s, " ")
|
||||
// })(testString),
|
||||
// "lowercase": assert.That(func(s string) bool {
|
||||
// return s == strings.ToLower(s)
|
||||
// })(testString),
|
||||
// }
|
||||
//
|
||||
// assert.SequenceRecord(tests)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Complex Object Validation
|
||||
//
|
||||
// func TestUserValidation(t *testing.T) {
|
||||
// type User struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// Email string
|
||||
// }
|
||||
//
|
||||
// user := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
|
||||
//
|
||||
// tests := map[string]assert.Reader{
|
||||
// "name_not_empty": assert.StringNotEmpty(user.Name),
|
||||
// "age_positive": assert.That(func(age int) bool { return age > 0 })(user.Age),
|
||||
// "age_reasonable": assert.That(func(age int) bool { return age < 150 })(user.Age),
|
||||
// "email_valid": assert.That(func(email string) bool {
|
||||
// return strings.Contains(email, "@") && strings.Contains(email, ".")
|
||||
// })(user.Email),
|
||||
// }
|
||||
//
|
||||
// assert.SequenceRecord(tests)(t)
|
||||
// }
|
||||
//
|
||||
// # Example - Array Validation Suite
|
||||
//
|
||||
// func TestArrayValidations(t *testing.T) {
|
||||
// numbers := []int{1, 2, 3, 4, 5}
|
||||
//
|
||||
// tests := map[string]assert.Reader{
|
||||
// "not_empty": assert.ArrayNotEmpty(numbers),
|
||||
// "correct_length": assert.ArrayLength[int](5)(numbers),
|
||||
// "contains_three": assert.ArrayContains(3)(numbers),
|
||||
// "all_positive": assert.That(func(arr []int) bool {
|
||||
// for _, n := range arr {
|
||||
// if n <= 0 {
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
// return true
|
||||
// })(numbers),
|
||||
// }
|
||||
//
|
||||
// assert.SequenceRecord(tests)(t)
|
||||
// }
|
||||
//
|
||||
// # Comparison with TraverseRecord
|
||||
//
|
||||
// SequenceRecord and [TraverseRecord] are closely related:
|
||||
//
|
||||
// - SequenceRecord: Executes pre-defined assertions
|
||||
//
|
||||
// - Input: map[string]Reader (assertions already created)
|
||||
//
|
||||
// - Use when: You have already defined test cases with assertions
|
||||
//
|
||||
// - TraverseRecord: Transforms values into assertions
|
||||
//
|
||||
// - Input: map[string]T + function T -> Reader
|
||||
//
|
||||
// - Use when: You need to transform data before asserting
|
||||
//
|
||||
// # Comparison with SequenceSeq2
|
||||
//
|
||||
// SequenceRecord and [SequenceSeq2] serve similar purposes but differ in their input:
|
||||
//
|
||||
// - SequenceRecord: Works with maps
|
||||
//
|
||||
// - Input: map[string]Reader
|
||||
//
|
||||
// - Use when: You have named test cases in a map
|
||||
//
|
||||
// - Iteration order: Non-deterministic (map iteration)
|
||||
//
|
||||
// - SequenceSeq2: Works with iterators
|
||||
//
|
||||
// - Input: Seq2[string, Reader]
|
||||
//
|
||||
// - Use when: You have test cases in an iterator
|
||||
//
|
||||
// - Iteration order: Deterministic (iterator order)
|
||||
//
|
||||
// # Note on Map Iteration Order
|
||||
//
|
||||
// Go maps have non-deterministic iteration order. If test execution order matters,
|
||||
// consider using [SequenceSeq2] with an iterator that provides deterministic ordering,
|
||||
// or use [TraverseArray] with a slice of test cases.
|
||||
//
|
||||
// # Related Functions
|
||||
//
|
||||
// - [RunAll]: Alias for SequenceRecord
|
||||
// - [TraverseRecord]: Similar but transforms values into assertions
|
||||
// - [SequenceSeq2]: Similar but works with iterators
|
||||
// - [TraverseArray]: Similar but works with arrays
|
||||
//
|
||||
// # References
|
||||
//
|
||||
// - Go subtests: https://go.dev/blog/subtests
|
||||
// - Haskell sequence: https://hackage.haskell.org/package/base/docs/Data-Traversable.html#v:sequence
|
||||
func SequenceRecord(m map[string]Reader) Reader {
|
||||
return TraverseRecord(reader.Ask[Reader]())(m)
|
||||
}
|
||||
960
v2/assert/traverse_test.go
Normal file
960
v2/assert/traverse_test.go
Normal file
@@ -0,0 +1,960 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// TestTraverseArray_EmptyArray tests that TraverseArray handles empty arrays correctly
|
||||
func TestTraverseArray_EmptyArray(t *testing.T) {
|
||||
traverse := TraverseArray(func(n int) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("test_%d", n),
|
||||
Equal(n)(n),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse([]int{})(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseArray to pass with empty array")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_SingleElement tests TraverseArray with a single element
|
||||
func TestTraverseArray_SingleElement(t *testing.T) {
|
||||
traverse := TraverseArray(func(n int) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("test_%d", n),
|
||||
Equal(n*2)(n*2),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse([]int{5})(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseArray to pass with single element")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_MultipleElements tests TraverseArray with multiple passing elements
|
||||
func TestTraverseArray_MultipleElements(t *testing.T) {
|
||||
traverse := TraverseArray(func(n int) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("square_%d", n),
|
||||
Equal(n*n)(n*n),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse([]int{1, 2, 3, 4, 5})(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseArray to pass with all passing elements")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_WithFailure tests that TraverseArray fails when one element fails
|
||||
func TestTraverseArray_WithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
traverse := TraverseArray(func(n int) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("test_%d", n),
|
||||
Equal(10)(n), // Will fail for all except 10
|
||||
)
|
||||
})
|
||||
|
||||
// Run in a subtest - we expect the subtests to fail, so t.Run returns false
|
||||
result := traverse([]int{1, 2, 3})(t)
|
||||
|
||||
// The traverse should return false because assertions fail
|
||||
if result {
|
||||
t.Error("Expected traverse to return false when elements don't match")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_MixedResults tests TraverseArray with some passing and some failing
|
||||
func TestTraverseArray_MixedResults(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
traverse := TraverseArray(func(n int) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("is_even_%d", n),
|
||||
Equal(0)(n%2), // Only passes for even numbers
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse([]int{2, 3, 4})(t) // 3 is odd, should fail
|
||||
|
||||
// The traverse should return false because one assertion fails
|
||||
if result {
|
||||
t.Error("Expected traverse to return false when some elements fail")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_StringData tests TraverseArray with string data
|
||||
func TestTraverseArray_StringData(t *testing.T) {
|
||||
words := []string{"hello", "world", "test"}
|
||||
|
||||
traverse := TraverseArray(func(s string) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("validate_%s", s),
|
||||
AllOf([]Reader{
|
||||
StringNotEmpty(s),
|
||||
That(func(str string) bool { return len(str) > 0 })(s),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse(words)(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseArray to pass with valid strings")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_ComplexObjects tests TraverseArray with complex objects
|
||||
func TestTraverseArray_ComplexObjects(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
users := []User{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "Bob", Age: 25},
|
||||
{Name: "Charlie", Age: 35},
|
||||
}
|
||||
|
||||
traverse := TraverseArray(func(u User) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("user_%s", u.Name),
|
||||
AllOf([]Reader{
|
||||
StringNotEmpty(u.Name),
|
||||
That(func(age int) bool { return age > 0 && age < 150 })(u.Age),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse(users)(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseArray to pass with valid users")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_ComplexObjectsWithFailure tests TraverseArray with invalid complex objects
|
||||
func TestTraverseArray_ComplexObjectsWithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
users := []User{
|
||||
{Name: "Alice", Age: 30},
|
||||
{Name: "", Age: 25}, // Invalid: empty name
|
||||
{Name: "Charlie", Age: 35},
|
||||
}
|
||||
|
||||
traverse := TraverseArray(func(u User) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("user_%s", u.Name),
|
||||
AllOf([]Reader{
|
||||
StringNotEmpty(u.Name),
|
||||
That(func(age int) bool { return age > 0 })(u.Age),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse(users)(t)
|
||||
|
||||
// The traverse should return false because one user is invalid
|
||||
if result {
|
||||
t.Error("Expected traverse to return false with invalid user")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_DataDrivenTesting demonstrates data-driven testing pattern
|
||||
func TestTraverseArray_DataDrivenTesting(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input int
|
||||
Expected int
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Input: 2, Expected: 4},
|
||||
{Input: 3, Expected: 9},
|
||||
{Input: 4, Expected: 16},
|
||||
{Input: 5, Expected: 25},
|
||||
}
|
||||
|
||||
square := func(n int) int { return n * n }
|
||||
|
||||
traverse := TraverseArray(func(tc TestCase) Pair[string, Reader] {
|
||||
return pair.MakePair(
|
||||
fmt.Sprintf("square(%d)=%d", tc.Input, tc.Expected),
|
||||
Equal(tc.Expected)(square(tc.Input)),
|
||||
)
|
||||
})
|
||||
|
||||
result := traverse(testCases)(t)
|
||||
if !result {
|
||||
t.Error("Expected all test cases to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_EmptySequence tests that SequenceSeq2 handles empty sequences correctly
|
||||
func TestSequenceSeq2_EmptySequence(t *testing.T) {
|
||||
emptySeq := func(yield func(string, Reader) bool) {
|
||||
// Empty - yields nothing
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](emptySeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceSeq2 to pass with empty sequence")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_SingleTest tests SequenceSeq2 with a single test
|
||||
func TestSequenceSeq2_SingleTest(t *testing.T) {
|
||||
singleSeq := func(yield func(string, Reader) bool) {
|
||||
yield("test_one", Equal(42)(42))
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](singleSeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceSeq2 to pass with single test")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_MultipleTests tests SequenceSeq2 with multiple passing tests
|
||||
func TestSequenceSeq2_MultipleTests(t *testing.T) {
|
||||
multiSeq := func(yield func(string, Reader) bool) {
|
||||
if !yield("test_addition", Equal(4)(2+2)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_subtraction", Equal(1)(3-2)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_multiplication", Equal(6)(2*3)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](multiSeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceSeq2 to pass with all passing tests")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_WithFailure tests that SequenceSeq2 fails when one test fails
|
||||
func TestSequenceSeq2_WithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
failSeq := func(yield func(string, Reader) bool) {
|
||||
if !yield("test_pass", Equal(4)(2+2)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_fail", Equal(5)(2+2)) { // This will fail
|
||||
return
|
||||
}
|
||||
if !yield("test_pass2", Equal(6)(2*3)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](failSeq)(t)
|
||||
|
||||
// The sequence should return false because one test fails
|
||||
if result {
|
||||
t.Error("Expected sequence to return false when one test fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_GeneratedTests tests SequenceSeq2 with generated test cases
|
||||
func TestSequenceSeq2_GeneratedTests(t *testing.T) {
|
||||
generateTests := func(yield func(string, Reader) bool) {
|
||||
for i := 1; i <= 5; i++ {
|
||||
name := fmt.Sprintf("test_%d", i)
|
||||
assertion := Equal(i * i)(i * i)
|
||||
if !yield(name, assertion) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](generateTests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all generated tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_StringTests tests SequenceSeq2 with string assertions
|
||||
func TestSequenceSeq2_StringTests(t *testing.T) {
|
||||
stringSeq := func(yield func(string, Reader) bool) {
|
||||
if !yield("test_hello", StringNotEmpty("hello")) {
|
||||
return
|
||||
}
|
||||
if !yield("test_world", StringNotEmpty("world")) {
|
||||
return
|
||||
}
|
||||
if !yield("test_length", StringLength[any, any](5)("hello")) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](stringSeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected all string tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_ArrayTests tests SequenceSeq2 with array assertions
|
||||
func TestSequenceSeq2_ArrayTests(t *testing.T) {
|
||||
arr := []int{1, 2, 3, 4, 5}
|
||||
|
||||
arraySeq := func(yield func(string, Reader) bool) {
|
||||
if !yield("test_not_empty", ArrayNotEmpty(arr)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_length", ArrayLength[int](5)(arr)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_contains", ArrayContains(3)(arr)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](arraySeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected all array tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_ComplexAssertions tests SequenceSeq2 with complex combined assertions
|
||||
func TestSequenceSeq2_ComplexAssertions(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
user := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
|
||||
|
||||
userSeq := func(yield func(string, Reader) bool) {
|
||||
if !yield("test_name", StringNotEmpty(user.Name)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_age", That(func(age int) bool { return age > 0 && age < 150 })(user.Age)) {
|
||||
return
|
||||
}
|
||||
if !yield("test_email", That(func(email string) bool {
|
||||
for _, ch := range email {
|
||||
if ch == '@' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})(user.Email)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](userSeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected all user validation tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_EarlyTermination tests that SequenceSeq2 respects early termination
|
||||
func TestSequenceSeq2_EarlyTermination(t *testing.T) {
|
||||
executionCount := 0
|
||||
|
||||
earlyTermSeq := func(yield func(string, Reader) bool) {
|
||||
executionCount++
|
||||
if !yield("test_1", Equal(1)(1)) {
|
||||
return
|
||||
}
|
||||
executionCount++
|
||||
if !yield("test_2", Equal(2)(2)) {
|
||||
return
|
||||
}
|
||||
executionCount++
|
||||
// This should execute even though we don't check the return
|
||||
yield("test_3", Equal(3)(3))
|
||||
executionCount++
|
||||
}
|
||||
|
||||
SequenceSeq2[Reader](earlyTermSeq)(t)
|
||||
|
||||
// All iterations should execute since we're not terminating early
|
||||
if executionCount != 4 {
|
||||
t.Errorf("Expected 4 executions, got %d", executionCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceSeq2_WithMapConversion demonstrates converting a map to Seq2
|
||||
func TestSequenceSeq2_WithMapConversion(t *testing.T) {
|
||||
testMap := map[string]Reader{
|
||||
"test_addition": Equal(4)(2 + 2),
|
||||
"test_multiplication": Equal(6)(2 * 3),
|
||||
"test_subtraction": Equal(1)(3 - 2),
|
||||
}
|
||||
|
||||
// Convert map to Seq2
|
||||
mapSeq := func(yield func(string, Reader) bool) {
|
||||
for name, assertion := range testMap {
|
||||
if !yield(name, assertion) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := SequenceSeq2[Reader](mapSeq)(t)
|
||||
if !result {
|
||||
t.Error("Expected all map-based tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseArray_vs_SequenceSeq2 demonstrates the relationship between the two functions
|
||||
func TestTraverseArray_vs_SequenceSeq2(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Name string
|
||||
Input int
|
||||
Expected int
|
||||
}
|
||||
|
||||
testCases := []TestCase{
|
||||
{Name: "test_1", Input: 2, Expected: 4},
|
||||
{Name: "test_2", Input: 3, Expected: 9},
|
||||
{Name: "test_3", Input: 4, Expected: 16},
|
||||
}
|
||||
|
||||
// Using TraverseArray
|
||||
traverseResult := TraverseArray(func(tc TestCase) Pair[string, Reader] {
|
||||
return pair.MakePair(tc.Name, Equal(tc.Expected)(tc.Input*tc.Input))
|
||||
})(testCases)(t)
|
||||
|
||||
// Using SequenceSeq2
|
||||
seqResult := SequenceSeq2[Reader](func(yield func(string, Reader) bool) {
|
||||
for _, tc := range testCases {
|
||||
if !yield(tc.Name, Equal(tc.Expected)(tc.Input*tc.Input)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
})(t)
|
||||
|
||||
if traverseResult != seqResult {
|
||||
t.Error("Expected TraverseArray and SequenceSeq2 to produce same result")
|
||||
}
|
||||
|
||||
if !traverseResult || !seqResult {
|
||||
t.Error("Expected both approaches to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_EmptyMap tests that TraverseRecord handles empty maps correctly
|
||||
func TestTraverseRecord_EmptyMap(t *testing.T) {
|
||||
traverse := TraverseRecord(func(n int) Reader {
|
||||
return Equal(n)(n)
|
||||
})
|
||||
|
||||
result := traverse(map[string]int{})(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseRecord to pass with empty map")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_SingleEntry tests TraverseRecord with a single map entry
|
||||
func TestTraverseRecord_SingleEntry(t *testing.T) {
|
||||
traverse := TraverseRecord(func(n int) Reader {
|
||||
return Equal(n * 2)(n * 2)
|
||||
})
|
||||
|
||||
result := traverse(map[string]int{"test_5": 5})(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseRecord to pass with single entry")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_MultipleEntries tests TraverseRecord with multiple passing entries
|
||||
func TestTraverseRecord_MultipleEntries(t *testing.T) {
|
||||
traverse := TraverseRecord(func(n int) Reader {
|
||||
return Equal(n * n)(n * n)
|
||||
})
|
||||
|
||||
result := traverse(map[string]int{
|
||||
"square_1": 1,
|
||||
"square_2": 2,
|
||||
"square_3": 3,
|
||||
"square_4": 4,
|
||||
"square_5": 5,
|
||||
})(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected TraverseRecord to pass with all passing entries")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_WithFailure tests that TraverseRecord fails when one entry fails
|
||||
func TestTraverseRecord_WithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
traverse := TraverseRecord(func(n int) Reader {
|
||||
return Equal(10)(n) // Will fail for all except 10
|
||||
})
|
||||
|
||||
result := traverse(map[string]int{
|
||||
"test_1": 1,
|
||||
"test_2": 2,
|
||||
"test_3": 3,
|
||||
})(t)
|
||||
|
||||
// The traverse should return false because entries don't match
|
||||
if result {
|
||||
t.Error("Expected traverse to return false when entries don't match")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_MixedResults tests TraverseRecord with some passing and some failing
|
||||
func TestTraverseRecord_MixedResults(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
traverse := TraverseRecord(func(n int) Reader {
|
||||
return Equal(0)(n % 2) // Only passes for even numbers
|
||||
})
|
||||
|
||||
result := traverse(map[string]int{
|
||||
"even_2": 2,
|
||||
"odd_3": 3,
|
||||
"even_4": 4,
|
||||
})(t)
|
||||
|
||||
// The traverse should return false because some entries fail
|
||||
if result {
|
||||
t.Error("Expected traverse to return false when some entries fail")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_StringData tests TraverseRecord with string data
|
||||
func TestTraverseRecord_StringData(t *testing.T) {
|
||||
words := map[string]string{
|
||||
"greeting": "hello",
|
||||
"world": "world",
|
||||
"test": "test",
|
||||
}
|
||||
|
||||
traverse := TraverseRecord(func(s string) Reader {
|
||||
return AllOf([]Reader{
|
||||
StringNotEmpty(s),
|
||||
That(func(str string) bool { return len(str) > 0 })(s),
|
||||
})
|
||||
})
|
||||
|
||||
result := traverse(words)(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseRecord to pass with valid strings")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_ComplexObjects tests TraverseRecord with complex objects
|
||||
func TestTraverseRecord_ComplexObjects(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
users := map[string]User{
|
||||
"alice": {Name: "Alice", Age: 30},
|
||||
"bob": {Name: "Bob", Age: 25},
|
||||
"charlie": {Name: "Charlie", Age: 35},
|
||||
}
|
||||
|
||||
traverse := TraverseRecord(func(u User) Reader {
|
||||
return AllOf([]Reader{
|
||||
StringNotEmpty(u.Name),
|
||||
That(func(age int) bool { return age > 0 && age < 150 })(u.Age),
|
||||
})
|
||||
})
|
||||
|
||||
result := traverse(users)(t)
|
||||
if !result {
|
||||
t.Error("Expected TraverseRecord to pass with valid users")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_ComplexObjectsWithFailure tests TraverseRecord with invalid complex objects
|
||||
func TestTraverseRecord_ComplexObjectsWithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
users := map[string]User{
|
||||
"alice": {Name: "Alice", Age: 30},
|
||||
"invalid": {Name: "", Age: 25}, // Invalid: empty name
|
||||
"charlie": {Name: "Charlie", Age: 35},
|
||||
}
|
||||
|
||||
traverse := TraverseRecord(func(u User) Reader {
|
||||
return AllOf([]Reader{
|
||||
StringNotEmpty(u.Name),
|
||||
That(func(age int) bool { return age > 0 })(u.Age),
|
||||
})
|
||||
})
|
||||
|
||||
result := traverse(users)(t)
|
||||
|
||||
// The traverse should return false because one user is invalid
|
||||
if result {
|
||||
t.Error("Expected traverse to return false with invalid user")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_ConfigurationTesting demonstrates configuration testing pattern
|
||||
func TestTraverseRecord_ConfigurationTesting(t *testing.T) {
|
||||
configs := map[string]int{
|
||||
"timeout": 30,
|
||||
"maxRetries": 3,
|
||||
"bufferSize": 1024,
|
||||
}
|
||||
|
||||
validatePositive := That(func(n int) bool { return n > 0 })
|
||||
|
||||
traverse := TraverseRecord(validatePositive)
|
||||
result := traverse(configs)(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected all configuration values to be positive")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_APIEndpointTesting demonstrates API endpoint testing pattern
|
||||
func TestTraverseRecord_APIEndpointTesting(t *testing.T) {
|
||||
type Endpoint struct {
|
||||
Path string
|
||||
Method string
|
||||
}
|
||||
|
||||
endpoints := map[string]Endpoint{
|
||||
"get_users": {Path: "/api/users", Method: "GET"},
|
||||
"create_user": {Path: "/api/users", Method: "POST"},
|
||||
"delete_user": {Path: "/api/users/:id", Method: "DELETE"},
|
||||
}
|
||||
|
||||
validateEndpoint := func(e Endpoint) Reader {
|
||||
return AllOf([]Reader{
|
||||
StringNotEmpty(e.Path),
|
||||
That(func(path string) bool {
|
||||
return len(path) > 0 && path[0] == '/'
|
||||
})(e.Path),
|
||||
That(func(method string) bool {
|
||||
return method == "GET" || method == "POST" ||
|
||||
method == "PUT" || method == "DELETE"
|
||||
})(e.Method),
|
||||
})
|
||||
}
|
||||
|
||||
traverse := TraverseRecord(validateEndpoint)
|
||||
result := traverse(endpoints)(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected all endpoints to be valid")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_EmptyMap tests that SequenceRecord handles empty maps correctly
|
||||
func TestSequenceRecord_EmptyMap(t *testing.T) {
|
||||
result := SequenceRecord(map[string]Reader{})(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceRecord to pass with empty map")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_SingleTest tests SequenceRecord with a single test
|
||||
func TestSequenceRecord_SingleTest(t *testing.T) {
|
||||
tests := map[string]Reader{
|
||||
"test_one": Equal(42)(42),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceRecord to pass with single test")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_MultipleTests tests SequenceRecord with multiple passing tests
|
||||
func TestSequenceRecord_MultipleTests(t *testing.T) {
|
||||
tests := map[string]Reader{
|
||||
"test_addition": Equal(4)(2 + 2),
|
||||
"test_subtraction": Equal(1)(3 - 2),
|
||||
"test_multiplication": Equal(6)(2 * 3),
|
||||
"test_division": Equal(2)(6 / 3),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected SequenceRecord to pass with all passing tests")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_WithFailure tests that SequenceRecord fails when one test fails
|
||||
func TestSequenceRecord_WithFailure(t *testing.T) {
|
||||
t.Skip("Skipping test that intentionally creates failing subtests")
|
||||
tests := map[string]Reader{
|
||||
"test_pass": Equal(4)(2 + 2),
|
||||
"test_fail": Equal(5)(2 + 2), // This will fail
|
||||
"test_pass2": Equal(6)(2 * 3),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
|
||||
// The sequence should return false because one test fails
|
||||
if result {
|
||||
t.Error("Expected sequence to return false when one test fails")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_StringTests tests SequenceRecord with string assertions
|
||||
func TestSequenceRecord_StringTests(t *testing.T) {
|
||||
testString := "hello world"
|
||||
|
||||
tests := map[string]Reader{
|
||||
"not_empty": StringNotEmpty(testString),
|
||||
"correct_length": StringLength[any, any](11)(testString),
|
||||
"has_space": That(func(s string) bool {
|
||||
for _, ch := range s {
|
||||
if ch == ' ' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})(testString),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all string tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_ArrayTests tests SequenceRecord with array assertions
|
||||
func TestSequenceRecord_ArrayTests(t *testing.T) {
|
||||
arr := []int{1, 2, 3, 4, 5}
|
||||
|
||||
tests := map[string]Reader{
|
||||
"not_empty": ArrayNotEmpty(arr),
|
||||
"correct_length": ArrayLength[int](5)(arr),
|
||||
"contains_three": ArrayContains(3)(arr),
|
||||
"all_positive": That(func(arr []int) bool {
|
||||
for _, n := range arr {
|
||||
if n <= 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})(arr),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all array tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_ComplexAssertions tests SequenceRecord with complex combined assertions
|
||||
func TestSequenceRecord_ComplexAssertions(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
user := User{Name: "Alice", Age: 30, Email: "alice@example.com"}
|
||||
|
||||
tests := map[string]Reader{
|
||||
"name_not_empty": StringNotEmpty(user.Name),
|
||||
"age_positive": That(func(age int) bool { return age > 0 })(user.Age),
|
||||
"age_reasonable": That(func(age int) bool { return age < 150 })(user.Age),
|
||||
"email_valid": That(func(email string) bool {
|
||||
hasAt := false
|
||||
hasDot := false
|
||||
for _, ch := range email {
|
||||
if ch == '@' {
|
||||
hasAt = true
|
||||
}
|
||||
if ch == '.' {
|
||||
hasDot = true
|
||||
}
|
||||
}
|
||||
return hasAt && hasDot
|
||||
})(user.Email),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all user validation tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_MathOperations demonstrates basic math operations testing
|
||||
func TestSequenceRecord_MathOperations(t *testing.T) {
|
||||
tests := map[string]Reader{
|
||||
"addition": Equal(4)(2 + 2),
|
||||
"subtraction": Equal(1)(3 - 2),
|
||||
"multiplication": Equal(6)(2 * 3),
|
||||
"division": Equal(2)(6 / 3),
|
||||
"modulo": Equal(1)(7 % 3),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all math operations to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_BooleanTests tests SequenceRecord with boolean assertions
|
||||
func TestSequenceRecord_BooleanTests(t *testing.T) {
|
||||
tests := map[string]Reader{
|
||||
"true_is_true": Equal(true)(true),
|
||||
"false_is_false": Equal(false)(false),
|
||||
"not_true": Equal(false)(!true),
|
||||
"not_false": Equal(true)(!false),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all boolean tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_ErrorTests tests SequenceRecord with error assertions
|
||||
func TestSequenceRecord_ErrorTests(t *testing.T) {
|
||||
tests := map[string]Reader{
|
||||
"no_error": NoError(nil),
|
||||
"equal_value": Equal("test")("test"),
|
||||
"not_empty": StringNotEmpty("hello"),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected all error tests to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_vs_SequenceRecord demonstrates the relationship between the two functions
|
||||
func TestTraverseRecord_vs_SequenceRecord(t *testing.T) {
|
||||
type TestCase struct {
|
||||
Input int
|
||||
Expected int
|
||||
}
|
||||
|
||||
testData := map[string]TestCase{
|
||||
"test_1": {Input: 2, Expected: 4},
|
||||
"test_2": {Input: 3, Expected: 9},
|
||||
"test_3": {Input: 4, Expected: 16},
|
||||
}
|
||||
|
||||
// Using TraverseRecord
|
||||
traverseResult := TraverseRecord(func(tc TestCase) Reader {
|
||||
return Equal(tc.Expected)(tc.Input * tc.Input)
|
||||
})(testData)(t)
|
||||
|
||||
// Using SequenceRecord (manually creating the map)
|
||||
tests := make(map[string]Reader)
|
||||
for name, tc := range testData {
|
||||
tests[name] = Equal(tc.Expected)(tc.Input * tc.Input)
|
||||
}
|
||||
seqResult := SequenceRecord(tests)(t)
|
||||
|
||||
if traverseResult != seqResult {
|
||||
t.Error("Expected TraverseRecord and SequenceRecord to produce same result")
|
||||
}
|
||||
|
||||
if !traverseResult || !seqResult {
|
||||
t.Error("Expected both approaches to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_WithAllOf demonstrates combining SequenceRecord with AllOf
|
||||
func TestSequenceRecord_WithAllOf(t *testing.T) {
|
||||
arr := []int{1, 2, 3, 4, 5}
|
||||
|
||||
tests := map[string]Reader{
|
||||
"array_validations": AllOf([]Reader{
|
||||
ArrayNotEmpty(arr),
|
||||
ArrayLength[int](5)(arr),
|
||||
ArrayContains(3)(arr),
|
||||
}),
|
||||
"element_checks": AllOf([]Reader{
|
||||
That(func(a []int) bool { return a[0] == 1 })(arr),
|
||||
That(func(a []int) bool { return a[4] == 5 })(arr),
|
||||
}),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected combined assertions to pass")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTraverseRecord_ConfigValidation demonstrates real-world configuration validation
|
||||
func TestTraverseRecord_ConfigValidation(t *testing.T) {
|
||||
type Config struct {
|
||||
Value int
|
||||
Min int
|
||||
Max int
|
||||
}
|
||||
|
||||
configs := map[string]Config{
|
||||
"timeout": {Value: 30, Min: 1, Max: 60},
|
||||
"maxRetries": {Value: 3, Min: 1, Max: 10},
|
||||
"bufferSize": {Value: 1024, Min: 512, Max: 4096},
|
||||
}
|
||||
|
||||
validateConfig := func(c Config) Reader {
|
||||
return AllOf([]Reader{
|
||||
That(func(val int) bool { return val >= c.Min })(c.Value),
|
||||
That(func(val int) bool { return val <= c.Max })(c.Value),
|
||||
})
|
||||
}
|
||||
|
||||
traverse := TraverseRecord(validateConfig)
|
||||
result := traverse(configs)(t)
|
||||
|
||||
if !result {
|
||||
t.Error("Expected all configurations to be within valid ranges")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSequenceRecord_RealWorldExample demonstrates a realistic use case
|
||||
func TestSequenceRecord_RealWorldExample(t *testing.T) {
|
||||
type Response struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
response := Response{StatusCode: 200, Body: `{"status":"ok"}`}
|
||||
|
||||
tests := map[string]Reader{
|
||||
"status_ok": Equal(200)(response.StatusCode),
|
||||
"body_not_empty": StringNotEmpty(response.Body),
|
||||
"body_is_json": That(func(s string) bool {
|
||||
return len(s) > 0 && s[0] == '{' && s[len(s)-1] == '}'
|
||||
})(response.Body),
|
||||
}
|
||||
|
||||
result := SequenceRecord(tests)(t)
|
||||
if !result {
|
||||
t.Error("Expected response validation to pass")
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,32 @@
|
||||
// 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 assert
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerio"
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/IBM/fp-go/v2/optics/optional"
|
||||
"github.com/IBM/fp-go/v2/optics/prism"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/predicate"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
@@ -13,23 +34,506 @@ import (
|
||||
|
||||
type (
|
||||
// Result represents a computation that may fail with an error.
|
||||
//
|
||||
// This is an alias for [result.Result][T], which encapsulates either a successful
|
||||
// value of type T or an error. It's commonly used in test assertions to represent
|
||||
// operations that might fail, allowing for functional error handling without exceptions.
|
||||
//
|
||||
// A Result can be in one of two states:
|
||||
// - Success: Contains a value of type T
|
||||
// - Failure: Contains an error
|
||||
//
|
||||
// This type is particularly useful in testing scenarios where you need to:
|
||||
// - Test functions that return results
|
||||
// - Chain operations that might fail
|
||||
// - Handle errors functionally
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestResultHandling(t *testing.T) {
|
||||
// successResult := result.Of[int](42)
|
||||
// assert.Success(successResult)(t) // Passes
|
||||
//
|
||||
// failureResult := result.Error[int](errors.New("failed"))
|
||||
// assert.Failure(failureResult)(t) // Passes
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [Success]: Asserts a Result is successful
|
||||
// - [Failure]: Asserts a Result contains an error
|
||||
// - [result.Result]: The underlying Result type
|
||||
Result[T any] = result.Result[T]
|
||||
|
||||
// Reader represents a test assertion that depends on a testing.T context and returns a boolean.
|
||||
// Reader represents a test assertion that depends on a [testing.T] context and returns a boolean.
|
||||
//
|
||||
// This is the core type for all assertions in this package. It's an alias for
|
||||
// [reader.Reader][*testing.T, bool], which is a function that takes a testing context
|
||||
// and produces a boolean result indicating whether the assertion passed.
|
||||
//
|
||||
// The Reader pattern enables:
|
||||
// - Composable assertions that can be combined using functional operators
|
||||
// - Deferred execution - assertions are defined but not executed until applied to a test
|
||||
// - Reusable assertion logic that can be applied to multiple tests
|
||||
// - Functional composition of complex test conditions
|
||||
//
|
||||
// All assertion functions in this package return a Reader, which must be applied
|
||||
// to a *testing.T to execute the assertion:
|
||||
//
|
||||
// assertion := assert.Equal(42)(result) // Creates a Reader
|
||||
// assertion(t) // Executes the assertion
|
||||
//
|
||||
// Readers can be composed using functions like [AllOf], [ApplicativeMonoid], or
|
||||
// functional operators from the reader package.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestReaderComposition(t *testing.T) {
|
||||
// // Create individual assertions
|
||||
// assertion1 := assert.Equal(42)(42)
|
||||
// assertion2 := assert.StringNotEmpty("hello")
|
||||
//
|
||||
// // Combine them
|
||||
// combined := assert.AllOf([]assert.Reader{assertion1, assertion2})
|
||||
//
|
||||
// // Execute the combined assertion
|
||||
// combined(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [Kleisli]: Function that produces a Reader from a value
|
||||
// - [AllOf]: Combines multiple Readers
|
||||
// - [ApplicativeMonoid]: Monoid for combining Readers
|
||||
Reader = reader.Reader[*testing.T, bool]
|
||||
|
||||
// Kleisli represents a function that produces a test assertion Reader from a value of type T.
|
||||
// Kleisli represents a function that produces a test assertion [Reader] from a value of type T.
|
||||
//
|
||||
// This is an alias for [reader.Reader][T, Reader], which is a function that takes a value
|
||||
// of type T and returns a Reader (test assertion). This pattern is fundamental to the
|
||||
// "data last" principle used throughout this package.
|
||||
//
|
||||
// Kleisli functions enable:
|
||||
// - Partial application of assertions - configure the expected value first, apply actual value later
|
||||
// - Reusable assertion builders that can be applied to different values
|
||||
// - Functional composition of assertion pipelines
|
||||
// - Point-free style programming with assertions
|
||||
//
|
||||
// Most assertion functions in this package return a Kleisli, which must be applied
|
||||
// to the actual value being tested, and then to a *testing.T:
|
||||
//
|
||||
// kleisli := assert.Equal(42) // Kleisli[int] - expects an int
|
||||
// reader := kleisli(result) // Reader - assertion ready to execute
|
||||
// reader(t) // Execute the assertion
|
||||
//
|
||||
// Or more concisely:
|
||||
//
|
||||
// assert.Equal(42)(result)(t)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestKleisliPattern(t *testing.T) {
|
||||
// // Create a reusable assertion for positive numbers
|
||||
// isPositive := assert.That(func(n int) bool { return n > 0 })
|
||||
//
|
||||
// // Apply it to different values
|
||||
// isPositive(42)(t) // Passes
|
||||
// isPositive(100)(t) // Passes
|
||||
// // isPositive(-5)(t) would fail
|
||||
//
|
||||
// // Can be used with Local for property testing
|
||||
// type User struct { Age int }
|
||||
// checkAge := assert.Local(func(u User) int { return u.Age })(isPositive)
|
||||
// checkAge(User{Age: 25})(t) // Passes
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [Reader]: The assertion type produced by Kleisli
|
||||
// - [Local]: Focuses a Kleisli on a property of a larger structure
|
||||
Kleisli[T any] = reader.Reader[T, Reader]
|
||||
|
||||
// Predicate represents a function that tests a value of type T and returns a boolean.
|
||||
//
|
||||
// This is an alias for [predicate.Predicate][T], which is a simple function that
|
||||
// takes a value and returns true or false based on some condition. Predicates are
|
||||
// used with the [That] function to create custom assertions.
|
||||
//
|
||||
// Predicates enable:
|
||||
// - Custom validation logic for any type
|
||||
// - Reusable test conditions
|
||||
// - Composition of complex validation rules
|
||||
// - Integration with functional programming patterns
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestPredicates(t *testing.T) {
|
||||
// // Simple predicate
|
||||
// isEven := func(n int) bool { return n%2 == 0 }
|
||||
// assert.That(isEven)(42)(t) // Passes
|
||||
//
|
||||
// // String predicate
|
||||
// hasPrefix := func(s string) bool { return strings.HasPrefix(s, "test") }
|
||||
// assert.That(hasPrefix)("test_file.go")(t) // Passes
|
||||
//
|
||||
// // Complex predicate
|
||||
// isValidEmail := func(s string) bool {
|
||||
// return strings.Contains(s, "@") && strings.Contains(s, ".")
|
||||
// }
|
||||
// assert.That(isValidEmail)("user@example.com")(t) // Passes
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [That]: Creates an assertion from a Predicate
|
||||
// - [predicate.Predicate]: The underlying predicate type
|
||||
Predicate[T any] = predicate.Predicate[T]
|
||||
|
||||
// Lens is a functional reference to a subpart of a data structure.
|
||||
//
|
||||
// This is an alias for [lens.Lens][S, T], which provides a composable way to focus
|
||||
// on a specific field within a larger structure. Lenses enable getting and setting
|
||||
// values in nested data structures in a functional, immutable way.
|
||||
//
|
||||
// In the context of testing, lenses are used with [LocalL] to focus assertions
|
||||
// on specific properties of complex objects without manually extracting those properties.
|
||||
//
|
||||
// A Lens[S, T] focuses on a value of type T within a structure of type S.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestLensUsage(t *testing.T) {
|
||||
// type Address struct { City string }
|
||||
// type User struct { Name string; Address Address }
|
||||
//
|
||||
// // Define lenses (typically generated)
|
||||
// addressLens := lens.Lens[User, Address]{...}
|
||||
// cityLens := lens.Lens[Address, string]{...}
|
||||
//
|
||||
// // Compose lenses to focus on nested field
|
||||
// userCityLens := lens.Compose(addressLens, cityLens)
|
||||
//
|
||||
// // Use with LocalL to assert on nested property
|
||||
// user := User{Name: "Alice", Address: Address{City: "NYC"}}
|
||||
// assert.LocalL(userCityLens)(assert.Equal("NYC"))(user)(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [LocalL]: Uses a Lens to focus assertions on a property
|
||||
// - [lens.Lens]: The underlying lens type
|
||||
// - [Optional]: Similar but for values that may not exist
|
||||
Lens[S, T any] = lens.Lens[S, T]
|
||||
|
||||
// Optional is an optic that focuses on a value that may or may not be present.
|
||||
//
|
||||
// This is an alias for [optional.Optional][S, T], which is similar to a [Lens] but
|
||||
// handles cases where the focused value might not exist. Optionals are useful for
|
||||
// working with nullable fields, optional properties, or values that might be absent.
|
||||
//
|
||||
// In testing, Optionals are used with [FromOptional] to create assertions that
|
||||
// verify whether an optional value is present and, if so, whether it satisfies
|
||||
// certain conditions.
|
||||
//
|
||||
// An Optional[S, T] focuses on an optional value of type T within a structure of type S.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestOptionalUsage(t *testing.T) {
|
||||
// type Config struct { Timeout *int }
|
||||
//
|
||||
// // Define optional (typically generated)
|
||||
// timeoutOptional := optional.Optional[Config, int]{...}
|
||||
//
|
||||
// // Test when value is present
|
||||
// config1 := Config{Timeout: ptr(30)}
|
||||
// assert.FromOptional(timeoutOptional)(
|
||||
// assert.Equal(30),
|
||||
// )(config1)(t) // Passes
|
||||
//
|
||||
// // Test when value is absent
|
||||
// config2 := Config{Timeout: nil}
|
||||
// // FromOptional would fail because value is not present
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [FromOptional]: Creates assertions for optional values
|
||||
// - [optional.Optional]: The underlying optional type
|
||||
// - [Lens]: Similar but for values that always exist
|
||||
Optional[S, T any] = optional.Optional[S, T]
|
||||
|
||||
// Prism is an optic that focuses on a case of a sum type.
|
||||
//
|
||||
// This is an alias for [prism.Prism][S, T], which provides a way to focus on one
|
||||
// variant of a sum type (like Result, Option, Either, etc.). Prisms enable pattern
|
||||
// matching and extraction of values from sum types in a functional way.
|
||||
//
|
||||
// In testing, Prisms are used with [FromPrism] to create assertions that verify
|
||||
// whether a value matches a specific case and, if so, whether the contained value
|
||||
// satisfies certain conditions.
|
||||
//
|
||||
// A Prism[S, T] focuses on a value of type T that may be contained within a sum type S.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestPrismUsage(t *testing.T) {
|
||||
// // Prism for extracting success value from Result
|
||||
// successPrism := prism.Success[int]()
|
||||
//
|
||||
// // Test successful result
|
||||
// successResult := result.Of[int](42)
|
||||
// assert.FromPrism(successPrism)(
|
||||
// assert.Equal(42),
|
||||
// )(successResult)(t) // Passes
|
||||
//
|
||||
// // Prism for extracting error from Result
|
||||
// failurePrism := prism.Failure[int]()
|
||||
//
|
||||
// // Test failed result
|
||||
// failureResult := result.Error[int](errors.New("failed"))
|
||||
// assert.FromPrism(failurePrism)(
|
||||
// assert.Error,
|
||||
// )(failureResult)(t) // Passes
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [FromPrism]: Creates assertions for prism-focused values
|
||||
// - [prism.Prism]: The underlying prism type
|
||||
// - [Optional]: Similar but for optional values
|
||||
Prism[S, T any] = prism.Prism[S, T]
|
||||
|
||||
// ReaderIOResult represents a context-aware, IO-based computation that may fail.
|
||||
//
|
||||
// This is an alias for [readerioresult.ReaderIOResult][A], which combines three
|
||||
// computational effects:
|
||||
// - Reader: Depends on a context (like context.Context)
|
||||
// - IO: Performs side effects (like file I/O, network calls)
|
||||
// - Result: May fail with an error
|
||||
//
|
||||
// In testing, ReaderIOResult is used with [FromReaderIOResult] to convert
|
||||
// context-aware, effectful computations into test assertions. This is useful
|
||||
// when your test assertions need to:
|
||||
// - Access a context for cancellation or deadlines
|
||||
// - Perform IO operations (database queries, API calls, file access)
|
||||
// - Handle potential errors gracefully
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestReaderIOResult(t *testing.T) {
|
||||
// // Create a ReaderIOResult that performs IO and may fail
|
||||
// checkDatabase := func(ctx context.Context) func() result.Result[assert.Reader] {
|
||||
// return func() result.Result[assert.Reader] {
|
||||
// // Perform database check with context
|
||||
// if err := db.PingContext(ctx); err != nil {
|
||||
// return result.Error[assert.Reader](err)
|
||||
// }
|
||||
// return result.Of[assert.Reader](assert.NoError(nil))
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Convert to Reader and execute
|
||||
// assertion := assert.FromReaderIOResult(checkDatabase)
|
||||
// assertion(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [FromReaderIOResult]: Converts ReaderIOResult to Reader
|
||||
// - [ReaderIO]: Similar but without error handling
|
||||
// - [readerioresult.ReaderIOResult]: The underlying type
|
||||
ReaderIOResult[A any] = readerioresult.ReaderIOResult[A]
|
||||
|
||||
// ReaderIO represents a context-aware, IO-based computation.
|
||||
//
|
||||
// This is an alias for [readerio.ReaderIO][A], which combines two computational effects:
|
||||
// - Reader: Depends on a context (like context.Context)
|
||||
// - IO: Performs side effects (like logging, metrics)
|
||||
//
|
||||
// In testing, ReaderIO is used with [FromReaderIO] to convert context-aware,
|
||||
// effectful computations into test assertions. This is useful when your test
|
||||
// assertions need to:
|
||||
// - Access a context for cancellation or deadlines
|
||||
// - Perform IO operations that don't fail (or handle failures internally)
|
||||
// - Integrate with context-aware utilities
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestReaderIO(t *testing.T) {
|
||||
// // Create a ReaderIO that performs IO
|
||||
// logAndCheck := func(ctx context.Context) func() assert.Reader {
|
||||
// return func() assert.Reader {
|
||||
// // Log with context
|
||||
// logger.InfoContext(ctx, "Running test")
|
||||
// // Return assertion
|
||||
// return assert.Equal(42)(computeValue())
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Convert to Reader and execute
|
||||
// assertion := assert.FromReaderIO(logAndCheck)
|
||||
// assertion(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [FromReaderIO]: Converts ReaderIO to Reader
|
||||
// - [ReaderIOResult]: Similar but with error handling
|
||||
// - [readerio.ReaderIO]: The underlying type
|
||||
ReaderIO[A any] = readerio.ReaderIO[A]
|
||||
|
||||
// Seq2 represents a Go iterator that yields key-value pairs.
|
||||
//
|
||||
// This is an alias for [iter.Seq2][K, A], which is Go's standard iterator type
|
||||
// introduced in Go 1.23. It represents a sequence of key-value pairs that can be
|
||||
// iterated over using a for-range loop.
|
||||
//
|
||||
// In testing, Seq2 is used with [SequenceSeq2] to execute a sequence of named
|
||||
// test cases provided as an iterator. This enables:
|
||||
// - Lazy evaluation of test cases
|
||||
// - Memory-efficient testing of large test suites
|
||||
// - Integration with Go's iterator patterns
|
||||
// - Dynamic generation of test cases
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestSeq2Usage(t *testing.T) {
|
||||
// // Create an iterator of test cases
|
||||
// testCases := func(yield func(string, assert.Reader) bool) {
|
||||
// if !yield("test_addition", assert.Equal(4)(2+2)) {
|
||||
// return
|
||||
// }
|
||||
// if !yield("test_multiplication", assert.Equal(6)(2*3)) {
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Execute all test cases
|
||||
// assert.SequenceSeq2[assert.Reader](testCases)(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [SequenceSeq2]: Executes a Seq2 of test cases
|
||||
// - [TraverseArray]: Similar but for arrays
|
||||
// - [iter.Seq2]: The underlying iterator type
|
||||
Seq2[K, A any] = iter.Seq2[K, A]
|
||||
|
||||
// Pair represents a tuple of two values with potentially different types.
|
||||
//
|
||||
// This is an alias for [pair.Pair][L, R], which holds two values: a "head" (or "left")
|
||||
// of type L and a "tail" (or "right") of type R. Pairs are useful for grouping
|
||||
// related values together without defining a custom struct.
|
||||
//
|
||||
// In testing, Pairs are used with [TraverseArray] to associate test names with
|
||||
// their corresponding assertions. Each element in the array is transformed into
|
||||
// a Pair[string, Reader] where the string is the test name and the Reader is
|
||||
// the assertion to execute.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestPairUsage(t *testing.T) {
|
||||
// type TestCase struct {
|
||||
// Input int
|
||||
// Expected int
|
||||
// }
|
||||
//
|
||||
// testCases := []TestCase{
|
||||
// {Input: 2, Expected: 4},
|
||||
// {Input: 3, Expected: 9},
|
||||
// }
|
||||
//
|
||||
// // Transform each test case into a named assertion
|
||||
// traverse := assert.TraverseArray(func(tc TestCase) assert.Pair[string, assert.Reader] {
|
||||
// name := fmt.Sprintf("square(%d)=%d", tc.Input, tc.Expected)
|
||||
// assertion := assert.Equal(tc.Expected)(tc.Input * tc.Input)
|
||||
// return pair.MakePair(name, assertion)
|
||||
// })
|
||||
//
|
||||
// traverse(testCases)(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [TraverseArray]: Uses Pairs to create named test cases
|
||||
// - [pair.Pair]: The underlying pair type
|
||||
// - [pair.MakePair]: Creates a Pair
|
||||
// - [pair.Head]: Extracts the first value
|
||||
// - [pair.Tail]: Extracts the second value
|
||||
Pair[L, R any] = pair.Pair[L, R]
|
||||
|
||||
// Void represents the absence of a meaningful value, similar to unit type in functional programming.
|
||||
//
|
||||
// This is an alias for [function.Void], which is used to represent operations that don't
|
||||
// return a meaningful value but may perform side effects. In the context of testing, Void
|
||||
// is used with IO operations that perform actions without producing a result.
|
||||
//
|
||||
// Void is conceptually similar to:
|
||||
// - Unit type in functional languages (Haskell's (), Scala's Unit)
|
||||
// - void in languages like C/Java (but as a value, not just a type)
|
||||
// - Empty struct{} in Go (but with clearer semantic meaning)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestWithSideEffect(t *testing.T) {
|
||||
// // An IO operation that logs but returns Void
|
||||
// logOperation := func() function.Void {
|
||||
// log.Println("Test executed")
|
||||
// return function.Void{}
|
||||
// }
|
||||
//
|
||||
// // Execute the operation
|
||||
// logOperation()
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [IO]: Wraps side-effecting operations
|
||||
// - [function.Void]: The underlying void type
|
||||
Void = function.Void
|
||||
|
||||
// IO represents a side-effecting computation that produces a value of type A.
|
||||
//
|
||||
// This is an alias for [io.IO][A], which encapsulates operations that perform side effects
|
||||
// (like I/O operations, logging, or state mutations) and return a value. IO is a lazy
|
||||
// computation - it describes an effect but doesn't execute it until explicitly run.
|
||||
//
|
||||
// In testing, IO is used to:
|
||||
// - Defer execution of side effects until needed
|
||||
// - Compose multiple side-effecting operations
|
||||
// - Maintain referential transparency in test setup
|
||||
// - Separate effect description from effect execution
|
||||
//
|
||||
// An IO[A] is essentially a function `func() A` that:
|
||||
// - Encapsulates a side effect
|
||||
// - Returns a value of type A when executed
|
||||
// - Can be composed with other IO operations
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// func TestIOOperation(t *testing.T) {
|
||||
// // Define an IO operation that reads a file
|
||||
// readConfig := func() io.IO[string] {
|
||||
// return func() string {
|
||||
// data, _ := os.ReadFile("config.txt")
|
||||
// return string(data)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // The IO is not executed yet - it's just a description
|
||||
// configIO := readConfig()
|
||||
//
|
||||
// // Execute the IO to get the result
|
||||
// config := configIO()
|
||||
// assert.StringNotEmpty(config)(t)
|
||||
// }
|
||||
//
|
||||
// Example with composition:
|
||||
//
|
||||
// func TestIOComposition(t *testing.T) {
|
||||
// // Chain multiple IO operations
|
||||
// pipeline := io.Map(
|
||||
// func(s string) int { return len(s) },
|
||||
// )(readFileIO)
|
||||
//
|
||||
// // Execute the composed operation
|
||||
// length := pipeline()
|
||||
// assert.That(func(n int) bool { return n > 0 })(length)(t)
|
||||
// }
|
||||
//
|
||||
// See also:
|
||||
// - [ReaderIO]: Combines Reader and IO effects
|
||||
// - [ReaderIOResult]: Adds error handling to ReaderIO
|
||||
// - [io.IO]: The underlying IO type
|
||||
// - [Void]: Represents operations without meaningful return values
|
||||
IO[A any] = io.IO[A]
|
||||
)
|
||||
|
||||
273
v2/cli/README.md
Normal file
273
v2/cli/README.md
Normal file
@@ -0,0 +1,273 @@
|
||||
# CLI Package - Functional Wrappers for urfave/cli/v3
|
||||
|
||||
This package provides functional programming wrappers for the `github.com/urfave/cli/v3` library, enabling Effect-based command actions and type-safe flag handling through Prisms.
|
||||
|
||||
## Features
|
||||
|
||||
### 1. Effect-Based Command Actions
|
||||
|
||||
Transform CLI command actions into composable Effects that follow functional programming principles.
|
||||
|
||||
#### Key Functions
|
||||
|
||||
- **`ToAction(effect CommandEffect) func(context.Context, *C.Command) error`**
|
||||
- Converts a CommandEffect into a standard urfave/cli Action function
|
||||
- Enables Effect-based command handlers to work with cli/v3 framework
|
||||
|
||||
- **`FromAction(action func(context.Context, *C.Command) error) CommandEffect`**
|
||||
- Lifts existing cli/v3 action handlers into the Effect type
|
||||
- Allows gradual migration to functional style
|
||||
|
||||
- **`MakeCommand(name, usage string, flags []C.Flag, effect CommandEffect) *C.Command`**
|
||||
- Creates a new Command with an Effect-based action
|
||||
- Convenience function combining command creation with Effect conversion
|
||||
|
||||
- **`MakeCommandWithSubcommands(...) *C.Command`**
|
||||
- Creates a Command with subcommands and an Effect-based action
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```go
|
||||
import (
|
||||
"context"
|
||||
|
||||
E "github.com/IBM/fp-go/v2/effect"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/IBM/fp-go/v2/cli"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// Define an Effect-based command action
|
||||
processEffect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
input := cmd.String("input")
|
||||
// Process input...
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create command with Effect
|
||||
command := cli.MakeCommand(
|
||||
"process",
|
||||
"Process input files",
|
||||
[]C.Flag{
|
||||
&C.StringFlag{Name: "input", Usage: "Input file path"},
|
||||
},
|
||||
processEffect,
|
||||
)
|
||||
|
||||
// Or convert existing action to Effect
|
||||
existingAction := func(ctx context.Context, cmd *C.Command) error {
|
||||
// Existing logic...
|
||||
return nil
|
||||
}
|
||||
effect := cli.FromAction(existingAction)
|
||||
```
|
||||
|
||||
### 2. Flag Type Prisms
|
||||
|
||||
Type-safe extraction and manipulation of CLI flags using Prisms from the optics package.
|
||||
|
||||
#### Available Prisms
|
||||
|
||||
- `StringFlagPrism()` - Extract `*C.StringFlag` from `C.Flag`
|
||||
- `IntFlagPrism()` - Extract `*C.IntFlag` from `C.Flag`
|
||||
- `BoolFlagPrism()` - Extract `*C.BoolFlag` from `C.Flag`
|
||||
- `Float64FlagPrism()` - Extract `*C.Float64Flag` from `C.Flag`
|
||||
- `DurationFlagPrism()` - Extract `*C.DurationFlag` from `C.Flag`
|
||||
- `TimestampFlagPrism()` - Extract `*C.TimestampFlag` from `C.Flag`
|
||||
- `StringSliceFlagPrism()` - Extract `*C.StringSliceFlag` from `C.Flag`
|
||||
- `IntSliceFlagPrism()` - Extract `*C.IntSliceFlag` from `C.Flag`
|
||||
- `Float64SliceFlagPrism()` - Extract `*C.Float64SliceFlag` from `C.Flag`
|
||||
- `UintFlagPrism()` - Extract `*C.UintFlag` from `C.Flag`
|
||||
- `Uint64FlagPrism()` - Extract `*C.Uint64Flag` from `C.Flag`
|
||||
- `Int64FlagPrism()` - Extract `*C.Int64Flag` from `C.Flag`
|
||||
|
||||
#### Example Usage
|
||||
|
||||
```go
|
||||
import (
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/cli"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// Extract a StringFlag from a Flag interface
|
||||
var flag C.Flag = &C.StringFlag{Name: "input", Value: "default"}
|
||||
prism := cli.StringFlagPrism()
|
||||
|
||||
// Safe extraction returns Option
|
||||
result := prism.GetOption(flag)
|
||||
if O.IsSome(result) {
|
||||
strFlag := O.MonadFold(result,
|
||||
func() *C.StringFlag { return nil },
|
||||
func(f *C.StringFlag) *C.StringFlag { return f },
|
||||
)
|
||||
// Use strFlag...
|
||||
}
|
||||
|
||||
// Type mismatch returns None
|
||||
var intFlag C.Flag = &C.IntFlag{Name: "count"}
|
||||
result = prism.GetOption(intFlag) // Returns None
|
||||
|
||||
// Convert back to Flag
|
||||
strFlag := &C.StringFlag{Name: "output"}
|
||||
flag = prism.ReverseGet(strFlag)
|
||||
```
|
||||
|
||||
## Type Definitions
|
||||
|
||||
### CommandEffect
|
||||
|
||||
```go
|
||||
type CommandEffect = E.Effect[*C.Command, F.Void]
|
||||
```
|
||||
|
||||
A CommandEffect represents a CLI command action as an Effect. It takes a `*C.Command` as context and produces a result wrapped in the Effect monad.
|
||||
|
||||
The Effect structure is:
|
||||
```
|
||||
func(*C.Command) -> func(context.Context) -> func() -> Result[Void]
|
||||
```
|
||||
|
||||
This allows for:
|
||||
- **Composability**: Effects can be composed using standard functional combinators
|
||||
- **Testability**: Pure functions are easier to test
|
||||
- **Error Handling**: Errors are explicitly represented in the Result type
|
||||
- **Context Management**: Context flows naturally through the Effect
|
||||
|
||||
## Benefits
|
||||
|
||||
### 1. Functional Composition
|
||||
|
||||
Effects can be composed using standard functional programming patterns:
|
||||
|
||||
```go
|
||||
import (
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
RRIOE "github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
)
|
||||
|
||||
// Compose multiple effects
|
||||
validateInput := func(cmd *C.Command) E.Thunk[F.Void] { /* ... */ }
|
||||
processData := func(cmd *C.Command) E.Thunk[F.Void] { /* ... */ }
|
||||
saveResults := func(cmd *C.Command) E.Thunk[F.Void] { /* ... */ }
|
||||
|
||||
// Chain effects together
|
||||
pipeline := F.Pipe3(
|
||||
validateInput,
|
||||
RRIOE.Chain(func(F.Void) E.Effect[*C.Command, F.Void] { return processData }),
|
||||
RRIOE.Chain(func(F.Void) E.Effect[*C.Command, F.Void] { return saveResults }),
|
||||
)
|
||||
```
|
||||
|
||||
### 2. Type Safety
|
||||
|
||||
Prisms provide compile-time type safety when working with flags:
|
||||
|
||||
```go
|
||||
// Type-safe flag extraction
|
||||
flags := []C.Flag{
|
||||
&C.StringFlag{Name: "input"},
|
||||
&C.IntFlag{Name: "count"},
|
||||
}
|
||||
|
||||
for _, flag := range flags {
|
||||
// Safe extraction with pattern matching
|
||||
O.MonadFold(
|
||||
cli.StringFlagPrism().GetOption(flag),
|
||||
func() { /* Not a string flag */ },
|
||||
func(sf *C.StringFlag) { /* Handle string flag */ },
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Error Handling
|
||||
|
||||
Errors are explicitly represented in the Result type:
|
||||
|
||||
```go
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
if err := validateInput(cmd); err != nil {
|
||||
return R.Left[F.Void](err) // Explicit error
|
||||
}
|
||||
return R.Of(F.Void{}) // Success
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4. Testability
|
||||
|
||||
Pure functions are easier to test:
|
||||
|
||||
```go
|
||||
func TestCommandEffect(t *testing.T) {
|
||||
cmd := &C.Command{Name: "test"}
|
||||
effect := myCommandEffect(cmd)
|
||||
|
||||
// Execute effect
|
||||
result := effect(context.Background())()
|
||||
|
||||
// Assert on result
|
||||
assert.True(t, R.IsRight(result))
|
||||
}
|
||||
```
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### From Standard Actions to Effects
|
||||
|
||||
**Before:**
|
||||
```go
|
||||
command := &C.Command{
|
||||
Name: "process",
|
||||
Action: func(ctx context.Context, cmd *C.Command) error {
|
||||
input := cmd.String("input")
|
||||
// Process...
|
||||
return nil
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
**After:**
|
||||
```go
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
input := cmd.String("input")
|
||||
// Process...
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
command := cli.MakeCommand("process", "Process files", flags, effect)
|
||||
```
|
||||
|
||||
### Gradual Migration
|
||||
|
||||
You can mix both styles during migration:
|
||||
|
||||
```go
|
||||
// Wrap existing action
|
||||
existingAction := func(ctx context.Context, cmd *C.Command) error {
|
||||
// Legacy code...
|
||||
return nil
|
||||
}
|
||||
|
||||
// Use as Effect
|
||||
effect := cli.FromAction(existingAction)
|
||||
command := cli.MakeCommand("legacy", "Legacy command", flags, effect)
|
||||
```
|
||||
|
||||
## See Also
|
||||
|
||||
- [Effect Package](../effect/) - Core Effect type definitions
|
||||
- [Optics Package](../optics/) - Prism and other optics
|
||||
- [urfave/cli/v3](https://github.com/urfave/cli) - Underlying CLI framework
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
C "github.com/urfave/cli/v2"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func Commands() []*C.Command {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
C "github.com/urfave/cli/v2"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
199
v2/cli/effect.go
Normal file
199
v2/cli/effect.go
Normal file
@@ -0,0 +1,199 @@
|
||||
// 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 cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
E "github.com/IBM/fp-go/v2/effect"
|
||||
ET "github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CommandEffect represents a CLI command action as an Effect.
|
||||
// The Effect takes a *C.Command as context and produces a result.
|
||||
type CommandEffect = E.Effect[*C.Command, F.Void]
|
||||
|
||||
// ToAction converts a CommandEffect into a standard urfave/cli Action function.
|
||||
// This allows Effect-based command handlers to be used with the cli/v3 framework.
|
||||
//
|
||||
// The conversion process:
|
||||
// 1. Takes the Effect which expects a *C.Command context
|
||||
// 2. Executes it with the provided command
|
||||
// 3. Runs the resulting IO operation
|
||||
// 4. Converts the Result to either nil (success) or error (failure)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - effect: The CommandEffect to convert
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A function compatible with C.Command.Action signature
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
// return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
// return func() R.Result[F.Void] {
|
||||
// // Command logic here
|
||||
// return R.Of(F.Void{})
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// action := ToAction(effect)
|
||||
// command := &C.Command{
|
||||
// Name: "example",
|
||||
// Action: action,
|
||||
// }
|
||||
func ToAction(effect CommandEffect) func(context.Context, *C.Command) error {
|
||||
return func(ctx context.Context, cmd *C.Command) error {
|
||||
// Execute the effect: cmd -> ctx -> IO -> Result
|
||||
return F.Pipe3(
|
||||
ctx,
|
||||
effect(cmd),
|
||||
io.Run,
|
||||
// Convert Result[Void] to error
|
||||
ET.Fold(F.Identity[error], F.Constant1[F.Void, error](nil)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// FromAction converts a standard urfave/cli Action function into a CommandEffect.
|
||||
// This allows existing cli/v3 action handlers to be lifted into the Effect type.
|
||||
//
|
||||
// The conversion process:
|
||||
// 1. Takes a standard action function (context.Context, *C.Command) -> error
|
||||
// 2. Wraps it in the Effect structure
|
||||
// 3. Converts the error result to a Result type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - action: The standard cli/v3 action function to convert
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A CommandEffect that wraps the original action
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// standardAction := func(ctx context.Context, cmd *C.Command) error {
|
||||
// // Existing command logic
|
||||
// return nil
|
||||
// }
|
||||
// effect := FromAction(standardAction)
|
||||
// // Now can be composed with other Effects
|
||||
func FromAction(action func(context.Context, *C.Command) error) CommandEffect {
|
||||
return func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
err := action(ctx, cmd)
|
||||
if err != nil {
|
||||
return R.Left[F.Void](err)
|
||||
}
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MakeCommand creates a new Command with an Effect-based action.
|
||||
// This is a convenience function that combines command creation with Effect conversion.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - name: The command name
|
||||
// - usage: The command usage description
|
||||
// - flags: The command flags
|
||||
// - effect: The CommandEffect to use as the action
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A *C.Command configured with the Effect-based action
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// cmd := MakeCommand(
|
||||
// "process",
|
||||
// "Process data files",
|
||||
// []C.Flag{
|
||||
// &C.StringFlag{Name: "input", Usage: "Input file"},
|
||||
// },
|
||||
// func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
// return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
// return func() R.Result[F.Void] {
|
||||
// input := cmd.String("input")
|
||||
// // Process input...
|
||||
// return R.Of(F.Void{})
|
||||
// }
|
||||
// }
|
||||
// },
|
||||
// )
|
||||
func MakeCommand(
|
||||
name string,
|
||||
usage string,
|
||||
flags []C.Flag,
|
||||
effect CommandEffect,
|
||||
) *C.Command {
|
||||
return &C.Command{
|
||||
Name: name,
|
||||
Usage: usage,
|
||||
Flags: flags,
|
||||
Action: ToAction(effect),
|
||||
}
|
||||
}
|
||||
|
||||
// MakeCommandWithSubcommands creates a new Command with subcommands and an Effect-based action.
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - name: The command name
|
||||
// - usage: The command usage description
|
||||
// - flags: The command flags
|
||||
// - commands: The subcommands
|
||||
// - effect: The CommandEffect to use as the action
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A *C.Command configured with subcommands and the Effect-based action
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// cmd := MakeCommandWithSubcommands(
|
||||
// "app",
|
||||
// "Application commands",
|
||||
// []C.Flag{},
|
||||
// []*C.Command{subCmd1, subCmd2},
|
||||
// defaultEffect,
|
||||
// )
|
||||
func MakeCommandWithSubcommands(
|
||||
name string,
|
||||
usage string,
|
||||
flags []C.Flag,
|
||||
commands []*C.Command,
|
||||
effect CommandEffect,
|
||||
) *C.Command {
|
||||
return &C.Command{
|
||||
Name: name,
|
||||
Usage: usage,
|
||||
Flags: flags,
|
||||
Commands: commands,
|
||||
Action: ToAction(effect),
|
||||
}
|
||||
}
|
||||
204
v2/cli/effect_test.go
Normal file
204
v2/cli/effect_test.go
Normal file
@@ -0,0 +1,204 @@
|
||||
// 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 cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
E "github.com/IBM/fp-go/v2/effect"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func TestToAction_Success(t *testing.T) {
|
||||
t.Run("converts successful Effect to action", func(t *testing.T) {
|
||||
// Arrange
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
action := ToAction(effect)
|
||||
cmd := &C.Command{Name: "test"}
|
||||
|
||||
// Act
|
||||
err := action(context.Background(), cmd)
|
||||
|
||||
// Assert
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestToAction_Failure(t *testing.T) {
|
||||
t.Run("converts failed Effect to error", func(t *testing.T) {
|
||||
// Arrange
|
||||
expectedErr := errors.New("test error")
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
return R.Left[F.Void](expectedErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
action := ToAction(effect)
|
||||
cmd := &C.Command{Name: "test"}
|
||||
|
||||
// Act
|
||||
err := action(context.Background(), cmd)
|
||||
|
||||
// Assert
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromAction_Success(t *testing.T) {
|
||||
t.Run("converts successful action to Effect", func(t *testing.T) {
|
||||
// Arrange
|
||||
action := func(ctx context.Context, cmd *C.Command) error {
|
||||
return nil
|
||||
}
|
||||
effect := FromAction(action)
|
||||
cmd := &C.Command{Name: "test"}
|
||||
|
||||
// Act
|
||||
result := effect(cmd)(context.Background())()
|
||||
|
||||
// Assert
|
||||
assert.True(t, R.IsRight(result))
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromAction_Failure(t *testing.T) {
|
||||
t.Run("converts failed action to Effect", func(t *testing.T) {
|
||||
// Arrange
|
||||
expectedErr := errors.New("test error")
|
||||
action := func(ctx context.Context, cmd *C.Command) error {
|
||||
return expectedErr
|
||||
}
|
||||
effect := FromAction(action)
|
||||
cmd := &C.Command{Name: "test"}
|
||||
|
||||
// Act
|
||||
result := effect(cmd)(context.Background())()
|
||||
|
||||
// Assert
|
||||
assert.True(t, R.IsLeft(result))
|
||||
err := R.MonadFold(result, F.Identity[error], func(F.Void) error { return nil })
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMakeCommand(t *testing.T) {
|
||||
t.Run("creates command with Effect-based action", func(t *testing.T) {
|
||||
// Arrange
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Act
|
||||
cmd := MakeCommand(
|
||||
"test",
|
||||
"Test command",
|
||||
[]C.Flag{},
|
||||
effect,
|
||||
)
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, cmd)
|
||||
assert.Equal(t, "test", cmd.Name)
|
||||
assert.Equal(t, "Test command", cmd.Usage)
|
||||
assert.NotNil(t, cmd.Action)
|
||||
|
||||
// Test the action
|
||||
err := cmd.Action(context.Background(), cmd)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMakeCommandWithSubcommands(t *testing.T) {
|
||||
t.Run("creates command with subcommands and Effect-based action", func(t *testing.T) {
|
||||
// Arrange
|
||||
subCmd := &C.Command{Name: "sub"}
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Act
|
||||
cmd := MakeCommandWithSubcommands(
|
||||
"parent",
|
||||
"Parent command",
|
||||
[]C.Flag{},
|
||||
[]*C.Command{subCmd},
|
||||
effect,
|
||||
)
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, cmd)
|
||||
assert.Equal(t, "parent", cmd.Name)
|
||||
assert.Equal(t, "Parent command", cmd.Usage)
|
||||
assert.Len(t, cmd.Commands, 1)
|
||||
assert.Equal(t, "sub", cmd.Commands[0].Name)
|
||||
assert.NotNil(t, cmd.Action)
|
||||
})
|
||||
}
|
||||
|
||||
func TestToAction_Integration(t *testing.T) {
|
||||
t.Run("Effect can access command flags", func(t *testing.T) {
|
||||
// Arrange
|
||||
var capturedValue string
|
||||
effect := func(cmd *C.Command) E.Thunk[F.Void] {
|
||||
return func(ctx context.Context) E.IOResult[F.Void] {
|
||||
return func() R.Result[F.Void] {
|
||||
capturedValue = cmd.String("input")
|
||||
return R.Of(F.Void{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmd := &C.Command{
|
||||
Name: "test",
|
||||
Flags: []C.Flag{
|
||||
&C.StringFlag{
|
||||
Name: "input",
|
||||
Value: "default-value",
|
||||
},
|
||||
},
|
||||
Action: ToAction(effect),
|
||||
}
|
||||
|
||||
// Act
|
||||
err := cmd.Action(context.Background(), cmd)
|
||||
|
||||
// Assert
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "default-value", capturedValue)
|
||||
})
|
||||
}
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
359
v2/cli/flags.go
Normal file
359
v2/cli/flags.go
Normal file
@@ -0,0 +1,359 @@
|
||||
// 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 cli
|
||||
|
||||
import (
|
||||
P "github.com/IBM/fp-go/v2/optics/prism"
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// StringFlagPrism creates a Prism for extracting a StringFlag from a Flag.
|
||||
// This provides a type-safe way to work with string flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// The prism's GetOption attempts to cast a Flag to *C.StringFlag.
|
||||
// If the cast succeeds, it returns Some(*C.StringFlag); if it fails, it returns None.
|
||||
//
|
||||
// The prism's ReverseGet converts a *C.StringFlag back to a Flag.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.StringFlag] for safe StringFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := StringFlagPrism()
|
||||
//
|
||||
// // Extract StringFlag from Flag
|
||||
// var flag C.Flag = &C.StringFlag{Name: "input", Value: "default"}
|
||||
// result := prism.GetOption(flag) // Some(*C.StringFlag{...})
|
||||
//
|
||||
// // Type mismatch returns None
|
||||
// var intFlag C.Flag = &C.IntFlag{Name: "count"}
|
||||
// result = prism.GetOption(intFlag) // None[*C.StringFlag]()
|
||||
//
|
||||
// // Convert back to Flag
|
||||
// strFlag := &C.StringFlag{Name: "output"}
|
||||
// flag = prism.ReverseGet(strFlag)
|
||||
func StringFlagPrism() P.Prism[C.Flag, *C.StringFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.StringFlag] {
|
||||
if sf, ok := flag.(*C.StringFlag); ok {
|
||||
return O.Some(sf)
|
||||
}
|
||||
return O.None[*C.StringFlag]()
|
||||
},
|
||||
func(f *C.StringFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// IntFlagPrism creates a Prism for extracting an IntFlag from a Flag.
|
||||
// This provides a type-safe way to work with integer flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.IntFlag] for safe IntFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := IntFlagPrism()
|
||||
//
|
||||
// // Extract IntFlag from Flag
|
||||
// var flag C.Flag = &C.IntFlag{Name: "count", Value: 10}
|
||||
// result := prism.GetOption(flag) // Some(*C.IntFlag{...})
|
||||
func IntFlagPrism() P.Prism[C.Flag, *C.IntFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.IntFlag] {
|
||||
if f, ok := flag.(*C.IntFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.IntFlag]()
|
||||
},
|
||||
func(f *C.IntFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// BoolFlagPrism creates a Prism for extracting a BoolFlag from a Flag.
|
||||
// This provides a type-safe way to work with boolean flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.BoolFlag] for safe BoolFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := BoolFlagPrism()
|
||||
//
|
||||
// // Extract BoolFlag from Flag
|
||||
// var flag C.Flag = &C.BoolFlag{Name: "verbose", Value: true}
|
||||
// result := prism.GetOption(flag) // Some(*C.BoolFlag{...})
|
||||
func BoolFlagPrism() P.Prism[C.Flag, *C.BoolFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.BoolFlag] {
|
||||
if f, ok := flag.(*C.BoolFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.BoolFlag]()
|
||||
},
|
||||
func(f *C.BoolFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// Float64FlagPrism creates a Prism for extracting a Float64Flag from a Flag.
|
||||
// This provides a type-safe way to work with float64 flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.Float64Flag] for safe Float64Flag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := Float64FlagPrism()
|
||||
//
|
||||
// // Extract Float64Flag from Flag
|
||||
// var flag C.Flag = &C.Float64Flag{Name: "ratio", Value: 0.5}
|
||||
// result := prism.GetOption(flag) // Some(*C.Float64Flag{...})
|
||||
func Float64FlagPrism() P.Prism[C.Flag, *C.Float64Flag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.Float64Flag] {
|
||||
if f, ok := flag.(*C.Float64Flag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.Float64Flag]()
|
||||
},
|
||||
func(f *C.Float64Flag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// DurationFlagPrism creates a Prism for extracting a DurationFlag from a Flag.
|
||||
// This provides a type-safe way to work with duration flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.DurationFlag] for safe DurationFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := DurationFlagPrism()
|
||||
//
|
||||
// // Extract DurationFlag from Flag
|
||||
// var flag C.Flag = &C.DurationFlag{Name: "timeout", Value: 30 * time.Second}
|
||||
// result := prism.GetOption(flag) // Some(*C.DurationFlag{...})
|
||||
func DurationFlagPrism() P.Prism[C.Flag, *C.DurationFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.DurationFlag] {
|
||||
if f, ok := flag.(*C.DurationFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.DurationFlag]()
|
||||
},
|
||||
func(f *C.DurationFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// TimestampFlagPrism creates a Prism for extracting a TimestampFlag from a Flag.
|
||||
// This provides a type-safe way to work with timestamp flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.TimestampFlag] for safe TimestampFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := TimestampFlagPrism()
|
||||
//
|
||||
// // Extract TimestampFlag from Flag
|
||||
// var flag C.Flag = &C.TimestampFlag{Name: "created"}
|
||||
// result := prism.GetOption(flag) // Some(*C.TimestampFlag{...})
|
||||
func TimestampFlagPrism() P.Prism[C.Flag, *C.TimestampFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.TimestampFlag] {
|
||||
if f, ok := flag.(*C.TimestampFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.TimestampFlag]()
|
||||
},
|
||||
func(f *C.TimestampFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// StringSliceFlagPrism creates a Prism for extracting a StringSliceFlag from a Flag.
|
||||
// This provides a type-safe way to work with string slice flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.StringSliceFlag] for safe StringSliceFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := StringSliceFlagPrism()
|
||||
//
|
||||
// // Extract StringSliceFlag from Flag
|
||||
// var flag C.Flag = &C.StringSliceFlag{Name: "tags"}
|
||||
// result := prism.GetOption(flag) // Some(*C.StringSliceFlag{...})
|
||||
func StringSliceFlagPrism() P.Prism[C.Flag, *C.StringSliceFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.StringSliceFlag] {
|
||||
if f, ok := flag.(*C.StringSliceFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.StringSliceFlag]()
|
||||
},
|
||||
func(f *C.StringSliceFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// IntSliceFlagPrism creates a Prism for extracting an IntSliceFlag from a Flag.
|
||||
// This provides a type-safe way to work with int slice flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.IntSliceFlag] for safe IntSliceFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := IntSliceFlagPrism()
|
||||
//
|
||||
// // Extract IntSliceFlag from Flag
|
||||
// var flag C.Flag = &C.IntSliceFlag{Name: "ports"}
|
||||
// result := prism.GetOption(flag) // Some(*C.IntSliceFlag{...})
|
||||
func IntSliceFlagPrism() P.Prism[C.Flag, *C.IntSliceFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.IntSliceFlag] {
|
||||
if f, ok := flag.(*C.IntSliceFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.IntSliceFlag]()
|
||||
},
|
||||
func(f *C.IntSliceFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// Float64SliceFlagPrism creates a Prism for extracting a Float64SliceFlag from a Flag.
|
||||
// This provides a type-safe way to work with float64 slice flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.Float64SliceFlag] for safe Float64SliceFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := Float64SliceFlagPrism()
|
||||
//
|
||||
// // Extract Float64SliceFlag from Flag
|
||||
// var flag C.Flag = &C.Float64SliceFlag{Name: "ratios"}
|
||||
// result := prism.GetOption(flag) // Some(*C.Float64SliceFlag{...})
|
||||
func Float64SliceFlagPrism() P.Prism[C.Flag, *C.Float64SliceFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.Float64SliceFlag] {
|
||||
if f, ok := flag.(*C.Float64SliceFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.Float64SliceFlag]()
|
||||
},
|
||||
func(f *C.Float64SliceFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// UintFlagPrism creates a Prism for extracting a UintFlag from a Flag.
|
||||
// This provides a type-safe way to work with unsigned integer flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.UintFlag] for safe UintFlag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := UintFlagPrism()
|
||||
//
|
||||
// // Extract UintFlag from Flag
|
||||
// var flag C.Flag = &C.UintFlag{Name: "workers", Value: 4}
|
||||
// result := prism.GetOption(flag) // Some(*C.UintFlag{...})
|
||||
func UintFlagPrism() P.Prism[C.Flag, *C.UintFlag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.UintFlag] {
|
||||
if f, ok := flag.(*C.UintFlag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.UintFlag]()
|
||||
},
|
||||
func(f *C.UintFlag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// Uint64FlagPrism creates a Prism for extracting a Uint64Flag from a Flag.
|
||||
// This provides a type-safe way to work with uint64 flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.Uint64Flag] for safe Uint64Flag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := Uint64FlagPrism()
|
||||
//
|
||||
// // Extract Uint64Flag from Flag
|
||||
// var flag C.Flag = &C.Uint64Flag{Name: "size"}
|
||||
// result := prism.GetOption(flag) // Some(*C.Uint64Flag{...})
|
||||
func Uint64FlagPrism() P.Prism[C.Flag, *C.Uint64Flag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.Uint64Flag] {
|
||||
if f, ok := flag.(*C.Uint64Flag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.Uint64Flag]()
|
||||
},
|
||||
func(f *C.Uint64Flag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
|
||||
// Int64FlagPrism creates a Prism for extracting an Int64Flag from a Flag.
|
||||
// This provides a type-safe way to work with int64 flags, handling type
|
||||
// mismatches gracefully through the Option type.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - A Prism[C.Flag, *C.Int64Flag] for safe Int64Flag extraction
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// prism := Int64FlagPrism()
|
||||
//
|
||||
// // Extract Int64Flag from Flag
|
||||
// var flag C.Flag = &C.Int64Flag{Name: "offset"}
|
||||
// result := prism.GetOption(flag) // Some(*C.Int64Flag{...})
|
||||
func Int64FlagPrism() P.Prism[C.Flag, *C.Int64Flag] {
|
||||
return P.MakePrism(
|
||||
func(flag C.Flag) O.Option[*C.Int64Flag] {
|
||||
if f, ok := flag.(*C.Int64Flag); ok {
|
||||
return O.Some(f)
|
||||
}
|
||||
return O.None[*C.Int64Flag]()
|
||||
},
|
||||
func(f *C.Int64Flag) C.Flag { return f },
|
||||
)
|
||||
}
|
||||
287
v2/cli/flags_test.go
Normal file
287
v2/cli/flags_test.go
Normal file
@@ -0,0 +1,287 @@
|
||||
// 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 cli
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
O "github.com/IBM/fp-go/v2/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
C "github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func TestStringFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts StringFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := StringFlagPrism()
|
||||
var flag C.Flag = &C.StringFlag{Name: "input", Value: "test"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.StringFlag { return nil }, func(f *C.StringFlag) *C.StringFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "input", extracted.Name)
|
||||
assert.Equal(t, "test", extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringFlagPrism_Failure(t *testing.T) {
|
||||
t.Run("returns None for non-StringFlag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := StringFlagPrism()
|
||||
var flag C.Flag = &C.IntFlag{Name: "count"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsNone(result))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringFlagPrism_ReverseGet(t *testing.T) {
|
||||
t.Run("converts StringFlag back to Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := StringFlagPrism()
|
||||
strFlag := &C.StringFlag{Name: "output", Value: "result"}
|
||||
|
||||
// Act
|
||||
flag := prism.ReverseGet(strFlag)
|
||||
|
||||
// Assert
|
||||
assert.NotNil(t, flag)
|
||||
assert.IsType(t, &C.StringFlag{}, flag)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts IntFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := IntFlagPrism()
|
||||
var flag C.Flag = &C.IntFlag{Name: "count", Value: 42}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.IntFlag { return nil }, func(f *C.IntFlag) *C.IntFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "count", extracted.Name)
|
||||
assert.Equal(t, 42, extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBoolFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts BoolFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := BoolFlagPrism()
|
||||
var flag C.Flag = &C.BoolFlag{Name: "verbose", Value: true}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.BoolFlag { return nil }, func(f *C.BoolFlag) *C.BoolFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "verbose", extracted.Name)
|
||||
assert.Equal(t, true, extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFloat64FlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts Float64Flag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := Float64FlagPrism()
|
||||
var flag C.Flag = &C.Float64Flag{Name: "ratio", Value: 0.5}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.Float64Flag { return nil }, func(f *C.Float64Flag) *C.Float64Flag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "ratio", extracted.Name)
|
||||
assert.Equal(t, 0.5, extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDurationFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts DurationFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := DurationFlagPrism()
|
||||
duration := 30 * time.Second
|
||||
var flag C.Flag = &C.DurationFlag{Name: "timeout", Value: duration}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.DurationFlag { return nil }, func(f *C.DurationFlag) *C.DurationFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "timeout", extracted.Name)
|
||||
assert.Equal(t, duration, extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTimestampFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts TimestampFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := TimestampFlagPrism()
|
||||
var flag C.Flag = &C.TimestampFlag{Name: "created"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.TimestampFlag { return nil }, func(f *C.TimestampFlag) *C.TimestampFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "created", extracted.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStringSliceFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts StringSliceFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := StringSliceFlagPrism()
|
||||
var flag C.Flag = &C.StringSliceFlag{Name: "tags"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.StringSliceFlag { return nil }, func(f *C.StringSliceFlag) *C.StringSliceFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "tags", extracted.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSliceFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts IntSliceFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := IntSliceFlagPrism()
|
||||
var flag C.Flag = &C.IntSliceFlag{Name: "ports"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.IntSliceFlag { return nil }, func(f *C.IntSliceFlag) *C.IntSliceFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "ports", extracted.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFloat64SliceFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts Float64SliceFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := Float64SliceFlagPrism()
|
||||
var flag C.Flag = &C.Float64SliceFlag{Name: "ratios"}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.Float64SliceFlag { return nil }, func(f *C.Float64SliceFlag) *C.Float64SliceFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "ratios", extracted.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUintFlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts UintFlag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := UintFlagPrism()
|
||||
var flag C.Flag = &C.UintFlag{Name: "workers", Value: 4}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.UintFlag { return nil }, func(f *C.UintFlag) *C.UintFlag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "workers", extracted.Name)
|
||||
assert.Equal(t, uint(4), extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUint64FlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts Uint64Flag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := Uint64FlagPrism()
|
||||
var flag C.Flag = &C.Uint64Flag{Name: "size", Value: 1024}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.Uint64Flag { return nil }, func(f *C.Uint64Flag) *C.Uint64Flag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "size", extracted.Name)
|
||||
assert.Equal(t, uint64(1024), extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInt64FlagPrism_Success(t *testing.T) {
|
||||
t.Run("extracts Int64Flag from Flag", func(t *testing.T) {
|
||||
// Arrange
|
||||
prism := Int64FlagPrism()
|
||||
var flag C.Flag = &C.Int64Flag{Name: "offset", Value: -100}
|
||||
|
||||
// Act
|
||||
result := prism.GetOption(flag)
|
||||
|
||||
// Assert
|
||||
assert.True(t, O.IsSome(result))
|
||||
extracted := O.MonadFold(result, func() *C.Int64Flag { return nil }, func(f *C.Int64Flag) *C.Int64Flag { return f })
|
||||
assert.NotNil(t, extracted)
|
||||
assert.Equal(t, "offset", extracted.Name)
|
||||
assert.Equal(t, int64(-100), extracted.Value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrisms_EdgeCases(t *testing.T) {
|
||||
t.Run("all prisms return None for wrong type", func(t *testing.T) {
|
||||
// Arrange
|
||||
var flag C.Flag = &C.StringFlag{Name: "test"}
|
||||
|
||||
// Act & Assert
|
||||
assert.True(t, O.IsNone(IntFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(BoolFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(Float64FlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(DurationFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(TimestampFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(StringSliceFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(IntSliceFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(Float64SliceFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(UintFlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(Uint64FlagPrism().GetOption(flag)))
|
||||
assert.True(t, O.IsNone(Int64FlagPrism().GetOption(flag)))
|
||||
})
|
||||
}
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
106
v2/cli/lens.go
106
v2/cli/lens.go
@@ -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 (
|
||||
@@ -86,7 +87,9 @@ type templateData struct {
|
||||
}
|
||||
|
||||
const lensStructTemplate = `
|
||||
// {{.Name}}Lenses provides lenses for accessing fields of {{.Name}}
|
||||
// {{.Name}}Lenses provides [lenses] for accessing fields of [{{.Name}}]
|
||||
//
|
||||
// [lenses]: __lens.Lens
|
||||
type {{.Name}}Lenses{{.TypeParams}} struct {
|
||||
// mandatory fields
|
||||
{{- range .Fields}}
|
||||
@@ -100,7 +103,10 @@ type {{.Name}}Lenses{{.TypeParams}} struct {
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
// {{.Name}}RefLenses provides lenses for accessing fields of {{.Name}} via a reference to {{.Name}}
|
||||
// {{.Name}}RefLenses provides [lenses] for accessing fields of [{{.Name}}] via a reference to [{{.Name}}]
|
||||
//
|
||||
//
|
||||
// [lenses]: __lens.Lens
|
||||
type {{.Name}}RefLenses{{.TypeParams}} struct {
|
||||
// mandatory fields
|
||||
{{- range .Fields}}
|
||||
@@ -111,23 +117,32 @@ type {{.Name}}RefLenses{{.TypeParams}} struct {
|
||||
{{- if .IsComparable}}
|
||||
{{.Name}}O __lens_option.LensO[*{{$.Name}}{{$.TypeParamNames}}, {{.TypeName}}]
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
// prisms
|
||||
{{- range .Fields}}
|
||||
{{.Name}}P __prism.Prism[*{{$.Name}}{{$.TypeParamNames}}, {{.TypeName}}]
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
// {{.Name}}Prisms provides prisms for accessing fields of {{.Name}}
|
||||
// {{.Name}}Prisms provides [prisms] for accessing fields of [{{.Name}}]
|
||||
//
|
||||
// [prisms]: __prism.Prism
|
||||
type {{.Name}}Prisms{{.TypeParams}} struct {
|
||||
{{- range .Fields}}
|
||||
{{.Name}} __prism.Prism[{{$.Name}}{{$.TypeParamNames}}, {{.TypeName}}]
|
||||
{{- end}}
|
||||
}
|
||||
|
||||
// {{.Name}}RefPrisms provides [prisms] for accessing fields of [{{.Name}}] via a reference to [{{.Name}}]
|
||||
//
|
||||
// [prisms]: __prism.Prism
|
||||
type {{.Name}}RefPrisms{{.TypeParams}} struct {
|
||||
{{- range .Fields}}
|
||||
{{.Name}} __prism.Prism[*{{$.Name}}{{$.TypeParamNames}}, {{.TypeName}}]
|
||||
{{- end}}
|
||||
}
|
||||
`
|
||||
|
||||
const lensConstructorTemplate = `
|
||||
// Make{{.Name}}Lenses creates a new {{.Name}}Lenses with lenses for all fields
|
||||
// Make{{.Name}}Lenses creates a new [{{.Name}}Lenses] with [lenses] for all fields
|
||||
//
|
||||
// [lenses]:__lens.Lens
|
||||
func Make{{.Name}}Lenses{{.TypeParams}}() {{.Name}}Lenses{{.TypeParamNames}} {
|
||||
// mandatory lenses
|
||||
{{- range .Fields}}
|
||||
@@ -157,7 +172,9 @@ func Make{{.Name}}Lenses{{.TypeParams}}() {{.Name}}Lenses{{.TypeParamNames}} {
|
||||
}
|
||||
}
|
||||
|
||||
// Make{{.Name}}RefLenses creates a new {{.Name}}RefLenses with lenses for all fields
|
||||
// Make{{.Name}}RefLenses creates a new [{{.Name}}RefLenses] with [lenses] for all fields
|
||||
//
|
||||
// [lenses]:__lens.Lens
|
||||
func Make{{.Name}}RefLenses{{.TypeParams}}() {{.Name}}RefLenses{{.TypeParamNames}} {
|
||||
// mandatory lenses
|
||||
{{- range .Fields}}
|
||||
@@ -195,7 +212,9 @@ func Make{{.Name}}RefLenses{{.TypeParams}}() {{.Name}}RefLenses{{.TypeParamNames
|
||||
}
|
||||
}
|
||||
|
||||
// Make{{.Name}}Prisms creates a new {{.Name}}Prisms with prisms for all fields
|
||||
// Make{{.Name}}Prisms creates a new [{{.Name}}Prisms] with [prisms] for all fields
|
||||
//
|
||||
// [prisms]:__prism.Prism
|
||||
func Make{{.Name}}Prisms{{.TypeParams}}() {{.Name}}Prisms{{.TypeParamNames}} {
|
||||
{{- range .Fields}}
|
||||
{{- if .IsComparable}}
|
||||
@@ -235,6 +254,49 @@ func Make{{.Name}}Prisms{{.TypeParams}}() {{.Name}}Prisms{{.TypeParamNames}} {
|
||||
{{- end}}
|
||||
}
|
||||
}
|
||||
|
||||
// Make{{.Name}}RefPrisms creates a new [{{.Name}}RefPrisms] with [prisms] for all fields
|
||||
//
|
||||
// [prisms]:__prism.Prism
|
||||
func Make{{.Name}}RefPrisms{{.TypeParams}}() {{.Name}}RefPrisms{{.TypeParamNames}} {
|
||||
{{- range .Fields}}
|
||||
{{- if .IsComparable}}
|
||||
_fromNonZero{{.Name}} := __option.FromNonZero[{{.TypeName}}]()
|
||||
_prism{{.Name}} := __prism.MakePrismWithName(
|
||||
func(s *{{$.Name}}{{$.TypeParamNames}}) __option.Option[{{.TypeName}}] { return _fromNonZero{{.Name}}(s.{{.Name}}) },
|
||||
func(v {{.TypeName}}) *{{$.Name}}{{$.TypeParamNames}} {
|
||||
{{- if .IsEmbedded}}
|
||||
var result {{$.Name}}{{$.TypeParamNames}}
|
||||
result.{{.Name}} = v
|
||||
return &result
|
||||
{{- else}}
|
||||
return &{{$.Name}}{{$.TypeParamNames}}{ {{.Name}}: v }
|
||||
{{- end}}
|
||||
},
|
||||
"{{$.Name}}{{$.TypeParamNames}}.{{.Name}}",
|
||||
)
|
||||
{{- else}}
|
||||
_prism{{.Name}} := __prism.MakePrismWithName(
|
||||
func(s *{{$.Name}}{{$.TypeParamNames}}) __option.Option[{{.TypeName}}] { return __option.Some(s.{{.Name}}) },
|
||||
func(v {{.TypeName}}) *{{$.Name}}{{$.TypeParamNames}} {
|
||||
{{- if .IsEmbedded}}
|
||||
var result {{$.Name}}{{$.TypeParamNames}}
|
||||
result.{{.Name}} = v
|
||||
return &result
|
||||
{{- else}}
|
||||
return &{{$.Name}}{{$.TypeParamNames}}{ {{.Name}}: v }
|
||||
{{- end}}
|
||||
},
|
||||
"{{$.Name}}{{$.TypeParamNames}}.{{.Name}}",
|
||||
)
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
return {{.Name}}RefPrisms{{.TypeParamNames}} {
|
||||
{{- range .Fields}}
|
||||
{{.Name}}: _prism{{.Name}},
|
||||
{{- end}}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var (
|
||||
@@ -535,9 +597,9 @@ func extractEmbeddedFields(embedType ast.Expr, fileImports map[string]string, fi
|
||||
}
|
||||
|
||||
for _, name := range field.Names {
|
||||
// Only export lenses for exported fields
|
||||
if name.IsExported() {
|
||||
fieldTypeName := getTypeName(field.Type)
|
||||
// Generate lenses for both exported and unexported fields
|
||||
fieldTypeName := getTypeName(field.Type)
|
||||
if true { // Keep the block structure for minimal changes
|
||||
isOptional := false
|
||||
baseType := fieldTypeName
|
||||
|
||||
@@ -697,9 +759,9 @@ func parseFile(filename string) ([]structInfo, string, error) {
|
||||
continue
|
||||
}
|
||||
for _, name := range field.Names {
|
||||
// Only export lenses for exported fields
|
||||
if name.IsExported() {
|
||||
typeName := getTypeName(field.Type)
|
||||
// Generate lenses for both exported and unexported fields
|
||||
typeName := getTypeName(field.Type)
|
||||
if true { // Keep the block structure for minimal changes
|
||||
isOptional := false
|
||||
baseType := typeName
|
||||
isComparable := false
|
||||
@@ -934,12 +996,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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1086,3 +1086,255 @@ type ComparableBox[T comparable] struct {
|
||||
// Verify that MakeLensRef is NOT used (since both fields are comparable)
|
||||
assert.NotContains(t, contentStr, "__lens.MakeLensRefWithName(", "Should not use MakeLensRefWithName when all fields are comparable")
|
||||
}
|
||||
|
||||
func TestParseFileWithUnexportedFields(t *testing.T) {
|
||||
// Create a temporary test file
|
||||
tmpDir := t.TempDir()
|
||||
testFile := filepath.Join(tmpDir, "test.go")
|
||||
|
||||
testCode := `package testpkg
|
||||
|
||||
// fp-go:Lens
|
||||
type Config struct {
|
||||
PublicName string
|
||||
privateName string
|
||||
PublicValue int
|
||||
privateValue *int
|
||||
}
|
||||
`
|
||||
|
||||
err := os.WriteFile(testFile, []byte(testCode), 0o644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Parse the file
|
||||
structs, pkg, err := parseFile(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify results
|
||||
assert.Equal(t, "testpkg", pkg)
|
||||
assert.Len(t, structs, 1)
|
||||
|
||||
// Check Config struct
|
||||
config := structs[0]
|
||||
assert.Equal(t, "Config", config.Name)
|
||||
assert.Len(t, config.Fields, 4, "Should include both exported and unexported fields")
|
||||
|
||||
// Check exported field
|
||||
assert.Equal(t, "PublicName", config.Fields[0].Name)
|
||||
assert.Equal(t, "string", config.Fields[0].TypeName)
|
||||
assert.False(t, config.Fields[0].IsOptional)
|
||||
|
||||
// Check unexported field
|
||||
assert.Equal(t, "privateName", config.Fields[1].Name)
|
||||
assert.Equal(t, "string", config.Fields[1].TypeName)
|
||||
assert.False(t, config.Fields[1].IsOptional)
|
||||
|
||||
// Check exported int field
|
||||
assert.Equal(t, "PublicValue", config.Fields[2].Name)
|
||||
assert.Equal(t, "int", config.Fields[2].TypeName)
|
||||
assert.False(t, config.Fields[2].IsOptional)
|
||||
|
||||
// Check unexported pointer field
|
||||
assert.Equal(t, "privateValue", config.Fields[3].Name)
|
||||
assert.Equal(t, "*int", config.Fields[3].TypeName)
|
||||
assert.True(t, config.Fields[3].IsOptional)
|
||||
}
|
||||
|
||||
func TestGenerateLensHelpersWithUnexportedFields(t *testing.T) {
|
||||
// Create a temporary directory with test files
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
testCode := `package testpkg
|
||||
|
||||
// fp-go:Lens
|
||||
type MixedStruct struct {
|
||||
PublicField string
|
||||
privateField int
|
||||
OptionalPrivate *string
|
||||
}
|
||||
`
|
||||
|
||||
testFile := filepath.Join(tmpDir, "test.go")
|
||||
err := os.WriteFile(testFile, []byte(testCode), 0o644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Generate lens code
|
||||
outputFile := "gen_lens.go"
|
||||
err = generateLensHelpers(tmpDir, outputFile, false, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the generated file exists
|
||||
genPath := filepath.Join(tmpDir, outputFile)
|
||||
_, err = os.Stat(genPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read and verify the generated content
|
||||
content, err := os.ReadFile(genPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
// Check for expected content
|
||||
assert.Contains(t, contentStr, "package testpkg")
|
||||
assert.Contains(t, contentStr, "MixedStructLenses")
|
||||
assert.Contains(t, contentStr, "MakeMixedStructLenses")
|
||||
|
||||
// Check that lenses are generated for all fields (exported and unexported)
|
||||
assert.Contains(t, contentStr, "PublicField __lens.Lens[MixedStruct, string]")
|
||||
assert.Contains(t, contentStr, "privateField __lens.Lens[MixedStruct, int]")
|
||||
assert.Contains(t, contentStr, "OptionalPrivate __lens.Lens[MixedStruct, *string]")
|
||||
|
||||
// Check lens constructors
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct) string { return s.PublicField }")
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct) int { return s.privateField }")
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct) *string { return s.OptionalPrivate }")
|
||||
|
||||
// Check setters
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct, v string) MixedStruct { s.PublicField = v; return s }")
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct, v int) MixedStruct { s.privateField = v; return s }")
|
||||
assert.Contains(t, contentStr, "func(s MixedStruct, v *string) MixedStruct { s.OptionalPrivate = v; return s }")
|
||||
}
|
||||
|
||||
func TestParseFileWithOnlyUnexportedFields(t *testing.T) {
|
||||
// Create a temporary test file
|
||||
tmpDir := t.TempDir()
|
||||
testFile := filepath.Join(tmpDir, "test.go")
|
||||
|
||||
testCode := `package testpkg
|
||||
|
||||
// fp-go:Lens
|
||||
type PrivateConfig struct {
|
||||
name string
|
||||
value int
|
||||
enabled bool
|
||||
}
|
||||
`
|
||||
|
||||
err := os.WriteFile(testFile, []byte(testCode), 0o644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Parse the file
|
||||
structs, pkg, err := parseFile(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify results
|
||||
assert.Equal(t, "testpkg", pkg)
|
||||
assert.Len(t, structs, 1)
|
||||
|
||||
// Check PrivateConfig struct
|
||||
config := structs[0]
|
||||
assert.Equal(t, "PrivateConfig", config.Name)
|
||||
assert.Len(t, config.Fields, 3, "Should include all unexported fields")
|
||||
|
||||
// Check all fields are unexported
|
||||
assert.Equal(t, "name", config.Fields[0].Name)
|
||||
assert.Equal(t, "value", config.Fields[1].Name)
|
||||
assert.Equal(t, "enabled", config.Fields[2].Name)
|
||||
}
|
||||
|
||||
func TestGenerateLensHelpersWithUnexportedEmbeddedFields(t *testing.T) {
|
||||
// Create a temporary directory with test files
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
testCode := `package testpkg
|
||||
|
||||
type BaseConfig struct {
|
||||
publicBase string
|
||||
privateBase int
|
||||
}
|
||||
|
||||
// fp-go:Lens
|
||||
type ExtendedConfig struct {
|
||||
BaseConfig
|
||||
PublicField string
|
||||
privateField bool
|
||||
}
|
||||
`
|
||||
|
||||
testFile := filepath.Join(tmpDir, "test.go")
|
||||
err := os.WriteFile(testFile, []byte(testCode), 0o644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Generate lens code
|
||||
outputFile := "gen_lens.go"
|
||||
err = generateLensHelpers(tmpDir, outputFile, false, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the generated file exists
|
||||
genPath := filepath.Join(tmpDir, outputFile)
|
||||
_, err = os.Stat(genPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Read and verify the generated content
|
||||
content, err := os.ReadFile(genPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
// Check for expected content
|
||||
assert.Contains(t, contentStr, "package testpkg")
|
||||
assert.Contains(t, contentStr, "ExtendedConfigLenses")
|
||||
|
||||
// Check that lenses are generated for embedded unexported fields
|
||||
assert.Contains(t, contentStr, "publicBase __lens.Lens[ExtendedConfig, string]")
|
||||
assert.Contains(t, contentStr, "privateBase __lens.Lens[ExtendedConfig, int]")
|
||||
|
||||
// Check that lenses are generated for direct fields (both exported and unexported)
|
||||
assert.Contains(t, contentStr, "PublicField __lens.Lens[ExtendedConfig, string]")
|
||||
assert.Contains(t, contentStr, "privateField __lens.Lens[ExtendedConfig, bool]")
|
||||
}
|
||||
|
||||
func TestParseFileWithMixedFieldVisibility(t *testing.T) {
|
||||
// Create a temporary test file with various field visibility patterns
|
||||
tmpDir := t.TempDir()
|
||||
testFile := filepath.Join(tmpDir, "test.go")
|
||||
|
||||
testCode := `package testpkg
|
||||
|
||||
// fp-go:Lens
|
||||
type ComplexStruct struct {
|
||||
// Exported fields
|
||||
Name string
|
||||
Age int
|
||||
Email *string
|
||||
|
||||
// Unexported fields
|
||||
password string
|
||||
secretKey []byte
|
||||
internalID *int
|
||||
|
||||
// Mixed with tags
|
||||
PublicWithTag string ` + "`json:\"public,omitempty\"`" + `
|
||||
privateWithTag int ` + "`json:\"private,omitempty\"`" + `
|
||||
}
|
||||
`
|
||||
|
||||
err := os.WriteFile(testFile, []byte(testCode), 0o644)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Parse the file
|
||||
structs, pkg, err := parseFile(testFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify results
|
||||
assert.Equal(t, "testpkg", pkg)
|
||||
assert.Len(t, structs, 1)
|
||||
|
||||
// Check ComplexStruct
|
||||
complex := structs[0]
|
||||
assert.Equal(t, "ComplexStruct", complex.Name)
|
||||
assert.Len(t, complex.Fields, 8, "Should include all fields regardless of visibility")
|
||||
|
||||
// Verify field names and types
|
||||
fieldNames := []string{"Name", "Age", "Email", "password", "secretKey", "internalID", "PublicWithTag", "privateWithTag"}
|
||||
for i, expectedName := range fieldNames {
|
||||
assert.Equal(t, expectedName, complex.Fields[i].Name, "Field %d should be %s", i, expectedName)
|
||||
}
|
||||
|
||||
// Check optional fields
|
||||
assert.False(t, complex.Fields[0].IsOptional, "Name should not be optional")
|
||||
assert.True(t, complex.Fields[2].IsOptional, "Email (pointer) should be optional")
|
||||
assert.True(t, complex.Fields[5].IsOptional, "internalID (pointer) should be optional")
|
||||
assert.True(t, complex.Fields[6].IsOptional, "PublicWithTag (with omitempty) should be optional")
|
||||
assert.True(t, complex.Fields[7].IsOptional, "privateWithTag (with omitempty) should be optional")
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -13,6 +13,37 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package constant provides the Const functor, a phantom type that ignores its second type parameter.
|
||||
//
|
||||
// The Const functor is a fundamental building block in functional programming that wraps a value
|
||||
// of type E while having a phantom type parameter A. This makes it useful for:
|
||||
// - Accumulating values during traversals (e.g., collecting metadata)
|
||||
// - Implementing optics (lenses, prisms) where you need to track information
|
||||
// - Building applicative functors that combine values using a semigroup
|
||||
//
|
||||
// # The Const Functor
|
||||
//
|
||||
// Const[E, A] wraps a value of type E and has a phantom type parameter A that doesn't affect
|
||||
// the runtime value. This allows it to participate in functor and applicative operations while
|
||||
// maintaining the wrapped value unchanged.
|
||||
//
|
||||
// # Key Properties
|
||||
//
|
||||
// - Map operations ignore the function and preserve the wrapped value
|
||||
// - Ap operations combine wrapped values using a semigroup
|
||||
// - The phantom type A allows type-safe composition with other functors
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// // Accumulate string values
|
||||
// c1 := Make[string, int]("hello")
|
||||
// c2 := Make[string, int]("world")
|
||||
//
|
||||
// // Map doesn't change the wrapped value
|
||||
// mapped := Map[string, int, string](strconv.Itoa)(c1) // Still contains "hello"
|
||||
//
|
||||
// // Ap combines values using a semigroup
|
||||
// combined := Ap[string, int, int](S.Monoid)(c1)(c2) // Contains "helloworld"
|
||||
package constant
|
||||
|
||||
import (
|
||||
@@ -21,36 +52,209 @@ import (
|
||||
S "github.com/IBM/fp-go/v2/semigroup"
|
||||
)
|
||||
|
||||
// Const is a functor that wraps a value of type E with a phantom type parameter A.
|
||||
//
|
||||
// The Const functor is useful for accumulating values during traversals or implementing
|
||||
// optics. The type parameter A is phantom - it doesn't affect the runtime value but allows
|
||||
// the type to participate in functor and applicative operations.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped value (the actual data)
|
||||
// - A: The phantom type parameter (not stored, only used for type-level operations)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Create a Const that wraps a string
|
||||
// c := Make[string, int]("metadata")
|
||||
//
|
||||
// // The int type parameter is phantom - no int value is stored
|
||||
// value := Unwrap(c) // "metadata"
|
||||
type Const[E, A any] struct {
|
||||
value E
|
||||
}
|
||||
|
||||
// Make creates a Const value wrapping the given value.
|
||||
//
|
||||
// This is the primary constructor for Const values. The second type parameter A
|
||||
// is phantom and must be specified explicitly when needed for type inference.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the value to wrap
|
||||
// - A: The phantom type parameter
|
||||
//
|
||||
// Parameters:
|
||||
// - e: The value to wrap
|
||||
//
|
||||
// Returns:
|
||||
// - A Const[E, A] wrapping the value
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// c := Make[string, int]("hello")
|
||||
// value := Unwrap(c) // "hello"
|
||||
func Make[E, A any](e E) Const[E, A] {
|
||||
return Const[E, A]{value: e}
|
||||
}
|
||||
|
||||
// Unwrap extracts the wrapped value from a Const.
|
||||
//
|
||||
// This is the inverse of Make, retrieving the actual value stored in the Const.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped value
|
||||
// - A: The phantom type parameter
|
||||
//
|
||||
// Parameters:
|
||||
// - c: The Const to unwrap
|
||||
//
|
||||
// Returns:
|
||||
// - The wrapped value of type E
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// c := Make[string, int]("world")
|
||||
// value := Unwrap(c) // "world"
|
||||
func Unwrap[E, A any](c Const[E, A]) E {
|
||||
return c.value
|
||||
}
|
||||
|
||||
// Of creates a Const containing the monoid's empty value, ignoring the input.
|
||||
//
|
||||
// This implements the Applicative's "pure" operation for Const. It creates a Const
|
||||
// wrapping the monoid's identity element, regardless of the input value.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped value (must have a monoid)
|
||||
// - A: The input type (ignored)
|
||||
//
|
||||
// Parameters:
|
||||
// - m: The monoid providing the empty value
|
||||
//
|
||||
// Returns:
|
||||
// - A function that ignores its input and returns Const[E, A] with the empty value
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import S "github.com/IBM/fp-go/v2/string"
|
||||
//
|
||||
// of := Of[string, int](S.Monoid)
|
||||
// c := of(42) // Const[string, int] containing ""
|
||||
// value := Unwrap(c) // ""
|
||||
func Of[E, A any](m M.Monoid[E]) func(A) Const[E, A] {
|
||||
return F.Constant1[A](Make[E, A](m.Empty()))
|
||||
}
|
||||
|
||||
// MonadMap applies a function to the phantom type parameter without changing the wrapped value.
|
||||
//
|
||||
// This implements the Functor's map operation for Const. Since the type parameter A is phantom,
|
||||
// the function is never actually called - the wrapped value E remains unchanged.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped value
|
||||
// - A: The input phantom type
|
||||
// - B: The output phantom type
|
||||
//
|
||||
// Parameters:
|
||||
// - fa: The Const to map over
|
||||
// - _: The function to apply (ignored)
|
||||
//
|
||||
// Returns:
|
||||
// - A Const[E, B] with the same wrapped value
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// c := Make[string, int]("hello")
|
||||
// mapped := MonadMap(c, func(i int) string { return strconv.Itoa(i) })
|
||||
// // mapped still contains "hello", function was never called
|
||||
func MonadMap[E, A, B any](fa Const[E, A], _ func(A) B) Const[E, B] {
|
||||
return Make[E, B](fa.value)
|
||||
}
|
||||
|
||||
// MonadAp combines two Const values using a semigroup.
|
||||
//
|
||||
// This implements the Applicative's ap operation for Const. It combines the wrapped
|
||||
// values from both Const instances using the provided semigroup, ignoring the function
|
||||
// type in the first argument.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped values (must have a semigroup)
|
||||
// - A: The input phantom type
|
||||
// - B: The output phantom type
|
||||
//
|
||||
// Parameters:
|
||||
// - s: The semigroup for combining wrapped values
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes two Const values and combines their wrapped values
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import S "github.com/IBM/fp-go/v2/string"
|
||||
//
|
||||
// ap := MonadAp[string, int, int](S.Monoid)
|
||||
// c1 := Make[string, func(int) int]("hello")
|
||||
// c2 := Make[string, int]("world")
|
||||
// result := ap(c1, c2) // Const containing "helloworld"
|
||||
func MonadAp[E, A, B any](s S.Semigroup[E]) func(fab Const[E, func(A) B], fa Const[E, A]) Const[E, B] {
|
||||
return func(fab Const[E, func(A) B], fa Const[E, A]) Const[E, B] {
|
||||
return Make[E, B](s.Concat(fab.value, fa.value))
|
||||
}
|
||||
}
|
||||
|
||||
// Map applies a function to the phantom type parameter without changing the wrapped value.
|
||||
//
|
||||
// This is the curried version of MonadMap, providing a more functional programming style.
|
||||
// The function is never actually called since A is a phantom type.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped value
|
||||
// - A: The input phantom type
|
||||
// - B: The output phantom type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: The function to apply (ignored)
|
||||
//
|
||||
// Returns:
|
||||
// - A function that transforms Const[E, A] to Const[E, B]
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import F "github.com/IBM/fp-go/v2/function"
|
||||
//
|
||||
// c := Make[string, int]("data")
|
||||
// mapped := F.Pipe1(c, Map[string, int, string](strconv.Itoa))
|
||||
// // mapped still contains "data"
|
||||
func Map[E, A, B any](f func(A) B) func(fa Const[E, A]) Const[E, B] {
|
||||
return F.Bind2nd(MonadMap[E, A, B], f)
|
||||
}
|
||||
|
||||
// Ap combines Const values using a semigroup in a curried style.
|
||||
//
|
||||
// This is the curried version of MonadAp, providing data-last style for better composition.
|
||||
// It combines the wrapped values from both Const instances using the provided semigroup.
|
||||
//
|
||||
// Type Parameters:
|
||||
// - E: The type of the wrapped values (must have a semigroup)
|
||||
// - A: The input phantom type
|
||||
// - B: The output phantom type
|
||||
//
|
||||
// Parameters:
|
||||
// - s: The semigroup for combining wrapped values
|
||||
//
|
||||
// Returns:
|
||||
// - A curried function for combining Const values
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// F "github.com/IBM/fp-go/v2/function"
|
||||
// S "github.com/IBM/fp-go/v2/string"
|
||||
// )
|
||||
//
|
||||
// c1 := Make[string, int]("hello")
|
||||
// c2 := Make[string, func(int) int]("world")
|
||||
// result := F.Pipe1(c1, Ap[string, int, int](S.Monoid)(c2))
|
||||
// // result contains "helloworld"
|
||||
func Ap[E, A, B any](s S.Semigroup[E]) func(fa Const[E, A]) func(fab Const[E, func(A) B]) Const[E, B] {
|
||||
monadap := MonadAp[E, A, B](s)
|
||||
return func(fa Const[E, A]) func(fab Const[E, func(A) B]) Const[E, B] {
|
||||
|
||||
@@ -16,25 +16,340 @@
|
||||
package constant
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/internal/utils"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
S "github.com/IBM/fp-go/v2/string"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
fa := Make[string, int]("foo")
|
||||
assert.Equal(t, fa, F.Pipe1(fa, Map[string](utils.Double)))
|
||||
// TestMake tests the Make constructor
|
||||
func TestMake(t *testing.T) {
|
||||
t.Run("creates Const with string value", func(t *testing.T) {
|
||||
c := Make[string, int]("hello")
|
||||
assert.Equal(t, "hello", Unwrap(c))
|
||||
})
|
||||
|
||||
t.Run("creates Const with int value", func(t *testing.T) {
|
||||
c := Make[int, string](42)
|
||||
assert.Equal(t, 42, Unwrap(c))
|
||||
})
|
||||
|
||||
t.Run("creates Const with struct value", func(t *testing.T) {
|
||||
type Config struct {
|
||||
Name string
|
||||
Port int
|
||||
}
|
||||
cfg := Config{Name: "server", Port: 8080}
|
||||
c := Make[Config, bool](cfg)
|
||||
assert.Equal(t, cfg, Unwrap(c))
|
||||
})
|
||||
}
|
||||
|
||||
// TestUnwrap tests extracting values from Const
|
||||
func TestUnwrap(t *testing.T) {
|
||||
t.Run("unwraps string value", func(t *testing.T) {
|
||||
c := Make[string, int]("world")
|
||||
value := Unwrap(c)
|
||||
assert.Equal(t, "world", value)
|
||||
})
|
||||
|
||||
t.Run("unwraps empty string", func(t *testing.T) {
|
||||
c := Make[string, int]("")
|
||||
value := Unwrap(c)
|
||||
assert.Equal(t, "", value)
|
||||
})
|
||||
|
||||
t.Run("unwraps zero value", func(t *testing.T) {
|
||||
c := Make[int, string](0)
|
||||
value := Unwrap(c)
|
||||
assert.Equal(t, 0, value)
|
||||
})
|
||||
}
|
||||
|
||||
// TestOf tests the Of function
|
||||
func TestOf(t *testing.T) {
|
||||
assert.Equal(t, Make[string, int](""), Of[string, int](S.Monoid)(1))
|
||||
t.Run("creates Const with monoid empty value", func(t *testing.T) {
|
||||
of := Of[string, int](S.Monoid)
|
||||
c := of(42)
|
||||
assert.Equal(t, "", Unwrap(c))
|
||||
})
|
||||
|
||||
t.Run("ignores input value", func(t *testing.T) {
|
||||
of := Of[string, int](S.Monoid)
|
||||
c1 := of(1)
|
||||
c2 := of(100)
|
||||
assert.Equal(t, Unwrap(c1), Unwrap(c2))
|
||||
})
|
||||
|
||||
t.Run("works with int monoid", func(t *testing.T) {
|
||||
of := Of[int, string](N.MonoidSum[int]())
|
||||
c := of("ignored")
|
||||
assert.Equal(t, 0, Unwrap(c))
|
||||
})
|
||||
}
|
||||
|
||||
func TestAp(t *testing.T) {
|
||||
fab := Make[string, int]("bar")
|
||||
assert.Equal(t, Make[string, int]("foobar"), Ap[string, int, int](S.Monoid)(fab)(Make[string, func(int) int]("foo")))
|
||||
// TestMap tests the Map function
|
||||
func TestMap(t *testing.T) {
|
||||
t.Run("preserves wrapped value", func(t *testing.T) {
|
||||
fa := Make[string, int]("foo")
|
||||
result := F.Pipe1(fa, Map[string](utils.Double))
|
||||
assert.Equal(t, "foo", Unwrap(result))
|
||||
})
|
||||
|
||||
t.Run("changes phantom type", func(t *testing.T) {
|
||||
fa := Make[string, int]("data")
|
||||
fb := Map[string, int, string](strconv.Itoa)(fa)
|
||||
// Value unchanged, but type changed from Const[string, int] to Const[string, string]
|
||||
assert.Equal(t, "data", Unwrap(fb))
|
||||
})
|
||||
|
||||
t.Run("function is never called", func(t *testing.T) {
|
||||
called := false
|
||||
fa := Make[string, int]("test")
|
||||
fb := Map[string, int, string](func(i int) string {
|
||||
called = true
|
||||
return strconv.Itoa(i)
|
||||
})(fa)
|
||||
assert.False(t, called, "Map function should not be called")
|
||||
assert.Equal(t, "test", Unwrap(fb))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonadMap tests the MonadMap function
|
||||
func TestMonadMap(t *testing.T) {
|
||||
t.Run("preserves wrapped value", func(t *testing.T) {
|
||||
fa := Make[string, int]("original")
|
||||
fb := MonadMap(fa, func(i int) string { return strconv.Itoa(i) })
|
||||
assert.Equal(t, "original", Unwrap(fb))
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
fa := Make[int, string](42)
|
||||
fb := MonadMap(fa, func(s string) bool { return len(s) > 0 })
|
||||
assert.Equal(t, 42, Unwrap(fb))
|
||||
})
|
||||
}
|
||||
|
||||
// TestAp tests the Ap function
|
||||
func TestAp(t *testing.T) {
|
||||
t.Run("combines string values", func(t *testing.T) {
|
||||
fab := Make[string, int]("bar")
|
||||
fa := Make[string, func(int) int]("foo")
|
||||
result := Ap[string, int, int](S.Monoid)(fab)(fa)
|
||||
assert.Equal(t, "foobar", Unwrap(result))
|
||||
})
|
||||
|
||||
t.Run("combines int values with sum", func(t *testing.T) {
|
||||
fab := Make[int, string](10)
|
||||
fa := Make[int, func(string) string](5)
|
||||
result := Ap[int, string, string](N.SemigroupSum[int]())(fab)(fa)
|
||||
assert.Equal(t, 15, Unwrap(result))
|
||||
})
|
||||
|
||||
t.Run("combines int values with product", func(t *testing.T) {
|
||||
fab := Make[int, bool](3)
|
||||
fa := Make[int, func(bool) bool](4)
|
||||
result := Ap[int, bool, bool](N.SemigroupProduct[int]())(fab)(fa)
|
||||
assert.Equal(t, 12, Unwrap(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonadAp tests the MonadAp function
|
||||
func TestMonadAp(t *testing.T) {
|
||||
t.Run("combines values using semigroup", func(t *testing.T) {
|
||||
ap := MonadAp[string, int, int](S.Monoid)
|
||||
fab := Make[string, func(int) int]("hello")
|
||||
fa := Make[string, int]("world")
|
||||
result := ap(fab, fa)
|
||||
assert.Equal(t, "helloworld", Unwrap(result))
|
||||
})
|
||||
|
||||
t.Run("works with empty strings", func(t *testing.T) {
|
||||
ap := MonadAp[string, int, int](S.Monoid)
|
||||
fab := Make[string, func(int) int]("")
|
||||
fa := Make[string, int]("test")
|
||||
result := ap(fab, fa)
|
||||
assert.Equal(t, "test", Unwrap(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestMonoid tests the Monoid function
|
||||
func TestMonoid(t *testing.T) {
|
||||
t.Run("always returns constant value", func(t *testing.T) {
|
||||
m := Monoid(42)
|
||||
assert.Equal(t, 42, m.Concat(1, 2))
|
||||
assert.Equal(t, 42, m.Concat(100, 200))
|
||||
assert.Equal(t, 42, m.Empty())
|
||||
})
|
||||
|
||||
t.Run("works with strings", func(t *testing.T) {
|
||||
m := Monoid("constant")
|
||||
assert.Equal(t, "constant", m.Concat("a", "b"))
|
||||
assert.Equal(t, "constant", m.Empty())
|
||||
})
|
||||
|
||||
t.Run("works with structs", func(t *testing.T) {
|
||||
type Point struct{ X, Y int }
|
||||
p := Point{X: 1, Y: 2}
|
||||
m := Monoid(p)
|
||||
assert.Equal(t, p, m.Concat(Point{X: 3, Y: 4}, Point{X: 5, Y: 6}))
|
||||
assert.Equal(t, p, m.Empty())
|
||||
})
|
||||
|
||||
t.Run("satisfies monoid laws", func(t *testing.T) {
|
||||
m := Monoid(10)
|
||||
|
||||
// Left identity: Concat(Empty(), x) = x (both return constant)
|
||||
assert.Equal(t, 10, m.Concat(m.Empty(), 5))
|
||||
|
||||
// Right identity: Concat(x, Empty()) = x (both return constant)
|
||||
assert.Equal(t, 10, m.Concat(5, m.Empty()))
|
||||
|
||||
// Associativity: Concat(Concat(x, y), z) = Concat(x, Concat(y, z))
|
||||
left := m.Concat(m.Concat(1, 2), 3)
|
||||
right := m.Concat(1, m.Concat(2, 3))
|
||||
assert.Equal(t, left, right)
|
||||
assert.Equal(t, 10, left)
|
||||
})
|
||||
}
|
||||
|
||||
// TestConstFunctorLaws tests functor laws for Const
|
||||
func TestConstFunctorLaws(t *testing.T) {
|
||||
t.Run("identity law", func(t *testing.T) {
|
||||
// map id = id
|
||||
fa := Make[string, int]("test")
|
||||
mapped := Map[string, int, int](F.Identity[int])(fa)
|
||||
assert.Equal(t, Unwrap(fa), Unwrap(mapped))
|
||||
})
|
||||
|
||||
t.Run("composition law", func(t *testing.T) {
|
||||
// map (g . f) = map g . map f
|
||||
fa := Make[string, int]("data")
|
||||
f := func(i int) string { return strconv.Itoa(i) }
|
||||
g := func(s string) bool { return len(s) > 0 }
|
||||
|
||||
// map (g . f)
|
||||
composed := Map[string, int, bool](func(i int) bool { return g(f(i)) })(fa)
|
||||
|
||||
// map g . map f
|
||||
intermediate := F.Pipe1(fa, Map[string, int, string](f))
|
||||
chained := Map[string, string, bool](g)(intermediate)
|
||||
|
||||
assert.Equal(t, Unwrap(composed), Unwrap(chained))
|
||||
})
|
||||
}
|
||||
|
||||
// TestConstApplicativeLaws tests applicative laws for Const
|
||||
func TestConstApplicativeLaws(t *testing.T) {
|
||||
t.Run("identity law", func(t *testing.T) {
|
||||
// For Const, ap combines the wrapped values using the semigroup
|
||||
// ap (of id) v combines empty (from of) with v's value
|
||||
v := Make[string, int]("value")
|
||||
ofId := Of[string, func(int) int](S.Monoid)(F.Identity[int])
|
||||
result := Ap[string, int, int](S.Monoid)(v)(ofId)
|
||||
// Result combines "" (from Of) with "value" using string monoid
|
||||
assert.Equal(t, "value", Unwrap(result))
|
||||
})
|
||||
|
||||
t.Run("homomorphism law", func(t *testing.T) {
|
||||
// ap (of f) (of x) = of (f x)
|
||||
f := func(i int) string { return strconv.Itoa(i) }
|
||||
x := 42
|
||||
|
||||
ofF := Of[string, func(int) string](S.Monoid)(f)
|
||||
ofX := Of[string, int](S.Monoid)(x)
|
||||
left := Ap[string, int, string](S.Monoid)(ofX)(ofF)
|
||||
|
||||
right := Of[string, string](S.Monoid)(f(x))
|
||||
|
||||
assert.Equal(t, Unwrap(left), Unwrap(right))
|
||||
})
|
||||
}
|
||||
|
||||
// TestConstEdgeCases tests edge cases
|
||||
func TestConstEdgeCases(t *testing.T) {
|
||||
t.Run("empty string values", func(t *testing.T) {
|
||||
c := Make[string, int]("")
|
||||
assert.Equal(t, "", Unwrap(c))
|
||||
|
||||
mapped := Map[string, int, string](strconv.Itoa)(c)
|
||||
assert.Equal(t, "", Unwrap(mapped))
|
||||
})
|
||||
|
||||
t.Run("zero values", func(t *testing.T) {
|
||||
c := Make[int, string](0)
|
||||
assert.Equal(t, 0, Unwrap(c))
|
||||
})
|
||||
|
||||
t.Run("nil pointer", func(t *testing.T) {
|
||||
var ptr *int
|
||||
c := Make[*int, string](ptr)
|
||||
assert.Nil(t, Unwrap(c))
|
||||
})
|
||||
|
||||
t.Run("multiple map operations", func(t *testing.T) {
|
||||
c := Make[string, int]("original")
|
||||
// Chain multiple map operations
|
||||
step1 := Map[string, int, string](strconv.Itoa)(c)
|
||||
step2 := Map[string, string, bool](func(s string) bool { return len(s) > 0 })(step1)
|
||||
result := Map[string, bool, int](func(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})(step2)
|
||||
assert.Equal(t, "original", Unwrap(result))
|
||||
})
|
||||
}
|
||||
|
||||
// BenchmarkMake benchmarks the Make constructor
|
||||
func BenchmarkMake(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
_ = Make[string, int]("test")
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkUnwrap benchmarks the Unwrap function
|
||||
func BenchmarkUnwrap(b *testing.B) {
|
||||
c := Make[string, int]("test")
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
_ = Unwrap(c)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMap benchmarks the Map function
|
||||
func BenchmarkMap(b *testing.B) {
|
||||
c := Make[string, int]("test")
|
||||
mapFn := Map[string, int, string](strconv.Itoa)
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
_ = mapFn(c)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkAp benchmarks the Ap function
|
||||
func BenchmarkAp(b *testing.B) {
|
||||
fab := Make[string, int]("hello")
|
||||
fa := Make[string, func(int) int]("world")
|
||||
apFn := Ap[string, int, int](S.Monoid)
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
_ = apFn(fab)(fa)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMonoid benchmarks the Monoid function
|
||||
func BenchmarkMonoid(b *testing.B) {
|
||||
m := Monoid(42)
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
_ = m.Concat(1, 2)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
// 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 constant
|
||||
|
||||
import (
|
||||
@@ -5,7 +20,47 @@ import (
|
||||
M "github.com/IBM/fp-go/v2/monoid"
|
||||
)
|
||||
|
||||
// Monoid returns a [M.Monoid] that returns a constant value in all operations
|
||||
// Monoid creates a monoid that always returns a constant value.
|
||||
//
|
||||
// This creates a trivial monoid where both the Concat operation and Empty
|
||||
// always return the same constant value, regardless of inputs. This is useful
|
||||
// for testing, placeholder implementations, or when you need a monoid instance
|
||||
// but the actual combining behavior doesn't matter.
|
||||
//
|
||||
// # Monoid Laws
|
||||
//
|
||||
// The constant monoid satisfies all monoid laws trivially:
|
||||
// - Associativity: Concat(Concat(x, y), z) = Concat(x, Concat(y, z)) - always returns 'a'
|
||||
// - Left Identity: Concat(Empty(), x) = x - both return 'a'
|
||||
// - Right Identity: Concat(x, Empty()) = x - both return 'a'
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The type of the constant value
|
||||
//
|
||||
// Parameters:
|
||||
// - a: The constant value to return in all operations
|
||||
//
|
||||
// Returns:
|
||||
// - A Monoid[A] that always returns the constant value
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Create a monoid that always returns 42
|
||||
// m := Monoid(42)
|
||||
// result := m.Concat(1, 2) // 42
|
||||
// empty := m.Empty() // 42
|
||||
//
|
||||
// // Useful for testing or placeholder implementations
|
||||
// type Config struct {
|
||||
// Timeout int
|
||||
// }
|
||||
// defaultConfig := Monoid(Config{Timeout: 30})
|
||||
// config := defaultConfig.Concat(Config{Timeout: 10}, Config{Timeout: 20})
|
||||
// // config is Config{Timeout: 30}
|
||||
//
|
||||
// See also:
|
||||
// - function.Constant2: The underlying constant function
|
||||
// - M.MakeMonoid: The monoid constructor
|
||||
func Monoid[A any](a A) M.Monoid[A] {
|
||||
return M.MakeMonoid(function.Constant2[A, A](a), a)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
CIOE "github.com/IBM/fp-go/v2/context/ioresult"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// WithContext wraps an existing [ReaderIOResult] and performs a context check for cancellation before delegating.
|
||||
@@ -85,3 +86,7 @@ func WithContextK[A, B any](f Kleisli[A, B]) Kleisli[A, B] {
|
||||
WithContext,
|
||||
)
|
||||
}
|
||||
|
||||
func pairFromContextCancel(newCtx context.Context, cancelFct context.CancelFunc) ContextCancel {
|
||||
return pair.MakePair(cancelFct, newCtx)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,25 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package file provides context-aware file operations that integrate with the ReaderIOResult monad.
|
||||
// It offers safe, composable file I/O operations that respect context cancellation and properly
|
||||
// manage resources using the RAII pattern.
|
||||
//
|
||||
// All operations in this package:
|
||||
// - Respect context.Context for cancellation and timeouts
|
||||
// - Return ReaderIOResult for composable error handling
|
||||
// - Automatically manage resource cleanup
|
||||
// - Are safe to use in concurrent environments
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// // Read a file with automatic resource management
|
||||
// readOp := ReadFile("data.txt")
|
||||
// result := readOp(ctx)()
|
||||
//
|
||||
// // Open and manually manage a file
|
||||
// fileOp := Open("config.json")
|
||||
// fileResult := fileOp(ctx)()
|
||||
package file
|
||||
|
||||
import (
|
||||
@@ -29,32 +48,181 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// Open opens a file for reading within the given context
|
||||
// Open opens a file for reading within the given context.
|
||||
// The operation respects context cancellation and returns a ReaderIOResult
|
||||
// that produces an os.File handle on success.
|
||||
//
|
||||
// The returned file handle should be closed using the Close function when no longer needed,
|
||||
// or managed automatically using WithResource or ReadFile.
|
||||
//
|
||||
// Parameters:
|
||||
// - path: The path to the file to open
|
||||
//
|
||||
// Returns:
|
||||
// - ReaderIOResult[*os.File]: A context-aware computation that opens the file
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// openFile := Open("data.txt")
|
||||
// result := openFile(ctx)()
|
||||
// either.Fold(
|
||||
// result,
|
||||
// func(err error) { log.Printf("Error: %v", err) },
|
||||
// func(f *os.File) {
|
||||
// defer f.Close()
|
||||
// // Use file...
|
||||
// },
|
||||
// )
|
||||
//
|
||||
// See Also:
|
||||
// - ReadFile: For reading entire file contents with automatic resource management
|
||||
// - Close: For closing file handles
|
||||
Open = F.Flow3(
|
||||
IOEF.Open,
|
||||
RIOE.FromIOEither[*os.File],
|
||||
RIOE.WithContext[*os.File],
|
||||
)
|
||||
|
||||
// Remove removes a file by name
|
||||
// Create creates or truncates a file for writing within the given context.
|
||||
// If the file already exists, it is truncated. If it doesn't exist, it is created
|
||||
// with mode 0666 (before umask).
|
||||
//
|
||||
// The operation respects context cancellation and returns a ReaderIOResult
|
||||
// that produces an os.File handle on success.
|
||||
//
|
||||
// The returned file handle should be closed using the Close function when no longer needed,
|
||||
// or managed automatically using WithResource or WriteFile.
|
||||
//
|
||||
// Parameters:
|
||||
// - path: The path to the file to create or truncate
|
||||
//
|
||||
// Returns:
|
||||
// - ReaderIOResult[*os.File]: A context-aware computation that creates the file
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// createFile := Create("output.txt")
|
||||
// result := createFile(ctx)()
|
||||
// either.Fold(
|
||||
// result,
|
||||
// func(err error) { log.Printf("Error: %v", err) },
|
||||
// func(f *os.File) {
|
||||
// defer f.Close()
|
||||
// f.WriteString("Hello, World!")
|
||||
// },
|
||||
// )
|
||||
//
|
||||
// See Also:
|
||||
// - WriteFile: For writing data to a file with automatic resource management
|
||||
// - Open: For opening files for reading
|
||||
// - Close: For closing file handles
|
||||
Create = F.Flow3(
|
||||
IOEF.Create,
|
||||
RIOE.FromIOEither[*os.File],
|
||||
RIOE.WithContext[*os.File],
|
||||
)
|
||||
|
||||
// Remove removes a file by name.
|
||||
// The operation returns the filename on success, allowing for easy composition
|
||||
// with other file operations.
|
||||
//
|
||||
// Parameters:
|
||||
// - name: The path to the file to remove
|
||||
//
|
||||
// Returns:
|
||||
// - ReaderIOResult[string]: A computation that removes the file and returns its name
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// removeOp := Remove("temp.txt")
|
||||
// result := removeOp(ctx)()
|
||||
// either.Fold(
|
||||
// result,
|
||||
// func(err error) { log.Printf("Failed to remove: %v", err) },
|
||||
// func(name string) { log.Printf("Removed: %s", name) },
|
||||
// )
|
||||
//
|
||||
// See Also:
|
||||
// - Open: For opening files
|
||||
// - ReadFile: For reading file contents
|
||||
Remove = F.Flow2(
|
||||
IOEF.Remove,
|
||||
RIOE.FromIOEither[string],
|
||||
)
|
||||
)
|
||||
|
||||
// Close closes an object
|
||||
func Close[C io.Closer](c C) RIOE.ReaderIOResult[struct{}] {
|
||||
// Close closes an io.Closer resource and returns a ReaderIOResult.
|
||||
// This function is generic and works with any type that implements io.Closer,
|
||||
// including os.File, network connections, and other closeable resources.
|
||||
//
|
||||
// The function captures any error that occurs during closing and returns it
|
||||
// as part of the ReaderIOResult. On success, it returns Void (empty struct).
|
||||
//
|
||||
// Type Parameters:
|
||||
// - C: Any type that implements io.Closer
|
||||
//
|
||||
// Parameters:
|
||||
// - c: The resource to close
|
||||
//
|
||||
// Returns:
|
||||
// - ReaderIOResult[Void]: A computation that closes the resource
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// file, _ := os.Open("data.txt")
|
||||
// closeOp := Close(file)
|
||||
// result := closeOp(ctx)()
|
||||
//
|
||||
// Note: This function is typically used with WithResource for automatic resource management
|
||||
// rather than being called directly.
|
||||
//
|
||||
// See Also:
|
||||
// - Open: For opening files
|
||||
// - ReadFile: For reading files with automatic closing
|
||||
func Close[C io.Closer](c C) ReaderIOResult[Void] {
|
||||
return F.Pipe2(
|
||||
c,
|
||||
IOEF.Close[C],
|
||||
RIOE.FromIOEither[struct{}],
|
||||
RIOE.FromIOEither[Void],
|
||||
)
|
||||
}
|
||||
|
||||
// ReadFile reads a file in the scope of a context
|
||||
func ReadFile(path string) RIOE.ReaderIOResult[[]byte] {
|
||||
return RIOE.WithResource[[]byte](Open(path), Close[*os.File])(func(r *os.File) RIOE.ReaderIOResult[[]byte] {
|
||||
// ReadFile reads the entire contents of a file in a context-aware manner.
|
||||
// This function automatically manages the file resource using the RAII pattern,
|
||||
// ensuring the file is properly closed even if an error occurs or the context is canceled.
|
||||
//
|
||||
// The operation:
|
||||
// - Opens the file for reading
|
||||
// - Reads all contents into a byte slice
|
||||
// - Automatically closes the file when done
|
||||
// - Respects context cancellation during the read operation
|
||||
//
|
||||
// Parameters:
|
||||
// - path: The path to the file to read
|
||||
//
|
||||
// Returns:
|
||||
// - ReaderIOResult[[]byte]: A computation that reads the file contents
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// readOp := ReadFile("config.json")
|
||||
// result := readOp(ctx)()
|
||||
// either.Fold(
|
||||
// result,
|
||||
// func(err error) { log.Printf("Read error: %v", err) },
|
||||
// func(data []byte) { log.Printf("Read %d bytes", len(data)) },
|
||||
// )
|
||||
//
|
||||
// The function uses WithResource internally to ensure proper cleanup:
|
||||
//
|
||||
// ReadFile(path) = WithResource(Open(path), Close)(readAllBytes)
|
||||
//
|
||||
// See Also:
|
||||
// - Open: For opening files without automatic reading
|
||||
// - Close: For closing file handles
|
||||
// - WithResource: For custom resource management patterns
|
||||
func ReadFile(path string) ReaderIOResult[[]byte] {
|
||||
return RIOE.WithResource[[]byte](Open(path), Close[*os.File])(func(r *os.File) ReaderIOResult[[]byte] {
|
||||
return func(ctx context.Context) IOE.IOEither[error, []byte] {
|
||||
return func() ET.Either[error, []byte] {
|
||||
return file.ReadAll(ctx, r)
|
||||
@@ -62,3 +230,48 @@ func ReadFile(path string) RIOE.ReaderIOResult[[]byte] {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// WriteFile writes data to a file in a context-aware manner.
|
||||
// This function automatically manages the file resource using the RAII pattern,
|
||||
// ensuring the file is properly closed even if an error occurs or the context is canceled.
|
||||
//
|
||||
// If the file doesn't exist, it is created with mode 0666 (before umask).
|
||||
// If the file already exists, it is truncated before writing.
|
||||
//
|
||||
// The operation:
|
||||
// - Creates or truncates the file for writing
|
||||
// - Writes all data to the file
|
||||
// - Automatically closes the file when done
|
||||
// - Respects context cancellation during the write operation
|
||||
//
|
||||
// Parameters:
|
||||
// - data: The byte slice to write to the file
|
||||
//
|
||||
// Returns:
|
||||
// - Kleisli[string, []byte]: A function that takes a file path and returns a computation
|
||||
// that writes the data and returns the written bytes on success
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// writeOp := WriteFile([]byte("Hello, World!"))
|
||||
// result := writeOp("output.txt")(ctx)()
|
||||
// either.Fold(
|
||||
// result,
|
||||
// func(err error) { log.Printf("Write error: %v", err) },
|
||||
// func(data []byte) { log.Printf("Wrote %d bytes", len(data)) },
|
||||
// )
|
||||
//
|
||||
// The function uses WithResource internally to ensure proper cleanup:
|
||||
//
|
||||
// WriteFile(data) = Create >> WriteAll(data) >> Close
|
||||
//
|
||||
// See Also:
|
||||
// - ReadFile: For reading file contents with automatic resource management
|
||||
// - Create: For creating files without automatic writing
|
||||
// - WriteAll: For writing to an already-open file handle
|
||||
func WriteFile(data []byte) Kleisli[string, []byte] {
|
||||
return F.Flow2(
|
||||
Create,
|
||||
WriteAll[*os.File](data),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -18,11 +18,16 @@ package file
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
R "github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
E "github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
J "github.com/IBM/fp-go/v2/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type RecordType struct {
|
||||
@@ -49,3 +54,267 @@ func ExampleReadFile() {
|
||||
// Output:
|
||||
// Right[string](Carsten)
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("Success - creates new file", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_create.txt")
|
||||
|
||||
createOp := Create(tempFile)
|
||||
result := createOp(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify file was created
|
||||
_, err := os.Stat(tempFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Clean up file handle
|
||||
E.MonadFold(result,
|
||||
func(error) *os.File { return nil },
|
||||
func(f *os.File) *os.File { f.Close(); return f },
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Success - truncates existing file", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_truncate.txt")
|
||||
|
||||
// Create file with initial content
|
||||
err := os.WriteFile(tempFile, []byte("initial content"), 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Create should truncate
|
||||
createOp := Create(tempFile)
|
||||
result := createOp(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Close the file
|
||||
E.MonadFold(result,
|
||||
func(error) *os.File { return nil },
|
||||
func(f *os.File) *os.File { f.Close(); return f },
|
||||
)
|
||||
|
||||
// Verify file was truncated
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, content)
|
||||
})
|
||||
|
||||
t.Run("Failure - invalid path", func(t *testing.T) {
|
||||
// Try to create file in non-existent directory
|
||||
invalidPath := filepath.Join(t.TempDir(), "nonexistent", "test.txt")
|
||||
|
||||
createOp := Create(invalidPath)
|
||||
result := createOp(ctx)()
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("Success - file can be written to", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_write.txt")
|
||||
|
||||
createOp := Create(tempFile)
|
||||
result := createOp(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Write to the file
|
||||
E.MonadFold(result,
|
||||
func(err error) *os.File { t.Fatalf("Unexpected error: %v", err); return nil },
|
||||
func(f *os.File) *os.File {
|
||||
defer f.Close()
|
||||
_, err := f.WriteString("test content")
|
||||
assert.NoError(t, err)
|
||||
return f
|
||||
},
|
||||
)
|
||||
|
||||
// Verify content was written
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test content", string(content))
|
||||
})
|
||||
|
||||
t.Run("Context cancellation", func(t *testing.T) {
|
||||
cancelCtx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // Cancel immediately
|
||||
|
||||
tempFile := filepath.Join(t.TempDir(), "test_cancel.txt")
|
||||
|
||||
createOp := Create(tempFile)
|
||||
result := createOp(cancelCtx)()
|
||||
|
||||
// Note: File creation itself doesn't check context, but this tests the pattern
|
||||
// In practice, context cancellation would affect subsequent operations
|
||||
_ = result
|
||||
})
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("Success - writes data to new file", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_write.txt")
|
||||
testData := []byte("Hello, World!")
|
||||
|
||||
writeOp := WriteFile(testData)
|
||||
result := writeOp(tempFile)(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify returned data
|
||||
E.MonadFold(result,
|
||||
func(err error) []byte { t.Fatalf("Unexpected error: %v", err); return nil },
|
||||
func(data []byte) []byte {
|
||||
assert.Equal(t, testData, data)
|
||||
return data
|
||||
},
|
||||
)
|
||||
|
||||
// Verify file content
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testData, content)
|
||||
})
|
||||
|
||||
t.Run("Success - overwrites existing file", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_overwrite.txt")
|
||||
|
||||
// Write initial content
|
||||
err := os.WriteFile(tempFile, []byte("old content"), 0644)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Overwrite with new content
|
||||
newData := []byte("new content")
|
||||
writeOp := WriteFile(newData)
|
||||
result := writeOp(tempFile)(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify file was overwritten
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, newData, content)
|
||||
})
|
||||
|
||||
t.Run("Success - writes empty data", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_empty.txt")
|
||||
emptyData := []byte{}
|
||||
|
||||
writeOp := WriteFile(emptyData)
|
||||
result := writeOp(tempFile)(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify file is empty
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, content)
|
||||
})
|
||||
|
||||
t.Run("Success - writes large data", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_large.txt")
|
||||
largeData := make([]byte, 1024*1024) // 1MB
|
||||
for i := range largeData {
|
||||
largeData[i] = byte(i % 256)
|
||||
}
|
||||
|
||||
writeOp := WriteFile(largeData)
|
||||
result := writeOp(tempFile)(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify file content
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, largeData, content)
|
||||
})
|
||||
|
||||
t.Run("Failure - invalid path", func(t *testing.T) {
|
||||
invalidPath := filepath.Join(t.TempDir(), "nonexistent", "test.txt")
|
||||
testData := []byte("test")
|
||||
|
||||
writeOp := WriteFile(testData)
|
||||
result := writeOp(invalidPath)(ctx)()
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("Success - writes binary data", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_binary.bin")
|
||||
binaryData := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD}
|
||||
|
||||
writeOp := WriteFile(binaryData)
|
||||
result := writeOp(tempFile)(ctx)()
|
||||
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
// Verify binary content
|
||||
content, err := os.ReadFile(tempFile)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, binaryData, content)
|
||||
})
|
||||
|
||||
t.Run("Integration - write then read", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_roundtrip.txt")
|
||||
testData := []byte("Round trip test data")
|
||||
|
||||
// Write data
|
||||
writeOp := WriteFile(testData)
|
||||
writeResult := writeOp(tempFile)(ctx)()
|
||||
assert.True(t, E.IsRight(writeResult))
|
||||
|
||||
// Read data back
|
||||
readOp := ReadFile(tempFile)
|
||||
readResult := readOp(ctx)()
|
||||
assert.True(t, E.IsRight(readResult))
|
||||
|
||||
// Verify data matches
|
||||
E.MonadFold(readResult,
|
||||
func(err error) []byte { t.Fatalf("Unexpected error: %v", err); return nil },
|
||||
func(data []byte) []byte {
|
||||
assert.Equal(t, testData, data)
|
||||
return data
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Composition with Map", func(t *testing.T) {
|
||||
tempFile := filepath.Join(t.TempDir(), "test_compose.txt")
|
||||
testData := []byte("test data")
|
||||
|
||||
// Write and transform result
|
||||
pipeline := F.Pipe1(
|
||||
WriteFile(testData)(tempFile),
|
||||
R.Map(func(data []byte) int { return len(data) }),
|
||||
)
|
||||
|
||||
result := pipeline(ctx)()
|
||||
assert.True(t, E.IsRight(result))
|
||||
|
||||
E.MonadFold(result,
|
||||
func(err error) int { t.Fatalf("Unexpected error: %v", err); return 0 },
|
||||
func(length int) int {
|
||||
assert.Equal(t, len(testData), length)
|
||||
return length
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
t.Run("Context cancellation during write", func(t *testing.T) {
|
||||
cancelCtx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // Cancel immediately
|
||||
|
||||
tempFile := filepath.Join(t.TempDir(), "test_cancel.txt")
|
||||
testData := []byte("test")
|
||||
|
||||
writeOp := WriteFile(testData)
|
||||
result := writeOp(tempFile)(cancelCtx)()
|
||||
|
||||
// Note: The actual write may complete before cancellation is checked
|
||||
// This test verifies the pattern works with cancelled contexts
|
||||
_ = result
|
||||
})
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
)
|
||||
|
||||
// CreateTemp created a temp file with proper parametrization
|
||||
func CreateTemp(dir, pattern string) RIOE.ReaderIOResult[*os.File] {
|
||||
func CreateTemp(dir, pattern string) ReaderIOResult[*os.File] {
|
||||
return F.Pipe2(
|
||||
IOEF.CreateTemp(dir, pattern),
|
||||
RIOE.FromIOEither[*os.File],
|
||||
@@ -47,6 +47,6 @@ func CreateTemp(dir, pattern string) RIOE.ReaderIOResult[*os.File] {
|
||||
}
|
||||
|
||||
// WithTempFile creates a temporary file, then invokes a callback to create a resource based on the file, then close and remove the temp file
|
||||
func WithTempFile[A any](f func(*os.File) RIOE.ReaderIOResult[A]) RIOE.ReaderIOResult[A] {
|
||||
func WithTempFile[A any](f Kleisli[*os.File, A]) ReaderIOResult[A] {
|
||||
return RIOE.WithResource[A](onCreateTempFile, onReleaseTempFile)(f)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestWithTempFile(t *testing.T) {
|
||||
|
||||
func TestWithTempFileOnClosedFile(t *testing.T) {
|
||||
|
||||
res := WithTempFile(func(f *os.File) RIOE.ReaderIOResult[[]byte] {
|
||||
res := WithTempFile(func(f *os.File) ReaderIOResult[[]byte] {
|
||||
return F.Pipe2(
|
||||
f,
|
||||
onWriteAll[*os.File]([]byte("Carsten")),
|
||||
|
||||
90
v2/context/readerioresult/file/types.go
Normal file
90
v2/context/readerioresult/file/types.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// 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 file
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
type (
|
||||
// ReaderIOResult represents a context-aware computation that performs side effects
|
||||
// and can fail with an error. This is the main type used throughout the file package
|
||||
// for all file operations.
|
||||
//
|
||||
// ReaderIOResult[A] is equivalent to:
|
||||
// func(context.Context) func() Either[error, A]
|
||||
//
|
||||
// The computation:
|
||||
// - Takes a context.Context for cancellation and timeouts
|
||||
// - Performs side effects (IO operations)
|
||||
// - Returns Either an error or a value of type A
|
||||
//
|
||||
// See Also:
|
||||
// - readerioresult.ReaderIOResult: The underlying type definition
|
||||
ReaderIOResult[A any] = readerioresult.ReaderIOResult[A]
|
||||
|
||||
// Void represents the absence of a meaningful value, similar to unit type in other languages.
|
||||
// It is used when a function performs side effects but doesn't return a meaningful result.
|
||||
//
|
||||
// Void is typically used as the success type in operations like Close that perform
|
||||
// an action but don't produce a useful value.
|
||||
//
|
||||
// Example:
|
||||
// Close[*os.File](file) // Returns ReaderIOResult[Void]
|
||||
//
|
||||
// See Also:
|
||||
// - function.Void: The underlying type definition
|
||||
Void = function.Void
|
||||
|
||||
// Kleisli represents a Kleisli arrow for ReaderIOResult.
|
||||
// It is a function that takes a value of type A and returns a ReaderIOResult[B].
|
||||
//
|
||||
// Kleisli arrows are used for monadic composition, allowing you to chain operations
|
||||
// that produce ReaderIOResults. They are particularly useful with Chain and Bind operations.
|
||||
//
|
||||
// Kleisli[A, B] is equivalent to:
|
||||
// func(A) ReaderIOResult[B]
|
||||
//
|
||||
// Example:
|
||||
// // A Kleisli arrow that reads a file given its path
|
||||
// var readFileK Kleisli[string, []byte] = ReadFile
|
||||
//
|
||||
// See Also:
|
||||
// - readerioresult.Kleisli: The underlying type definition
|
||||
// - Operator: For transforming ReaderIOResults
|
||||
Kleisli[A, B any] = readerioresult.Kleisli[A, B]
|
||||
|
||||
// Operator represents a transformation from one ReaderIOResult to another.
|
||||
// This is useful for point-free style composition and building reusable transformations.
|
||||
//
|
||||
// Operator[A, B] is equivalent to:
|
||||
// func(ReaderIOResult[A]) ReaderIOResult[B]
|
||||
//
|
||||
// Operators are used to transform computations without executing them, enabling
|
||||
// powerful composition patterns.
|
||||
//
|
||||
// Example:
|
||||
// // An operator that maps over file contents
|
||||
// var toUpper Operator[[]byte, string] = Map(func(data []byte) string {
|
||||
// return strings.ToUpper(string(data))
|
||||
// })
|
||||
//
|
||||
// See Also:
|
||||
// - readerioresult.Operator: The underlying type definition
|
||||
// - Kleisli: For functions that produce ReaderIOResults
|
||||
Operator[A, B any] = readerioresult.Operator[A, B]
|
||||
)
|
||||
@@ -23,8 +23,8 @@ import (
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
)
|
||||
|
||||
func onWriteAll[W io.Writer](data []byte) func(w W) RIOE.ReaderIOResult[[]byte] {
|
||||
return func(w W) RIOE.ReaderIOResult[[]byte] {
|
||||
func onWriteAll[W io.Writer](data []byte) Kleisli[W, []byte] {
|
||||
return func(w W) ReaderIOResult[[]byte] {
|
||||
return F.Pipe1(
|
||||
RIOE.TryCatch(func(_ context.Context) func() ([]byte, error) {
|
||||
return func() ([]byte, error) {
|
||||
@@ -38,9 +38,9 @@ func onWriteAll[W io.Writer](data []byte) func(w W) RIOE.ReaderIOResult[[]byte]
|
||||
}
|
||||
|
||||
// WriteAll uses a generator function to create a stream, writes data to it and closes it
|
||||
func WriteAll[W io.WriteCloser](data []byte) func(acquire RIOE.ReaderIOResult[W]) RIOE.ReaderIOResult[[]byte] {
|
||||
func WriteAll[W io.WriteCloser](data []byte) Operator[W, []byte] {
|
||||
onWrite := onWriteAll[W](data)
|
||||
return func(onCreate RIOE.ReaderIOResult[W]) RIOE.ReaderIOResult[[]byte] {
|
||||
return func(onCreate ReaderIOResult[W]) ReaderIOResult[[]byte] {
|
||||
return RIOE.WithResource[[]byte](
|
||||
onCreate,
|
||||
Close[W])(
|
||||
@@ -50,7 +50,7 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire RIOE.ReaderIOResult[W]
|
||||
}
|
||||
|
||||
// Write uses a generator function to create a stream, writes data to it and closes it
|
||||
func Write[R any, W io.WriteCloser](acquire RIOE.ReaderIOResult[W]) func(use func(W) RIOE.ReaderIOResult[R]) RIOE.ReaderIOResult[R] {
|
||||
func Write[R any, W io.WriteCloser](acquire ReaderIOResult[W]) Kleisli[Kleisli[W, R], R] {
|
||||
return RIOE.WithResource[R](
|
||||
acquire,
|
||||
Close[W])
|
||||
|
||||
@@ -46,7 +46,7 @@ func TestBuilderWithQuery(t *testing.T) {
|
||||
RIOE.Map(func(r *http.Request) *url.URL {
|
||||
return r.URL
|
||||
}),
|
||||
RIOE.ChainFirstIOK(func(u *url.URL) IO.IO[any] {
|
||||
RIOE.ChainFirstIOK(func(u *url.URL) IO.IO[Void] {
|
||||
return IO.FromImpure(func() {
|
||||
q := u.Query()
|
||||
assert.Equal(t, "10", q.Get("limit"))
|
||||
|
||||
7
v2/context/readerioresult/http/builder/types.go
Normal file
7
v2/context/readerioresult/http/builder/types.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package builder
|
||||
|
||||
import "github.com/IBM/fp-go/v2/function"
|
||||
|
||||
type (
|
||||
Void = function.Void
|
||||
)
|
||||
@@ -158,7 +158,7 @@ func MakeClient(httpClient *http.Client) Client {
|
||||
// request := MakeGetRequest("https://api.example.com/data")
|
||||
// fullResp := ReadFullResponse(client)(request)
|
||||
// result := fullResp(t.Context())()
|
||||
func ReadFullResponse(client Client) RIOE.Kleisli[Requester, H.FullResponse] {
|
||||
func ReadFullResponse(client Client) RIOE.Operator[*http.Request, H.FullResponse] {
|
||||
return func(req Requester) RIOE.ReaderIOResult[H.FullResponse] {
|
||||
return F.Flow3(
|
||||
client.Do(req),
|
||||
@@ -195,7 +195,7 @@ func ReadFullResponse(client Client) RIOE.Kleisli[Requester, H.FullResponse] {
|
||||
// request := MakeGetRequest("https://api.example.com/data")
|
||||
// readBytes := ReadAll(client)
|
||||
// result := readBytes(request)(t.Context())()
|
||||
func ReadAll(client Client) RIOE.Kleisli[Requester, []byte] {
|
||||
func ReadAll(client Client) RIOE.Operator[*http.Request, []byte] {
|
||||
return F.Flow2(
|
||||
ReadFullResponse(client),
|
||||
RIOE.Map(H.Body),
|
||||
@@ -219,7 +219,7 @@ func ReadAll(client Client) RIOE.Kleisli[Requester, []byte] {
|
||||
// request := MakeGetRequest("https://api.example.com/text")
|
||||
// readText := ReadText(client)
|
||||
// result := readText(request)(t.Context())()
|
||||
func ReadText(client Client) RIOE.Kleisli[Requester, string] {
|
||||
func ReadText(client Client) RIOE.Operator[*http.Request, string] {
|
||||
return F.Flow2(
|
||||
ReadAll(client),
|
||||
RIOE.Map(B.ToString),
|
||||
@@ -231,7 +231,7 @@ func ReadText(client Client) RIOE.Kleisli[Requester, string] {
|
||||
// Deprecated: Use [ReadJSON] instead. This function is kept for backward compatibility
|
||||
// but will be removed in a future version. The capitalized version follows Go naming
|
||||
// conventions for acronyms.
|
||||
func ReadJson[A any](client Client) RIOE.Kleisli[Requester, A] {
|
||||
func ReadJson[A any](client Client) RIOE.Operator[*http.Request, A] {
|
||||
return ReadJSON[A](client)
|
||||
}
|
||||
|
||||
@@ -242,7 +242,7 @@ func ReadJson[A any](client Client) RIOE.Kleisli[Requester, A] {
|
||||
// 3. Reads the response body as bytes
|
||||
//
|
||||
// This function is used internally by ReadJSON to ensure proper JSON response handling.
|
||||
func readJSON(client Client) RIOE.Kleisli[Requester, []byte] {
|
||||
func readJSON(client Client) RIOE.Operator[*http.Request, []byte] {
|
||||
return F.Flow3(
|
||||
ReadFullResponse(client),
|
||||
RIOE.ChainFirstEitherK(F.Flow2(
|
||||
@@ -278,7 +278,7 @@ func readJSON(client Client) RIOE.Kleisli[Requester, []byte] {
|
||||
// request := MakeGetRequest("https://api.example.com/user/1")
|
||||
// readUser := ReadJSON[User](client)
|
||||
// result := readUser(request)(t.Context())()
|
||||
func ReadJSON[A any](client Client) RIOE.Kleisli[Requester, A] {
|
||||
func ReadJSON[A any](client Client) RIOE.Operator[*http.Request, A] {
|
||||
return F.Flow2(
|
||||
readJSON(client),
|
||||
RIOE.ChainEitherK(J.Unmarshal[A]),
|
||||
|
||||
@@ -65,7 +65,7 @@ var (
|
||||
// This function assumes the context contains logging information; it will panic if not present.
|
||||
getLoggingContext = F.Flow3(
|
||||
loggingContextValue,
|
||||
option.ToType[loggingContext],
|
||||
option.InstanceOf[loggingContext],
|
||||
option.GetOrElse(getDefaultLoggingContext),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -19,6 +19,10 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// Promap is the profunctor map operation that transforms both the input and output of a context-based ReaderIOResult.
|
||||
@@ -45,7 +49,7 @@ import (
|
||||
// - An Operator that takes a ReaderIOResult[A] and returns a ReaderIOResult[B]
|
||||
//
|
||||
//go:inline
|
||||
func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFunc), g func(A) B) Operator[A, B] {
|
||||
func Promap[A, B any](f pair.Kleisli[context.CancelFunc, context.Context, context.Context], g func(A) B) Operator[A, B] {
|
||||
return function.Flow2(
|
||||
Local[A](f),
|
||||
Map(g),
|
||||
@@ -70,6 +74,139 @@ func Promap[A, B any](f func(context.Context) (context.Context, context.CancelFu
|
||||
// - An Operator that takes a ReaderIOResult[A] and returns a ReaderIOResult[A]
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
func Contramap[A any](f pair.Kleisli[context.CancelFunc, context.Context, context.Context]) Operator[A, A] {
|
||||
return Local[A](f)
|
||||
}
|
||||
|
||||
func ContramapIOK[A any](f io.Kleisli[context.Context, ContextCancel]) Operator[A, A] {
|
||||
return LocalIOK[A](f)
|
||||
}
|
||||
|
||||
// LocalIOK transforms the context using an IO-based function before passing it to a ReaderIOResult.
|
||||
// This is similar to Local but the context transformation itself is wrapped in an IO effect.
|
||||
//
|
||||
// The function f takes a context and returns an IO effect that produces a ContextCancel
|
||||
// (a pair of CancelFunc and the new Context). This allows the context transformation to
|
||||
// perform side effects.
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// This function is useful for sharing information via the Context that is computed through
|
||||
// side effects that cannot fail, such as:
|
||||
// - Generating unique request IDs or trace IDs
|
||||
// - Recording timestamps or metrics
|
||||
// - Logging context information
|
||||
// - Computing derived values from existing context data
|
||||
//
|
||||
// The side effect is executed during the context transformation, and the resulting data is
|
||||
// stored in the context for downstream computations to access.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The success type (unchanged through the transformation)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: An IO-based Kleisli function that transforms the context
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - An Operator that applies the context transformation before executing the ReaderIOResult
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// // Generate a request ID via side effect and add to context
|
||||
// addRequestID := func(ctx context.Context) io.IO[ContextCancel] {
|
||||
// return func() ContextCancel {
|
||||
// // Side effect: generate unique ID
|
||||
// requestID := uuid.New().String()
|
||||
// // Share the ID via context
|
||||
// newCtx := context.WithValue(ctx, "requestID", requestID)
|
||||
// return pair.MakePair(func() {}, newCtx)
|
||||
// }
|
||||
// }
|
||||
// adapted := LocalIOK[int](addRequestID)(computation)
|
||||
//
|
||||
// # See Also
|
||||
//
|
||||
// - Local: For pure context transformations
|
||||
// - LocalIOResultK: For context transformations that can fail
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOK[A any](f io.Kleisli[context.Context, ContextCancel]) Operator[A, A] {
|
||||
return LocalIOResultK[A](function.Flow2(f, ioresult.FromIO))
|
||||
}
|
||||
|
||||
// LocalIOResultK transforms the context using an IOResult-based function before passing it to a ReaderIOResult.
|
||||
// This is similar to Local but the context transformation can fail with an error.
|
||||
//
|
||||
// The function f takes a context and returns an IOResult that produces either an error or a ContextCancel
|
||||
// (a pair of CancelFunc and the new Context). If the transformation fails, the error is propagated
|
||||
// and the original ReaderIOResult is not executed.
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// This function is particularly useful for sharing information via the Context that is computed
|
||||
// through side effects, such as:
|
||||
// - Loading configuration from a file or database
|
||||
// - Fetching authentication tokens from an external service
|
||||
// - Computing derived values that require I/O operations
|
||||
// - Validating and enriching context with data from external sources
|
||||
//
|
||||
// The side effect is executed during the context transformation, and the resulting data is
|
||||
// stored in the context for downstream computations to access.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The success type (unchanged through the transformation)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: An IOResult-based Kleisli function that transforms the context and may fail
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - An Operator that applies the context transformation before executing the ReaderIOResult
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// // Load configuration via side effect and add to context
|
||||
// loadConfig := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
// return func() result.Result[ContextCancel] {
|
||||
// // Side effect: read from file system
|
||||
// config, err := os.ReadFile("config.json")
|
||||
// if err != nil {
|
||||
// return result.Left[ContextCancel](err)
|
||||
// }
|
||||
// // Share the loaded config via context
|
||||
// newCtx := context.WithValue(ctx, "config", config)
|
||||
// return result.Of(pair.MakePair(func() {}, newCtx))
|
||||
// }
|
||||
// }
|
||||
// adapted := LocalIOResultK[int](loadConfig)(computation)
|
||||
//
|
||||
// # See Also
|
||||
//
|
||||
// - Local: For pure context transformations
|
||||
// - LocalIOK: For context transformations with side effects that cannot fail
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOResultK[A any](f ioresult.Kleisli[context.Context, ContextCancel]) Operator[A, A] {
|
||||
return func(rr ReaderIOResult[A]) ReaderIOResult[A] {
|
||||
return func(ctx context.Context) IOResult[A] {
|
||||
return func() Result[A] {
|
||||
if ctx.Err() != nil {
|
||||
return result.Left[A](context.Cause(ctx))
|
||||
}
|
||||
p, err := result.Unwrap(f(ctx)())
|
||||
if err != nil {
|
||||
return result.Left[A](err)
|
||||
}
|
||||
// unwrap
|
||||
otherCancel, otherCtx := pair.Unpack(p)
|
||||
defer otherCancel()
|
||||
return rr(otherCtx)()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/ioresult"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
R "github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -36,9 +40,9 @@ func TestPromapBasic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
addKey := func(ctx context.Context) pair.Pair[context.CancelFunc, context.Context] {
|
||||
newCtx := context.WithValue(ctx, "key", 42)
|
||||
return newCtx, func() {}
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
toString := strconv.Itoa
|
||||
|
||||
@@ -61,9 +65,9 @@ func TestContramapBasic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
addKey := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
addKey := func(ctx context.Context) pair.Pair[context.CancelFunc, context.Context] {
|
||||
newCtx := context.WithValue(ctx, "key", 100)
|
||||
return newCtx, func() {}
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
|
||||
adapted := Contramap[int](addKey)(getValue)
|
||||
@@ -85,9 +89,9 @@ func TestLocalBasic(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
addUser := func(ctx context.Context) pair.Pair[context.CancelFunc, context.Context] {
|
||||
newCtx := context.WithValue(ctx, "user", "Alice")
|
||||
return newCtx, func() {}
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
|
||||
adapted := Local[string](addUser)(getValue)
|
||||
@@ -96,3 +100,311 @@ func TestLocalBasic(t *testing.T) {
|
||||
assert.Equal(t, R.Of("Alice"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOK_Success tests LocalIOK with successful context transformation
|
||||
func TestLocalIOK_Success(t *testing.T) {
|
||||
t.Run("transforms context with IO effect", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
if v := ctx.Value("user"); v != nil {
|
||||
return R.Of(v.(string))
|
||||
}
|
||||
return R.Of("unknown")
|
||||
}
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) io.IO[ContextCancel] {
|
||||
return func() ContextCancel {
|
||||
newCtx := context.WithValue(ctx, "user", "Bob")
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOK[string](addUser)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.Equal(t, R.Of("Bob"), result)
|
||||
})
|
||||
|
||||
t.Run("preserves original value type", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
if v := ctx.Value("count"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
}
|
||||
|
||||
addCount := func(ctx context.Context) io.IO[ContextCancel] {
|
||||
return func() ContextCancel {
|
||||
newCtx := context.WithValue(ctx, "count", 42)
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOK[int](addCount)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.Equal(t, R.Of(42), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOK_CancelledContext tests LocalIOK with cancelled context
|
||||
func TestLocalIOK_CancelledContext(t *testing.T) {
|
||||
t.Run("returns error when context is cancelled", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("should not reach here")
|
||||
}
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) io.IO[ContextCancel] {
|
||||
return func() ContextCancel {
|
||||
newCtx := context.WithValue(ctx, "user", "Charlie")
|
||||
return pair.MakePair(context.CancelFunc(func() {}), newCtx)
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
|
||||
adapted := LocalIOK[string](addUser)(getValue)
|
||||
result := adapted(ctx)()
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOK_CancelFuncCalled tests that CancelFunc is properly called
|
||||
func TestLocalIOK_CancelFuncCalled(t *testing.T) {
|
||||
t.Run("calls cancel function after execution", func(t *testing.T) {
|
||||
cancelCalled := false
|
||||
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("test")
|
||||
}
|
||||
}
|
||||
|
||||
addUser := func(ctx context.Context) io.IO[ContextCancel] {
|
||||
return func() ContextCancel {
|
||||
newCtx := context.WithValue(ctx, "user", "Dave")
|
||||
cancelFunc := context.CancelFunc(func() {
|
||||
cancelCalled = true
|
||||
})
|
||||
return pair.MakePair(cancelFunc, newCtx)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOK[string](addUser)(getValue)
|
||||
_ = adapted(t.Context())()
|
||||
|
||||
assert.True(t, cancelCalled, "cancel function should be called")
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK_Success tests LocalIOResultK with successful context transformation
|
||||
func TestLocalIOResultK_Success(t *testing.T) {
|
||||
t.Run("transforms context with IOResult effect", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
if v := ctx.Value("role"); v != nil {
|
||||
return R.Of(v.(string))
|
||||
}
|
||||
return R.Of("guest")
|
||||
}
|
||||
}
|
||||
|
||||
addRole := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
newCtx := context.WithValue(ctx, "role", "admin")
|
||||
return R.Of(pair.MakePair(context.CancelFunc(func() {}), newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[string](addRole)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.Equal(t, R.Of("admin"), result)
|
||||
})
|
||||
|
||||
t.Run("preserves original value type", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
if v := ctx.Value("score"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
}
|
||||
|
||||
addScore := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
newCtx := context.WithValue(ctx, "score", 100)
|
||||
return R.Of(pair.MakePair(context.CancelFunc(func() {}), newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[int](addScore)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.Equal(t, R.Of(100), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK_Failure tests LocalIOResultK with failed context transformation
|
||||
func TestLocalIOResultK_Failure(t *testing.T) {
|
||||
t.Run("propagates transformation error", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("should not reach here")
|
||||
}
|
||||
}
|
||||
|
||||
failTransform := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
return R.Left[ContextCancel](assert.AnError)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[string](failTransform)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
_, err := R.UnwrapError(result)
|
||||
assert.Equal(t, assert.AnError, err)
|
||||
})
|
||||
|
||||
t.Run("does not execute original computation on transformation failure", func(t *testing.T) {
|
||||
executed := false
|
||||
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
executed = true
|
||||
return R.Of("should not execute")
|
||||
}
|
||||
}
|
||||
|
||||
failTransform := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
return R.Left[ContextCancel](assert.AnError)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[string](failTransform)(getValue)
|
||||
_ = adapted(t.Context())()
|
||||
|
||||
assert.False(t, executed, "original computation should not execute")
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK_CancelledContext tests LocalIOResultK with cancelled context
|
||||
func TestLocalIOResultK_CancelledContext(t *testing.T) {
|
||||
t.Run("returns error when context is cancelled", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("should not reach here")
|
||||
}
|
||||
}
|
||||
|
||||
addRole := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
newCtx := context.WithValue(ctx, "role", "user")
|
||||
return R.Of(pair.MakePair(context.CancelFunc(func() {}), newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
|
||||
adapted := LocalIOResultK[string](addRole)(getValue)
|
||||
result := adapted(ctx)()
|
||||
|
||||
assert.True(t, R.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK_CancelFuncCalled tests that CancelFunc is properly called
|
||||
func TestLocalIOResultK_CancelFuncCalled(t *testing.T) {
|
||||
t.Run("calls cancel function after successful execution", func(t *testing.T) {
|
||||
cancelCalled := false
|
||||
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("test")
|
||||
}
|
||||
}
|
||||
|
||||
addRole := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
newCtx := context.WithValue(ctx, "role", "user")
|
||||
cancelFunc := context.CancelFunc(func() {
|
||||
cancelCalled = true
|
||||
})
|
||||
return R.Of(pair.MakePair(cancelFunc, newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[string](addRole)(getValue)
|
||||
_ = adapted(t.Context())()
|
||||
|
||||
assert.True(t, cancelCalled, "cancel function should be called")
|
||||
})
|
||||
|
||||
t.Run("does not call cancel function on transformation failure", func(t *testing.T) {
|
||||
cancelCalled := false
|
||||
|
||||
getValue := func(ctx context.Context) IOResult[string] {
|
||||
return func() R.Result[string] {
|
||||
return R.Of("test")
|
||||
}
|
||||
}
|
||||
|
||||
failTransform := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
cancelFunc := context.CancelFunc(func() {
|
||||
cancelCalled = true
|
||||
})
|
||||
_ = cancelFunc // avoid unused warning
|
||||
return R.Left[ContextCancel](assert.AnError)
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOResultK[string](failTransform)(getValue)
|
||||
_ = adapted(t.Context())()
|
||||
|
||||
assert.False(t, cancelCalled, "cancel function should not be called on failure")
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK_Integration tests integration with other operations
|
||||
func TestLocalIOResultK_Integration(t *testing.T) {
|
||||
t.Run("composes with Map", func(t *testing.T) {
|
||||
getValue := func(ctx context.Context) IOResult[int] {
|
||||
return func() R.Result[int] {
|
||||
if v := ctx.Value("value"); v != nil {
|
||||
return R.Of(v.(int))
|
||||
}
|
||||
return R.Of(0)
|
||||
}
|
||||
}
|
||||
|
||||
addValue := func(ctx context.Context) ioresult.IOResult[ContextCancel] {
|
||||
return func() R.Result[ContextCancel] {
|
||||
newCtx := context.WithValue(ctx, "value", 10)
|
||||
return R.Of(pair.MakePair(context.CancelFunc(func() {}), newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
double := func(x int) int { return x * 2 }
|
||||
|
||||
adapted := F.Flow2(
|
||||
LocalIOResultK[int](addValue),
|
||||
Map(double),
|
||||
)(getValue)
|
||||
result := adapted(t.Context())()
|
||||
|
||||
assert.Equal(t, R.Of(20), result)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
RIOR "github.com/IBM/fp-go/v2/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/readeroption"
|
||||
@@ -222,7 +223,7 @@ func withCancelCauseFunc[A any](cancel context.CancelCauseFunc, ma IOResult[A])
|
||||
return function.Pipe3(
|
||||
ma,
|
||||
ioresult.Swap[A],
|
||||
ioeither.ChainFirstIOK[A](func(err error) func() any {
|
||||
ioeither.ChainFirstIOK[A](func(err error) func() Void {
|
||||
return io.FromImpure(func() { cancel(err) })
|
||||
}),
|
||||
ioeither.Swap[A],
|
||||
@@ -452,7 +453,7 @@ func TapEitherK[A, B any](f either.Kleisli[error, A, B]) Operator[A, A] {
|
||||
// Returns a function that chains Option-returning functions into ReaderIOResult.
|
||||
//
|
||||
//go:inline
|
||||
func ChainOptionK[A, B any](onNone func() error) func(option.Kleisli[A, B]) Operator[A, B] {
|
||||
func ChainOptionK[A, B any](onNone Lazy[error]) func(option.Kleisli[A, B]) Operator[A, B] {
|
||||
return RIOR.ChainOptionK[context.Context, A, B](onNone)
|
||||
}
|
||||
|
||||
@@ -800,7 +801,7 @@ func FromReaderResult[A any](ma ReaderResult[A]) ReaderIOResult[A] {
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func FromReaderOption[A any](onNone func() error) Kleisli[ReaderOption[context.Context, A], A] {
|
||||
func FromReaderOption[A any](onNone Lazy[error]) Kleisli[ReaderOption[context.Context, A], A] {
|
||||
return RIOR.FromReaderOption[context.Context, A](onNone)
|
||||
}
|
||||
|
||||
@@ -895,17 +896,17 @@ func TapReaderIOK[A, B any](f readerio.Kleisli[A, B]) Operator[A, A] {
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ChainReaderOptionK[A, B any](onNone func() error) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, B] {
|
||||
func ChainReaderOptionK[A, B any](onNone Lazy[error]) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, B] {
|
||||
return RIOR.ChainReaderOptionK[context.Context, A, B](onNone)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ChainFirstReaderOptionK[A, B any](onNone func() error) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, A] {
|
||||
func ChainFirstReaderOptionK[A, B any](onNone Lazy[error]) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, A] {
|
||||
return RIOR.ChainFirstReaderOptionK[context.Context, A, B](onNone)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func TapReaderOptionK[A, B any](onNone func() error) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, A] {
|
||||
func TapReaderOptionK[A, B any](onNone Lazy[error]) func(readeroption.Kleisli[context.Context, A, B]) Operator[A, A] {
|
||||
return RIOR.TapReaderOptionK[context.Context, A, B](onNone)
|
||||
}
|
||||
|
||||
@@ -1054,14 +1055,14 @@ func TapLeftIOK[A, B any](f io.Kleisli[error, B]) Operator[A, A] {
|
||||
// fetchData,
|
||||
// withTimeout,
|
||||
// )
|
||||
func Local[A any](f func(context.Context) (context.Context, context.CancelFunc)) Operator[A, A] {
|
||||
func Local[A any](f pair.Kleisli[context.CancelFunc, context.Context, context.Context]) Operator[A, A] {
|
||||
return func(rr ReaderIOResult[A]) ReaderIOResult[A] {
|
||||
return func(ctx context.Context) IOResult[A] {
|
||||
return func() Result[A] {
|
||||
if ctx.Err() != nil {
|
||||
return result.Left[A](context.Cause(ctx))
|
||||
}
|
||||
otherCtx, otherCancel := f(ctx)
|
||||
otherCancel, otherCtx := pair.Unpack(f(ctx))
|
||||
defer otherCancel()
|
||||
return rr(otherCtx)()
|
||||
}
|
||||
@@ -1123,9 +1124,10 @@ func Local[A any](f func(context.Context) (context.Context, context.CancelFunc))
|
||||
// )
|
||||
// value, err := result(t.Context())() // Returns (Data{Value: "quick"}, nil)
|
||||
func WithTimeout[A any](timeout time.Duration) Operator[A, A] {
|
||||
return Local[A](func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, timeout)
|
||||
})
|
||||
return Local[A](
|
||||
func(ctx context.Context) ContextCancel {
|
||||
return pairFromContextCancel(context.WithTimeout(ctx, timeout))
|
||||
})
|
||||
}
|
||||
|
||||
// WithDeadline adds an absolute deadline to the context for a ReaderIOResult computation.
|
||||
@@ -1188,7 +1190,7 @@ func WithTimeout[A any](timeout time.Duration) Operator[A, A] {
|
||||
// )
|
||||
// value, err := result(parentCtx)() // Will use parent's 1-hour deadline
|
||||
func WithDeadline[A any](deadline time.Time) Operator[A, A] {
|
||||
return Local[A](func(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithDeadline(ctx, deadline)
|
||||
return Local[A](func(ctx context.Context) ContextCancel {
|
||||
return pairFromContextCancel(context.WithDeadline(ctx, deadline))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func WithLock[A any](lock ReaderIOResult[context.CancelFunc]) Operator[A, A] {
|
||||
function.Constant1[context.CancelFunc, ReaderIOResult[A]],
|
||||
WithResource[A](lock, function.Flow2(
|
||||
io.FromImpure[context.CancelFunc],
|
||||
FromIO[any],
|
||||
FromIO[Void],
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/context/readerresult"
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/endomorphism"
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/ioref"
|
||||
@@ -54,6 +55,10 @@ type (
|
||||
// Either[A] is equivalent to Either[error, A] from the either package.
|
||||
Either[A any] = either.Either[error, A]
|
||||
|
||||
// Result represents a computation that can either succeed with a value of type A
|
||||
// or fail with an error. This is an alias for result.Result[A].
|
||||
//
|
||||
// Result[A] is equivalent to Either[error, A]
|
||||
Result[A any] = result.Result[A]
|
||||
|
||||
// Lazy represents a deferred computation that produces a value of type A when executed.
|
||||
@@ -72,6 +77,10 @@ type (
|
||||
// IOEither[A] is equivalent to func() Either[error, A]
|
||||
IOEither[A any] = ioeither.IOEither[error, A]
|
||||
|
||||
// IOResult represents a side-effectful computation that can fail with an error.
|
||||
// This combines IO (side effects) with Result (error handling).
|
||||
//
|
||||
// IOResult[A] is equivalent to func() Result[A]
|
||||
IOResult[A any] = ioresult.IOResult[A]
|
||||
|
||||
// Reader represents a computation that depends on a context of type R.
|
||||
@@ -117,6 +126,13 @@ type (
|
||||
// result := fetchUser("123")(ctx)()
|
||||
ReaderIOResult[A any] = RIOR.ReaderIOResult[context.Context, A]
|
||||
|
||||
// Kleisli represents a Kleisli arrow for ReaderIOResult.
|
||||
// It is a function that takes a value of type A and returns a ReaderIOResult[B].
|
||||
//
|
||||
// Kleisli arrows are used for monadic composition, allowing you to chain operations
|
||||
// that produce ReaderIOResults. They are particularly useful with Chain operations.
|
||||
//
|
||||
// Kleisli[A, B] is equivalent to func(A) ReaderIOResult[B]
|
||||
Kleisli[A, B any] = reader.Reader[A, ReaderIOResult[B]]
|
||||
|
||||
// Operator represents a transformation from one ReaderIOResult to another.
|
||||
@@ -132,24 +148,76 @@ type (
|
||||
// result := toUpper(computation)
|
||||
Operator[A, B any] = Kleisli[ReaderIOResult[A], B]
|
||||
|
||||
ReaderResult[A any] = readerresult.ReaderResult[A]
|
||||
ReaderEither[R, E, A any] = readereither.ReaderEither[R, E, A]
|
||||
ReaderOption[R, A any] = readeroption.ReaderOption[R, A]
|
||||
// ReaderResult represents a context-dependent computation that can fail.
|
||||
// This is specialized to use context.Context as the context type.
|
||||
//
|
||||
// ReaderResult[A] is equivalent to func(context.Context) Result[A]
|
||||
ReaderResult[A any] = readerresult.ReaderResult[A]
|
||||
|
||||
// ReaderEither represents a context-dependent computation that can fail.
|
||||
// It takes a context of type R and produces an Either[E, A].
|
||||
//
|
||||
// ReaderEither[R, E, A] is equivalent to func(R) Either[E, A]
|
||||
ReaderEither[R, E, A any] = readereither.ReaderEither[R, E, A]
|
||||
|
||||
// ReaderOption represents a context-dependent computation that may not produce a value.
|
||||
// It takes a context of type R and produces an Option[A].
|
||||
//
|
||||
// ReaderOption[R, A] is equivalent to func(R) Option[A]
|
||||
ReaderOption[R, A any] = readeroption.ReaderOption[R, A]
|
||||
|
||||
// Endomorphism represents a function from a type to itself.
|
||||
// It is used for transformations that preserve the type.
|
||||
//
|
||||
// Endomorphism[A] is equivalent to func(A) A
|
||||
Endomorphism[A any] = endomorphism.Endomorphism[A]
|
||||
|
||||
// Consumer represents a function that consumes a value without producing a result.
|
||||
// It is used for side effects like logging or updating state.
|
||||
//
|
||||
// Consumer[A] is equivalent to func(A)
|
||||
Consumer[A any] = consumer.Consumer[A]
|
||||
|
||||
// Prism represents an optic for working with sum types (tagged unions).
|
||||
// It provides a way to focus on a specific variant of a sum type.
|
||||
Prism[S, T any] = prism.Prism[S, T]
|
||||
Lens[S, T any] = lens.Lens[S, T]
|
||||
|
||||
// Lens represents an optic for working with product types (records/structs).
|
||||
// It provides a way to focus on a specific field of a product type.
|
||||
Lens[S, T any] = lens.Lens[S, T]
|
||||
|
||||
// Trampoline represents a computation that can be executed in a stack-safe manner.
|
||||
// It is used for tail-recursive computations that would otherwise overflow the stack.
|
||||
Trampoline[B, L any] = tailrec.Trampoline[B, L]
|
||||
|
||||
// Predicate represents a function that tests a value of type A.
|
||||
// It returns true if the value satisfies the predicate, false otherwise.
|
||||
//
|
||||
// Predicate[A] is equivalent to func(A) bool
|
||||
Predicate[A any] = predicate.Predicate[A]
|
||||
|
||||
// Pair represents a tuple of two values of types A and B.
|
||||
// It is used to group two related values together.
|
||||
Pair[A, B any] = pair.Pair[A, B]
|
||||
|
||||
// IORef represents a mutable reference that can be safely accessed in IO computations.
|
||||
// It provides thread-safe read and write operations.
|
||||
IORef[A any] = ioref.IORef[A]
|
||||
|
||||
// State represents a stateful computation that transforms a state of type S
|
||||
// and produces a value of type A.
|
||||
//
|
||||
// State[S, A] is equivalent to func(S) Pair[A, S]
|
||||
State[S, A any] = state.State[S, A]
|
||||
|
||||
// Void represents the absence of a value, similar to unit type in other languages.
|
||||
// It is used when a function performs side effects but doesn't return a meaningful value.
|
||||
Void = function.Void
|
||||
|
||||
// ContextCancel represents a pair of a cancel function and a context.
|
||||
// It is used in operations that create new contexts with cancellation capabilities.
|
||||
//
|
||||
// The first element is the CancelFunc that should be called to release resources.
|
||||
// The second element is the new Context that was created.
|
||||
ContextCancel = Pair[context.CancelFunc, context.Context]
|
||||
)
|
||||
|
||||
168
v2/context/readerreaderioresult/promap.go
Normal file
168
v2/context/readerreaderioresult/promap.go
Normal file
@@ -0,0 +1,168 @@
|
||||
package readerreaderioresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
RRIOE "github.com/IBM/fp-go/v2/readerreaderioeither"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// Local modifies the outer environment before passing it to a computation.
|
||||
// Useful for providing different configurations to sub-computations.
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R1, R2 any](f func(R2) R1) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.Local[context.Context, error, A](f)
|
||||
}
|
||||
|
||||
// LocalIOK transforms the outer environment of a ReaderReaderIOResult using an IO-based Kleisli arrow.
|
||||
// It allows you to modify the outer environment through an effectful computation before
|
||||
// passing it to the ReaderReaderIOResult.
|
||||
//
|
||||
// This is useful when the outer environment transformation itself requires IO effects,
|
||||
// such as reading from a file, making a network call, or accessing system resources,
|
||||
// but these effects cannot fail (or failures are not relevant).
|
||||
//
|
||||
// The transformation happens in two stages:
|
||||
// 1. The IO effect f is executed with the R2 environment to produce an R1 value
|
||||
// 2. The resulting R1 value is passed as the outer environment to the ReaderReaderIOResult[R1, A]
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type produced by the ReaderReaderIOResult
|
||||
// - R1: The original outer environment type expected by the ReaderReaderIOResult
|
||||
// - R2: The new input outer environment type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An IO Kleisli arrow that transforms R2 to R1 with IO effects
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes a ReaderReaderIOResult[R1, A] and returns a ReaderReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOK[A, R1, R2 any](f io.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalIOK[context.Context, error, A](f)
|
||||
}
|
||||
|
||||
// LocalIOEitherK transforms the outer environment of a ReaderReaderIOResult using an IOResult-based Kleisli arrow.
|
||||
// It allows you to modify the outer environment through an effectful computation that can fail before
|
||||
// passing it to the ReaderReaderIOResult.
|
||||
//
|
||||
// This is useful when the outer environment transformation itself requires IO effects that can fail,
|
||||
// such as reading from a file that might not exist, making a network call that might timeout,
|
||||
// or parsing data that might be invalid.
|
||||
//
|
||||
// The transformation happens in two stages:
|
||||
// 1. The IOResult effect f is executed with the R2 environment to produce Result[R1]
|
||||
// 2. If successful (Ok), the R1 value is passed as the outer environment to the ReaderReaderIOResult[R1, A]
|
||||
// 3. If failed (Err), the error is propagated without executing the ReaderReaderIOResult
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type produced by the ReaderReaderIOResult
|
||||
// - R1: The original outer environment type expected by the ReaderReaderIOResult
|
||||
// - R2: The new input outer environment type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An IOResult Kleisli arrow that transforms R2 to R1 with IO effects that can fail
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes a ReaderReaderIOResult[R1, A] and returns a ReaderReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOEitherK[A, R1, R2 any](f ioresult.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalIOEitherK[context.Context, A](f)
|
||||
}
|
||||
|
||||
// LocalIOResultK transforms the outer environment of a ReaderReaderIOResult using an IOResult-based Kleisli arrow.
|
||||
// This is a type-safe alias for LocalIOEitherK specialized for Result types (which use error as the error type).
|
||||
//
|
||||
// It allows you to modify the outer environment through an effectful computation that can fail before
|
||||
// passing it to the ReaderReaderIOResult.
|
||||
//
|
||||
// The transformation happens in two stages:
|
||||
// 1. The IOResult effect f is executed with the R2 environment to produce Result[R1]
|
||||
// 2. If successful (Ok), the R1 value is passed as the outer environment to the ReaderReaderIOResult[R1, A]
|
||||
// 3. If failed (Err), the error is propagated without executing the ReaderReaderIOResult
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type produced by the ReaderReaderIOResult
|
||||
// - R1: The original outer environment type expected by the ReaderReaderIOResult
|
||||
// - R2: The new input outer environment type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An IOResult Kleisli arrow that transforms R2 to R1 with IO effects that can fail
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes a ReaderReaderIOResult[R1, A] and returns a ReaderReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOResultK[A, R1, R2 any](f ioresult.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalIOEitherK[context.Context, A](f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func LocalResultK[A, R1, R2 any](f result.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalEitherK[context.Context, A](f)
|
||||
}
|
||||
|
||||
// LocalReaderIOEitherK transforms the outer environment of a ReaderReaderIOResult using a ReaderIOResult-based Kleisli arrow.
|
||||
// It allows you to modify the outer environment through a computation that depends on the inner context
|
||||
// and can perform IO effects that may fail.
|
||||
//
|
||||
// This is useful when the outer environment transformation requires access to the inner context (e.g., context.Context)
|
||||
// and may perform IO operations that can fail, such as database queries, API calls, or file operations.
|
||||
//
|
||||
// The transformation happens in three stages:
|
||||
// 1. The ReaderIOResult effect f is executed with the R2 outer environment and inner context
|
||||
// 2. If successful (Ok), the R1 value is passed as the outer environment to the ReaderReaderIOResult[R1, A]
|
||||
// 3. If failed (Err), the error is propagated without executing the ReaderReaderIOResult
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type produced by the ReaderReaderIOResult
|
||||
// - R1: The original outer environment type expected by the ReaderReaderIOResult
|
||||
// - R2: The new input outer environment type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A ReaderIOResult Kleisli arrow that transforms R2 to R1 with context-aware IO effects that can fail
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes a ReaderReaderIOResult[R1, A] and returns a ReaderReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func LocalReaderIOEitherK[A, R1, R2 any](f readerioresult.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalReaderIOEitherK[A](f)
|
||||
}
|
||||
|
||||
// LocalReaderIOResultK transforms the outer environment of a ReaderReaderIOResult using a ReaderIOResult-based Kleisli arrow.
|
||||
// This is a type-safe alias for LocalReaderIOEitherK specialized for Result types (which use error as the error type).
|
||||
//
|
||||
// It allows you to modify the outer environment through a computation that depends on the inner context
|
||||
// and can perform IO effects that may fail.
|
||||
//
|
||||
// The transformation happens in three stages:
|
||||
// 1. The ReaderIOResult effect f is executed with the R2 outer environment and inner context
|
||||
// 2. If successful (Ok), the R1 value is passed as the outer environment to the ReaderReaderIOResult[R1, A]
|
||||
// 3. If failed (Err), the error is propagated without executing the ReaderReaderIOResult
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The success type produced by the ReaderReaderIOResult
|
||||
// - R1: The original outer environment type expected by the ReaderReaderIOResult
|
||||
// - R2: The new input outer environment type
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A ReaderIOResult Kleisli arrow that transforms R2 to R1 with context-aware IO effects that can fail
|
||||
//
|
||||
// Returns:
|
||||
// - A function that takes a ReaderReaderIOResult[R1, A] and returns a ReaderReaderIOResult[R2, A]
|
||||
//
|
||||
//go:inline
|
||||
func LocalReaderIOResultK[A, R1, R2 any](f readerioresult.Kleisli[R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalReaderIOEitherK[A](f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func LocalReaderReaderIOEitherK[A, R1, R2 any](f Kleisli[R2, R2, R1]) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.LocalReaderReaderIOEitherK[A](f)
|
||||
}
|
||||
428
v2/context/readerreaderioresult/promap_test.go
Normal file
428
v2/context/readerreaderioresult/promap_test.go
Normal file
@@ -0,0 +1,428 @@
|
||||
// Copyright (c) 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 readerreaderioresult
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type SimpleConfig struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type DetailedConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
// TestLocalIOK tests LocalIOK functionality
|
||||
func TestLocalIOK(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("basic IO transformation", func(t *testing.T) {
|
||||
// IO effect that loads config from a path
|
||||
loadConfig := func(path string) io.IO[SimpleConfig] {
|
||||
return func() SimpleConfig {
|
||||
// Simulate loading config
|
||||
return SimpleConfig{Port: 8080}
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose using LocalIOK
|
||||
adapted := LocalIOK[string](loadConfig)(useConfig)
|
||||
res := adapted("config.json")(ctx)()
|
||||
|
||||
assert.Equal(t, result.Of("Port: 8080"), res)
|
||||
})
|
||||
|
||||
t.Run("IO transformation with side effects", func(t *testing.T) {
|
||||
var loadLog []string
|
||||
|
||||
loadData := func(key string) io.IO[int] {
|
||||
return func() int {
|
||||
loadLog = append(loadLog, "Loading: "+key)
|
||||
return len(key) * 10
|
||||
}
|
||||
}
|
||||
|
||||
processData := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Processed: %d", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOK[string](loadData)(processData)
|
||||
res := adapted("test")(ctx)()
|
||||
|
||||
assert.Equal(t, result.Of("Processed: 40"), res)
|
||||
assert.Equal(t, []string{"Loading: test"}, loadLog)
|
||||
})
|
||||
|
||||
t.Run("error propagation in ReaderReaderIOResult", func(t *testing.T) {
|
||||
loadConfig := func(path string) io.IO[SimpleConfig] {
|
||||
return func() SimpleConfig {
|
||||
return SimpleConfig{Port: 8080}
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that returns an error
|
||||
failingOperation := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Left[string](errors.New("operation failed"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOK[string](loadConfig)(failingOperation)
|
||||
res := adapted("config.json")(ctx)()
|
||||
|
||||
assert.True(t, result.IsLeft(res))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOEitherK tests LocalIOEitherK functionality
|
||||
func TestLocalIOEitherK(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("basic IOResult transformation", func(t *testing.T) {
|
||||
// IOResult effect that loads config from a path (can fail)
|
||||
loadConfig := func(path string) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if path == "" {
|
||||
return result.Left[SimpleConfig](errors.New("empty path"))
|
||||
}
|
||||
return result.Of(SimpleConfig{Port: 8080})
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose using LocalIOEitherK
|
||||
adapted := LocalIOEitherK[string](loadConfig)(useConfig)
|
||||
|
||||
// Success case
|
||||
res := adapted("config.json")(ctx)()
|
||||
assert.Equal(t, result.Of("Port: 8080"), res)
|
||||
|
||||
// Failure case
|
||||
resErr := adapted("")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
|
||||
t.Run("error propagation from environment transformation", func(t *testing.T) {
|
||||
loadConfig := func(path string) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
return result.Left[SimpleConfig](errors.New("file not found"))
|
||||
}
|
||||
}
|
||||
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalIOEitherK[string](loadConfig)(useConfig)
|
||||
res := adapted("missing.json")(ctx)()
|
||||
|
||||
// Error from loadConfig should propagate
|
||||
assert.True(t, result.IsLeft(res))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalIOResultK tests LocalIOResultK functionality
|
||||
func TestLocalIOResultK(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("basic IOResult transformation", func(t *testing.T) {
|
||||
// IOResult effect that loads config from a path (can fail)
|
||||
loadConfig := func(path string) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if path == "" {
|
||||
return result.Left[SimpleConfig](errors.New("empty path"))
|
||||
}
|
||||
return result.Of(SimpleConfig{Port: 8080})
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose using LocalIOResultK
|
||||
adapted := LocalIOResultK[string](loadConfig)(useConfig)
|
||||
|
||||
// Success case
|
||||
res := adapted("config.json")(ctx)()
|
||||
assert.Equal(t, result.Of("Port: 8080"), res)
|
||||
|
||||
// Failure case
|
||||
resErr := adapted("")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
|
||||
t.Run("compose multiple LocalIOResultK", func(t *testing.T) {
|
||||
// First transformation: string -> int (can fail)
|
||||
parseID := func(s string) ioresult.IOResult[int] {
|
||||
return func() result.Result[int] {
|
||||
if s == "" {
|
||||
return result.Left[int](errors.New("empty string"))
|
||||
}
|
||||
return result.Of(len(s) * 10)
|
||||
}
|
||||
}
|
||||
|
||||
// Second transformation: int -> SimpleConfig (can fail)
|
||||
loadConfig := func(id int) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if id < 0 {
|
||||
return result.Left[SimpleConfig](errors.New("invalid ID"))
|
||||
}
|
||||
return result.Of(SimpleConfig{Port: 8000 + id})
|
||||
}
|
||||
}
|
||||
|
||||
// Use the config
|
||||
formatConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose transformations
|
||||
step1 := LocalIOResultK[string](loadConfig)(formatConfig)
|
||||
step2 := LocalIOResultK[string](parseID)(step1)
|
||||
|
||||
// Success case
|
||||
res := step2("test")(ctx)()
|
||||
assert.Equal(t, result.Of("Port: 8040"), res)
|
||||
|
||||
// Failure in first transformation
|
||||
resErr1 := step2("")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr1))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalReaderIOEitherK tests LocalReaderIOEitherK functionality
|
||||
func TestLocalReaderIOEitherK(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("basic ReaderIOResult transformation", func(t *testing.T) {
|
||||
// ReaderIOResult effect that loads config from a path (can fail, uses context)
|
||||
loadConfig := func(path string) readerioresult.ReaderIOResult[SimpleConfig] {
|
||||
return func(ctx context.Context) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if path == "" {
|
||||
return result.Left[SimpleConfig](errors.New("empty path"))
|
||||
}
|
||||
// Could use context here for cancellation, logging, etc.
|
||||
return result.Of(SimpleConfig{Port: 8080})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose using LocalReaderIOEitherK
|
||||
adapted := LocalReaderIOEitherK[string](loadConfig)(useConfig)
|
||||
|
||||
// Success case
|
||||
res := adapted("config.json")(ctx)()
|
||||
assert.Equal(t, result.Of("Port: 8080"), res)
|
||||
|
||||
// Failure case
|
||||
resErr := adapted("")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
|
||||
t.Run("context propagation", func(t *testing.T) {
|
||||
type ctxKey string
|
||||
const key ctxKey = "test-key"
|
||||
|
||||
// ReaderIOResult that reads from context
|
||||
loadFromContext := func(path string) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
if val := ctx.Value(key); val != nil {
|
||||
return result.Of(val.(string))
|
||||
}
|
||||
return result.Left[string](errors.New("key not found in context"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the loaded value
|
||||
useValue := func(val string) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of("Loaded: " + val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
adapted := LocalReaderIOEitherK[string](loadFromContext)(useValue)
|
||||
|
||||
// With context value
|
||||
ctxWithValue := context.WithValue(ctx, key, "test-value")
|
||||
res := adapted("ignored")(ctxWithValue)()
|
||||
assert.Equal(t, result.Of("Loaded: test-value"), res)
|
||||
|
||||
// Without context value
|
||||
resErr := adapted("ignored")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
}
|
||||
|
||||
// TestLocalReaderIOResultK tests LocalReaderIOResultK functionality
|
||||
func TestLocalReaderIOResultK(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("basic ReaderIOResult transformation", func(t *testing.T) {
|
||||
// ReaderIOResult effect that loads config from a path (can fail, uses context)
|
||||
loadConfig := func(path string) readerioresult.ReaderIOResult[SimpleConfig] {
|
||||
return func(ctx context.Context) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if path == "" {
|
||||
return result.Left[SimpleConfig](errors.New("empty path"))
|
||||
}
|
||||
return result.Of(SimpleConfig{Port: 8080})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ReaderReaderIOResult that uses the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose using LocalReaderIOResultK
|
||||
adapted := LocalReaderIOResultK[string](loadConfig)(useConfig)
|
||||
|
||||
// Success case
|
||||
res := adapted("config.json")(ctx)()
|
||||
assert.Equal(t, result.Of("Port: 8080"), res)
|
||||
|
||||
// Failure case
|
||||
resErr := adapted("")(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
|
||||
t.Run("real-world: load and validate config with context", func(t *testing.T) {
|
||||
type ConfigFile struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
// Read file with context (can fail, uses context for cancellation)
|
||||
readFile := func(cf ConfigFile) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
// Check context cancellation
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return result.Left[string](ctx.Err())
|
||||
default:
|
||||
}
|
||||
|
||||
if cf.Path == "" {
|
||||
return result.Left[string](errors.New("empty path"))
|
||||
}
|
||||
return result.Of(`{"port":9000}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse config with context (can fail)
|
||||
parseConfig := func(content string) readerioresult.ReaderIOResult[SimpleConfig] {
|
||||
return func(ctx context.Context) ioresult.IOResult[SimpleConfig] {
|
||||
return func() result.Result[SimpleConfig] {
|
||||
if content == "" {
|
||||
return result.Left[SimpleConfig](errors.New("empty content"))
|
||||
}
|
||||
return result.Of(SimpleConfig{Port: 9000})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use the config
|
||||
useConfig := func(cfg SimpleConfig) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) ioresult.IOResult[string] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Using port: %d", cfg.Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose the pipeline
|
||||
step1 := LocalReaderIOResultK[string](parseConfig)(useConfig)
|
||||
step2 := LocalReaderIOResultK[string](readFile)(step1)
|
||||
|
||||
// Success case
|
||||
res := step2(ConfigFile{Path: "app.json"})(ctx)()
|
||||
assert.Equal(t, result.Of("Using port: 9000"), res)
|
||||
|
||||
// Failure case
|
||||
resErr := step2(ConfigFile{Path: ""})(ctx)()
|
||||
assert.True(t, result.IsLeft(resErr))
|
||||
})
|
||||
}
|
||||
@@ -37,6 +37,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/IBM/fp-go/v2/readeroption"
|
||||
RRIOE "github.com/IBM/fp-go/v2/readerreaderioeither"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// FromReaderOption converts a ReaderOption to a ReaderReaderIOResult.
|
||||
@@ -170,6 +171,15 @@ func ChainEitherK[R, A, B any](f either.Kleisli[error, A, B]) Operator[R, A, B]
|
||||
)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ChainResultK[R, A, B any](f result.Kleisli[A, B]) Operator[R, A, B] {
|
||||
return fromeither.ChainEitherK(
|
||||
Chain[R, A, B],
|
||||
FromEither[R, B],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// MonadChainFirstEitherK chains a computation that returns an Either but preserves the original value.
|
||||
// Useful for validation or side effects that may fail.
|
||||
// This is the monadic version that takes the computation as the first parameter.
|
||||
@@ -837,14 +847,6 @@ func MapLeft[R, A any](f Endmorphism[error]) Operator[R, A, A] {
|
||||
return RRIOE.MapLeft[R, context.Context, A](f)
|
||||
}
|
||||
|
||||
// Local modifies the outer environment before passing it to a computation.
|
||||
// Useful for providing different configurations to sub-computations.
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, R1, R2 any](f func(R2) R1) func(ReaderReaderIOResult[R1, A]) ReaderReaderIOResult[R2, A] {
|
||||
return RRIOE.Local[context.Context, error, A](f)
|
||||
}
|
||||
|
||||
// Read provides a specific outer environment value to a computation.
|
||||
// Converts ReaderReaderIOResult[R, A] to ReaderIOResult[context.Context, A].
|
||||
//
|
||||
@@ -892,3 +894,8 @@ func ChainLeft[R, A any](f Kleisli[R, error, A]) func(ReaderReaderIOResult[R, A]
|
||||
func Delay[R, A any](delay time.Duration) Operator[R, A, A] {
|
||||
return reader.Map[R](RIOE.Delay[A](delay))
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func Defer[R, A any](fa Lazy[ReaderReaderIOResult[R, A]]) ReaderReaderIOResult[R, A] {
|
||||
return RRIOE.Defer(fa)
|
||||
}
|
||||
|
||||
9
v2/context/readerreaderioresult/traverse.go
Normal file
9
v2/context/readerreaderioresult/traverse.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package readerreaderioresult
|
||||
|
||||
import (
|
||||
RRIOE "github.com/IBM/fp-go/v2/readerreaderioeither"
|
||||
)
|
||||
|
||||
func TraverseArray[R, A, B any](f Kleisli[R, A, B]) Kleisli[R, []A, []B] {
|
||||
return RRIOE.TraverseArray(f)
|
||||
}
|
||||
@@ -87,8 +87,8 @@ var (
|
||||
// assembleProviders constructs the provider map for item and non-item providers
|
||||
assembleProviders = F.Flow3(
|
||||
A.Partition(isItemProvider),
|
||||
T.Map2(collectProviders, collectItemProviders),
|
||||
T.Tupled2(mergeProviders.Concat),
|
||||
pair.BiMap(collectProviders, collectItemProviders),
|
||||
pair.Paired(mergeProviders.Concat),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -30,8 +30,8 @@ import (
|
||||
|
||||
type (
|
||||
// InjectableFactory is a factory function that can create an untyped instance of a service based on its [Dependency] identifier
|
||||
InjectableFactory = func(Dependency) IOResult[any]
|
||||
ProviderFactory = func(InjectableFactory) IOResult[any]
|
||||
InjectableFactory = ReaderIOResult[Dependency, any]
|
||||
ProviderFactory = ReaderIOResult[InjectableFactory, any]
|
||||
|
||||
paramIndex = map[int]int
|
||||
paramValue = map[int]any
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/iooption"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/record"
|
||||
)
|
||||
|
||||
@@ -12,4 +13,5 @@ type (
|
||||
IOResult[T any] = ioresult.IOResult[T]
|
||||
IOOption[T any] = iooption.IOOption[T]
|
||||
Entry[K comparable, V any] = record.Entry[K, V]
|
||||
ReaderIOResult[R, T any] = readerioresult.ReaderIOResult[R, T]
|
||||
)
|
||||
|
||||
556
v2/effect/bind.go
Normal file
556
v2/effect/bind.go
Normal file
@@ -0,0 +1,556 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
)
|
||||
|
||||
// Do creates an Effect with an initial state value.
|
||||
// This is the starting point for do-notation style effect composition,
|
||||
// allowing you to build up complex state transformations step by step.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S: The state type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - empty: The initial state value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, S]: An effect that produces the initial state
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// type State struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// }
|
||||
// eff := effect.Do[MyContext](State{})
|
||||
//
|
||||
//go:inline
|
||||
func Do[C, S any](
|
||||
empty S,
|
||||
) Effect[C, S] {
|
||||
return readerreaderioresult.Of[C](empty)
|
||||
}
|
||||
|
||||
// Bind executes an effectful computation and binds its result to the state.
|
||||
// This is the core operation for do-notation, allowing you to sequence effects
|
||||
// while accumulating results in a state structure.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - S1: The input state type
|
||||
// - S2: The output state type
|
||||
// - T: The type of value produced by the effect
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - setter: A function that takes the effect result and returns a state updater
|
||||
// - f: An effectful computation that depends on the current state
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S1, S2]: A function that transforms the state effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Bind(
|
||||
// func(age int) func(State) State {
|
||||
// return func(s State) State {
|
||||
// s.Age = age
|
||||
// return s
|
||||
// }
|
||||
// },
|
||||
// func(s State) Effect[MyContext, int] {
|
||||
// return effect.Of[MyContext](30)
|
||||
// },
|
||||
// )(effect.Do[MyContext](State{}))
|
||||
//
|
||||
//go:inline
|
||||
func Bind[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f Kleisli[C, S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.Bind(setter, f)
|
||||
}
|
||||
|
||||
// Let computes a pure value from the current state and binds it to the state.
|
||||
// Unlike Bind, this doesn't perform any effects - it's for pure computations.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S1: The input state type
|
||||
// - S2: The output state type
|
||||
// - T: The type of computed value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - setter: A function that takes the computed value and returns a state updater
|
||||
// - f: A pure function that computes a value from the current state
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S1, S2]: A function that transforms the state effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Let[MyContext](
|
||||
// func(nameLen int) func(State) State {
|
||||
// return func(s State) State {
|
||||
// s.NameLength = nameLen
|
||||
// return s
|
||||
// }
|
||||
// },
|
||||
// func(s State) int {
|
||||
// return len(s.Name)
|
||||
// },
|
||||
// )(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func Let[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f func(S1) T,
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.Let[C](setter, f)
|
||||
}
|
||||
|
||||
// LetTo binds a constant value to the state.
|
||||
// This is useful for setting fixed values in your state structure.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S1: The input state type
|
||||
// - S2: The output state type
|
||||
// - T: The type of the constant value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - setter: A function that takes the constant and returns a state updater
|
||||
// - b: The constant value to bind
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S1, S2]: A function that transforms the state effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.LetTo[MyContext](
|
||||
// func(age int) func(State) State {
|
||||
// return func(s State) State {
|
||||
// s.Age = age
|
||||
// return s
|
||||
// }
|
||||
// },
|
||||
// 42,
|
||||
// )(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func LetTo[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
b T,
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.LetTo[C](setter, b)
|
||||
}
|
||||
|
||||
// BindTo wraps a value in an initial state structure.
|
||||
// This is typically used to start a bind chain by converting a simple value
|
||||
// into a state structure.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S1: The state type to create
|
||||
// - T: The type of the input value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - setter: A function that creates a state from the value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, T, S1]: A function that wraps the value in state
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.BindTo[MyContext](func(name string) State {
|
||||
// return State{Name: name}
|
||||
// })(effect.Of[MyContext]("Alice"))
|
||||
//
|
||||
//go:inline
|
||||
func BindTo[C, S1, T any](
|
||||
setter func(T) S1,
|
||||
) Operator[C, T, S1] {
|
||||
return readerreaderioresult.BindTo[C](setter)
|
||||
}
|
||||
|
||||
// ApS applies an effect and binds its result to the state using a setter function.
|
||||
// This is similar to Bind but takes a pre-existing effect rather than a function
|
||||
// that creates an effect from the state.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - S1: The input state type
|
||||
// - S2: The output state type
|
||||
// - T: The type of value produced by the effect
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - setter: A function that takes the effect result and returns a state updater
|
||||
// - fa: The effect to apply
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S1, S2]: A function that transforms the state effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ageEffect := effect.Of[MyContext](30)
|
||||
// eff := effect.ApS(
|
||||
// func(age int) func(State) State {
|
||||
// return func(s State) State {
|
||||
// s.Age = age
|
||||
// return s
|
||||
// }
|
||||
// },
|
||||
// ageEffect,
|
||||
// )(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func ApS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Effect[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApS(setter, fa)
|
||||
}
|
||||
|
||||
// ApSL applies an effect and updates a field in the state using a lens.
|
||||
// This provides a more ergonomic way to update nested state structures.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - S: The state type
|
||||
// - T: The type of the field being updated
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - lens: A lens focusing on the field to update
|
||||
// - fa: The effect producing the new field value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S, S]: A function that updates the state field
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ageLens := lens.MakeLens(
|
||||
// func(s State) int { return s.Age },
|
||||
// func(s State, age int) State { s.Age = age; return s },
|
||||
// )
|
||||
// ageEffect := effect.Of[MyContext](30)
|
||||
// eff := effect.ApSL(ageLens, ageEffect)(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func ApSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa Effect[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApSL(lens, fa)
|
||||
}
|
||||
|
||||
// BindL executes an effectful computation on a field and updates it using a lens.
|
||||
// The effect function receives the current field value and produces a new value.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - S: The state type
|
||||
// - T: The type of the field being updated
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - lens: A lens focusing on the field to update
|
||||
// - f: An effectful function that transforms the field value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S, S]: A function that updates the state field
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ageLens := lens.MakeLens(
|
||||
// func(s State) int { return s.Age },
|
||||
// func(s State, age int) State { s.Age = age; return s },
|
||||
// )
|
||||
// eff := effect.BindL(
|
||||
// ageLens,
|
||||
// func(age int) Effect[MyContext, int] {
|
||||
// return effect.Of[MyContext](age + 1)
|
||||
// },
|
||||
// )(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func BindL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f func(T) Effect[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindL(lens, f)
|
||||
}
|
||||
|
||||
// LetL computes a new field value from the current value using a lens.
|
||||
// This is a pure transformation of a field within the state.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S: The state type
|
||||
// - T: The type of the field being updated
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - lens: A lens focusing on the field to update
|
||||
// - f: A pure function that transforms the field value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S, S]: A function that updates the state field
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ageLens := lens.MakeLens(
|
||||
// func(s State) int { return s.Age },
|
||||
// func(s State, age int) State { s.Age = age; return s },
|
||||
// )
|
||||
// eff := effect.LetL[MyContext](
|
||||
// ageLens,
|
||||
// func(age int) int { return age * 2 },
|
||||
// )(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func LetL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f func(T) T,
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.LetL[C](lens, f)
|
||||
}
|
||||
|
||||
// LetToL sets a field to a constant value using a lens.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - S: The state type
|
||||
// - T: The type of the field being updated
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - lens: A lens focusing on the field to update
|
||||
// - b: The constant value to set
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, S, S]: A function that updates the state field
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ageLens := lens.MakeLens(
|
||||
// func(s State) int { return s.Age },
|
||||
// func(s State, age int) State { s.Age = age; return s },
|
||||
// )
|
||||
// eff := effect.LetToL[MyContext](ageLens, 42)(stateEff)
|
||||
//
|
||||
//go:inline
|
||||
func LetToL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
b T,
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.LetToL[C](lens, b)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindIOEitherK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f ioeither.Kleisli[error, S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindIOEitherK[C](setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindIOResultK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f ioresult.Kleisli[S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindIOResultK[C](setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindIOK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f io.Kleisli[S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindIOK[C](setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindReaderK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f reader.Kleisli[C, S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindReaderK(setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindReaderIOK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f readerio.Kleisli[C, S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindReaderIOK(setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindEitherK[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
f either.Kleisli[error, S1, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.BindEitherK[C](setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindIOEitherKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f ioeither.Kleisli[error, T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindIOEitherKL[C](lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindIOKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f io.Kleisli[T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindIOKL[C](lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindReaderKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f reader.Kleisli[C, T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindReaderKL(lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func BindReaderIOKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f readerio.Kleisli[C, T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindReaderIOKL(lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApIOEitherS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa IOEither[error, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApIOEitherS[C](setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApIOS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa IO[T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApIOS[C](setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApReaderS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Reader[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApReaderS(setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApReaderIOS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa ReaderIO[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApReaderIOS(setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApEitherS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Either[error, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApEitherS[C](setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApIOEitherSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa IOEither[error, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApIOEitherSL[C](lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApIOSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa IO[T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApIOSL[C](lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApReaderSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa Reader[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApReaderSL(lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApReaderIOSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa ReaderIO[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApReaderIOSL(lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ApEitherSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa Either[error, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApEitherSL[C](lens, fa)
|
||||
}
|
||||
768
v2/effect/bind_test.go
Normal file
768
v2/effect/bind_test.go
Normal file
@@ -0,0 +1,768 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type BindState struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
func TestDo(t *testing.T) {
|
||||
t.Run("creates effect with initial state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 30}
|
||||
eff := Do[TestContext](initial)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, initial, result)
|
||||
})
|
||||
|
||||
t.Run("creates effect with empty struct", func(t *testing.T) {
|
||||
type Empty struct{}
|
||||
eff := Do[TestContext](Empty{})
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, Empty{}, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
t.Run("binds effect result to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("chains multiple binds", func(t *testing.T) {
|
||||
initial := BindState{}
|
||||
|
||||
eff := Bind(
|
||||
func(email string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Email = email
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, string] {
|
||||
return Of[TestContext]("alice@example.com")
|
||||
},
|
||||
)(Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext](30)
|
||||
},
|
||||
)(Bind(
|
||||
func(name string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Name = name
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, string] {
|
||||
return Of[TestContext]("Alice")
|
||||
},
|
||||
)(Do[TestContext](initial))))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
assert.Equal(t, "alice@example.com", result.Email)
|
||||
})
|
||||
|
||||
t.Run("propagates errors", func(t *testing.T) {
|
||||
expectedErr := errors.New("bind error")
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Fail[TestContext, int](expectedErr)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLet(t *testing.T) {
|
||||
t.Run("computes value and binds to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := Let[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) int {
|
||||
return len(s.Name) * 10
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 50, result.Age) // len("Alice") * 10
|
||||
})
|
||||
|
||||
t.Run("chains with Bind", func(t *testing.T) {
|
||||
initial := BindState{Name: "Bob"}
|
||||
|
||||
eff := Let[TestContext](
|
||||
func(email string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Email = email
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) string {
|
||||
return s.Name + "@example.com"
|
||||
},
|
||||
)(Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext](25)
|
||||
},
|
||||
)(Do[TestContext](initial)))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Bob", result.Name)
|
||||
assert.Equal(t, 25, result.Age)
|
||||
assert.Equal(t, "Bob@example.com", result.Email)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetTo(t *testing.T) {
|
||||
t.Run("binds constant value to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := LetTo[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
42,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 42, result.Age)
|
||||
})
|
||||
|
||||
t.Run("chains multiple LetTo", func(t *testing.T) {
|
||||
initial := BindState{}
|
||||
|
||||
eff := LetTo[TestContext](
|
||||
func(email string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Email = email
|
||||
return s
|
||||
}
|
||||
},
|
||||
"test@example.com",
|
||||
)(LetTo[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
30,
|
||||
)(LetTo[TestContext](
|
||||
func(name string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Name = name
|
||||
return s
|
||||
}
|
||||
},
|
||||
"Alice",
|
||||
)(Do[TestContext](initial))))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
assert.Equal(t, "test@example.com", result.Email)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindTo(t *testing.T) {
|
||||
t.Run("wraps value in state", func(t *testing.T) {
|
||||
type SimpleState struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
eff := BindTo[TestContext](func(v int) SimpleState {
|
||||
return SimpleState{Value: v}
|
||||
})(Of[TestContext](42))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, result.Value)
|
||||
})
|
||||
|
||||
t.Run("starts a bind chain", func(t *testing.T) {
|
||||
type State struct {
|
||||
X int
|
||||
Y string
|
||||
}
|
||||
|
||||
eff := Let[TestContext](
|
||||
func(y string) func(State) State {
|
||||
return func(s State) State {
|
||||
s.Y = y
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s State) string {
|
||||
return "computed"
|
||||
},
|
||||
)(BindTo[TestContext](func(x int) State {
|
||||
return State{X: x}
|
||||
})(Of[TestContext](10)))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 10, result.X)
|
||||
assert.Equal(t, "computed", result.Y)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApS(t *testing.T) {
|
||||
t.Run("applies effect and binds result to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
ageEffect := Of[TestContext](30)
|
||||
|
||||
eff := ApS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
ageEffect,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("propagates errors from applied effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("aps error")
|
||||
initial := BindState{Name: "Alice"}
|
||||
ageEffect := Fail[TestContext, int](expectedErr)
|
||||
|
||||
eff := ApS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
ageEffect,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindIOK(t *testing.T) {
|
||||
t.Run("binds IO operation to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindIOK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) io.IO[int] {
|
||||
return func() int {
|
||||
return 30
|
||||
}
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindIOEitherK(t *testing.T) {
|
||||
t.Run("binds successful IOEither to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindIOEitherK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) ioeither.IOEither[error, int] {
|
||||
return ioeither.Of[error](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("propagates IOEither error", func(t *testing.T) {
|
||||
expectedErr := errors.New("ioeither error")
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindIOEitherK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) ioeither.IOEither[error, int] {
|
||||
return ioeither.Left[int](expectedErr)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindIOResultK(t *testing.T) {
|
||||
t.Run("binds successful IOResult to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindIOResultK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) ioresult.IOResult[int] {
|
||||
return ioresult.Of(30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindReaderK(t *testing.T) {
|
||||
t.Run("binds Reader operation to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindReaderK(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) reader.Reader[TestContext, int] {
|
||||
return func(ctx TestContext) int {
|
||||
return 30
|
||||
}
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindReaderIOK(t *testing.T) {
|
||||
t.Run("binds ReaderIO operation to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindReaderIOK(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) readerio.ReaderIO[TestContext, int] {
|
||||
return func(ctx TestContext) io.IO[int] {
|
||||
return func() int {
|
||||
return 30
|
||||
}
|
||||
}
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindEitherK(t *testing.T) {
|
||||
t.Run("binds successful Either to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindEitherK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) either.Either[error, int] {
|
||||
return either.Of[error](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("propagates Either error", func(t *testing.T) {
|
||||
expectedErr := errors.New("either error")
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindEitherK[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s BindState) either.Either[error, int] {
|
||||
return either.Left[int](expectedErr)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLensOperations(t *testing.T) {
|
||||
// Create lenses for BindState
|
||||
nameLens := lens.MakeLens(
|
||||
func(s BindState) string { return s.Name },
|
||||
func(s BindState, name string) BindState {
|
||||
s.Name = name
|
||||
return s
|
||||
},
|
||||
)
|
||||
|
||||
ageLens := lens.MakeLens(
|
||||
func(s BindState) int { return s.Age },
|
||||
func(s BindState, age int) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
},
|
||||
)
|
||||
|
||||
t.Run("ApSL applies effect using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
ageEffect := Of[TestContext](30)
|
||||
|
||||
eff := ApSL(ageLens, ageEffect)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("BindL binds effect using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
|
||||
eff := BindL(
|
||||
ageLens,
|
||||
func(age int) Effect[TestContext, int] {
|
||||
return Of[TestContext](age + 5)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("LetL computes value using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
|
||||
eff := LetL[TestContext](
|
||||
ageLens,
|
||||
func(age int) int {
|
||||
return age * 2
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 50, result.Age)
|
||||
})
|
||||
|
||||
t.Run("LetToL sets constant using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
|
||||
eff := LetToL[TestContext](ageLens, 100)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 100, result.Age)
|
||||
})
|
||||
|
||||
t.Run("chains lens operations", func(t *testing.T) {
|
||||
initial := BindState{}
|
||||
|
||||
eff := LetToL[TestContext](
|
||||
ageLens,
|
||||
30,
|
||||
)(LetToL[TestContext](
|
||||
nameLens,
|
||||
"Bob",
|
||||
)(Do[TestContext](initial)))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Bob", result.Name)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApOperations(t *testing.T) {
|
||||
t.Run("ApIOS applies IO effect", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
ioEffect := func() int { return 30 }
|
||||
|
||||
eff := ApIOS[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
ioEffect,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("ApReaderS applies Reader effect", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
readerEffect := func(ctx TestContext) int { return 30 }
|
||||
|
||||
eff := ApReaderS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
readerEffect,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
|
||||
t.Run("ApEitherS applies Either effect", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
eitherEffect := either.Of[error](30)
|
||||
|
||||
eff := ApEitherS[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
eitherEffect,
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result.Age)
|
||||
})
|
||||
}
|
||||
|
||||
func TestComplexBindChain(t *testing.T) {
|
||||
t.Run("builds complex state with multiple operations", func(t *testing.T) {
|
||||
type ComplexState struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
IsAdmin bool
|
||||
Score int
|
||||
}
|
||||
|
||||
eff := LetTo[TestContext](
|
||||
func(score int) func(ComplexState) ComplexState {
|
||||
return func(s ComplexState) ComplexState {
|
||||
s.Score = score
|
||||
return s
|
||||
}
|
||||
},
|
||||
100,
|
||||
)(Let[TestContext](
|
||||
func(isAdmin bool) func(ComplexState) ComplexState {
|
||||
return func(s ComplexState) ComplexState {
|
||||
s.IsAdmin = isAdmin
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s ComplexState) bool {
|
||||
return s.Age >= 18
|
||||
},
|
||||
)(Let[TestContext](
|
||||
func(email string) func(ComplexState) ComplexState {
|
||||
return func(s ComplexState) ComplexState {
|
||||
s.Email = email
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s ComplexState) string {
|
||||
return s.Name + "@example.com"
|
||||
},
|
||||
)(Bind(
|
||||
func(age int) func(ComplexState) ComplexState {
|
||||
return func(s ComplexState) ComplexState {
|
||||
s.Age = age
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s ComplexState) Effect[TestContext, int] {
|
||||
return Of[TestContext](25)
|
||||
},
|
||||
)(BindTo[TestContext](func(name string) ComplexState {
|
||||
return ComplexState{Name: name}
|
||||
})(Of[TestContext]("Alice"))))))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Alice", result.Name)
|
||||
assert.Equal(t, 25, result.Age)
|
||||
assert.Equal(t, "Alice@example.com", result.Email)
|
||||
assert.True(t, result.IsAdmin)
|
||||
assert.Equal(t, 100, result.Score)
|
||||
})
|
||||
}
|
||||
32
v2/effect/common_test.go
Normal file
32
v2/effect/common_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// TestContext is a common test context type used across effect tests
|
||||
type TestContext struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
// runEffect is a helper function to run an effect with a context and return the result
|
||||
func runEffect[C, A any](eff Effect[C, A], ctx C) (A, error) {
|
||||
ioResult := Provide[A](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
return readerResult(context.Background())
|
||||
}
|
||||
276
v2/effect/dependencies.go
Normal file
276
v2/effect/dependencies.go
Normal file
@@ -0,0 +1,276 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
thunk "github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// Local transforms the context required by an effect using a pure function.
|
||||
// This allows you to adapt an effect that requires one context type to work
|
||||
// with a different context type by providing a transformation function.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C1: The outer context type (what you have)
|
||||
// - C2: The inner context type (what the effect needs)
|
||||
// - A: The value type produced by the effect
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - acc: A pure function that transforms C1 to C2
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Kleisli[C1, Effect[C2, A], A]: A function that adapts the effect to use C1
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// type AppConfig struct { DB DatabaseConfig }
|
||||
// type DatabaseConfig struct { Host string }
|
||||
// dbEffect := effect.Of[DatabaseConfig]("connected")
|
||||
// appEffect := effect.Local[AppConfig, DatabaseConfig, string](
|
||||
// func(app AppConfig) DatabaseConfig { return app.DB },
|
||||
// )(dbEffect)
|
||||
//
|
||||
//go:inline
|
||||
func Local[A, C1, C2 any](acc Reader[C1, C2]) Kleisli[C1, Effect[C2, A], A] {
|
||||
return readerreaderioresult.Local[A](acc)
|
||||
}
|
||||
|
||||
// Contramap is an alias for Local, following the contravariant functor naming convention.
|
||||
// It transforms the context required by an effect using a pure function.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C1: The outer context type (what you have)
|
||||
// - C2: The inner context type (what the effect needs)
|
||||
// - A: The value type produced by the effect
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - acc: A pure function that transforms C1 to C2
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Kleisli[C1, Effect[C2, A], A]: A function that adapts the effect to use C1
|
||||
//
|
||||
//go:inline
|
||||
func Contramap[A, C1, C2 any](acc Reader[C1, C2]) Kleisli[C1, Effect[C2, A], A] {
|
||||
return readerreaderioresult.Local[A](acc)
|
||||
}
|
||||
|
||||
// LocalIOK transforms the context using an IO-based function.
|
||||
// This allows the context transformation itself to perform I/O operations.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The value type produced by the effect
|
||||
// - C1: The inner context type (what the effect needs)
|
||||
// - C2: The outer context type (what you have)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: An IO function that transforms C2 to C1
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C1, A]) Effect[C2, A]: A function that adapts the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// loadConfig := func(path string) io.IO[Config] {
|
||||
// return func() Config { /* load from file */ }
|
||||
// }
|
||||
// transform := effect.LocalIOK[string](loadConfig)
|
||||
// adapted := transform(configEffect)
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOK[A, C1, C2 any](f io.Kleisli[C2, C1]) func(Effect[C1, A]) Effect[C2, A] {
|
||||
return readerreaderioresult.LocalIOK[A](f)
|
||||
}
|
||||
|
||||
// LocalIOResultK transforms the context using an IOResult-based function.
|
||||
// This allows the context transformation to perform I/O and handle errors.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The value type produced by the effect
|
||||
// - C1: The inner context type (what the effect needs)
|
||||
// - C2: The outer context type (what you have)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: An IOResult function that transforms C2 to C1
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C1, A]) Effect[C2, A]: A function that adapts the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// loadConfig := func(path string) ioresult.IOResult[Config] {
|
||||
// return func() result.Result[Config] {
|
||||
// // load from file, may fail
|
||||
// }
|
||||
// }
|
||||
// transform := effect.LocalIOResultK[string](loadConfig)
|
||||
// adapted := transform(configEffect)
|
||||
//
|
||||
//go:inline
|
||||
func LocalIOResultK[A, C1, C2 any](f ioresult.Kleisli[C2, C1]) func(Effect[C1, A]) Effect[C2, A] {
|
||||
return readerreaderioresult.LocalIOResultK[A](f)
|
||||
}
|
||||
|
||||
// LocalResultK transforms the context using a Result-based function.
|
||||
// This allows the context transformation to fail with an error.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The value type produced by the effect
|
||||
// - C1: The inner context type (what the effect needs)
|
||||
// - C2: The outer context type (what you have)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A Result function that transforms C2 to C1
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C1, A]) Effect[C2, A]: A function that adapts the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// validateConfig := func(raw RawConfig) result.Result[Config] {
|
||||
// if raw.IsValid() {
|
||||
// return result.Of(raw.ToConfig())
|
||||
// }
|
||||
// return result.Left[Config](errors.New("invalid"))
|
||||
// }
|
||||
// transform := effect.LocalResultK[string](validateConfig)
|
||||
// adapted := transform(configEffect)
|
||||
//
|
||||
//go:inline
|
||||
func LocalResultK[A, C1, C2 any](f result.Kleisli[C2, C1]) func(Effect[C1, A]) Effect[C2, A] {
|
||||
return readerreaderioresult.LocalResultK[A](f)
|
||||
}
|
||||
|
||||
// LocalThunkK transforms the context using a Thunk (ReaderIOResult) function.
|
||||
// This allows the context transformation to depend on context.Context, perform I/O, and handle errors.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The value type produced by the effect
|
||||
// - C1: The inner context type (what the effect needs)
|
||||
// - C2: The outer context type (what you have)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A Thunk function that transforms C2 to C1
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C1, A]) Effect[C2, A]: A function that adapts the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// loadConfig := func(path string) readerioresult.ReaderIOResult[Config] {
|
||||
// return func(ctx context.Context) ioresult.IOResult[Config] {
|
||||
// // load from file with context, may fail
|
||||
// }
|
||||
// }
|
||||
// transform := effect.LocalThunkK[string](loadConfig)
|
||||
// adapted := transform(configEffect)
|
||||
//
|
||||
//go:inline
|
||||
func LocalThunkK[A, C1, C2 any](f thunk.Kleisli[C2, C1]) func(Effect[C1, A]) Effect[C2, A] {
|
||||
return readerreaderioresult.LocalReaderIOResultK[A](f)
|
||||
}
|
||||
|
||||
// LocalEffectK transforms the context of an Effect using an Effect-returning function.
|
||||
// This is the most powerful context transformation function, allowing the transformation
|
||||
// itself to be effectful (can fail, perform I/O, and access the outer context).
|
||||
//
|
||||
// LocalEffectK takes a Kleisli arrow that:
|
||||
// - Accepts the outer context C2
|
||||
// - Returns an Effect that produces the inner context C1
|
||||
// - Can fail with an error during context transformation
|
||||
// - Can perform I/O operations during transformation
|
||||
//
|
||||
// This is useful when:
|
||||
// - Context transformation requires I/O (e.g., loading config from a file)
|
||||
// - Context transformation can fail (e.g., validating or parsing context)
|
||||
// - Context transformation needs to access the outer context
|
||||
//
|
||||
// Type Parameters:
|
||||
// - A: The value type produced by the effect
|
||||
// - C1: The inner context type (required by the original effect)
|
||||
// - C2: The outer context type (provided to the transformed effect)
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A Kleisli arrow (C2 -> Effect[C2, C1]) that transforms C2 to C1 effectfully
|
||||
//
|
||||
// Returns:
|
||||
// - A function that transforms Effect[C1, A] to Effect[C2, A]
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type DatabaseConfig struct {
|
||||
// ConnectionString string
|
||||
// }
|
||||
//
|
||||
// type AppConfig struct {
|
||||
// ConfigPath string
|
||||
// }
|
||||
//
|
||||
// // Effect that needs DatabaseConfig
|
||||
// dbEffect := effect.Of[DatabaseConfig, string]("query result")
|
||||
//
|
||||
// // Transform AppConfig to DatabaseConfig effectfully
|
||||
// // (e.g., load config from file, which can fail)
|
||||
// loadConfig := func(app AppConfig) Effect[AppConfig, DatabaseConfig] {
|
||||
// return effect.Chain[AppConfig](func(_ AppConfig) Effect[AppConfig, DatabaseConfig] {
|
||||
// // Simulate loading config from file (can fail)
|
||||
// return effect.Of[AppConfig, DatabaseConfig](DatabaseConfig{
|
||||
// ConnectionString: "loaded from " + app.ConfigPath,
|
||||
// })
|
||||
// })(effect.Of[AppConfig, AppConfig](app))
|
||||
// }
|
||||
//
|
||||
// // Apply the transformation
|
||||
// transform := effect.LocalEffectK[string, DatabaseConfig, AppConfig](loadConfig)
|
||||
// appEffect := transform(dbEffect)
|
||||
//
|
||||
// // Run with AppConfig
|
||||
// ioResult := effect.Provide(AppConfig{ConfigPath: "/etc/app.conf"})(appEffect)
|
||||
// readerResult := effect.RunSync(ioResult)
|
||||
// result, err := readerResult(context.Background())
|
||||
//
|
||||
// Comparison with other Local functions:
|
||||
// - Local/Contramap: Pure context transformation (C2 -> C1)
|
||||
// - LocalIOK: IO-based transformation (C2 -> IO[C1])
|
||||
// - LocalIOResultK: IO with error handling (C2 -> IOResult[C1])
|
||||
// - LocalReaderIOResultK: Reader-based with IO and errors (C2 -> ReaderIOResult[C1])
|
||||
// - LocalEffectK: Full Effect transformation (C2 -> Effect[C2, C1])
|
||||
//
|
||||
//go:inline
|
||||
func LocalEffectK[A, C1, C2 any](f Kleisli[C2, C2, C1]) func(Effect[C1, A]) Effect[C2, A] {
|
||||
return readerreaderioresult.LocalReaderReaderIOEitherK[A](f)
|
||||
}
|
||||
620
v2/effect/dependencies_test.go
Normal file
620
v2/effect/dependencies_test.go
Normal file
@@ -0,0 +1,620 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type OuterContext struct {
|
||||
Value string
|
||||
Number int
|
||||
}
|
||||
|
||||
type InnerContext struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func TestLocal(t *testing.T) {
|
||||
t.Run("transforms context for inner effect", func(t *testing.T) {
|
||||
// Create an effect that uses InnerContext
|
||||
innerEffect := Of[InnerContext]("result")
|
||||
|
||||
// Transform OuterContext to InnerContext
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value}
|
||||
}
|
||||
|
||||
// Apply Local to transform the context
|
||||
kleisli := Local[string](accessor)
|
||||
outerEffect := kleisli(innerEffect)
|
||||
|
||||
// Run with OuterContext
|
||||
ioResult := Provide[string](OuterContext{
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", result)
|
||||
})
|
||||
|
||||
t.Run("allows accessing outer context fields", func(t *testing.T) {
|
||||
// Create an effect that reads from InnerContext
|
||||
innerEffect := Chain(func(_ string) Effect[InnerContext, string] {
|
||||
return Of[InnerContext]("inner value")
|
||||
})(Of[InnerContext]("start"))
|
||||
|
||||
// Transform context
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value + " transformed"}
|
||||
}
|
||||
|
||||
kleisli := Local[string](accessor)
|
||||
outerEffect := kleisli(innerEffect)
|
||||
|
||||
// Run with OuterContext
|
||||
ioResult := Provide[string](OuterContext{
|
||||
Value: "original",
|
||||
Number: 100,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "inner value", result)
|
||||
})
|
||||
|
||||
t.Run("propagates errors from inner effect", func(t *testing.T) {
|
||||
expectedErr := assert.AnError
|
||||
innerEffect := Fail[InnerContext, string](expectedErr)
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value}
|
||||
}
|
||||
|
||||
kleisli := Local[string](accessor)
|
||||
outerEffect := kleisli(innerEffect)
|
||||
|
||||
ioResult := Provide[string](OuterContext{
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Local transformations", func(t *testing.T) {
|
||||
type Level1 struct {
|
||||
A string
|
||||
}
|
||||
type Level2 struct {
|
||||
B string
|
||||
}
|
||||
type Level3 struct {
|
||||
C string
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
level3Effect := Of[Level3]("deep result")
|
||||
|
||||
// Transform Level2 -> Level3
|
||||
local23 := Local[string](func(l2 Level2) Level3 {
|
||||
return Level3{C: l2.B + "-c"}
|
||||
})
|
||||
|
||||
// Transform Level1 -> Level2
|
||||
local12 := Local[string](func(l1 Level1) Level2 {
|
||||
return Level2{B: l1.A + "-b"}
|
||||
})
|
||||
|
||||
// Compose transformations
|
||||
level2Effect := local23(level3Effect)
|
||||
level1Effect := local12(level2Effect)
|
||||
|
||||
// Run with Level1 context
|
||||
ioResult := Provide[string](Level1{A: "a"})(level1Effect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "deep result", result)
|
||||
})
|
||||
|
||||
t.Run("works with complex context transformations", func(t *testing.T) {
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
Database string
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
DB DatabaseConfig
|
||||
APIKey string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
// Effect that needs only DatabaseConfig
|
||||
dbEffect := Of[DatabaseConfig]("connected")
|
||||
|
||||
// Extract DB config from AppConfig
|
||||
accessor := func(app AppConfig) DatabaseConfig {
|
||||
return app.DB
|
||||
}
|
||||
|
||||
kleisli := Local[string](accessor)
|
||||
appEffect := kleisli(dbEffect)
|
||||
|
||||
// Run with full AppConfig
|
||||
ioResult := Provide[string](AppConfig{
|
||||
DB: DatabaseConfig{
|
||||
Host: "localhost",
|
||||
Port: 5432,
|
||||
Database: "mydb",
|
||||
},
|
||||
APIKey: "secret",
|
||||
Timeout: 30,
|
||||
})(appEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "connected", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestContramap(t *testing.T) {
|
||||
t.Run("is equivalent to Local", func(t *testing.T) {
|
||||
innerEffect := Of[InnerContext](42)
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value}
|
||||
}
|
||||
|
||||
// Test Local
|
||||
localKleisli := Local[int](accessor)
|
||||
localEffect := localKleisli(innerEffect)
|
||||
|
||||
// Test Contramap
|
||||
contramapKleisli := Contramap[int](accessor)
|
||||
contramapEffect := contramapKleisli(innerEffect)
|
||||
|
||||
outerCtx := OuterContext{Value: "test", Number: 100}
|
||||
|
||||
// Run both
|
||||
localIO := Provide[int](outerCtx)(localEffect)
|
||||
localReader := RunSync(localIO)
|
||||
localResult, localErr := localReader(context.Background())
|
||||
|
||||
contramapIO := Provide[int](outerCtx)(contramapEffect)
|
||||
contramapReader := RunSync(contramapIO)
|
||||
contramapResult, contramapErr := contramapReader(context.Background())
|
||||
|
||||
assert.NoError(t, localErr)
|
||||
assert.NoError(t, contramapErr)
|
||||
assert.Equal(t, localResult, contramapResult)
|
||||
})
|
||||
|
||||
t.Run("transforms context correctly", func(t *testing.T) {
|
||||
innerEffect := Of[InnerContext]("success")
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value + " modified"}
|
||||
}
|
||||
|
||||
kleisli := Contramap[string](accessor)
|
||||
outerEffect := kleisli(innerEffect)
|
||||
|
||||
ioResult := Provide[string](OuterContext{
|
||||
Value: "original",
|
||||
Number: 50,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success", result)
|
||||
})
|
||||
|
||||
t.Run("handles errors from inner effect", func(t *testing.T) {
|
||||
expectedErr := assert.AnError
|
||||
innerEffect := Fail[InnerContext, int](expectedErr)
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value}
|
||||
}
|
||||
|
||||
kleisli := Contramap[int](accessor)
|
||||
outerEffect := kleisli(innerEffect)
|
||||
|
||||
ioResult := Provide[int](OuterContext{
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalAndContramapInteroperability(t *testing.T) {
|
||||
t.Run("can be used interchangeably", func(t *testing.T) {
|
||||
type Config1 struct {
|
||||
Value string
|
||||
}
|
||||
type Config2 struct {
|
||||
Data string
|
||||
}
|
||||
type Config3 struct {
|
||||
Info string
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
effect3 := Of[Config3]("result")
|
||||
|
||||
// Use Local for first transformation
|
||||
local23 := Local[string](func(c2 Config2) Config3 {
|
||||
return Config3{Info: c2.Data}
|
||||
})
|
||||
|
||||
// Use Contramap for second transformation
|
||||
contramap12 := Contramap[string](func(c1 Config1) Config2 {
|
||||
return Config2{Data: c1.Value}
|
||||
})
|
||||
|
||||
// Compose them
|
||||
effect2 := local23(effect3)
|
||||
effect1 := contramap12(effect2)
|
||||
|
||||
// Run
|
||||
ioResult := Provide[string](Config1{Value: "test"})(effect1)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocalEffectK(t *testing.T) {
|
||||
t.Run("transforms context using effectful function", func(t *testing.T) {
|
||||
type DatabaseConfig struct {
|
||||
ConnectionString string
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
// Effect that needs DatabaseConfig
|
||||
dbEffect := Of[DatabaseConfig]("query result")
|
||||
|
||||
// Transform AppConfig to DatabaseConfig effectfully
|
||||
loadConfig := func(app AppConfig) Effect[AppConfig, DatabaseConfig] {
|
||||
return Of[AppConfig](DatabaseConfig{
|
||||
ConnectionString: "loaded from " + app.ConfigPath,
|
||||
})
|
||||
}
|
||||
|
||||
// Apply the transformation
|
||||
transform := LocalEffectK[string](loadConfig)
|
||||
appEffect := transform(dbEffect)
|
||||
|
||||
// Run with AppConfig
|
||||
ioResult := Provide[string](AppConfig{
|
||||
ConfigPath: "/etc/app.conf",
|
||||
})(appEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "query result", result)
|
||||
})
|
||||
|
||||
t.Run("propagates errors from context transformation", func(t *testing.T) {
|
||||
type InnerCtx struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
type OuterCtx struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
innerEffect := Of[InnerCtx]("success")
|
||||
|
||||
expectedErr := assert.AnError
|
||||
// Context transformation that fails
|
||||
failingTransform := func(outer OuterCtx) Effect[OuterCtx, InnerCtx] {
|
||||
return Fail[OuterCtx, InnerCtx](expectedErr)
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string](failingTransform)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
ioResult := Provide[string](OuterCtx{Path: "test"})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("propagates errors from inner effect", func(t *testing.T) {
|
||||
type InnerCtx struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
type OuterCtx struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
expectedErr := assert.AnError
|
||||
innerEffect := Fail[InnerCtx, string](expectedErr)
|
||||
|
||||
// Successful context transformation
|
||||
transform := func(outer OuterCtx) Effect[OuterCtx, InnerCtx] {
|
||||
return Of[OuterCtx](InnerCtx{Value: outer.Path})
|
||||
}
|
||||
|
||||
transformK := LocalEffectK[string](transform)
|
||||
outerEffect := transformK(innerEffect)
|
||||
|
||||
ioResult := Provide[string](OuterCtx{Path: "test"})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("allows effectful context transformation with IO operations", func(t *testing.T) {
|
||||
type Config struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
type AppContext struct {
|
||||
ConfigFile string
|
||||
}
|
||||
|
||||
// Effect that uses Config
|
||||
configEffect := Chain(func(cfg Config) Effect[Config, string] {
|
||||
return Of[Config]("processed: " + cfg.Data)
|
||||
})(readerreaderioresult.Ask[Config]())
|
||||
|
||||
// Effectful transformation that simulates loading config
|
||||
loadConfigEffect := func(app AppContext) Effect[AppContext, Config] {
|
||||
// Simulate IO operation (e.g., reading file)
|
||||
return Of[AppContext](Config{
|
||||
Data: "loaded from " + app.ConfigFile,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string](loadConfigEffect)
|
||||
appEffect := transform(configEffect)
|
||||
|
||||
ioResult := Provide[string](AppContext{
|
||||
ConfigFile: "config.json",
|
||||
})(appEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "processed: loaded from config.json", result)
|
||||
})
|
||||
|
||||
t.Run("chains multiple LocalEffectK transformations", func(t *testing.T) {
|
||||
type Level1 struct {
|
||||
A string
|
||||
}
|
||||
type Level2 struct {
|
||||
B string
|
||||
}
|
||||
type Level3 struct {
|
||||
C string
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
level3Effect := Of[Level3]("deep result")
|
||||
|
||||
// Transform Level2 -> Level3 effectfully
|
||||
transform23 := LocalEffectK[string](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2](Level3{C: l2.B + "-c"})
|
||||
})
|
||||
|
||||
// Transform Level1 -> Level2 effectfully
|
||||
transform12 := LocalEffectK[string](func(l1 Level1) Effect[Level1, Level2] {
|
||||
return Of[Level1](Level2{B: l1.A + "-b"})
|
||||
})
|
||||
|
||||
// Compose transformations
|
||||
level2Effect := transform23(level3Effect)
|
||||
level1Effect := transform12(level2Effect)
|
||||
|
||||
// Run with Level1 context
|
||||
ioResult := Provide[string](Level1{A: "a"})(level1Effect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "deep result", result)
|
||||
})
|
||||
|
||||
t.Run("accesses outer context during transformation", func(t *testing.T) {
|
||||
type DatabaseConfig struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Environment string
|
||||
DBHost string
|
||||
DBPort int
|
||||
}
|
||||
|
||||
// Effect that needs DatabaseConfig
|
||||
dbEffect := Chain(func(cfg DatabaseConfig) Effect[DatabaseConfig, string] {
|
||||
return Of[DatabaseConfig](fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
|
||||
})(readerreaderioresult.Ask[DatabaseConfig]())
|
||||
|
||||
// Transform using outer context
|
||||
transformWithContext := func(app AppConfig) Effect[AppConfig, DatabaseConfig] {
|
||||
// Access outer context to build inner context
|
||||
prefix := ""
|
||||
if app.Environment == "prod" {
|
||||
prefix = "prod-"
|
||||
}
|
||||
return Of[AppConfig](DatabaseConfig{
|
||||
Host: prefix + app.DBHost,
|
||||
Port: app.DBPort,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string](transformWithContext)
|
||||
appEffect := transform(dbEffect)
|
||||
|
||||
ioResult := Provide[string](AppConfig{
|
||||
Environment: "prod",
|
||||
DBHost: "localhost",
|
||||
DBPort: 5432,
|
||||
})(appEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, result, "prod-localhost")
|
||||
})
|
||||
|
||||
t.Run("validates context during transformation", func(t *testing.T) {
|
||||
type ValidatedConfig struct {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
type RawConfig struct {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
innerEffect := Of[ValidatedConfig]("success")
|
||||
|
||||
// Validation that can fail
|
||||
validateConfig := func(raw RawConfig) Effect[RawConfig, ValidatedConfig] {
|
||||
if raw.APIKey == "" {
|
||||
return Fail[RawConfig, ValidatedConfig](assert.AnError)
|
||||
}
|
||||
return Of[RawConfig](ValidatedConfig{
|
||||
APIKey: raw.APIKey,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string](validateConfig)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
// Test with invalid config
|
||||
ioResult := Provide[string](RawConfig{APIKey: ""})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
// Test with valid config
|
||||
ioResult2 := Provide[string](RawConfig{APIKey: "valid-key"})(outerEffect)
|
||||
readerResult2 := RunSync(ioResult2)
|
||||
result, err2 := readerResult2(context.Background())
|
||||
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, "success", result)
|
||||
})
|
||||
|
||||
t.Run("composes with other Local functions", func(t *testing.T) {
|
||||
type Level1 struct {
|
||||
Value string
|
||||
}
|
||||
type Level2 struct {
|
||||
Data string
|
||||
}
|
||||
type Level3 struct {
|
||||
Info string
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
effect3 := Of[Level3]("result")
|
||||
|
||||
// Use LocalEffectK for first transformation (effectful)
|
||||
localEffectK23 := LocalEffectK[string](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2](Level3{Info: l2.Data})
|
||||
})
|
||||
|
||||
// Use Local for second transformation (pure)
|
||||
local12 := Local[string](func(l1 Level1) Level2 {
|
||||
return Level2{Data: l1.Value}
|
||||
})
|
||||
|
||||
// Compose them
|
||||
effect2 := localEffectK23(effect3)
|
||||
effect1 := local12(effect2)
|
||||
|
||||
// Run
|
||||
ioResult := Provide[string](Level1{Value: "test"})(effect1)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", result)
|
||||
})
|
||||
|
||||
t.Run("handles complex nested effects in transformation", func(t *testing.T) {
|
||||
type InnerCtx struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
type OuterCtx struct {
|
||||
Multiplier int
|
||||
}
|
||||
|
||||
// Effect that uses InnerCtx
|
||||
innerEffect := Chain(func(ctx InnerCtx) Effect[InnerCtx, int] {
|
||||
return Of[InnerCtx](ctx.Value * 2)
|
||||
})(readerreaderioresult.Ask[InnerCtx]())
|
||||
|
||||
// Complex transformation with nested effects
|
||||
complexTransform := func(outer OuterCtx) Effect[OuterCtx, InnerCtx] {
|
||||
return Of[OuterCtx](InnerCtx{
|
||||
Value: outer.Multiplier * 10,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[int](complexTransform)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
ioResult := Provide[int](OuterCtx{Multiplier: 3})(outerEffect)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 60, result) // 3 * 10 * 2
|
||||
})
|
||||
}
|
||||
222
v2/effect/doc.go
Normal file
222
v2/effect/doc.go
Normal file
@@ -0,0 +1,222 @@
|
||||
// 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 effect provides a functional effect system for managing side effects in Go.
|
||||
|
||||
# Overview
|
||||
|
||||
The effect package is a high-level abstraction for composing effectful computations
|
||||
that may fail, require dependencies (context), and perform I/O operations. It is built
|
||||
on top of ReaderReaderIOResult, providing a clean API for dependency injection and
|
||||
error handling.
|
||||
|
||||
# Naming Conventions
|
||||
|
||||
The naming conventions in this package are modeled after effect-ts (https://effect.website/),
|
||||
a popular TypeScript library for functional effect systems. This alignment helps developers
|
||||
familiar with effect-ts to quickly understand and use this Go implementation.
|
||||
|
||||
# Core Type
|
||||
|
||||
The central type is Effect[C, A], which represents:
|
||||
- C: The context/dependency type required by the effect
|
||||
- A: The success value type produced by the effect
|
||||
|
||||
An Effect can:
|
||||
- Succeed with a value of type A
|
||||
- Fail with an error
|
||||
- Require a context of type C
|
||||
- Perform I/O operations
|
||||
|
||||
# Basic Operations
|
||||
|
||||
Creating Effects:
|
||||
|
||||
// Create a successful effect
|
||||
effect.Succeed[MyContext, string]("hello")
|
||||
|
||||
// Create a failed effect
|
||||
effect.Fail[MyContext, string](errors.New("failed"))
|
||||
|
||||
// Lift a pure value into an effect
|
||||
effect.Of[MyContext, int](42)
|
||||
|
||||
Transforming Effects:
|
||||
|
||||
// Map over the success value
|
||||
effect.Map[MyContext](func(x int) string {
|
||||
return strconv.Itoa(x)
|
||||
})
|
||||
|
||||
// Chain effects together (flatMap)
|
||||
effect.Chain[MyContext](func(x int) Effect[MyContext, string] {
|
||||
return effect.Succeed[MyContext, string](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
// Tap into an effect without changing its value
|
||||
effect.Tap[MyContext](func(x int) Effect[MyContext, any] {
|
||||
return effect.Succeed[MyContext, any](fmt.Println(x))
|
||||
})
|
||||
|
||||
# Dependency Injection
|
||||
|
||||
Effects can access their required context:
|
||||
|
||||
// Transform the context before passing it to an effect
|
||||
effect.Local[OuterCtx, InnerCtx](func(outer OuterCtx) InnerCtx {
|
||||
return outer.Inner
|
||||
})
|
||||
|
||||
// Provide a context to run an effect
|
||||
effect.Provide[MyContext, string](myContext)
|
||||
|
||||
# Do Notation
|
||||
|
||||
The package provides "do notation" for composing effects in a sequential, imperative style:
|
||||
|
||||
type State struct {
|
||||
X int
|
||||
Y string
|
||||
}
|
||||
|
||||
result := effect.Do[MyContext](State{}).
|
||||
Bind(func(y string) func(State) State {
|
||||
return func(s State) State {
|
||||
s.Y = y
|
||||
return s
|
||||
}
|
||||
}, fetchString).
|
||||
Let(func(x int) func(State) State {
|
||||
return func(s State) State {
|
||||
s.X = x
|
||||
return s
|
||||
}
|
||||
}, func(s State) int {
|
||||
return len(s.Y)
|
||||
})
|
||||
|
||||
# Bind Operations
|
||||
|
||||
The package provides various bind operations for integrating with other effect types:
|
||||
|
||||
- BindIOK: Bind an IO operation
|
||||
- BindIOEitherK: Bind an IOEither operation
|
||||
- BindIOResultK: Bind an IOResult operation
|
||||
- BindReaderK: Bind a Reader operation
|
||||
- BindReaderIOK: Bind a ReaderIO operation
|
||||
- BindEitherK: Bind an Either operation
|
||||
|
||||
Each bind operation has a corresponding "L" variant for working with lenses:
|
||||
- BindL, BindIOKL, BindReaderKL, etc.
|
||||
|
||||
# Applicative Operations
|
||||
|
||||
Apply effects in parallel:
|
||||
|
||||
// Apply a function effect to a value effect
|
||||
effect.Ap[string, MyContext](valueEffect)(functionEffect)
|
||||
|
||||
// Apply effects to build up a structure
|
||||
effect.ApS[MyContext](setter, effect1)
|
||||
|
||||
# Traversal
|
||||
|
||||
Traverse collections with effects:
|
||||
|
||||
// Map an array with an effectful function
|
||||
effect.TraverseArray[MyContext](func(x int) Effect[MyContext, string] {
|
||||
return effect.Succeed[MyContext, string](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
# Retry Logic
|
||||
|
||||
Retry effects with configurable policies:
|
||||
|
||||
effect.Retrying[MyContext, string](
|
||||
retryPolicy,
|
||||
func(status retry.RetryStatus) Effect[MyContext, string] {
|
||||
return fetchData()
|
||||
},
|
||||
func(result Result[string]) bool {
|
||||
return result.IsLeft() // retry on error
|
||||
},
|
||||
)
|
||||
|
||||
# Monoids
|
||||
|
||||
Combine effects using monoid operations:
|
||||
|
||||
// Combine effects using applicative semantics
|
||||
effect.ApplicativeMonoid[MyContext](stringMonoid)
|
||||
|
||||
// Combine effects using alternative semantics (first success)
|
||||
effect.AlternativeMonoid[MyContext](stringMonoid)
|
||||
|
||||
# Running Effects
|
||||
|
||||
To execute an effect:
|
||||
|
||||
// Provide the context
|
||||
ioResult := effect.Provide[MyContext, string](myContext)(myEffect)
|
||||
|
||||
// Run synchronously
|
||||
readerResult := effect.RunSync(ioResult)
|
||||
|
||||
// Execute with a context.Context
|
||||
value, err := readerResult(ctx)
|
||||
|
||||
# Integration with Other Packages
|
||||
|
||||
The effect package integrates seamlessly with other fp-go packages:
|
||||
- either: For error handling
|
||||
- io: For I/O operations
|
||||
- reader: For dependency injection
|
||||
- result: For result types
|
||||
- retry: For retry logic
|
||||
- monoid: For combining effects
|
||||
|
||||
# Example
|
||||
|
||||
type Config struct {
|
||||
APIKey string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
func fetchUser(id int) Effect[Config, User] {
|
||||
return effect.Chain[Config](func(cfg Config) Effect[Config, User] {
|
||||
// Use cfg.APIKey and cfg.BaseURL
|
||||
return effect.Succeed[Config, User](User{ID: id})
|
||||
})(effect.Of[Config, Config](Config{}))
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := Config{APIKey: "key", BaseURL: "https://api.example.com"}
|
||||
userEffect := fetchUser(42)
|
||||
|
||||
// Run the effect
|
||||
ioResult := effect.Provide(cfg)(userEffect)
|
||||
readerResult := effect.RunSync(ioResult)
|
||||
user, err := readerResult(context.Background())
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("User: %+v\n", user)
|
||||
}
|
||||
*/
|
||||
package effect
|
||||
|
||||
//go:generate go run ../main.go lens --dir . --filename gen_lens.go --include-test-files
|
||||
614
v2/effect/effect.go
Normal file
614
v2/effect/effect.go
Normal file
@@ -0,0 +1,614 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
thunk "github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/internal/fromreader"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// FromThunk lifts a Thunk (context-independent IO computation with error handling) into an Effect.
|
||||
// This allows you to integrate computations that don't need the effect's context type C
|
||||
// into effect chains. The Thunk will be executed with the runtime context when the effect runs.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect (not used by the thunk)
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A Thunk[A] that performs IO with error handling
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that ignores its context and executes the thunk
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
// return func() result.Result[int] {
|
||||
// // Perform IO operation
|
||||
// return result.Of(42)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.FromThunk[MyContext](thunk)
|
||||
// // eff can be used in any context but executes the thunk
|
||||
//
|
||||
//go:inline
|
||||
func FromThunk[C, A any](f Thunk[A]) Effect[C, A] {
|
||||
return reader.Of[C](f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func FromResult[C, A any](r Result[A]) Effect[C, A] {
|
||||
return readerreaderioresult.FromEither[C](r)
|
||||
}
|
||||
|
||||
// Succeed creates a successful Effect that produces the given value.
|
||||
// This is the primary way to lift a pure value into the Effect context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - a: The value to wrap in a successful effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that always succeeds with the given value
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Succeed[MyContext](42)
|
||||
// result, err := runEffect(eff, myContext)
|
||||
// // result == 42, err == nil
|
||||
func Succeed[C, A any](a A) Effect[C, A] {
|
||||
return readerreaderioresult.Of[C](a)
|
||||
}
|
||||
|
||||
// Fail creates a failed Effect with the given error.
|
||||
// This is used to represent computations that have failed.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value (never produced)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - err: The error that caused the failure
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that always fails with the given error
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Fail[MyContext, int](errors.New("failed"))
|
||||
// _, err := runEffect(eff, myContext)
|
||||
// // err == errors.New("failed")
|
||||
func Fail[C, A any](err error) Effect[C, A] {
|
||||
return readerreaderioresult.Left[C, A](err)
|
||||
}
|
||||
|
||||
// Of creates a successful Effect that produces the given value.
|
||||
// This is an alias for Succeed and follows the pointed functor convention.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - a: The value to wrap in a successful effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that always succeeds with the given value
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Of[MyContext]("hello")
|
||||
// result, err := runEffect(eff, myContext)
|
||||
// // result == "hello", err == nil
|
||||
func Of[C, A any](a A) Effect[C, A] {
|
||||
return readerreaderioresult.Of[C](a)
|
||||
}
|
||||
|
||||
// Map transforms the success value of an Effect using the provided function.
|
||||
// If the effect fails, the error is propagated unchanged.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: The transformation function to apply to the success value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that transforms Effect[C, A] to Effect[C, B]
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// mapped := effect.Map[MyContext](func(x int) string {
|
||||
// return strconv.Itoa(x)
|
||||
// })(eff)
|
||||
// // mapped produces "42"
|
||||
func Map[C, A, B any](f func(A) B) Operator[C, A, B] {
|
||||
return readerreaderioresult.Map[C](f)
|
||||
}
|
||||
|
||||
// Chain sequences two effects, where the second effect depends on the result of the first.
|
||||
// This is the monadic bind operation (flatMap) for effects.
|
||||
// If the first effect fails, the second is not executed.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes the result of the first effect and returns a new effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that transforms Effect[C, A] to Effect[C, B]
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// chained := effect.Chain[MyContext](func(x int) Effect[MyContext, string] {
|
||||
// return effect.Of[MyContext](strconv.Itoa(x * 2))
|
||||
// })(eff)
|
||||
// // chained produces "84"
|
||||
//
|
||||
//go:inline
|
||||
func Chain[C, A, B any](f Kleisli[C, A, B]) Operator[C, A, B] {
|
||||
return readerreaderioresult.Chain(f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func ChainFirst[C, A, B any](f Kleisli[C, A, B]) Operator[C, A, A] {
|
||||
return readerreaderioresult.ChainFirst(f)
|
||||
}
|
||||
|
||||
// ChainIOK chains an effect with a function that returns an IO action.
|
||||
// This is useful for integrating IO-based computations (synchronous side effects)
|
||||
// into effect chains. The IO action is automatically lifted into the Effect context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns IO[B]
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that chains the IO-returning function with the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// performIO := func(n int) io.IO[string] {
|
||||
// return func() string {
|
||||
// // Perform synchronous side effect
|
||||
// return fmt.Sprintf("Value: %d", n)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// chained := effect.ChainIOK[MyContext](performIO)(eff)
|
||||
// // chained produces "Value: 42"
|
||||
//
|
||||
//go:inline
|
||||
func ChainIOK[C, A, B any](f io.Kleisli[A, B]) Operator[C, A, B] {
|
||||
return readerreaderioresult.ChainIOK[C](f)
|
||||
}
|
||||
|
||||
// ChainFirstIOK chains an effect with a function that returns an IO action,
|
||||
// but discards the result and returns the original value.
|
||||
// This is useful for performing side effects (like logging) without changing the value.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The value type (preserved)
|
||||
// - B: The type produced by the IO action (discarded)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns IO[B] for side effects
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, A]: A function that executes the IO action but preserves the original value
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// logValue := func(n int) io.IO[any] {
|
||||
// return func() any {
|
||||
// fmt.Printf("Processing: %d\n", n)
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// logged := effect.ChainFirstIOK[MyContext](logValue)(eff)
|
||||
// // Prints "Processing: 42" but still produces 42
|
||||
//
|
||||
//go:inline
|
||||
func ChainFirstIOK[C, A, B any](f io.Kleisli[A, B]) Operator[C, A, A] {
|
||||
return readerreaderioresult.ChainFirstIOK[C](f)
|
||||
}
|
||||
|
||||
// TapIOK is an alias for ChainFirstIOK.
|
||||
// It chains an effect with a function that returns an IO action for side effects,
|
||||
// but preserves the original value. This is useful for logging, debugging, or
|
||||
// performing actions without changing the result.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The value type (preserved)
|
||||
// - B: The type produced by the IO action (discarded)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns IO[B] for side effects
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, A]: A function that executes the IO action but preserves the original value
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// logValue := func(n int) io.IO[any] {
|
||||
// return func() any {
|
||||
// fmt.Printf("Value: %d\n", n)
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// tapped := effect.TapIOK[MyContext](logValue)(eff)
|
||||
// // Prints "Value: 42" but still produces 42
|
||||
//
|
||||
//go:inline
|
||||
func TapIOK[C, A, B any](f io.Kleisli[A, B]) Operator[C, A, A] {
|
||||
return readerreaderioresult.ChainFirstIOK[C](f)
|
||||
}
|
||||
|
||||
// Ap applies a function wrapped in an Effect to a value wrapped in an Effect.
|
||||
// This is the applicative apply operation, useful for applying effects in parallel.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - B: The output value type
|
||||
// - C: The context type required by the effects
|
||||
// - A: The input value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - fa: The effect containing the value to apply the function to
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, func(A) B, B]: A function that applies the function effect to the value effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// fnEff := effect.Of[MyContext](func(x int) int { return x * 2 })
|
||||
// valEff := effect.Of[MyContext](21)
|
||||
// result := effect.Ap[int](valEff)(fnEff)
|
||||
// // result produces 42
|
||||
func Ap[B, C, A any](fa Effect[C, A]) Operator[C, func(A) B, B] {
|
||||
return readerreaderioresult.Ap[B](fa)
|
||||
}
|
||||
|
||||
// Suspend delays the evaluation of an effect until it is run.
|
||||
// This is useful for recursive effects or when you need lazy evaluation.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - fa: A lazy computation that produces an effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that evaluates the lazy computation when run
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// var recursiveEff func(int) Effect[MyContext, int]
|
||||
// recursiveEff = func(n int) Effect[MyContext, int] {
|
||||
// if n <= 0 {
|
||||
// return effect.Of[MyContext](0)
|
||||
// }
|
||||
// return effect.Suspend(func() Effect[MyContext, int] {
|
||||
// return effect.Map[MyContext](func(x int) int {
|
||||
// return x + n
|
||||
// })(recursiveEff(n - 1))
|
||||
// })
|
||||
// }
|
||||
func Suspend[C, A any](fa Lazy[Effect[C, A]]) Effect[C, A] {
|
||||
return readerreaderioresult.Defer(fa)
|
||||
}
|
||||
|
||||
// Tap executes a side effect for its effect, but returns the original value.
|
||||
// This is useful for logging, debugging, or performing actions without changing the result.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The value type
|
||||
// - ANY: The type produced by the side effect (ignored)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that performs a side effect based on the value
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, A]: A function that executes the side effect but preserves the original value
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// tapped := effect.Tap[MyContext](func(x int) Effect[MyContext, any] {
|
||||
// fmt.Println("Value:", x)
|
||||
// return effect.Of[MyContext, any](nil)
|
||||
// })(eff)
|
||||
// // Prints "Value: 42" but still produces 42
|
||||
func Tap[C, A, ANY any](f Kleisli[C, A, ANY]) Operator[C, A, A] {
|
||||
return readerreaderioresult.Tap(f)
|
||||
}
|
||||
|
||||
// Ternary creates a conditional effect based on a predicate.
|
||||
// If the predicate returns true, onTrue is executed; otherwise, onFalse is executed.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - pred: A predicate function to test the input value
|
||||
// - onTrue: The effect to execute if the predicate is true
|
||||
// - onFalse: The effect to execute if the predicate is false
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Kleisli[C, A, B]: A function that conditionally executes one of two effects
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// kleisli := effect.Ternary(
|
||||
// func(x int) bool { return x > 10 },
|
||||
// func(x int) Effect[MyContext, string] {
|
||||
// return effect.Of[MyContext]("large")
|
||||
// },
|
||||
// func(x int) Effect[MyContext, string] {
|
||||
// return effect.Of[MyContext]("small")
|
||||
// },
|
||||
// )
|
||||
// result := kleisli(15) // produces "large"
|
||||
func Ternary[C, A, B any](pred Predicate[A], onTrue, onFalse Kleisli[C, A, B]) Kleisli[C, A, B] {
|
||||
return function.Ternary(pred, onTrue, onFalse)
|
||||
}
|
||||
|
||||
// ChainResultK chains an effect with a function that returns a Result.
|
||||
// This is useful for integrating Result-based computations into effect chains.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns Result[B]
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that chains the Result-returning function with the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
// eff := effect.Of[MyContext]("42")
|
||||
// chained := effect.ChainResultK[MyContext](parseIntResult)(eff)
|
||||
// // chained produces 42 as an int
|
||||
//
|
||||
//go:inline
|
||||
func ChainResultK[C, A, B any](f result.Kleisli[A, B]) Operator[C, A, B] {
|
||||
return readerreaderioresult.ChainResultK[C](f)
|
||||
}
|
||||
|
||||
// ChainReaderK chains an effect with a function that returns a Reader.
|
||||
// This is useful for integrating Reader-based computations (pure context-dependent functions)
|
||||
// into effect chains. The Reader is automatically lifted into the Effect context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns Reader[C, B]
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that chains the Reader-returning function with the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// type Config struct { Multiplier int }
|
||||
//
|
||||
// getMultiplied := func(n int) reader.Reader[Config, int] {
|
||||
// return func(cfg Config) int {
|
||||
// return n * cfg.Multiplier
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[Config](5)
|
||||
// chained := effect.ChainReaderK[Config](getMultiplied)(eff)
|
||||
// // With Config{Multiplier: 3}, produces 15
|
||||
//
|
||||
//go:inline
|
||||
func ChainReaderK[C, A, B any](f reader.Kleisli[C, A, B]) Operator[C, A, B] {
|
||||
return readerreaderioresult.ChainReaderK(f)
|
||||
}
|
||||
|
||||
// ChainThunkK chains an effect with a function that returns a Thunk.
|
||||
// This is useful for integrating Thunk-based computations (context-independent IO with error handling)
|
||||
// into effect chains. The Thunk is automatically lifted into the Effect context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns Thunk[B] (readerioresult.Kleisli[A, B])
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that chains the Thunk-returning function with the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// performIO := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
// return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
// return func() result.Result[string] {
|
||||
// // Perform IO operation that doesn't need effect context
|
||||
// return result.Of(fmt.Sprintf("Processed: %d", n))
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// chained := effect.ChainThunkK[MyContext](performIO)(eff)
|
||||
// // chained produces "Processed: 42"
|
||||
//
|
||||
//go:inline
|
||||
func ChainThunkK[C, A, B any](f thunk.Kleisli[A, B]) Operator[C, A, B] {
|
||||
return fromreader.ChainReaderK(
|
||||
Chain[C, A, B],
|
||||
FromThunk[C, B],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// ChainReaderIOK chains an effect with a function that returns a ReaderIO.
|
||||
// This is useful for integrating ReaderIO-based computations (context-dependent IO operations)
|
||||
// into effect chains. The ReaderIO is automatically lifted into the Effect context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The input value type
|
||||
// - B: The output value type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A function that takes A and returns ReaderIO[C, B]
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Operator[C, A, B]: A function that chains the ReaderIO-returning function with the effect
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// type Config struct { LogPrefix string }
|
||||
//
|
||||
// logAndDouble := func(n int) readerio.ReaderIO[Config, int] {
|
||||
// return func(cfg Config) io.IO[int] {
|
||||
// return func() int {
|
||||
// fmt.Printf("%s: %d\n", cfg.LogPrefix, n)
|
||||
// return n * 2
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// eff := effect.Of[Config](21)
|
||||
// chained := effect.ChainReaderIOK[Config](logAndDouble)(eff)
|
||||
// // Logs "prefix: 21" and produces 42
|
||||
//
|
||||
//go:inline
|
||||
func ChainReaderIOK[C, A, B any](f readerio.Kleisli[C, A, B]) Operator[C, A, B] {
|
||||
return readerreaderioresult.ChainReaderIOK(f)
|
||||
}
|
||||
|
||||
// Read provides a context to an effect, partially applying it.
|
||||
// This converts an Effect[C, A] to a Thunk[A] by supplying the required context.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The type of the success value
|
||||
// - C: The context type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - c: The context to provide to the effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C, A]) Thunk[A]: A function that converts an effect to a thunk
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ctx := MyContext{Value: "test"}
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// thunk := effect.Read[int](ctx)(eff)
|
||||
// // thunk is now a Thunk[int] that can be run without context
|
||||
//
|
||||
//go:inline
|
||||
func Read[A, C any](c C) func(Effect[C, A]) Thunk[A] {
|
||||
return readerreaderioresult.Read[A](c)
|
||||
}
|
||||
649
v2/effect/effect_additional_test.go
Normal file
649
v2/effect/effect_additional_test.go
Normal file
@@ -0,0 +1,649 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestSucceed tests the Succeed function
|
||||
func TestSucceed_Success(t *testing.T) {
|
||||
t.Run("creates successful effect with int", func(t *testing.T) {
|
||||
eff := Succeed[TestConfig](42)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("creates successful effect with string", func(t *testing.T) {
|
||||
eff := Succeed[TestConfig]("hello")
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("hello"), outcome)
|
||||
})
|
||||
|
||||
t.Run("creates successful effect with zero value", func(t *testing.T) {
|
||||
eff := Succeed[TestConfig](0)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(0), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestFail tests the Fail function
|
||||
func TestFail_Failure(t *testing.T) {
|
||||
t.Run("creates failed effect with error", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
eff := Fail[TestConfig, int](testErr)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
|
||||
t.Run("preserves error message", func(t *testing.T) {
|
||||
testErr := errors.New("specific error message")
|
||||
eff := Fail[TestConfig, string](testErr)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.True(t, result.IsLeft(outcome))
|
||||
extractedErr := result.MonadFold(outcome,
|
||||
F.Identity[error],
|
||||
func(string) error { return nil },
|
||||
)
|
||||
assert.Equal(t, testErr, extractedErr)
|
||||
})
|
||||
}
|
||||
|
||||
// TestOf tests the Of function
|
||||
func TestOf_Success(t *testing.T) {
|
||||
t.Run("creates successful effect with value", func(t *testing.T) {
|
||||
eff := Of[TestConfig](100)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(100), outcome)
|
||||
})
|
||||
|
||||
t.Run("is equivalent to Succeed", func(t *testing.T) {
|
||||
value := "test"
|
||||
eff1 := Of[TestConfig](value)
|
||||
eff2 := Succeed[TestConfig](value)
|
||||
outcome1 := eff1(testConfig)(context.Background())()
|
||||
outcome2 := eff2(testConfig)(context.Background())()
|
||||
assert.Equal(t, outcome1, outcome2)
|
||||
})
|
||||
}
|
||||
|
||||
// TestMap tests the Map function
|
||||
func TestMap_Success(t *testing.T) {
|
||||
t.Run("transforms success value", func(t *testing.T) {
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
Map[TestConfig](func(x int) int { return x * 2 }),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(84), outcome)
|
||||
})
|
||||
|
||||
t.Run("transforms type", func(t *testing.T) {
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
Map[TestConfig](func(x int) string { return strconv.Itoa(x) }),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("42"), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains multiple maps", func(t *testing.T) {
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
Map[TestConfig](func(x int) int { return x + 5 }),
|
||||
Map[TestConfig](func(x int) int { return x * 2 }),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMap_Failure(t *testing.T) {
|
||||
t.Run("propagates error unchanged", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
Map[TestConfig](func(x int) int { return x * 2 }),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestChain tests the Chain function
|
||||
func TestChain_Success(t *testing.T) {
|
||||
t.Run("sequences two effects", func(t *testing.T) {
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
Chain(func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig](strconv.Itoa(x))
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("42"), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains multiple effects", func(t *testing.T) {
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
Chain(func(x int) Effect[TestConfig, int] {
|
||||
return Of[TestConfig](x + 5)
|
||||
}),
|
||||
Chain(func(x int) Effect[TestConfig, int] {
|
||||
return Of[TestConfig](x * 2)
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChain_Failure(t *testing.T) {
|
||||
t.Run("propagates error from first effect", func(t *testing.T) {
|
||||
testErr := errors.New("first error")
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
Chain(func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("should not execute")
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[string](testErr), outcome)
|
||||
})
|
||||
|
||||
t.Run("propagates error from second effect", func(t *testing.T) {
|
||||
testErr := errors.New("second error")
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
Chain(func(x int) Effect[TestConfig, string] {
|
||||
return Fail[TestConfig, string](testErr)
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[string](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainIOK tests the ChainIOK function
|
||||
func TestChainIOK_Success(t *testing.T) {
|
||||
t.Run("chains with IO action", func(t *testing.T) {
|
||||
counter := 0
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainIOK[TestConfig](func(x int) io.IO[string] {
|
||||
return func() string {
|
||||
counter++
|
||||
return fmt.Sprintf("Value: %d", x)
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("Value: 42"), outcome)
|
||||
assert.Equal(t, 1, counter)
|
||||
})
|
||||
|
||||
t.Run("chains multiple IO actions", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
ChainIOK[TestConfig](func(x int) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, "first")
|
||||
return x + 5
|
||||
}
|
||||
}),
|
||||
ChainIOK[TestConfig](func(x int) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, "second")
|
||||
return x * 2
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
assert.Equal(t, []string{"first", "second"}, log)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainIOK_Failure(t *testing.T) {
|
||||
t.Run("propagates error from previous effect", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
executed := false
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
ChainIOK[TestConfig](func(x int) io.IO[string] {
|
||||
return func() string {
|
||||
executed = true
|
||||
return "should not execute"
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[string](testErr), outcome)
|
||||
assert.False(t, executed)
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainFirstIOK tests the ChainFirstIOK function
|
||||
func TestChainFirstIOK_Success(t *testing.T) {
|
||||
t.Run("executes IO but preserves value", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainFirstIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log = append(log, fmt.Sprintf("logged: %d", x))
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
assert.Equal(t, []string{"logged: 42"}, log)
|
||||
})
|
||||
|
||||
t.Run("chains multiple side effects", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
ChainFirstIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log = append(log, "first")
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
ChainFirstIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log = append(log, "second")
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(10), outcome)
|
||||
assert.Equal(t, []string{"first", "second"}, log)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainFirstIOK_Failure(t *testing.T) {
|
||||
t.Run("propagates error without executing IO", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
executed := false
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
ChainFirstIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
executed = true
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
assert.False(t, executed)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTapIOK tests the TapIOK function
|
||||
func TestTapIOK_Success(t *testing.T) {
|
||||
t.Run("executes IO but preserves value", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
TapIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log = append(log, fmt.Sprintf("tapped: %d", x))
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
assert.Equal(t, []string{"tapped: 42"}, log)
|
||||
})
|
||||
|
||||
t.Run("is equivalent to ChainFirstIOK", func(t *testing.T) {
|
||||
log1 := []string{}
|
||||
log2 := []string{}
|
||||
|
||||
eff1 := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
TapIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log1 = append(log1, "tap")
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
eff2 := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
ChainFirstIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
log2 = append(log2, "tap")
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
outcome1 := eff1(testConfig)(context.Background())()
|
||||
outcome2 := eff2(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, outcome1, outcome2)
|
||||
assert.Equal(t, log1, log2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTapIOK_Failure(t *testing.T) {
|
||||
t.Run("propagates error without executing IO", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
executed := false
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
TapIOK[TestConfig](func(x int) io.IO[any] {
|
||||
return func() any {
|
||||
executed = true
|
||||
return nil
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
assert.False(t, executed)
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainResultK tests the ChainResultK function
|
||||
func TestChainResultK_Success(t *testing.T) {
|
||||
t.Run("chains with Result-returning function", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig]("42"),
|
||||
ChainResultK[TestConfig](parseIntResult),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Result operations", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig]("10"),
|
||||
ChainResultK[TestConfig](parseIntResult),
|
||||
ChainResultK[TestConfig](func(x int) result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Value: %d", x*2))
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("Value: 20"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainResultK_Failure(t *testing.T) {
|
||||
t.Run("propagates error from previous effect", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, string](testErr),
|
||||
ChainResultK[TestConfig](parseIntResult),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
|
||||
t.Run("propagates error from Result function", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig]("not a number"),
|
||||
ChainResultK[TestConfig](parseIntResult),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.True(t, result.IsLeft(outcome))
|
||||
})
|
||||
}
|
||||
|
||||
// TestAp tests the Ap function
|
||||
func TestAp_Success(t *testing.T) {
|
||||
t.Run("applies function effect to value effect", func(t *testing.T) {
|
||||
fnEff := Of[TestConfig](func(x int) int { return x * 2 })
|
||||
valEff := Of[TestConfig](21)
|
||||
eff := Ap[int](valEff)(fnEff)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("applies function with different types", func(t *testing.T) {
|
||||
fnEff := Of[TestConfig](func(x int) string { return strconv.Itoa(x) })
|
||||
valEff := Of[TestConfig](42)
|
||||
eff := Ap[string](valEff)(fnEff)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("42"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAp_Failure(t *testing.T) {
|
||||
t.Run("propagates error from function effect", func(t *testing.T) {
|
||||
testErr := errors.New("function error")
|
||||
fnEff := Fail[TestConfig, func(int) int](testErr)
|
||||
valEff := Of[TestConfig](42)
|
||||
eff := Ap[int](valEff)(fnEff)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
|
||||
t.Run("propagates error from value effect", func(t *testing.T) {
|
||||
testErr := errors.New("value error")
|
||||
fnEff := Of[TestConfig](func(x int) int { return x * 2 })
|
||||
valEff := Fail[TestConfig, int](testErr)
|
||||
eff := Ap[int](valEff)(fnEff)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestSuspend tests the Suspend function
|
||||
func TestSuspend_Success(t *testing.T) {
|
||||
t.Run("delays evaluation of effect", func(t *testing.T) {
|
||||
counter := 0
|
||||
eff := Suspend(func() Effect[TestConfig, int] {
|
||||
counter++
|
||||
return Of[TestConfig](42)
|
||||
})
|
||||
assert.Equal(t, 0, counter, "should not evaluate immediately")
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, 1, counter, "should evaluate when run")
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("enables recursive effects", func(t *testing.T) {
|
||||
var factorial func(int) Effect[TestConfig, int]
|
||||
factorial = func(n int) Effect[TestConfig, int] {
|
||||
if n <= 1 {
|
||||
return Of[TestConfig](1)
|
||||
}
|
||||
return Suspend(func() Effect[TestConfig, int] {
|
||||
return F.Pipe1(
|
||||
factorial(n-1),
|
||||
Map[TestConfig](func(x int) int { return x * n }),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
outcome := factorial(5)(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(120), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTap tests the Tap function
|
||||
func TestTap_Success(t *testing.T) {
|
||||
t.Run("executes side effect but preserves value", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
Tap(func(x int) Effect[TestConfig, any] {
|
||||
log = append(log, fmt.Sprintf("tapped: %d", x))
|
||||
return Of[TestConfig, any](nil)
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
assert.Equal(t, []string{"tapped: 42"}, log)
|
||||
})
|
||||
|
||||
t.Run("chains multiple taps", func(t *testing.T) {
|
||||
log := []string{}
|
||||
eff := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
Tap(func(x int) Effect[TestConfig, any] {
|
||||
log = append(log, "first")
|
||||
return Of[TestConfig, any](nil)
|
||||
}),
|
||||
Tap(func(x int) Effect[TestConfig, any] {
|
||||
log = append(log, "second")
|
||||
return Of[TestConfig, any](nil)
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(10), outcome)
|
||||
assert.Equal(t, []string{"first", "second"}, log)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTap_Failure(t *testing.T) {
|
||||
t.Run("propagates error without executing tap", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
executed := false
|
||||
eff := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
Tap(func(x int) Effect[TestConfig, any] {
|
||||
executed = true
|
||||
return Of[TestConfig, any](nil)
|
||||
}),
|
||||
)
|
||||
outcome := eff(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
assert.False(t, executed)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTernary tests the Ternary function
|
||||
func TestTernary_Success(t *testing.T) {
|
||||
t.Run("executes onTrue when predicate is true", func(t *testing.T) {
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("large")
|
||||
},
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("small")
|
||||
},
|
||||
)
|
||||
outcome := kleisli(15)(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("large"), outcome)
|
||||
})
|
||||
|
||||
t.Run("executes onFalse when predicate is false", func(t *testing.T) {
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("large")
|
||||
},
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("small")
|
||||
},
|
||||
)
|
||||
outcome := kleisli(5)(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("small"), outcome)
|
||||
})
|
||||
|
||||
t.Run("works with boundary value", func(t *testing.T) {
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x >= 10 },
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("gte")
|
||||
},
|
||||
func(x int) Effect[TestConfig, string] {
|
||||
return Of[TestConfig]("lt")
|
||||
},
|
||||
)
|
||||
outcome := kleisli(10)(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("gte"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
// TestRead tests the Read function
|
||||
func TestRead_Success(t *testing.T) {
|
||||
t.Run("provides context to effect", func(t *testing.T) {
|
||||
eff := Of[TestConfig](42)
|
||||
thunk := Read[int](testConfig)(eff)
|
||||
outcome := thunk(context.Background())()
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("converts effect to thunk", func(t *testing.T) {
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
Map[TestConfig](func(x int) int { return x * testConfig.Multiplier }),
|
||||
)
|
||||
thunk := Read[int](testConfig)(eff)
|
||||
outcome := thunk(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
})
|
||||
|
||||
t.Run("works with different contexts", func(t *testing.T) {
|
||||
cfg1 := TestConfig{Multiplier: 2, Prefix: "A", DatabaseURL: ""}
|
||||
cfg2 := TestConfig{Multiplier: 5, Prefix: "B", DatabaseURL: ""}
|
||||
|
||||
// Create an effect that uses the context's Multiplier
|
||||
eff := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
ChainReaderK(func(x int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return x * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
thunk1 := Read[int](cfg1)(eff)
|
||||
thunk2 := Read[int](cfg2)(eff)
|
||||
|
||||
outcome1 := thunk1(context.Background())()
|
||||
outcome2 := thunk2(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of(20), outcome1)
|
||||
assert.Equal(t, result.Of(50), outcome2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRead_Failure(t *testing.T) {
|
||||
t.Run("propagates error from effect", func(t *testing.T) {
|
||||
testErr := errors.New("test error")
|
||||
eff := Fail[TestConfig, int](testErr)
|
||||
thunk := Read[int](testConfig)(eff)
|
||||
outcome := thunk(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
}
|
||||
213
v2/effect/effect_missing_test.go
Normal file
213
v2/effect/effect_missing_test.go
Normal file
@@ -0,0 +1,213 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
t.Run("provides context to effect", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test-context"}
|
||||
eff := Of[TestContext](42)
|
||||
|
||||
thunk := Read[int](ctx)(eff)
|
||||
ioResult := thunk(context.Background())
|
||||
res := ioResult()
|
||||
|
||||
assert.True(t, result.IsRight(res))
|
||||
value, err := result.Unwrap(res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, value)
|
||||
})
|
||||
|
||||
t.Run("provides context to failing effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("read error")
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Fail[TestContext, string](expectedErr)
|
||||
|
||||
thunk := Read[string](ctx)(eff)
|
||||
ioResult := thunk(context.Background())
|
||||
res := ioResult()
|
||||
|
||||
assert.True(t, result.IsLeft(res))
|
||||
_, err := result.Unwrap(res)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("provides context to chained effects", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "base"}
|
||||
eff := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x * 2))
|
||||
})(Of[TestContext](21))
|
||||
|
||||
thunk := Read[string](ctx)(eff)
|
||||
ioResult := thunk(context.Background())
|
||||
res := ioResult()
|
||||
|
||||
assert.True(t, result.IsRight(res))
|
||||
value, err := result.Unwrap(res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "42", value)
|
||||
})
|
||||
|
||||
t.Run("works with different context types", func(t *testing.T) {
|
||||
type CustomContext struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
ctx := CustomContext{ID: 100, Name: "custom"}
|
||||
eff := Of[CustomContext]("result")
|
||||
|
||||
thunk := Read[string](ctx)(eff)
|
||||
ioResult := thunk(context.Background())
|
||||
res := ioResult()
|
||||
|
||||
assert.True(t, result.IsRight(res))
|
||||
value, err := result.Unwrap(res)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", value)
|
||||
})
|
||||
|
||||
t.Run("can be composed with RunSync", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext](100)
|
||||
|
||||
thunk := Read[int](ctx)(eff)
|
||||
readerResult := RunSync(thunk)
|
||||
value, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainResultK(t *testing.T) {
|
||||
t.Run("chains successful Result function", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := Of[TestContext]("42")
|
||||
chained := ChainResultK[TestContext](parseIntResult)(eff)
|
||||
|
||||
result, err := runEffect(chained, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("chains failing Result function", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := Of[TestContext]("not-a-number")
|
||||
chained := ChainResultK[TestContext](parseIntResult)(eff)
|
||||
|
||||
_, err := runEffect(chained, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid syntax")
|
||||
})
|
||||
|
||||
t.Run("propagates error from original effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("original error")
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
eff := Fail[TestContext, string](expectedErr)
|
||||
chained := ChainResultK[TestContext](parseIntResult)(eff)
|
||||
|
||||
_, err := runEffect(chained, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Result functions", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
formatResult := func(x int) result.Result[string] {
|
||||
return result.Of("value: " + strconv.Itoa(x))
|
||||
}
|
||||
|
||||
eff := Of[TestContext]("42")
|
||||
chained := ChainResultK[TestContext](formatResult)(
|
||||
ChainResultK[TestContext](parseIntResult)(eff),
|
||||
)
|
||||
|
||||
result, err := runEffect(chained, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "value: 42", result)
|
||||
})
|
||||
|
||||
t.Run("integrates with other effect operations", func(t *testing.T) {
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
|
||||
eff := Map[TestContext](func(x int) string {
|
||||
return "final: " + strconv.Itoa(x)
|
||||
})(ChainResultK[TestContext](parseIntResult)(Of[TestContext]("100")))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "final: 100", result)
|
||||
})
|
||||
|
||||
t.Run("works with custom Result functions", func(t *testing.T) {
|
||||
validatePositive := func(x int) result.Result[int] {
|
||||
if x > 0 {
|
||||
return result.Of(x)
|
||||
}
|
||||
return result.Left[int](errors.New("must be positive"))
|
||||
}
|
||||
|
||||
parseIntResult := result.Eitherize1(strconv.Atoi)
|
||||
|
||||
// Test with positive number
|
||||
eff1 := ChainResultK[TestContext](validatePositive)(
|
||||
ChainResultK[TestContext](parseIntResult)(Of[TestContext]("42")),
|
||||
)
|
||||
result1, err1 := runEffect(eff1, TestContext{Value: "test"})
|
||||
assert.NoError(t, err1)
|
||||
assert.Equal(t, 42, result1)
|
||||
|
||||
// Test with negative number
|
||||
eff2 := ChainResultK[TestContext](validatePositive)(
|
||||
ChainResultK[TestContext](parseIntResult)(Of[TestContext]("-5")),
|
||||
)
|
||||
_, err2 := runEffect(eff2, TestContext{Value: "test"})
|
||||
assert.Error(t, err2)
|
||||
assert.Contains(t, err2.Error(), "must be positive")
|
||||
})
|
||||
|
||||
t.Run("preserves error context", func(t *testing.T) {
|
||||
customError := errors.New("custom validation error")
|
||||
validateFunc := func(s string) result.Result[string] {
|
||||
if len(s) > 0 {
|
||||
return result.Of(s)
|
||||
}
|
||||
return result.Left[string](customError)
|
||||
}
|
||||
|
||||
eff := ChainResultK[TestContext](validateFunc)(Of[TestContext](""))
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, customError, err)
|
||||
})
|
||||
}
|
||||
679
v2/effect/effect_test.go
Normal file
679
v2/effect/effect_test.go
Normal file
@@ -0,0 +1,679 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type TestConfig struct {
|
||||
Multiplier int
|
||||
Prefix string
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
var testConfig = TestConfig{
|
||||
Multiplier: 3,
|
||||
Prefix: "LOG",
|
||||
DatabaseURL: "postgres://localhost",
|
||||
}
|
||||
|
||||
func TestChainReaderK_Success(t *testing.T) {
|
||||
t.Run("chains with Reader that uses context", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](5),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(15), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Reader operations", func(t *testing.T) {
|
||||
computation := F.Pipe2(
|
||||
Of[TestConfig](10),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n + cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(39), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains with Reader that returns string", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, string] {
|
||||
return func(cfg TestConfig) string {
|
||||
return fmt.Sprintf("%s: %d", cfg.Prefix, n)
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("LOG: 42"), outcome)
|
||||
})
|
||||
|
||||
t.Run("accesses context fields", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n + len(cfg.DatabaseURL)
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderK_Failure(t *testing.T) {
|
||||
t.Run("propagates error from previous effect", func(t *testing.T) {
|
||||
testErr := fmt.Errorf("test error")
|
||||
computation := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderK_EdgeCases(t *testing.T) {
|
||||
t.Run("handles zero value", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](0),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(0), outcome)
|
||||
})
|
||||
|
||||
t.Run("handles empty string", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](""),
|
||||
ChainReaderK(func(s string) reader.Reader[TestConfig, string] {
|
||||
return func(cfg TestConfig) string {
|
||||
return cfg.Prefix + s
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("LOG"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderK_Integration(t *testing.T) {
|
||||
t.Run("composes with Map and Chain", func(t *testing.T) {
|
||||
computation := F.Pipe3(
|
||||
Of[TestConfig](10),
|
||||
Map[TestConfig](func(x int) int { return x + 5 }),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
Map[TestConfig](func(x int) string { return fmt.Sprintf("Result: %d", x) }),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("Result: 45"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderIOK_Success(t *testing.T) {
|
||||
t.Run("chains with ReaderIO that performs IO", func(t *testing.T) {
|
||||
counter := 0
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](7),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
counter++
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(21), outcome)
|
||||
assert.Equal(t, 1, counter)
|
||||
})
|
||||
|
||||
t.Run("chains multiple ReaderIO operations", func(t *testing.T) {
|
||||
log := []string{}
|
||||
computation := F.Pipe2(
|
||||
Of[TestConfig](5),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, fmt.Sprintf("add: %d", n))
|
||||
return n + cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, fmt.Sprintf("multiply: %d", n))
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(24), outcome)
|
||||
assert.Equal(t, 2, len(log))
|
||||
assert.Equal(t, "add: 5", log[0])
|
||||
assert.Equal(t, "multiply: 8", log[1])
|
||||
})
|
||||
|
||||
t.Run("chains with ReaderIO that formats string", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](100),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, string] {
|
||||
return func(cfg TestConfig) io.IO[string] {
|
||||
return func() string {
|
||||
return fmt.Sprintf("%s: %d", cfg.Prefix, n)
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("LOG: 100"), outcome)
|
||||
})
|
||||
|
||||
t.Run("accesses context in IO operation", func(t *testing.T) {
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](10),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
return n + len(cfg.DatabaseURL)
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(30), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderIOK_Failure(t *testing.T) {
|
||||
t.Run("propagates error from previous effect", func(t *testing.T) {
|
||||
testErr := fmt.Errorf("test error")
|
||||
sideEffectExecuted := false
|
||||
computation := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
sideEffectExecuted = true
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
assert.False(t, sideEffectExecuted)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderIOK_EdgeCases(t *testing.T) {
|
||||
t.Run("handles zero value with side effects", func(t *testing.T) {
|
||||
callCount := 0
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](0),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
callCount++
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(0), outcome)
|
||||
assert.Equal(t, 1, callCount)
|
||||
})
|
||||
|
||||
t.Run("handles empty context fields", func(t *testing.T) {
|
||||
emptyConfig := TestConfig{Multiplier: 0, Prefix: "", DatabaseURL: ""}
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, string] {
|
||||
return func(cfg TestConfig) io.IO[string] {
|
||||
return func() string {
|
||||
return fmt.Sprintf("%s%d", cfg.Prefix, n)
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(emptyConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("42"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainReaderIOK_Integration(t *testing.T) {
|
||||
t.Run("composes with Map and Chain", func(t *testing.T) {
|
||||
log := []string{}
|
||||
computation := F.Pipe3(
|
||||
Of[TestConfig](8),
|
||||
Map[TestConfig](func(x int) int { return x + 2 }),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, fmt.Sprintf("processing: %d", n))
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
Map[TestConfig](func(x int) string { return fmt.Sprintf("Final: %d", x) }),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of("Final: 30"), outcome)
|
||||
assert.Equal(t, 1, len(log))
|
||||
assert.Equal(t, "processing: 10", log[0])
|
||||
})
|
||||
|
||||
t.Run("composes ChainReaderK and ChainReaderIOK together", func(t *testing.T) {
|
||||
log := []string{}
|
||||
computation := F.Pipe2(
|
||||
Of[TestConfig](5),
|
||||
ChainReaderK(func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n + cfg.Multiplier
|
||||
}
|
||||
}),
|
||||
ChainReaderIOK(func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, fmt.Sprintf("value: %d", n))
|
||||
return n * cfg.Multiplier
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
assert.Equal(t, result.Of(24), outcome)
|
||||
assert.Equal(t, 1, len(log))
|
||||
assert.Equal(t, "value: 8", log[0])
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromThunk_Success(t *testing.T) {
|
||||
t.Run("lifts successful thunk into effect", func(t *testing.T) {
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
return result.Of(42)
|
||||
}
|
||||
}
|
||||
|
||||
computation := FromThunk[TestConfig](thunk)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
})
|
||||
|
||||
t.Run("thunk ignores effect context", func(t *testing.T) {
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
// Thunk doesn't use TestConfig, only runtime context
|
||||
return result.Of("context-independent")
|
||||
}
|
||||
}
|
||||
|
||||
computation := FromThunk[TestConfig](thunk)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("context-independent"), outcome)
|
||||
})
|
||||
|
||||
t.Run("thunk can perform IO operations", func(t *testing.T) {
|
||||
counter := 0
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
counter++
|
||||
return result.Of(counter)
|
||||
}
|
||||
}
|
||||
|
||||
computation := FromThunk[TestConfig](thunk)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of(1), outcome)
|
||||
assert.Equal(t, 1, counter)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromThunk_Failure(t *testing.T) {
|
||||
t.Run("propagates error from thunk", func(t *testing.T) {
|
||||
testErr := fmt.Errorf("thunk error")
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
return result.Left[int](testErr)
|
||||
}
|
||||
}
|
||||
|
||||
computation := FromThunk[TestConfig](thunk)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Left[int](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestFromThunk_Integration(t *testing.T) {
|
||||
t.Run("composes with other effect operations", func(t *testing.T) {
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
return result.Of(10)
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe2(
|
||||
FromThunk[TestConfig](thunk),
|
||||
Map[TestConfig](func(x int) int { return x * 2 }),
|
||||
Map[TestConfig](func(x int) string { return fmt.Sprintf("Result: %d", x) }),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("Result: 20"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainThunkK_Success(t *testing.T) {
|
||||
t.Run("chains with thunk that performs IO", func(t *testing.T) {
|
||||
processValue := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Processed: %d", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainThunkK[TestConfig](processValue),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("Processed: 42"), outcome)
|
||||
})
|
||||
|
||||
t.Run("chains multiple thunk operations", func(t *testing.T) {
|
||||
log := []string{}
|
||||
|
||||
logAndAdd := func(n int) readerioresult.ReaderIOResult[int] {
|
||||
return func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
log = append(log, fmt.Sprintf("add: %d", n))
|
||||
return result.Of(n + 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logAndMultiply := func(n int) readerioresult.ReaderIOResult[int] {
|
||||
return func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
log = append(log, fmt.Sprintf("multiply: %d", n))
|
||||
return result.Of(n * 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe2(
|
||||
Of[TestConfig](5),
|
||||
ChainThunkK[TestConfig](logAndAdd),
|
||||
ChainThunkK[TestConfig](logAndMultiply),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of(30), outcome) // (5 + 10) * 2
|
||||
assert.Equal(t, 2, len(log))
|
||||
assert.Equal(t, "add: 5", log[0])
|
||||
assert.Equal(t, "multiply: 15", log[1])
|
||||
})
|
||||
|
||||
t.Run("thunk can access runtime context", func(t *testing.T) {
|
||||
var capturedCtx context.Context
|
||||
|
||||
captureContext := func(n int) readerioresult.ReaderIOResult[int] {
|
||||
return func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
capturedCtx = ctx
|
||||
return result.Of(n * 2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](21),
|
||||
ChainThunkK[TestConfig](captureContext),
|
||||
)
|
||||
outcome := computation(testConfig)(ctx)()
|
||||
|
||||
assert.Equal(t, result.Of(42), outcome)
|
||||
assert.Equal(t, ctx, capturedCtx)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainThunkK_Failure(t *testing.T) {
|
||||
t.Run("propagates error from previous effect", func(t *testing.T) {
|
||||
testErr := fmt.Errorf("previous error")
|
||||
sideEffectExecuted := false
|
||||
|
||||
processValue := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
sideEffectExecuted = true
|
||||
return result.Of(fmt.Sprintf("%d", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
Fail[TestConfig, int](testErr),
|
||||
ChainThunkK[TestConfig](processValue),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Left[string](testErr), outcome)
|
||||
assert.False(t, sideEffectExecuted)
|
||||
})
|
||||
|
||||
t.Run("propagates error from thunk", func(t *testing.T) {
|
||||
testErr := fmt.Errorf("thunk error")
|
||||
|
||||
failingThunk := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
return result.Left[string](testErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](42),
|
||||
ChainThunkK[TestConfig](failingThunk),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Left[string](testErr), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainThunkK_EdgeCases(t *testing.T) {
|
||||
t.Run("handles zero value", func(t *testing.T) {
|
||||
callCount := 0
|
||||
|
||||
processZero := func(n int) readerioresult.ReaderIOResult[int] {
|
||||
return func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
callCount++
|
||||
return result.Of(n + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](0),
|
||||
ChainThunkK[TestConfig](processZero),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of(1), outcome)
|
||||
assert.Equal(t, 1, callCount)
|
||||
})
|
||||
|
||||
t.Run("handles empty string", func(t *testing.T) {
|
||||
addPrefix := func(s string) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of("prefix:" + s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
Of[TestConfig](""),
|
||||
ChainThunkK[TestConfig](addPrefix),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("prefix:"), outcome)
|
||||
})
|
||||
}
|
||||
|
||||
func TestChainThunkK_Integration(t *testing.T) {
|
||||
t.Run("composes with Map and Chain", func(t *testing.T) {
|
||||
log := []string{}
|
||||
|
||||
logAndProcess := func(n int) readerioresult.ReaderIOResult[int] {
|
||||
return func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
log = append(log, fmt.Sprintf("processing: %d", n))
|
||||
return result.Of(n * 3)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe3(
|
||||
Of[TestConfig](7),
|
||||
Map[TestConfig](func(x int) int { return x + 3 }),
|
||||
ChainThunkK[TestConfig](logAndProcess),
|
||||
Map[TestConfig](func(x int) string { return fmt.Sprintf("Final: %d", x) }),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("Final: 30"), outcome) // (7 + 3) * 3
|
||||
assert.Equal(t, 1, len(log))
|
||||
assert.Equal(t, "processing: 10", log[0])
|
||||
})
|
||||
|
||||
t.Run("composes with ChainReaderK and ChainReaderIOK", func(t *testing.T) {
|
||||
log := []string{}
|
||||
|
||||
addMultiplier := func(n int) reader.Reader[TestConfig, int] {
|
||||
return func(cfg TestConfig) int {
|
||||
return n + cfg.Multiplier
|
||||
}
|
||||
}
|
||||
|
||||
logValue := func(n int) readerio.ReaderIO[TestConfig, int] {
|
||||
return func(cfg TestConfig) io.IO[int] {
|
||||
return func() int {
|
||||
log = append(log, fmt.Sprintf("reader-io: %d", n))
|
||||
return n * 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
processThunk := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
log = append(log, fmt.Sprintf("thunk: %d", n))
|
||||
return result.Of(fmt.Sprintf("Result: %d", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe3(
|
||||
Of[TestConfig](5),
|
||||
ChainReaderK(addMultiplier),
|
||||
ChainReaderIOK(logValue),
|
||||
ChainThunkK[TestConfig](processThunk),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("Result: 16"), outcome) // (5 + 3) * 2
|
||||
assert.Equal(t, 2, len(log))
|
||||
assert.Equal(t, "reader-io: 8", log[0])
|
||||
assert.Equal(t, "thunk: 16", log[1])
|
||||
})
|
||||
|
||||
t.Run("composes FromThunk with ChainThunkK", func(t *testing.T) {
|
||||
thunk := func(ctx context.Context) io.IO[result.Result[int]] {
|
||||
return func() result.Result[int] {
|
||||
return result.Of(100)
|
||||
}
|
||||
}
|
||||
|
||||
processValue := func(n int) readerioresult.ReaderIOResult[string] {
|
||||
return func(ctx context.Context) io.IO[result.Result[string]] {
|
||||
return func() result.Result[string] {
|
||||
return result.Of(fmt.Sprintf("Value: %d", n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computation := F.Pipe1(
|
||||
FromThunk[TestConfig](thunk),
|
||||
ChainThunkK[TestConfig](processValue),
|
||||
)
|
||||
outcome := computation(testConfig)(context.Background())()
|
||||
|
||||
assert.Equal(t, result.Of("Value: 100"), outcome)
|
||||
})
|
||||
}
|
||||
118
v2/effect/gen_lens_test.go
Normal file
118
v2/effect/gen_lens_test.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package effect
|
||||
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// This file was generated by robots at
|
||||
// 2026-01-27 22:19:41.6840253 +0100 CET m=+0.008579701
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// ComplexServiceLenses provides lenses for accessing fields of ComplexService
|
||||
type ComplexServiceLenses struct {
|
||||
// mandatory fields
|
||||
service1 __lens.Lens[ComplexService, Service1]
|
||||
service2 __lens.Lens[ComplexService, Service2]
|
||||
// optional fields
|
||||
service1O __lens_option.LensO[ComplexService, Service1]
|
||||
service2O __lens_option.LensO[ComplexService, Service2]
|
||||
}
|
||||
|
||||
// ComplexServiceRefLenses provides lenses for accessing fields of ComplexService via a reference to ComplexService
|
||||
type ComplexServiceRefLenses struct {
|
||||
// mandatory fields
|
||||
service1 __lens.Lens[*ComplexService, Service1]
|
||||
service2 __lens.Lens[*ComplexService, Service2]
|
||||
// optional fields
|
||||
service1O __lens_option.LensO[*ComplexService, Service1]
|
||||
service2O __lens_option.LensO[*ComplexService, Service2]
|
||||
// prisms
|
||||
service1P __prism.Prism[*ComplexService, Service1]
|
||||
service2P __prism.Prism[*ComplexService, Service2]
|
||||
}
|
||||
|
||||
// ComplexServicePrisms provides prisms for accessing fields of ComplexService
|
||||
type ComplexServicePrisms struct {
|
||||
service1 __prism.Prism[ComplexService, Service1]
|
||||
service2 __prism.Prism[ComplexService, Service2]
|
||||
}
|
||||
|
||||
// MakeComplexServiceLenses creates a new ComplexServiceLenses with lenses for all fields
|
||||
func MakeComplexServiceLenses() ComplexServiceLenses {
|
||||
// mandatory lenses
|
||||
lensservice1 := __lens.MakeLensWithName(
|
||||
func(s ComplexService) Service1 { return s.service1 },
|
||||
func(s ComplexService, v Service1) ComplexService { s.service1 = v; return s },
|
||||
"ComplexService.service1",
|
||||
)
|
||||
lensservice2 := __lens.MakeLensWithName(
|
||||
func(s ComplexService) Service2 { return s.service2 },
|
||||
func(s ComplexService, v Service2) ComplexService { s.service2 = v; return s },
|
||||
"ComplexService.service2",
|
||||
)
|
||||
// optional lenses
|
||||
lensservice1O := __lens_option.FromIso[ComplexService](__iso_option.FromZero[Service1]())(lensservice1)
|
||||
lensservice2O := __lens_option.FromIso[ComplexService](__iso_option.FromZero[Service2]())(lensservice2)
|
||||
return ComplexServiceLenses{
|
||||
// mandatory lenses
|
||||
service1: lensservice1,
|
||||
service2: lensservice2,
|
||||
// optional lenses
|
||||
service1O: lensservice1O,
|
||||
service2O: lensservice2O,
|
||||
}
|
||||
}
|
||||
|
||||
// MakeComplexServiceRefLenses creates a new ComplexServiceRefLenses with lenses for all fields
|
||||
func MakeComplexServiceRefLenses() ComplexServiceRefLenses {
|
||||
// mandatory lenses
|
||||
lensservice1 := __lens.MakeLensStrictWithName(
|
||||
func(s *ComplexService) Service1 { return s.service1 },
|
||||
func(s *ComplexService, v Service1) *ComplexService { s.service1 = v; return s },
|
||||
"(*ComplexService).service1",
|
||||
)
|
||||
lensservice2 := __lens.MakeLensStrictWithName(
|
||||
func(s *ComplexService) Service2 { return s.service2 },
|
||||
func(s *ComplexService, v Service2) *ComplexService { s.service2 = v; return s },
|
||||
"(*ComplexService).service2",
|
||||
)
|
||||
// optional lenses
|
||||
lensservice1O := __lens_option.FromIso[*ComplexService](__iso_option.FromZero[Service1]())(lensservice1)
|
||||
lensservice2O := __lens_option.FromIso[*ComplexService](__iso_option.FromZero[Service2]())(lensservice2)
|
||||
return ComplexServiceRefLenses{
|
||||
// mandatory lenses
|
||||
service1: lensservice1,
|
||||
service2: lensservice2,
|
||||
// optional lenses
|
||||
service1O: lensservice1O,
|
||||
service2O: lensservice2O,
|
||||
}
|
||||
}
|
||||
|
||||
// MakeComplexServicePrisms creates a new ComplexServicePrisms with prisms for all fields
|
||||
func MakeComplexServicePrisms() ComplexServicePrisms {
|
||||
_fromNonZeroservice1 := __option.FromNonZero[Service1]()
|
||||
_prismservice1 := __prism.MakePrismWithName(
|
||||
func(s ComplexService) __option.Option[Service1] { return _fromNonZeroservice1(s.service1) },
|
||||
func(v Service1) ComplexService {
|
||||
return ComplexService{ service1: v }
|
||||
},
|
||||
"ComplexService.service1",
|
||||
)
|
||||
_fromNonZeroservice2 := __option.FromNonZero[Service2]()
|
||||
_prismservice2 := __prism.MakePrismWithName(
|
||||
func(s ComplexService) __option.Option[Service2] { return _fromNonZeroservice2(s.service2) },
|
||||
func(v Service2) ComplexService {
|
||||
return ComplexService{ service2: v }
|
||||
},
|
||||
"ComplexService.service2",
|
||||
)
|
||||
return ComplexServicePrisms {
|
||||
service1: _prismservice1,
|
||||
service2: _prismservice2,
|
||||
}
|
||||
}
|
||||
207
v2/effect/injection_test.go
Normal file
207
v2/effect/injection_test.go
Normal file
@@ -0,0 +1,207 @@
|
||||
// Package effect demonstrates dependency injection using the Effect pattern.
|
||||
//
|
||||
// This test file shows how to build a type-safe dependency injection system where:
|
||||
// - An InjectionContainer can resolve services by ID (InjectionToken)
|
||||
// - Services are generic effects that depend on the container
|
||||
// - Lookup methods convert from untyped container to typed dependencies
|
||||
// - Handler functions depend type-safely on specific service interfaces
|
||||
package effect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
thunk "github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
type (
|
||||
// InjectionToken is a unique identifier for services in the container
|
||||
InjectionToken string
|
||||
|
||||
// InjectionContainer is an Effect that resolves services by their token.
|
||||
// It takes an InjectionToken and returns a Thunk that produces any type.
|
||||
// This allows the container to store and retrieve services of different types.
|
||||
InjectionContainer = Effect[InjectionToken, any]
|
||||
|
||||
// Service is a generic Effect that depends on the InjectionContainer.
|
||||
// It represents a computation that needs access to the dependency injection
|
||||
// container to resolve its dependencies before producing a string result.
|
||||
Service[T any] = Effect[InjectionContainer, T]
|
||||
|
||||
// Service1 is an example service interface that can be resolved from the container
|
||||
Service1 interface {
|
||||
GetService1() string
|
||||
}
|
||||
|
||||
// Service2 is another example service interface
|
||||
Service2 interface {
|
||||
GetService2() string
|
||||
}
|
||||
|
||||
// impl1 is a concrete implementation of Service1
|
||||
impl1 struct{}
|
||||
// impl2 is a concrete implementation of Service2
|
||||
impl2 struct{}
|
||||
)
|
||||
|
||||
// ComplexService demonstrates a more complex dependency injection scenario
|
||||
// where a service depends on multiple other services. This struct aggregates
|
||||
// Service1 and Service2, showing how to compose dependencies.
|
||||
// The fp-go:Lens directive generates lens functions for type-safe field access.
|
||||
//
|
||||
// fp-go:Lens
|
||||
type ComplexService struct {
|
||||
service1 Service1
|
||||
service2 Service2
|
||||
}
|
||||
|
||||
func (_ *impl1) GetService1() string {
|
||||
return "service1"
|
||||
}
|
||||
|
||||
func (_ *impl2) GetService2() string {
|
||||
return "service2"
|
||||
}
|
||||
|
||||
const (
|
||||
// service1 is the injection token for Service1
|
||||
service1 = InjectionToken("service1")
|
||||
// service2 is the injection token for Service2
|
||||
service2 = InjectionToken("service2")
|
||||
)
|
||||
|
||||
var (
|
||||
// complexServiceLenses provides type-safe accessors for ComplexService fields,
|
||||
// generated by the fp-go:Lens directive. These lenses are used in applicative
|
||||
// composition to build the ComplexService from individual dependencies.
|
||||
complexServiceLenses = MakeComplexServiceLenses()
|
||||
)
|
||||
|
||||
// makeSampleInjectionContainer creates an InjectionContainer that can resolve services by ID.
|
||||
// The container maps InjectionTokens to their corresponding service implementations.
|
||||
// It returns an error if a requested service is not available.
|
||||
func makeSampleInjectionContainer() InjectionContainer {
|
||||
|
||||
return func(token InjectionToken) Thunk[any] {
|
||||
switch token {
|
||||
case service1:
|
||||
return thunk.Of(any(&impl1{}))
|
||||
case service2:
|
||||
return thunk.Of(any(&impl2{}))
|
||||
default:
|
||||
return thunk.Left[any](errors.New("dependency not available"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleService1 is an Effect that depends type-safely on Service1.
|
||||
// It demonstrates how to write handlers that work with specific service interfaces
|
||||
// rather than the untyped container, providing compile-time type safety.
|
||||
func handleService1() Effect[Service1, string] {
|
||||
return func(ctx Service1) ReaderIOResult[string] {
|
||||
return thunk.Of(fmt.Sprintf("Service1: %s", ctx.GetService1()))
|
||||
}
|
||||
}
|
||||
|
||||
// handleComplexService is an Effect that depends on ComplexService, which itself
|
||||
// aggregates multiple service dependencies (Service1 and Service2).
|
||||
// This demonstrates how to work with composite dependencies in a type-safe manner.
|
||||
func handleComplexService() Effect[ComplexService, string] {
|
||||
return func(ctx ComplexService) ReaderIOResult[string] {
|
||||
return thunk.Of(fmt.Sprintf("ComplexService: %s x %s", ctx.service1.GetService1(), ctx.service2.GetService2()))
|
||||
}
|
||||
}
|
||||
|
||||
// lookupService1 is a lookup method that converts from an untyped InjectionContainer
|
||||
// to a typed Service1 dependency. It performs two steps:
|
||||
// 1. Read[any](service1) - retrieves the service from the container by token
|
||||
// 2. ChainResultK(result.InstanceOf[Service1]) - safely casts from any to Service1
|
||||
// This conversion provides type safety when moving from the untyped container to typed handlers.
|
||||
var lookupService1 = F.Flow2(
|
||||
Read[any](service1),
|
||||
thunk.ChainResultK(result.InstanceOf[Service1]),
|
||||
)
|
||||
|
||||
// lookupService2 is a lookup method for Service2, following the same pattern as lookupService1.
|
||||
// It retrieves Service2 from the container and safely casts it to the correct type.
|
||||
var lookupService2 = F.Flow2(
|
||||
Read[any](service2),
|
||||
thunk.ChainResultK(result.InstanceOf[Service2]),
|
||||
)
|
||||
|
||||
// lookupComplexService demonstrates applicative composition for complex dependency injection.
|
||||
// It builds a ComplexService by composing multiple service lookups:
|
||||
// 1. Do[InjectionContainer](ComplexService{}) - starts with an empty ComplexService in the Effect context
|
||||
// 2. ApSL(complexServiceLenses.service1, lookupService1) - looks up Service1 and sets it using the lens
|
||||
// 3. ApSL(complexServiceLenses.service2, lookupService2) - looks up Service2 and sets it using the lens
|
||||
//
|
||||
// This applicative style allows parallel composition of independent dependencies,
|
||||
// building the complete ComplexService from its constituent parts in a type-safe way.
|
||||
var lookupComplexService = F.Pipe2(
|
||||
Do[InjectionContainer](ComplexService{}),
|
||||
ApSL(complexServiceLenses.service1, lookupService1),
|
||||
ApSL(complexServiceLenses.service2, lookupService2),
|
||||
)
|
||||
|
||||
// handleResult is a curried function that combines results from two services.
|
||||
// It demonstrates how to compose the outputs of multiple effects into a final result.
|
||||
// The curried form allows it to be used with applicative composition (ApS).
|
||||
func handleResult(s1 string) func(string) string {
|
||||
return func(s2 string) string {
|
||||
return fmt.Sprintf("Final Result: %s : %s", s1, s2)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDependencyLookup demonstrates both simple and complex dependency injection patterns:
|
||||
//
|
||||
// Simple Pattern (handle1):
|
||||
// 1. Create an InjectionContainer with registered services
|
||||
// 2. Define a handler (handleService1) that depends on a single typed service interface
|
||||
// 3. Use a lookup method (lookupService1) to resolve the dependency from the container
|
||||
// 4. Compose the handler with the lookup using LocalThunkK to inject the dependency
|
||||
//
|
||||
// Complex Pattern (handleComplex):
|
||||
// 1. Define a handler (handleComplexService) that depends on a composite service (ComplexService)
|
||||
// 2. Use applicative composition (lookupComplexService) to build the composite from multiple lookups
|
||||
// 3. Each sub-dependency is resolved independently and combined using lenses
|
||||
// 4. LocalThunkK injects the complete composite dependency into the handler
|
||||
//
|
||||
// Service Composition:
|
||||
// - ApS combines the results of handle1 and handleComplex using handleResult
|
||||
// - This demonstrates how to compose multiple independent effects that share the same container
|
||||
// - The final result aggregates outputs from both simple and complex dependency patterns
|
||||
func TestDependencyLookup(t *testing.T) {
|
||||
|
||||
// Create the dependency injection container
|
||||
container := makeSampleInjectionContainer()
|
||||
|
||||
// Simple dependency injection: single service lookup
|
||||
// LocalThunkK transforms the handler to work with the container
|
||||
handle1 := F.Pipe1(
|
||||
handleService1(),
|
||||
LocalThunkK[string](lookupService1),
|
||||
)
|
||||
|
||||
// Complex dependency injection: composite service with multiple dependencies
|
||||
// lookupComplexService uses applicative composition to build ComplexService
|
||||
handleComplex := F.Pipe1(
|
||||
handleComplexService(),
|
||||
LocalThunkK[string](lookupComplexService),
|
||||
)
|
||||
|
||||
// Compose both services using applicative style
|
||||
// ApS applies handleResult to combine outputs from handle1 and handleComplex
|
||||
result := F.Pipe1(
|
||||
handle1,
|
||||
ApS(handleResult, handleComplex),
|
||||
)
|
||||
|
||||
// Execute: provide container, then context, then run the IO operation
|
||||
res := result(container)(t.Context())()
|
||||
|
||||
fmt.Println(res)
|
||||
|
||||
}
|
||||
85
v2/effect/monoid.go
Normal file
85
v2/effect/monoid.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
)
|
||||
|
||||
// ApplicativeMonoid creates a monoid for effects using applicative semantics.
|
||||
// This combines effects by running both and combining their results using the provided monoid.
|
||||
// If either effect fails, the combined effect fails.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The value type that has a monoid instance
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - m: The monoid instance for combining values of type A
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Monoid[Effect[C, A]]: A monoid for combining effects
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// stringMonoid := monoid.MakeMonoid(
|
||||
// func(a, b string) string { return a + b },
|
||||
// "",
|
||||
// )
|
||||
// effectMonoid := effect.ApplicativeMonoid[MyContext](stringMonoid)
|
||||
// eff1 := effect.Of[MyContext]("Hello")
|
||||
// eff2 := effect.Of[MyContext](" World")
|
||||
// combined := effectMonoid.Concat(eff1, eff2)
|
||||
// // combined produces "Hello World"
|
||||
func ApplicativeMonoid[C, A any](m monoid.Monoid[A]) Monoid[Effect[C, A]] {
|
||||
return readerreaderioresult.ApplicativeMonoid[C](m)
|
||||
}
|
||||
|
||||
// AlternativeMonoid creates a monoid for effects using alternative semantics.
|
||||
// This tries the first effect, and if it fails, tries the second effect.
|
||||
// If both succeed, their results are combined using the provided monoid.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The value type that has a monoid instance
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - m: The monoid instance for combining values of type A
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Monoid[Effect[C, A]]: A monoid for combining effects with fallback behavior
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// stringMonoid := monoid.MakeMonoid(
|
||||
// func(a, b string) string { return a + b },
|
||||
// "",
|
||||
// )
|
||||
// effectMonoid := effect.AlternativeMonoid[MyContext](stringMonoid)
|
||||
// eff1 := effect.Fail[MyContext, string](errors.New("failed"))
|
||||
// eff2 := effect.Of[MyContext]("fallback")
|
||||
// combined := effectMonoid.Concat(eff1, eff2)
|
||||
// // combined produces "fallback" (first failed, so second is used)
|
||||
func AlternativeMonoid[C, A any](m monoid.Monoid[A]) Monoid[Effect[C, A]] {
|
||||
return readerreaderioresult.AlternativeMonoid[C](m)
|
||||
}
|
||||
350
v2/effect/monoid_test.go
Normal file
350
v2/effect/monoid_test.go
Normal file
@@ -0,0 +1,350 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestApplicativeMonoid(t *testing.T) {
|
||||
t.Run("combines successful effects with string monoid", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext]("Hello")
|
||||
eff2 := Of[TestContext](" ")
|
||||
eff3 := Of[TestContext]("World")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, effectMonoid.Concat(eff2, eff3))
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Hello World", result)
|
||||
})
|
||||
|
||||
t.Run("combines successful effects with int monoid", func(t *testing.T) {
|
||||
intMonoid := monoid.MakeMonoid(
|
||||
func(a, b int) int { return a + b },
|
||||
0,
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Of[TestContext](10)
|
||||
eff2 := Of[TestContext](20)
|
||||
eff3 := Of[TestContext](30)
|
||||
|
||||
combined := effectMonoid.Concat(eff1, effectMonoid.Concat(eff2, eff3))
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 60, result)
|
||||
})
|
||||
|
||||
t.Run("returns empty value for empty monoid", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"empty",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
result, err := runEffect(effectMonoid.Empty(), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "empty", result)
|
||||
})
|
||||
|
||||
t.Run("propagates first error", func(t *testing.T) {
|
||||
expectedErr := errors.New("first error")
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](expectedErr)
|
||||
eff2 := Of[TestContext]("World")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
_, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("propagates second error", func(t *testing.T) {
|
||||
expectedErr := errors.New("second error")
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext]("Hello")
|
||||
eff2 := Fail[TestContext, string](expectedErr)
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
_, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("combines multiple effects", func(t *testing.T) {
|
||||
intMonoid := monoid.MakeMonoid(
|
||||
func(a, b int) int { return a * b },
|
||||
1,
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
|
||||
effects := []Effect[TestContext, int]{
|
||||
Of[TestContext](2),
|
||||
Of[TestContext](3),
|
||||
Of[TestContext](4),
|
||||
Of[TestContext](5),
|
||||
}
|
||||
|
||||
combined := effectMonoid.Empty()
|
||||
for _, eff := range effects {
|
||||
combined = effectMonoid.Concat(combined, eff)
|
||||
}
|
||||
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 120, result) // 1 * 2 * 3 * 4 * 5
|
||||
})
|
||||
|
||||
t.Run("works with custom types", func(t *testing.T) {
|
||||
type Counter struct {
|
||||
Count int
|
||||
}
|
||||
|
||||
counterMonoid := monoid.MakeMonoid(
|
||||
func(a, b Counter) Counter {
|
||||
return Counter{Count: a.Count + b.Count}
|
||||
},
|
||||
Counter{Count: 0},
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext](counterMonoid)
|
||||
|
||||
eff1 := Of[TestContext](Counter{Count: 5})
|
||||
eff2 := Of[TestContext](Counter{Count: 10})
|
||||
eff3 := Of[TestContext](Counter{Count: 15})
|
||||
|
||||
combined := effectMonoid.Concat(eff1, effectMonoid.Concat(eff2, eff3))
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result.Count)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAlternativeMonoid(t *testing.T) {
|
||||
t.Run("combines successful effects with monoid", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext]("First")
|
||||
eff2 := Of[TestContext]("Second")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "FirstSecond", result) // Alternative still combines when both succeed
|
||||
})
|
||||
|
||||
t.Run("tries second effect if first fails", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](errors.New("first failed"))
|
||||
eff2 := Of[TestContext]("Second")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Second", result)
|
||||
})
|
||||
|
||||
t.Run("returns error if all effects fail", func(t *testing.T) {
|
||||
expectedErr := errors.New("second error")
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](errors.New("first error"))
|
||||
eff2 := Fail[TestContext, string](expectedErr)
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
_, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("returns empty value for empty monoid", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + b },
|
||||
"default",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
result, err := runEffect(effectMonoid.Empty(), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "default", result)
|
||||
})
|
||||
|
||||
t.Run("chains multiple alternatives", func(t *testing.T) {
|
||||
intMonoid := monoid.MakeMonoid(
|
||||
func(a, b int) int { return a + b },
|
||||
0,
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, int](errors.New("error 1"))
|
||||
eff2 := Fail[TestContext, int](errors.New("error 2"))
|
||||
eff3 := Of[TestContext](42)
|
||||
eff4 := Of[TestContext](100)
|
||||
|
||||
combined := effectMonoid.Concat(
|
||||
effectMonoid.Concat(eff1, eff2),
|
||||
effectMonoid.Concat(eff3, eff4),
|
||||
)
|
||||
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 142, result) // Combines successful values: 42 + 100
|
||||
})
|
||||
|
||||
t.Run("works with custom types", func(t *testing.T) {
|
||||
type Result struct {
|
||||
Value string
|
||||
Code int
|
||||
}
|
||||
|
||||
resultMonoid := monoid.MakeMonoid(
|
||||
func(a, b Result) Result {
|
||||
return Result{Value: a.Value + b.Value, Code: a.Code + b.Code}
|
||||
},
|
||||
Result{Value: "", Code: 0},
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext](resultMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, Result](errors.New("failed"))
|
||||
eff2 := Of[TestContext](Result{Value: "success", Code: 200})
|
||||
eff3 := Of[TestContext](Result{Value: "backup", Code: 201})
|
||||
|
||||
combined := effectMonoid.Concat(effectMonoid.Concat(eff1, eff2), eff3)
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "successbackup", result.Value) // Combines both successful values
|
||||
assert.Equal(t, 401, result.Code) // 200 + 201
|
||||
})
|
||||
}
|
||||
|
||||
func TestMonoidComparison(t *testing.T) {
|
||||
t.Run("ApplicativeMonoid vs AlternativeMonoid with all success", func(t *testing.T) {
|
||||
stringMonoid := monoid.MakeMonoid(
|
||||
func(a, b string) string { return a + "," + b },
|
||||
"",
|
||||
)
|
||||
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext]("A")
|
||||
eff2 := Of[TestContext]("B")
|
||||
|
||||
// Applicative combines values
|
||||
applicativeResult, err1 := runEffect(
|
||||
applicativeMonoid.Concat(eff1, eff2),
|
||||
TestContext{Value: "test"},
|
||||
)
|
||||
|
||||
// Alternative takes first
|
||||
alternativeResult, err2 := runEffect(
|
||||
alternativeMonoid.Concat(eff1, eff2),
|
||||
TestContext{Value: "test"},
|
||||
)
|
||||
|
||||
assert.NoError(t, err1)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, "A,B", applicativeResult) // Combined with comma separator
|
||||
assert.Equal(t, "A,B", alternativeResult) // Also combined (Alternative uses Alt semigroup)
|
||||
})
|
||||
|
||||
t.Run("ApplicativeMonoid vs AlternativeMonoid with failures", func(t *testing.T) {
|
||||
intMonoid := monoid.MakeMonoid(
|
||||
func(a, b int) int { return a + b },
|
||||
0,
|
||||
)
|
||||
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, int](errors.New("error 1"))
|
||||
eff2 := Of[TestContext](42)
|
||||
|
||||
// Applicative fails on first error
|
||||
_, err1 := runEffect(
|
||||
applicativeMonoid.Concat(eff1, eff2),
|
||||
TestContext{Value: "test"},
|
||||
)
|
||||
|
||||
// Alternative tries second on first failure
|
||||
result2, err2 := runEffect(
|
||||
alternativeMonoid.Concat(eff1, eff2),
|
||||
TestContext{Value: "test"},
|
||||
)
|
||||
|
||||
assert.Error(t, err1)
|
||||
assert.NoError(t, err2)
|
||||
assert.Equal(t, 42, result2)
|
||||
})
|
||||
}
|
||||
62
v2/effect/retry.go
Normal file
62
v2/effect/retry.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/retry"
|
||||
)
|
||||
|
||||
// Retrying executes an effect with retry logic based on a policy and check predicate.
|
||||
// The effect is retried according to the policy until either:
|
||||
// - The effect succeeds and the check predicate returns false
|
||||
// - The retry policy is exhausted
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - policy: The retry policy defining retry limits and delays
|
||||
// - action: An effectful computation that receives retry status and produces a value
|
||||
// - check: A predicate that determines if the result should trigger a retry
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Effect[C, A]: An effect that retries according to the policy
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// policy := retry.LimitRetries(3)
|
||||
// eff := effect.Retrying[MyContext, string](
|
||||
// policy,
|
||||
// func(status retry.RetryStatus) Effect[MyContext, string] {
|
||||
// return fetchData() // may fail
|
||||
// },
|
||||
// func(result Result[string]) bool {
|
||||
// return result.IsLeft() // retry on error
|
||||
// },
|
||||
// )
|
||||
// // Retries up to 3 times if fetchData fails
|
||||
func Retrying[C, A any](
|
||||
policy retry.RetryPolicy,
|
||||
action Kleisli[C, retry.RetryStatus, A],
|
||||
check Predicate[Result[A]],
|
||||
) Effect[C, A] {
|
||||
return readerreaderioresult.Retrying(policy, action, check)
|
||||
}
|
||||
377
v2/effect/retry_test.go
Normal file
377
v2/effect/retry_test.go
Normal file
@@ -0,0 +1,377 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
"github.com/IBM/fp-go/v2/retry"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRetrying(t *testing.T) {
|
||||
t.Run("succeeds on first attempt", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(3)
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success", result)
|
||||
assert.Equal(t, 1, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("retries on failure and eventually succeeds", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
if attemptCount < 3 {
|
||||
return Fail[TestContext, string](errors.New("temporary error"))
|
||||
}
|
||||
return Of[TestContext]("success after retries")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success after retries", result)
|
||||
assert.Equal(t, 3, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("exhausts retry limit", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
maxRetries := uint(3)
|
||||
policy := retry.LimitRetries(maxRetries)
|
||||
|
||||
eff := Retrying(
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
return Fail[TestContext, string](errors.New("persistent error"))
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
},
|
||||
)
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, int(maxRetries+1), attemptCount) // initial attempt + retries
|
||||
})
|
||||
|
||||
t.Run("does not retry on success", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
|
||||
eff := Retrying[TestContext, int](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, int] {
|
||||
attemptCount++
|
||||
return Of[TestContext](42)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, result)
|
||||
assert.Equal(t, 1, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("uses custom retry predicate", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
|
||||
eff := Retrying[TestContext, int](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, int] {
|
||||
attemptCount++
|
||||
return Of[TestContext](attemptCount * 10)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
// Retry if value is less than 30
|
||||
if result.IsRight(res) {
|
||||
val, _ := result.Unwrap(res)
|
||||
return val < 30
|
||||
}
|
||||
return true
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result)
|
||||
assert.Equal(t, 3, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("tracks retry status", func(t *testing.T) {
|
||||
var statuses []retry.RetryStatus
|
||||
policy := retry.LimitRetries(3)
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
statuses = append(statuses, status)
|
||||
if len(statuses) < 3 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext]("done")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "done", result)
|
||||
assert.Len(t, statuses, 3)
|
||||
// First attempt has iteration 0
|
||||
assert.Equal(t, uint(0), statuses[0].IterNumber)
|
||||
assert.Equal(t, uint(1), statuses[1].IterNumber)
|
||||
assert.Equal(t, uint(2), statuses[2].IterNumber)
|
||||
})
|
||||
|
||||
t.Run("works with exponential backoff", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.Monoid.Concat(
|
||||
retry.LimitRetries(3),
|
||||
retry.ExponentialBackoff(10*time.Millisecond),
|
||||
)
|
||||
|
||||
startTime := time.Now()
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
if attemptCount < 3 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
elapsed := time.Since(startTime)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success", result)
|
||||
assert.Equal(t, 3, attemptCount)
|
||||
// Should have some delay due to backoff
|
||||
assert.Greater(t, elapsed, 10*time.Millisecond)
|
||||
})
|
||||
|
||||
t.Run("combines with other effect operations", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(3)
|
||||
|
||||
eff := Map[TestContext](func(s string) string {
|
||||
return "mapped: " + s
|
||||
})(Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "mapped: success", result)
|
||||
assert.Equal(t, 2, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("retries with different error types", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
errors := []error{
|
||||
errors.New("error 1"),
|
||||
errors.New("error 2"),
|
||||
errors.New("error 3"),
|
||||
}
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
if attemptCount < len(errors) {
|
||||
err := errors[attemptCount]
|
||||
attemptCount++
|
||||
return Fail[TestContext, string](err)
|
||||
}
|
||||
attemptCount++
|
||||
return Of[TestContext]("finally succeeded")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "finally succeeded", result)
|
||||
assert.Equal(t, 4, attemptCount)
|
||||
})
|
||||
|
||||
t.Run("no retry when predicate returns false", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
|
||||
eff := Retrying(
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
return Fail[TestContext, string](errors.New("error"))
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return false // never retry
|
||||
},
|
||||
)
|
||||
|
||||
_, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 1, attemptCount) // only initial attempt
|
||||
})
|
||||
|
||||
t.Run("retries with context access", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(3)
|
||||
ctx := TestContext{Value: "retry-context"}
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext]("success with context")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, ctx)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "success with context", result)
|
||||
assert.Equal(t, 2, attemptCount)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRetryingWithComplexScenarios(t *testing.T) {
|
||||
t.Run("retry with state accumulation", func(t *testing.T) {
|
||||
type State struct {
|
||||
Attempts []int
|
||||
Value string
|
||||
}
|
||||
|
||||
policy := retry.LimitRetries(4)
|
||||
|
||||
eff := Retrying[TestContext, State](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, State] {
|
||||
state := State{
|
||||
Attempts: make([]int, status.IterNumber+1),
|
||||
Value: "attempt",
|
||||
}
|
||||
for i := uint(0); i <= status.IterNumber; i++ {
|
||||
state.Attempts[i] = int(i)
|
||||
}
|
||||
|
||||
if status.IterNumber < 2 {
|
||||
return Fail[TestContext, State](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext](state)
|
||||
},
|
||||
func(res Result[State]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
)
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "attempt", result.Value)
|
||||
assert.Equal(t, []int{0, 1, 2}, result.Attempts)
|
||||
})
|
||||
|
||||
t.Run("retry with chain operations", func(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(3)
|
||||
|
||||
eff := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext]("final: " + string(rune('0'+x)))
|
||||
})(Retrying[TestContext, int](
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, int] {
|
||||
attemptCount++
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, int](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext](attemptCount)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
return result.IsLeft(res)
|
||||
},
|
||||
))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, result, "final:")
|
||||
})
|
||||
}
|
||||
88
v2/effect/run.go
Normal file
88
v2/effect/run.go
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/idiomatic/context/readerresult"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
// Provide supplies a context to an effect, converting it to a Thunk.
|
||||
// This is the first step in running an effect - it eliminates the context dependency
|
||||
// by providing the required context value.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effect
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - c: The context value to provide to the effect
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - func(Effect[C, A]) ReaderIOResult[A]: A function that converts an effect to a thunk
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ctx := MyContext{APIKey: "secret"}
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// thunk := effect.Provide[MyContext, int](ctx)(eff)
|
||||
// // thunk is now a ReaderIOResult[int] that can be run
|
||||
func Provide[A, C any](c C) func(Effect[C, A]) ReaderIOResult[A] {
|
||||
return readerreaderioresult.Read[A](c)
|
||||
}
|
||||
|
||||
// RunSync executes a Thunk synchronously, converting it to a standard Go function.
|
||||
// This is the final step in running an effect - it executes the IO operations
|
||||
// and returns the result as a standard (value, error) tuple.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The type of the success value
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - fa: The thunk to execute
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - readerresult.ReaderResult[A]: A function that takes a context.Context and returns (A, error)
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// ctx := MyContext{APIKey: "secret"}
|
||||
// eff := effect.Of[MyContext](42)
|
||||
// thunk := effect.Provide[MyContext, int](ctx)(eff)
|
||||
// readerResult := effect.RunSync(thunk)
|
||||
// value, err := readerResult(context.Background())
|
||||
// // value == 42, err == nil
|
||||
//
|
||||
// # Complete Example
|
||||
//
|
||||
// // Typical usage pattern:
|
||||
// result, err := effect.RunSync(
|
||||
// effect.Provide[MyContext, string](myContext)(myEffect),
|
||||
// )(context.Background())
|
||||
func RunSync[A any](fa ReaderIOResult[A]) readerresult.ReaderResult[A] {
|
||||
return func(ctx context.Context) (A, error) {
|
||||
return result.Unwrap(fa(ctx)())
|
||||
}
|
||||
}
|
||||
326
v2/effect/run_test.go
Normal file
326
v2/effect/run_test.go
Normal file
@@ -0,0 +1,326 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestProvide(t *testing.T) {
|
||||
t.Run("provides context to effect", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test-value"}
|
||||
eff := Of[TestContext]("result")
|
||||
|
||||
ioResult := Provide[string](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", result)
|
||||
})
|
||||
|
||||
t.Run("provides context with specific values", func(t *testing.T) {
|
||||
type Config struct {
|
||||
Host string
|
||||
Port int
|
||||
}
|
||||
|
||||
cfg := Config{Host: "localhost", Port: 8080}
|
||||
eff := Of[Config]("connected")
|
||||
|
||||
ioResult := Provide[string](cfg)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "connected", result)
|
||||
})
|
||||
|
||||
t.Run("propagates errors", func(t *testing.T) {
|
||||
expectedErr := errors.New("provide error")
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Fail[TestContext, string](expectedErr)
|
||||
|
||||
ioResult := Provide[string](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("works with different context types", func(t *testing.T) {
|
||||
type SimpleContext struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
ctx := SimpleContext{ID: 42}
|
||||
eff := Of[SimpleContext](100)
|
||||
|
||||
ioResult := Provide[int](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100, result)
|
||||
})
|
||||
|
||||
t.Run("provides context to chained effects", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "base"}
|
||||
|
||||
eff := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext]("result")
|
||||
})(Of[TestContext](42))
|
||||
|
||||
ioResult := Provide[string](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "result", result)
|
||||
})
|
||||
|
||||
t.Run("provides context to mapped effects", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Map[TestContext](func(x int) string {
|
||||
return "mapped"
|
||||
})(Of[TestContext](42))
|
||||
|
||||
ioResult := Provide[string](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "mapped", result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunSync(t *testing.T) {
|
||||
t.Run("runs effect synchronously", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext](42)
|
||||
|
||||
ioResult := Provide[int](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("runs effect with context.Context", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext]("hello")
|
||||
|
||||
ioResult := Provide[string](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
|
||||
bgCtx := context.Background()
|
||||
result, err := readerResult(bgCtx)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "hello", result)
|
||||
})
|
||||
|
||||
t.Run("propagates errors synchronously", func(t *testing.T) {
|
||||
expectedErr := errors.New("sync error")
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Fail[TestContext, int](expectedErr)
|
||||
|
||||
ioResult := Provide[int](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("runs complex effect chains", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x + 10)
|
||||
})(Of[TestContext](5)))
|
||||
|
||||
ioResult := Provide[int](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 30, result) // (5 + 10) * 2
|
||||
})
|
||||
|
||||
t.Run("handles multiple sequential runs", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext](42)
|
||||
|
||||
ioResult := Provide[int](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
|
||||
// Run multiple times
|
||||
result1, err1 := readerResult(context.Background())
|
||||
result2, err2 := readerResult(context.Background())
|
||||
result3, err3 := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err1)
|
||||
assert.NoError(t, err2)
|
||||
assert.NoError(t, err3)
|
||||
assert.Equal(t, 42, result1)
|
||||
assert.Equal(t, 42, result2)
|
||||
assert.Equal(t, 42, result3)
|
||||
})
|
||||
|
||||
t.Run("works with different result types", func(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
ctx := TestContext{Value: "test"}
|
||||
user := User{Name: "Alice", Age: 30}
|
||||
eff := Of[TestContext](user)
|
||||
|
||||
ioResult := Provide[User](ctx)(eff)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, user, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
t.Run("complete workflow with success", func(t *testing.T) {
|
||||
type AppConfig struct {
|
||||
APIKey string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
cfg := AppConfig{APIKey: "secret", Timeout: 30}
|
||||
|
||||
// Create an effect that uses the config
|
||||
eff := Of[AppConfig]("API call successful")
|
||||
|
||||
// Provide config and run
|
||||
result, err := RunSync(Provide[string](cfg)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "API call successful", result)
|
||||
})
|
||||
|
||||
t.Run("complete workflow with error", func(t *testing.T) {
|
||||
type AppConfig struct {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
expectedErr := errors.New("API error")
|
||||
cfg := AppConfig{APIKey: "secret"}
|
||||
|
||||
eff := Fail[AppConfig, string](expectedErr)
|
||||
|
||||
_, err := RunSync(Provide[string](cfg)(eff))(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("workflow with transformations", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Map[TestContext](func(x int) string {
|
||||
return "final"
|
||||
})(Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(Of[TestContext](21)))
|
||||
|
||||
result, err := RunSync(Provide[string](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "final", result)
|
||||
})
|
||||
|
||||
t.Run("workflow with bind operations", func(t *testing.T) {
|
||||
type State struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Bind(
|
||||
func(y int) func(State) State {
|
||||
return func(s State) State {
|
||||
s.Y = y
|
||||
return s
|
||||
}
|
||||
},
|
||||
func(s State) Effect[TestContext, int] {
|
||||
return Of[TestContext](s.X * 2)
|
||||
},
|
||||
)(BindTo[TestContext](func(x int) State {
|
||||
return State{X: x}
|
||||
})(Of[TestContext](10)))
|
||||
|
||||
result, err := RunSync(Provide[State](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 10, result.X)
|
||||
assert.Equal(t, 20, result.Y)
|
||||
})
|
||||
|
||||
t.Run("workflow with context transformation", func(t *testing.T) {
|
||||
type OuterCtx struct {
|
||||
Value string
|
||||
}
|
||||
type InnerCtx struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
outerCtx := OuterCtx{Value: "outer"}
|
||||
innerEff := Of[InnerCtx]("inner result")
|
||||
|
||||
// Transform context
|
||||
transformedEff := Local[string](func(outer OuterCtx) InnerCtx {
|
||||
return InnerCtx{Data: outer.Value + "-transformed"}
|
||||
})(innerEff)
|
||||
|
||||
result, err := RunSync(Provide[string](outerCtx)(transformedEff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "inner result", result)
|
||||
})
|
||||
|
||||
t.Run("workflow with array traversal", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
|
||||
eff := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(input)
|
||||
|
||||
result, err := RunSync(Provide[[]int](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{2, 4, 6, 8, 10}, result)
|
||||
})
|
||||
}
|
||||
55
v2/effect/traverse.go
Normal file
55
v2/effect/traverse.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// 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 effect
|
||||
|
||||
import "github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
|
||||
// TraverseArray applies an effectful function to each element of an array,
|
||||
// collecting the results into a new array. If any effect fails, the entire
|
||||
// traversal fails and returns the first error encountered.
|
||||
//
|
||||
// This is useful for performing effectful operations on collections while
|
||||
// maintaining the sequential order of results.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - C: The context type required by the effects
|
||||
// - A: The input element type
|
||||
// - B: The output element type
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: An effectful function to apply to each element
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// - Kleisli[C, []A, []B]: A function that transforms an array of A to an effect producing an array of B
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// parseIntEff := func(s string) Effect[MyContext, int] {
|
||||
// val, err := strconv.Atoi(s)
|
||||
// if err != nil {
|
||||
// return effect.Fail[MyContext, int](err)
|
||||
// }
|
||||
// return effect.Of[MyContext](val)
|
||||
// }
|
||||
// input := []string{"1", "2", "3"}
|
||||
// eff := effect.TraverseArray[MyContext](parseIntEff)(input)
|
||||
// // eff produces []int{1, 2, 3}
|
||||
func TraverseArray[C, A, B any](f Kleisli[C, A, B]) Kleisli[C, []A, []B] {
|
||||
return readerreaderioresult.TraverseArray(f)
|
||||
}
|
||||
266
v2/effect/traverse_test.go
Normal file
266
v2/effect/traverse_test.go
Normal file
@@ -0,0 +1,266 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTraverseArray(t *testing.T) {
|
||||
t.Run("traverses empty array", func(t *testing.T) {
|
||||
input := []int{}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, result)
|
||||
})
|
||||
|
||||
t.Run("traverses array with single element", func(t *testing.T) {
|
||||
input := []int{42}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{"42"}, result)
|
||||
})
|
||||
|
||||
t.Run("traverses array with multiple elements", func(t *testing.T) {
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{"1", "2", "3", "4", "5"}, result)
|
||||
})
|
||||
|
||||
t.Run("transforms to different type", func(t *testing.T) {
|
||||
input := []string{"hello", "world", "test"}
|
||||
kleisli := TraverseArray(func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext](len(s))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{5, 5, 4}, result)
|
||||
})
|
||||
|
||||
t.Run("stops on first error", func(t *testing.T) {
|
||||
expectedErr := errors.New("traverse error")
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 3 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("handles complex transformations", func(t *testing.T) {
|
||||
type User struct {
|
||||
ID int
|
||||
Name string
|
||||
}
|
||||
|
||||
input := []int{1, 2, 3}
|
||||
kleisli := TraverseArray(func(id int) Effect[TestContext, User] {
|
||||
return Of[TestContext](User{
|
||||
ID: id,
|
||||
Name: fmt.Sprintf("User%d", id),
|
||||
})
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, result, 3)
|
||||
assert.Equal(t, 1, result[0].ID)
|
||||
assert.Equal(t, "User1", result[0].Name)
|
||||
assert.Equal(t, 2, result[1].ID)
|
||||
assert.Equal(t, "User2", result[1].Name)
|
||||
assert.Equal(t, 3, result[2].ID)
|
||||
assert.Equal(t, "User3", result[2].Name)
|
||||
})
|
||||
|
||||
t.Run("chains with other operations", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
|
||||
eff := Chain(func(strings []string) Effect[TestContext, int] {
|
||||
total := 0
|
||||
for _, s := range strings {
|
||||
val, _ := strconv.Atoi(s)
|
||||
total += val
|
||||
}
|
||||
return Of[TestContext](total)
|
||||
})(TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x * 2))
|
||||
})(input))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 12, result) // (1*2) + (2*2) + (3*2) = 2 + 4 + 6 = 12
|
||||
})
|
||||
|
||||
t.Run("uses context in transformation", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Chain(func(ctx TestContext) Effect[TestContext, string] {
|
||||
return Of[TestContext](fmt.Sprintf("%s-%d", ctx.Value, x))
|
||||
})(Of[TestContext](TestContext{Value: "prefix"}))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{"prefix-1", "prefix-2", "prefix-3"}, result)
|
||||
})
|
||||
|
||||
t.Run("preserves order", func(t *testing.T) {
|
||||
input := []int{5, 3, 8, 1, 9, 2}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 10)
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{50, 30, 80, 10, 90, 20}, result)
|
||||
})
|
||||
|
||||
t.Run("handles large arrays", func(t *testing.T) {
|
||||
size := 1000
|
||||
input := make([]int, size)
|
||||
for i := 0; i < size; i++ {
|
||||
input[i] = i
|
||||
}
|
||||
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, result, size)
|
||||
assert.Equal(t, 0, result[0])
|
||||
assert.Equal(t, 1998, result[999])
|
||||
})
|
||||
|
||||
t.Run("composes multiple traversals", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
|
||||
// First traversal: int -> string
|
||||
kleisli1 := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
// Second traversal: string -> int (length)
|
||||
kleisli2 := TraverseArray(func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext](len(s))
|
||||
})
|
||||
|
||||
eff := Chain(kleisli2)(kleisli1(input))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{1, 1, 1}, result) // All single-digit numbers have length 1
|
||||
})
|
||||
|
||||
t.Run("handles nil array", func(t *testing.T) {
|
||||
var input []int
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, result) // TraverseArray returns empty slice for nil input
|
||||
})
|
||||
|
||||
t.Run("works with Map for post-processing", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
|
||||
eff := Map[TestContext](func(strings []string) string {
|
||||
result := ""
|
||||
for _, s := range strings {
|
||||
result += s + ","
|
||||
}
|
||||
return result
|
||||
})(TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})(input))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1,2,3,", result)
|
||||
})
|
||||
|
||||
t.Run("error in middle of array", func(t *testing.T) {
|
||||
expectedErr := errors.New("middle error")
|
||||
input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 5 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
|
||||
t.Run("error at end of array", func(t *testing.T) {
|
||||
expectedErr := errors.New("end error")
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 5 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
})
|
||||
}
|
||||
92
v2/effect/types.go
Normal file
92
v2/effect/types.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// 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 effect
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/context/readerioresult"
|
||||
"github.com/IBM/fp-go/v2/context/readerreaderioresult"
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/ioeither"
|
||||
"github.com/IBM/fp-go/v2/ioresult"
|
||||
"github.com/IBM/fp-go/v2/lazy"
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/IBM/fp-go/v2/predicate"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
)
|
||||
|
||||
type (
|
||||
// Either represents a value that can be either a Left (error) or Right (success).
|
||||
Either[E, A any] = either.Either[E, A]
|
||||
|
||||
// Reader represents a computation that depends on a context R and produces a value A.
|
||||
Reader[R, A any] = reader.Reader[R, A]
|
||||
|
||||
// ReaderIO represents a computation that depends on a context R and produces an IO action returning A.
|
||||
ReaderIO[R, A any] = readerio.ReaderIO[R, A]
|
||||
|
||||
// IO represents a synchronous side effect that produces a value A.
|
||||
IO[A any] = io.IO[A]
|
||||
|
||||
// IOEither represents a synchronous side effect that can fail with error E or succeed with value A.
|
||||
IOEither[E, A any] = ioeither.IOEither[E, A]
|
||||
|
||||
// Lazy represents a lazily evaluated computation that produces a value A.
|
||||
Lazy[A any] = lazy.Lazy[A]
|
||||
|
||||
// IOResult represents a synchronous side effect that can fail with an error or succeed with value A.
|
||||
IOResult[A any] = ioresult.IOResult[A]
|
||||
|
||||
// ReaderIOResult represents a computation that depends on context and performs IO with error handling.
|
||||
ReaderIOResult[A any] = readerioresult.ReaderIOResult[A]
|
||||
|
||||
// Monoid represents an algebraic structure with an associative binary operation and an identity element.
|
||||
Monoid[A any] = monoid.Monoid[A]
|
||||
|
||||
// Effect represents an effectful computation that:
|
||||
// - Requires a context of type C
|
||||
// - Can perform I/O operations
|
||||
// - Can fail with an error
|
||||
// - Produces a value of type A on success
|
||||
//
|
||||
// This is the core type of the effect package, providing a complete effect system
|
||||
// for managing dependencies, errors, and side effects in a composable way.
|
||||
Effect[C, A any] = readerreaderioresult.ReaderReaderIOResult[C, A]
|
||||
|
||||
// Thunk represents a computation that performs IO with error handling but doesn't require context.
|
||||
// It's equivalent to ReaderIOResult and is used as an intermediate step when providing context to an Effect.
|
||||
Thunk[A any] = ReaderIOResult[A]
|
||||
|
||||
// Predicate represents a function that tests a value of type A and returns a boolean.
|
||||
Predicate[A any] = predicate.Predicate[A]
|
||||
|
||||
// Result represents a computation result that can be either an error (Left) or a success value (Right).
|
||||
Result[A any] = result.Result[A]
|
||||
|
||||
// Lens represents an optic for focusing on a field T within a structure S.
|
||||
Lens[S, T any] = lens.Lens[S, T]
|
||||
|
||||
// Kleisli represents a function from A to Effect[C, B], enabling monadic composition.
|
||||
// It's the fundamental building block for chaining effectful computations.
|
||||
Kleisli[C, A, B any] = readerreaderioresult.Kleisli[C, A, B]
|
||||
|
||||
// Operator represents a function that transforms Effect[C, A] to Effect[C, B].
|
||||
// It's used for lifting operations over effects.
|
||||
Operator[C, A, B any] = readerreaderioresult.Operator[C, A, B]
|
||||
)
|
||||
@@ -188,6 +188,81 @@ func MonadChain[E, A, B any](fa Either[E, A], f Kleisli[E, A, B]) Either[E, B] {
|
||||
return f(fa.r)
|
||||
}
|
||||
|
||||
// MonadChainLeft sequences a computation on the Left (error) value, allowing error recovery or transformation.
|
||||
// If the Either is Left, applies the provided function to the error value, which returns a new Either.
|
||||
// If the Either is Right, returns the Right value unchanged with the new error type.
|
||||
//
|
||||
// This is the dual of [MonadChain] - while MonadChain operates on Right values (success),
|
||||
// MonadChainLeft operates on Left values (errors). It's useful for error recovery, error transformation,
|
||||
// or chaining alternative computations when an error occurs.
|
||||
//
|
||||
// Note: MonadChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// The error type can be transformed from EA to EB, allowing flexible error type conversions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Error recovery: convert specific errors to success
|
||||
// result := either.MonadChainLeft(
|
||||
// either.Left[int](errors.New("not found")),
|
||||
// func(err error) either.Either[string, int] {
|
||||
// if err.Error() == "not found" {
|
||||
// return either.Right[string](0) // default value
|
||||
// }
|
||||
// return either.Left[int](err.Error()) // transform error
|
||||
// },
|
||||
// ) // Right(0)
|
||||
//
|
||||
// // Error transformation: change error type
|
||||
// result := either.MonadChainLeft(
|
||||
// either.Left[int](404),
|
||||
// func(code int) either.Either[string, int] {
|
||||
// return either.Left[int](fmt.Sprintf("Error code: %d", code))
|
||||
// },
|
||||
// ) // Left("Error code: 404")
|
||||
//
|
||||
// // Right values pass through unchanged
|
||||
// result := either.MonadChainLeft(
|
||||
// either.Right[error](42),
|
||||
// func(err error) either.Either[string, int] {
|
||||
// return either.Left[int]("error")
|
||||
// },
|
||||
// ) // Right(42)
|
||||
//
|
||||
//go:inline
|
||||
func MonadChainLeft[EA, EB, A any](fa Either[EA, A], f Kleisli[EB, EA, A]) Either[EB, A] {
|
||||
return MonadFold(fa, f, Of[EB])
|
||||
}
|
||||
|
||||
// ChainLeft is the curried version of [MonadChainLeft].
|
||||
// Returns a function that sequences a computation on the Left (error) value.
|
||||
//
|
||||
// Note: ChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// This is useful for creating reusable error handlers or transformers that can be
|
||||
// composed with other Either operations using pipes or function composition.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Create a reusable error handler
|
||||
// handleNotFound := either.ChainLeft[error, string](func(err error) either.Either[string, int] {
|
||||
// if err.Error() == "not found" {
|
||||
// return either.Right[string](0)
|
||||
// }
|
||||
// return either.Left[int](err.Error())
|
||||
// })
|
||||
//
|
||||
// // Use in a pipeline
|
||||
// result := F.Pipe1(
|
||||
// either.Left[int](errors.New("not found")),
|
||||
// handleNotFound,
|
||||
// ) // Right(0)
|
||||
//
|
||||
//go:inline
|
||||
func ChainLeft[EA, EB, A any](f Kleisli[EB, EA, A]) Kleisli[EB, Either[EA, A], A] {
|
||||
return Fold(f, Of[EB])
|
||||
}
|
||||
|
||||
// MonadChainFirst executes a side-effect computation but returns the original value.
|
||||
// Useful for performing actions (like logging) without changing the value.
|
||||
//
|
||||
@@ -471,6 +546,8 @@ func Alt[E, A any](that Lazy[Either[E, A]]) Operator[E, A, A] {
|
||||
// If the Either is Left, it applies the provided function to the error value,
|
||||
// which returns a new Either that replaces the original.
|
||||
//
|
||||
// Note: OrElse is identical to [ChainLeft] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// This is useful for error recovery, fallback logic, or chaining alternative computations.
|
||||
// The error type can be widened from E1 to E2, allowing transformation of error types.
|
||||
//
|
||||
@@ -504,7 +581,7 @@ func ToType[A, E any](onError func(any) E) func(any) Either[E, A] {
|
||||
return func(value any) Either[E, A] {
|
||||
return F.Pipe2(
|
||||
value,
|
||||
O.ToType[A],
|
||||
O.InstanceOf[A],
|
||||
O.Fold(F.Nullary3(F.Constant(value), onError, Left[A, E]), Right[E, A]),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -124,79 +124,52 @@ func TestStringer(t *testing.T) {
|
||||
func TestZeroWithIntegers(t *testing.T) {
|
||||
e := Zero[error, int]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
assert.False(t, IsLeft(e), "Zero should not create a Left value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Equal(t, 0, value, "Right value should be zero for int")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](0), e, "Zero should create a Right value with zero for int")
|
||||
}
|
||||
|
||||
// TestZeroWithStrings tests Zero function with string types
|
||||
func TestZeroWithStrings(t *testing.T) {
|
||||
e := Zero[error, string]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
assert.False(t, IsLeft(e), "Zero should not create a Left value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Equal(t, "", value, "Right value should be empty string")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](""), e, "Zero should create a Right value with empty string")
|
||||
}
|
||||
|
||||
// TestZeroWithBooleans tests Zero function with boolean types
|
||||
func TestZeroWithBooleans(t *testing.T) {
|
||||
e := Zero[error, bool]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Equal(t, false, value, "Right value should be false for bool")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](false), e, "Zero should create a Right value with false for bool")
|
||||
}
|
||||
|
||||
// TestZeroWithFloats tests Zero function with float types
|
||||
func TestZeroWithFloats(t *testing.T) {
|
||||
e := Zero[error, float64]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Equal(t, 0.0, value, "Right value should be 0.0 for float64")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](0.0), e, "Zero should create a Right value with 0.0 for float64")
|
||||
}
|
||||
|
||||
// TestZeroWithPointers tests Zero function with pointer types
|
||||
func TestZeroWithPointers(t *testing.T) {
|
||||
e := Zero[error, *int]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Nil(t, value, "Right value should be nil for pointer type")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
var nilPtr *int
|
||||
assert.Equal(t, Of[error](nilPtr), e, "Zero should create a Right value with nil pointer")
|
||||
}
|
||||
|
||||
// TestZeroWithSlices tests Zero function with slice types
|
||||
func TestZeroWithSlices(t *testing.T) {
|
||||
e := Zero[error, []int]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Nil(t, value, "Right value should be nil for slice type")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
var nilSlice []int
|
||||
assert.Equal(t, Of[error](nilSlice), e, "Zero should create a Right value with nil slice")
|
||||
}
|
||||
|
||||
// TestZeroWithMaps tests Zero function with map types
|
||||
func TestZeroWithMaps(t *testing.T) {
|
||||
e := Zero[error, map[string]int]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Nil(t, value, "Right value should be nil for map type")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
var nilMap map[string]int
|
||||
assert.Equal(t, Of[error](nilMap), e, "Zero should create a Right value with nil map")
|
||||
}
|
||||
|
||||
// TestZeroWithStructs tests Zero function with struct types
|
||||
@@ -208,23 +181,16 @@ func TestZeroWithStructs(t *testing.T) {
|
||||
|
||||
e := Zero[error, TestStruct]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
expected := TestStruct{Field1: 0, Field2: ""}
|
||||
assert.Equal(t, expected, value, "Right value should be zero value for struct")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](expected), e, "Zero should create a Right value with zero value for struct")
|
||||
}
|
||||
|
||||
// TestZeroWithInterfaces tests Zero function with interface types
|
||||
func TestZeroWithInterfaces(t *testing.T) {
|
||||
e := Zero[error, interface{}]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Nil(t, value, "Right value should be nil for interface type")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
var nilInterface interface{}
|
||||
assert.Equal(t, Of[error](nilInterface), e, "Zero should create a Right value with nil interface")
|
||||
}
|
||||
|
||||
// TestZeroWithCustomErrorType tests Zero function with custom error types
|
||||
@@ -236,12 +202,7 @@ func TestZeroWithCustomErrorType(t *testing.T) {
|
||||
|
||||
e := Zero[CustomError, string]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
assert.False(t, IsLeft(e), "Zero should not create a Left value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.Equal(t, "", value, "Right value should be empty string")
|
||||
assert.Equal(t, CustomError{Code: 0, Message: ""}, err, "Error should be zero value for CustomError")
|
||||
assert.Equal(t, Of[CustomError](""), e, "Zero should create a Right value with empty string")
|
||||
}
|
||||
|
||||
// TestZeroCanBeUsedWithOtherFunctions tests that Zero Eithers work with other either functions
|
||||
@@ -252,17 +213,13 @@ func TestZeroCanBeUsedWithOtherFunctions(t *testing.T) {
|
||||
mapped := MonadMap(e, func(n int) string {
|
||||
return fmt.Sprintf("%d", n)
|
||||
})
|
||||
assert.True(t, IsRight(mapped), "Mapped Zero should still be Right")
|
||||
value, _ := Unwrap(mapped)
|
||||
assert.Equal(t, "0", value, "Mapped value should be '0'")
|
||||
assert.Equal(t, Of[error]("0"), mapped, "Mapped Zero should be Right with '0'")
|
||||
|
||||
// Test with Chain
|
||||
chained := MonadChain(e, func(n int) Either[error, string] {
|
||||
return Right[error](fmt.Sprintf("value: %d", n))
|
||||
})
|
||||
assert.True(t, IsRight(chained), "Chained Zero should still be Right")
|
||||
chainedValue, _ := Unwrap(chained)
|
||||
assert.Equal(t, "value: 0", chainedValue, "Chained value should be 'value: 0'")
|
||||
assert.Equal(t, Of[error]("value: 0"), chained, "Chained Zero should be Right with 'value: 0'")
|
||||
|
||||
// Test with Fold
|
||||
folded := MonadFold(e,
|
||||
@@ -295,23 +252,15 @@ func TestZeroWithComplexTypes(t *testing.T) {
|
||||
|
||||
e := Zero[error, ComplexType]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
expected := ComplexType{Nested: nil, Ptr: nil}
|
||||
assert.Equal(t, expected, value, "Right value should be zero value for complex struct")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](expected), e, "Zero should create a Right value with zero value for complex struct")
|
||||
}
|
||||
|
||||
// TestZeroWithOption tests Zero with Option type
|
||||
func TestZeroWithOption(t *testing.T) {
|
||||
e := Zero[error, O.Option[int]]()
|
||||
|
||||
assert.True(t, IsRight(e), "Zero should create a Right value")
|
||||
|
||||
value, err := Unwrap(e)
|
||||
assert.True(t, O.IsNone(value), "Right value should be None for Option type")
|
||||
assert.Nil(t, err, "Error should be nil for Right value")
|
||||
assert.Equal(t, Of[error](O.None[int]()), e, "Zero should create a Right value with None option")
|
||||
}
|
||||
|
||||
// TestZeroIsNotLeft tests that Zero never creates a Left value
|
||||
@@ -343,3 +292,211 @@ func TestZeroEqualsDefaultInitialization(t *testing.T) {
|
||||
assert.Equal(t, IsRight(defaultInit), IsRight(zero), "Both should be Right")
|
||||
assert.Equal(t, IsLeft(defaultInit), IsLeft(zero), "Both should not be Left")
|
||||
}
|
||||
|
||||
// TestMonadChainLeft tests the MonadChainLeft function with various scenarios
|
||||
func TestMonadChainLeft(t *testing.T) {
|
||||
t.Run("Left value is transformed by function", func(t *testing.T) {
|
||||
// Transform error to success
|
||||
result := MonadChainLeft(
|
||||
Left[int](errors.New("not found")),
|
||||
func(err error) Either[string, int] {
|
||||
if err.Error() == "not found" {
|
||||
return Right[string](0) // default value
|
||||
}
|
||||
return Left[int](err.Error())
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of[string](0), result)
|
||||
})
|
||||
|
||||
t.Run("Left value error type is transformed", func(t *testing.T) {
|
||||
// Transform error type from int to string
|
||||
result := MonadChainLeft(
|
||||
Left[int](404),
|
||||
func(code int) Either[string, int] {
|
||||
return Left[int](fmt.Sprintf("Error code: %d", code))
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Left[int]("Error code: 404"), result)
|
||||
})
|
||||
|
||||
t.Run("Right value passes through unchanged", func(t *testing.T) {
|
||||
// Right value should not be affected
|
||||
result := MonadChainLeft(
|
||||
Right[error](42),
|
||||
func(err error) Either[string, int] {
|
||||
return Left[int]("should not be called")
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of[string](42), result)
|
||||
})
|
||||
|
||||
t.Run("Chain multiple error transformations", func(t *testing.T) {
|
||||
// First transformation
|
||||
step1 := MonadChainLeft(
|
||||
Left[int](errors.New("error1")),
|
||||
func(err error) Either[error, int] {
|
||||
return Left[int](errors.New("error2"))
|
||||
},
|
||||
)
|
||||
// Second transformation
|
||||
step2 := MonadChainLeft(
|
||||
step1,
|
||||
func(err error) Either[string, int] {
|
||||
return Left[int](err.Error())
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Left[int]("error2"), step2)
|
||||
})
|
||||
|
||||
t.Run("Error recovery with fallback", func(t *testing.T) {
|
||||
// Recover from specific errors
|
||||
result := MonadChainLeft(
|
||||
Left[int](errors.New("timeout")),
|
||||
func(err error) Either[error, int] {
|
||||
if err.Error() == "timeout" {
|
||||
return Right[error](999) // fallback value
|
||||
}
|
||||
return Left[int](err)
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of[error](999), result)
|
||||
})
|
||||
|
||||
t.Run("Transform error to different Left", func(t *testing.T) {
|
||||
// Transform one error to another
|
||||
result := MonadChainLeft(
|
||||
Left[string]("original error"),
|
||||
func(s string) Either[int, string] {
|
||||
return Left[string](len(s))
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Left[string](14), result) // length of "original error"
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainLeft tests the curried ChainLeft function
|
||||
func TestChainLeft(t *testing.T) {
|
||||
t.Run("Curried function transforms Left value", func(t *testing.T) {
|
||||
// Create a reusable error handler
|
||||
handleNotFound := ChainLeft(func(err error) Either[string, int] {
|
||||
if err.Error() == "not found" {
|
||||
return Right[string](0)
|
||||
}
|
||||
return Left[int](err.Error())
|
||||
})
|
||||
|
||||
result := handleNotFound(Left[int](errors.New("not found")))
|
||||
assert.Equal(t, Of[string](0), result)
|
||||
})
|
||||
|
||||
t.Run("Curried function with Right value", func(t *testing.T) {
|
||||
handler := ChainLeft(func(err error) Either[string, int] {
|
||||
return Left[int]("should not be called")
|
||||
})
|
||||
|
||||
result := handler(Right[error](42))
|
||||
assert.Equal(t, Of[string](42), result)
|
||||
})
|
||||
|
||||
t.Run("Use in pipeline with Pipe", func(t *testing.T) {
|
||||
// Create error transformer
|
||||
toStringError := ChainLeft(func(code int) Either[string, string] {
|
||||
return Left[string](fmt.Sprintf("Error: %d", code))
|
||||
})
|
||||
|
||||
result := F.Pipe1(
|
||||
Left[string](404),
|
||||
toStringError,
|
||||
)
|
||||
assert.Equal(t, Left[string]("Error: 404"), result)
|
||||
})
|
||||
|
||||
t.Run("Compose multiple ChainLeft operations", func(t *testing.T) {
|
||||
// First handler: convert error to string
|
||||
handler1 := ChainLeft(func(err error) Either[string, int] {
|
||||
return Left[int](err.Error())
|
||||
})
|
||||
|
||||
// Second handler: add prefix to string error
|
||||
handler2 := ChainLeft(func(s string) Either[string, int] {
|
||||
return Left[int]("Handled: " + s)
|
||||
})
|
||||
|
||||
result := F.Pipe2(
|
||||
Left[int](errors.New("original")),
|
||||
handler1,
|
||||
handler2,
|
||||
)
|
||||
assert.Equal(t, Left[int]("Handled: original"), result)
|
||||
})
|
||||
|
||||
t.Run("Error recovery in pipeline", func(t *testing.T) {
|
||||
// Handler that recovers from specific errors
|
||||
recoverFromTimeout := ChainLeft(func(err error) Either[error, int] {
|
||||
if err.Error() == "timeout" {
|
||||
return Right[error](0) // recovered value
|
||||
}
|
||||
return Left[int](err) // propagate other errors
|
||||
})
|
||||
|
||||
// Test with timeout error
|
||||
result1 := F.Pipe1(
|
||||
Left[int](errors.New("timeout")),
|
||||
recoverFromTimeout,
|
||||
)
|
||||
assert.Equal(t, Of[error](0), result1)
|
||||
|
||||
// Test with other error
|
||||
result2 := F.Pipe1(
|
||||
Left[int](errors.New("other error")),
|
||||
recoverFromTimeout,
|
||||
)
|
||||
assert.True(t, IsLeft(result2))
|
||||
})
|
||||
|
||||
t.Run("Transform error type in pipeline", func(t *testing.T) {
|
||||
// Convert numeric error codes to descriptive strings
|
||||
codeToMessage := ChainLeft(func(code int) Either[string, string] {
|
||||
messages := map[int]string{
|
||||
404: "Not Found",
|
||||
500: "Internal Server Error",
|
||||
}
|
||||
if msg, ok := messages[code]; ok {
|
||||
return Left[string](msg)
|
||||
}
|
||||
return Left[string](fmt.Sprintf("Unknown error: %d", code))
|
||||
})
|
||||
|
||||
result := F.Pipe1(
|
||||
Left[string](404),
|
||||
codeToMessage,
|
||||
)
|
||||
assert.Equal(t, Left[string]("Not Found"), result)
|
||||
})
|
||||
|
||||
t.Run("ChainLeft with Map combination", func(t *testing.T) {
|
||||
// Combine ChainLeft with Map to handle both channels
|
||||
errorHandler := ChainLeft(func(err error) Either[string, int] {
|
||||
return Left[int]("Error: " + err.Error())
|
||||
})
|
||||
|
||||
valueMapper := Map[string](S.Format[int]("Value: %d"))
|
||||
|
||||
// Test with Left
|
||||
result1 := F.Pipe2(
|
||||
Left[int](errors.New("fail")),
|
||||
errorHandler,
|
||||
valueMapper,
|
||||
)
|
||||
assert.Equal(t, Left[string]("Error: fail"), result1)
|
||||
|
||||
// Test with Right
|
||||
result2 := F.Pipe2(
|
||||
Right[error](42),
|
||||
errorHandler,
|
||||
valueMapper,
|
||||
)
|
||||
assert.Equal(t, Of[string]("Value: 42"), result2)
|
||||
})
|
||||
}
|
||||
|
||||
351
v2/either/filterable.go
Normal file
351
v2/either/filterable.go
Normal file
@@ -0,0 +1,351 @@
|
||||
// Copyright (c) 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 either provides implementations of the Either type and related operations.
|
||||
//
|
||||
// This package implements several Fantasy Land algebraic structures:
|
||||
// - Filterable: https://github.com/fantasyland/fantasy-land#filterable
|
||||
//
|
||||
// The Filterable specification defines operations for filtering and partitioning
|
||||
// data structures based on predicates and mapping functions.
|
||||
package either
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/option"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
)
|
||||
|
||||
// Partition separates an [Either] value into a [Pair] based on a predicate function.
|
||||
// It returns a function that takes an Either and produces a Pair of Either values,
|
||||
// where the first element contains values that fail the predicate and the second
|
||||
// contains values that pass the predicate.
|
||||
//
|
||||
// This function implements the Filterable specification's partition operation:
|
||||
// https://github.com/fantasyland/fantasy-land#filterable
|
||||
//
|
||||
// The behavior is as follows:
|
||||
// - If the input is Left, both elements of the resulting Pair will be the same Left value
|
||||
// - If the input is Right and the predicate returns true, the result is (Left(empty), Right(value))
|
||||
// - If the input is Right and the predicate returns false, the result is (Right(value), Left(empty))
|
||||
//
|
||||
// This function is useful for separating Either values into two categories based on
|
||||
// a condition, commonly used in filtering operations where you want to keep track of
|
||||
// both the values that pass and fail a test.
|
||||
//
|
||||
// Parameters:
|
||||
// - p: A predicate function that tests values of type A
|
||||
// - empty: The default Left value to use when creating Left instances for partitioning
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// A function that takes an Either[E, A] and returns a Pair where:
|
||||
// - First element: Either values that fail the predicate (or original Left)
|
||||
// - Second element: Either values that pass the predicate (or original Left)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// E "github.com/IBM/fp-go/v2/either"
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// P "github.com/IBM/fp-go/v2/pair"
|
||||
// )
|
||||
//
|
||||
// // Partition positive and non-positive numbers
|
||||
// isPositive := N.MoreThan(0)
|
||||
// partition := E.Partition(isPositive, "not positive")
|
||||
//
|
||||
// // Right value that passes predicate
|
||||
// result1 := partition(E.Right[string](5))
|
||||
// // result1 = Pair(Left("not positive"), Right(5))
|
||||
// left1, right1 := P.Unpack(result1)
|
||||
// // left1 = Left("not positive"), right1 = Right(5)
|
||||
//
|
||||
// // Right value that fails predicate
|
||||
// result2 := partition(E.Right[string](-3))
|
||||
// // result2 = Pair(Right(-3), Left("not positive"))
|
||||
// left2, right2 := P.Unpack(result2)
|
||||
// // left2 = Right(-3), right2 = Left("not positive")
|
||||
//
|
||||
// // Left value passes through unchanged in both positions
|
||||
// result3 := partition(E.Left[int]("error"))
|
||||
// // result3 = Pair(Left("error"), Left("error"))
|
||||
// left3, right3 := P.Unpack(result3)
|
||||
// // left3 = Left("error"), right3 = Left("error")
|
||||
func Partition[E, A any](p Predicate[A], empty E) func(Either[E, A]) Pair[Either[E, A], Either[E, A]] {
|
||||
l := Left[A](empty)
|
||||
return func(e Either[E, A]) Pair[Either[E, A], Either[E, A]] {
|
||||
if e.isLeft {
|
||||
return pair.Of(e)
|
||||
}
|
||||
if p(e.r) {
|
||||
return pair.MakePair(l, e)
|
||||
}
|
||||
return pair.MakePair(e, l)
|
||||
}
|
||||
}
|
||||
|
||||
// Filter creates a filtering operation for [Either] values based on a predicate function.
|
||||
// It returns a function that takes an Either and produces an Either, where Right values
|
||||
// that fail the predicate are converted to Left values with the provided empty value.
|
||||
//
|
||||
// This function implements the Filterable specification's filter operation:
|
||||
// https://github.com/fantasyland/fantasy-land#filterable
|
||||
//
|
||||
// The behavior is as follows:
|
||||
// - If the input is Left, it passes through unchanged
|
||||
// - If the input is Right and the predicate returns true, the Right value passes through unchanged
|
||||
// - If the input is Right and the predicate returns false, it's converted to Left(empty)
|
||||
//
|
||||
// This function is useful for conditional validation or filtering of Either values,
|
||||
// where you want to reject Right values that don't meet certain criteria by converting
|
||||
// them to Left values with a default error.
|
||||
//
|
||||
// Parameters:
|
||||
// - p: A predicate function that tests values of type A
|
||||
// - empty: The default Left value to use when filtering out Right values that fail the predicate
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// An Operator function that takes an Either[E, A] and returns an Either[E, A] where:
|
||||
// - Left values pass through unchanged
|
||||
// - Right values that pass the predicate remain as Right
|
||||
// - Right values that fail the predicate become Left(empty)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// E "github.com/IBM/fp-go/v2/either"
|
||||
// N "github.com/IBM/fp-go/v2/number"
|
||||
// )
|
||||
//
|
||||
// // Filter to keep only positive numbers
|
||||
// isPositive := N.MoreThan(0)
|
||||
// filterPositive := E.Filter(isPositive, "not positive")
|
||||
//
|
||||
// // Right value that passes predicate - remains Right
|
||||
// result1 := filterPositive(E.Right[string](5))
|
||||
// // result1 = Right(5)
|
||||
//
|
||||
// // Right value that fails predicate - becomes Left
|
||||
// result2 := filterPositive(E.Right[string](-3))
|
||||
// // result2 = Left("not positive")
|
||||
//
|
||||
// // Left value passes through unchanged
|
||||
// result3 := filterPositive(E.Left[int]("original error"))
|
||||
// // result3 = Left("original error")
|
||||
//
|
||||
// // Chaining filters
|
||||
// isEven := func(n int) bool { return n%2 == 0 }
|
||||
// filterEven := E.Filter(isEven, "not even")
|
||||
//
|
||||
// // Apply multiple filters in sequence
|
||||
// result4 := filterEven(filterPositive(E.Right[string](4)))
|
||||
// // result4 = Right(4) - passes both filters
|
||||
//
|
||||
// result5 := filterEven(filterPositive(E.Right[string](3)))
|
||||
// // result5 = Left("not even") - passes first, fails second
|
||||
func Filter[E, A any](p Predicate[A], empty E) Operator[E, A, A] {
|
||||
l := Left[A](empty)
|
||||
return func(e Either[E, A]) Either[E, A] {
|
||||
if e.isLeft || p(e.r) {
|
||||
return e
|
||||
}
|
||||
return l
|
||||
}
|
||||
}
|
||||
|
||||
// FilterMap combines filtering and mapping operations for [Either] values using an [Option]-returning function.
|
||||
// It returns a function that takes an Either[E, A] and produces an Either[E, B], where Right values
|
||||
// are transformed by applying the function f. If f returns Some(B), the result is Right(B). If f returns
|
||||
// None, the result is Left(empty).
|
||||
//
|
||||
// This function implements the Filterable specification's filterMap operation:
|
||||
// https://github.com/fantasyland/fantasy-land#filterable
|
||||
//
|
||||
// The behavior is as follows:
|
||||
// - If the input is Left, it passes through with its error value preserved as Left[B]
|
||||
// - If the input is Right and f returns Some(B), the result is Right(B)
|
||||
// - If the input is Right and f returns None, the result is Left(empty)
|
||||
//
|
||||
// This function is useful for operations that combine validation/filtering with transformation,
|
||||
// such as parsing strings to numbers (where invalid strings result in None), or extracting
|
||||
// optional fields from structures.
|
||||
//
|
||||
// Parameters:
|
||||
// - f: An Option Kleisli function that transforms values of type A to Option[B]
|
||||
// - empty: The default Left value to use when f returns None
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// An Operator function that takes an Either[E, A] and returns an Either[E, B] where:
|
||||
// - Left values pass through with error preserved
|
||||
// - Right values are transformed by f: Some(B) becomes Right(B), None becomes Left(empty)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// E "github.com/IBM/fp-go/v2/either"
|
||||
// O "github.com/IBM/fp-go/v2/option"
|
||||
// "strconv"
|
||||
// )
|
||||
//
|
||||
// // Parse string to int, filtering out invalid values
|
||||
// parseInt := func(s string) O.Option[int] {
|
||||
// if n, err := strconv.Atoi(s); err == nil {
|
||||
// return O.Some(n)
|
||||
// }
|
||||
// return O.None[int]()
|
||||
// }
|
||||
// filterMapInt := E.FilterMap(parseInt, "invalid number")
|
||||
//
|
||||
// // Valid number string - transforms to Right(int)
|
||||
// result1 := filterMapInt(E.Right[string]("42"))
|
||||
// // result1 = Right(42)
|
||||
//
|
||||
// // Invalid number string - becomes Left
|
||||
// result2 := filterMapInt(E.Right[string]("abc"))
|
||||
// // result2 = Left("invalid number")
|
||||
//
|
||||
// // Left value passes through with error preserved
|
||||
// result3 := filterMapInt(E.Left[string]("original error"))
|
||||
// // result3 = Left("original error")
|
||||
//
|
||||
// // Extract optional field from struct
|
||||
// type Person struct {
|
||||
// Name string
|
||||
// Email O.Option[string]
|
||||
// }
|
||||
// extractEmail := func(p Person) O.Option[string] { return p.Email }
|
||||
// filterMapEmail := E.FilterMap(extractEmail, "no email")
|
||||
//
|
||||
// result4 := filterMapEmail(E.Right[string](Person{Name: "Alice", Email: O.Some("alice@example.com")}))
|
||||
// // result4 = Right("alice@example.com")
|
||||
//
|
||||
// result5 := filterMapEmail(E.Right[string](Person{Name: "Bob", Email: O.None[string]()}))
|
||||
// // result5 = Left("no email")
|
||||
func FilterMap[E, A, B any](f option.Kleisli[A, B], empty E) Operator[E, A, B] {
|
||||
l := Left[B](empty)
|
||||
return func(e Either[E, A]) Either[E, B] {
|
||||
if e.isLeft {
|
||||
return Left[B](e.l)
|
||||
}
|
||||
if b, ok := option.Unwrap(f(e.r)); ok {
|
||||
return Right[E](b)
|
||||
}
|
||||
return l
|
||||
}
|
||||
}
|
||||
|
||||
// PartitionMap separates and transforms an [Either] value into a [Pair] of Either values using a mapping function.
|
||||
// It returns a function that takes an Either[E, A] and produces a Pair of Either values, where the mapping
|
||||
// function f transforms the Right value into Either[B, C]. The result is partitioned based on whether f
|
||||
// produces a Left or Right value.
|
||||
//
|
||||
// This function implements the Filterable specification's partitionMap operation:
|
||||
// https://github.com/fantasyland/fantasy-land#filterable
|
||||
//
|
||||
// The behavior is as follows:
|
||||
// - If the input is Left, both elements of the resulting Pair will be Left with the original error
|
||||
// - If the input is Right and f returns Left(B), the result is (Right(B), Left(empty))
|
||||
// - If the input is Right and f returns Right(C), the result is (Left(empty), Right(C))
|
||||
//
|
||||
// This function is useful for operations that need to categorize and transform values simultaneously,
|
||||
// such as separating valid and invalid data while applying different transformations to each category.
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A Kleisli function that transforms values of type A to Either[B, C]
|
||||
// - empty: The default error value to use when creating Left instances for partitioning
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// A function that takes an Either[E, A] and returns a Pair[Either[E, B], Either[E, C]] where:
|
||||
// - If input is Left: (Left(original_error), Left(original_error))
|
||||
// - If f returns Left(B): (Right(B), Left(empty))
|
||||
// - If f returns Right(C): (Left(empty), Right(C))
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import (
|
||||
// E "github.com/IBM/fp-go/v2/either"
|
||||
// P "github.com/IBM/fp-go/v2/pair"
|
||||
// )
|
||||
//
|
||||
// // Classify and transform numbers: negative -> error message, positive -> squared value
|
||||
// classifyNumber := func(n int) E.Either[string, int] {
|
||||
// if n < 0 {
|
||||
// return E.Left[int]("negative: " + strconv.Itoa(n))
|
||||
// }
|
||||
// return E.Right[string](n * n)
|
||||
// }
|
||||
// partitionMap := E.PartitionMap(classifyNumber, "not classified")
|
||||
//
|
||||
// // Positive number - goes to right side as squared value
|
||||
// result1 := partitionMap(E.Right[string](5))
|
||||
// // result1 = Pair(Left("not classified"), Right(25))
|
||||
// left1, right1 := P.Unpack(result1)
|
||||
// // left1 = Left("not classified"), right1 = Right(25)
|
||||
//
|
||||
// // Negative number - goes to left side with error message
|
||||
// result2 := partitionMap(E.Right[string](-3))
|
||||
// // result2 = Pair(Right("negative: -3"), Left("not classified"))
|
||||
// left2, right2 := P.Unpack(result2)
|
||||
// // left2 = Right("negative: -3"), right2 = Left("not classified")
|
||||
//
|
||||
// // Original Left value - appears in both positions
|
||||
// result3 := partitionMap(E.Left[int]("original error"))
|
||||
// // result3 = Pair(Left("original error"), Left("original error"))
|
||||
// left3, right3 := P.Unpack(result3)
|
||||
// // left3 = Left("original error"), right3 = Left("original error")
|
||||
//
|
||||
// // Validate and transform user input
|
||||
// type ValidationError struct{ Field, Message string }
|
||||
// type User struct{ Name string; Age int }
|
||||
//
|
||||
// validateUser := func(input map[string]string) E.Either[ValidationError, User] {
|
||||
// name, hasName := input["name"]
|
||||
// ageStr, hasAge := input["age"]
|
||||
// if !hasName {
|
||||
// return E.Left[User](ValidationError{"name", "missing"})
|
||||
// }
|
||||
// if !hasAge {
|
||||
// return E.Left[User](ValidationError{"age", "missing"})
|
||||
// }
|
||||
// age, err := strconv.Atoi(ageStr)
|
||||
// if err != nil {
|
||||
// return E.Left[User](ValidationError{"age", "invalid"})
|
||||
// }
|
||||
// return E.Right[ValidationError](User{name, age})
|
||||
// }
|
||||
// partitionUsers := E.PartitionMap(validateUser, ValidationError{"", "not processed"})
|
||||
//
|
||||
// validInput := map[string]string{"name": "Alice", "age": "30"}
|
||||
// result4 := partitionUsers(E.Right[string](validInput))
|
||||
// // result4 = Pair(Left(ValidationError{"", "not processed"}), Right(User{"Alice", 30}))
|
||||
//
|
||||
// invalidInput := map[string]string{"name": "Bob"}
|
||||
// result5 := partitionUsers(E.Right[string](invalidInput))
|
||||
// // result5 = Pair(Right(ValidationError{"age", "missing"}), Left(ValidationError{"", "not processed"}))
|
||||
func PartitionMap[E, A, B, C any](f Kleisli[B, A, C], empty E) func(Either[E, A]) Pair[Either[E, B], Either[E, C]] {
|
||||
return func(e Either[E, A]) Pair[Either[E, B], Either[E, C]] {
|
||||
if e.isLeft {
|
||||
return pair.MakePair(Left[B](e.l), Left[C](e.l))
|
||||
}
|
||||
res := f(e.r)
|
||||
if res.isLeft {
|
||||
return pair.MakePair(Right[E](res.l), Left[C](empty))
|
||||
}
|
||||
return pair.MakePair(Left[B](empty), Right[E](res.r))
|
||||
}
|
||||
}
|
||||
1433
v2/either/filterable_test.go
Normal file
1433
v2/either/filterable_test.go
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user