mirror of
https://github.com/IBM/fp-go.git
synced 2026-02-06 11:37:45 +02:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
49deb57d24 | ||
|
|
abb55ddbd0 | ||
|
|
f6b01dffdc | ||
|
|
43b666edbb |
1
v2/.bob/mcp.json
Normal file
1
v2/.bob/mcp.json
Normal file
@@ -0,0 +1 @@
|
||||
{"mcpServers":{}}
|
||||
@@ -68,7 +68,7 @@ func ApS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Effect[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApS[C](setter, fa)
|
||||
return readerreaderioresult.ApS(setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -76,7 +76,7 @@ func ApSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa Effect[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApSL[C](lens, fa)
|
||||
return readerreaderioresult.ApSL(lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -84,7 +84,7 @@ func BindL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f func(T) Effect[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindL[C](lens, f)
|
||||
return readerreaderioresult.BindL(lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -132,7 +132,7 @@ 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[C](setter, f)
|
||||
return readerreaderioresult.BindReaderK(setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -140,7 +140,7 @@ 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[C](setter, f)
|
||||
return readerreaderioresult.BindReaderIOK(setter, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -172,7 +172,7 @@ func BindReaderKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f reader.Kleisli[C, T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindReaderKL[C](lens, f)
|
||||
return readerreaderioresult.BindReaderKL(lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -180,7 +180,7 @@ func BindReaderIOKL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
f readerio.Kleisli[C, T, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.BindReaderIOKL[C](lens, f)
|
||||
return readerreaderioresult.BindReaderIOKL(lens, f)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -204,7 +204,7 @@ func ApReaderS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Reader[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApReaderS[C](setter, fa)
|
||||
return readerreaderioresult.ApReaderS(setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -212,7 +212,7 @@ func ApReaderIOS[C, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa ReaderIO[C, T],
|
||||
) Operator[C, S1, S2] {
|
||||
return readerreaderioresult.ApReaderIOS[C](setter, fa)
|
||||
return readerreaderioresult.ApReaderIOS(setter, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -244,7 +244,7 @@ func ApReaderSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa Reader[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApReaderSL[C](lens, fa)
|
||||
return readerreaderioresult.ApReaderSL(lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
@@ -252,7 +252,7 @@ func ApReaderIOSL[C, S, T any](
|
||||
lens Lens[S, T],
|
||||
fa ReaderIO[C, T],
|
||||
) Operator[C, S, S] {
|
||||
return readerreaderioresult.ApReaderIOSL[C](lens, fa)
|
||||
return readerreaderioresult.ApReaderIOSL(lens, fa)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
|
||||
@@ -61,7 +61,7 @@ func TestBind(t *testing.T) {
|
||||
t.Run("binds effect result to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := Bind[TestContext](
|
||||
eff := Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -69,7 +69,7 @@ func TestBind(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](30)
|
||||
return Of[TestContext](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -83,7 +83,7 @@ func TestBind(t *testing.T) {
|
||||
t.Run("chains multiple binds", func(t *testing.T) {
|
||||
initial := BindState{}
|
||||
|
||||
eff := Bind[TestContext](
|
||||
eff := Bind(
|
||||
func(email string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Email = email
|
||||
@@ -91,9 +91,9 @@ func TestBind(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("alice@example.com")
|
||||
return Of[TestContext]("alice@example.com")
|
||||
},
|
||||
)(Bind[TestContext](
|
||||
)(Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -101,9 +101,9 @@ func TestBind(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](30)
|
||||
return Of[TestContext](30)
|
||||
},
|
||||
)(Bind[TestContext](
|
||||
)(Bind(
|
||||
func(name string) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Name = name
|
||||
@@ -111,7 +111,7 @@ func TestBind(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("Alice")
|
||||
return Of[TestContext]("Alice")
|
||||
},
|
||||
)(Do[TestContext](initial))))
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestBind(t *testing.T) {
|
||||
expectedErr := errors.New("bind error")
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := Bind[TestContext](
|
||||
eff := Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -182,7 +182,7 @@ func TestLet(t *testing.T) {
|
||||
func(s BindState) string {
|
||||
return s.Name + "@example.com"
|
||||
},
|
||||
)(Bind[TestContext](
|
||||
)(Bind(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -190,7 +190,7 @@ func TestLet(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](25)
|
||||
return Of[TestContext](25)
|
||||
},
|
||||
)(Do[TestContext](initial)))
|
||||
|
||||
@@ -270,7 +270,7 @@ func TestBindTo(t *testing.T) {
|
||||
|
||||
eff := BindTo[TestContext](func(v int) SimpleState {
|
||||
return SimpleState{Value: v}
|
||||
})(Of[TestContext, int](42))
|
||||
})(Of[TestContext](42))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
@@ -296,7 +296,7 @@ func TestBindTo(t *testing.T) {
|
||||
},
|
||||
)(BindTo[TestContext](func(x int) State {
|
||||
return State{X: x}
|
||||
})(Of[TestContext, int](10)))
|
||||
})(Of[TestContext](10)))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
@@ -309,9 +309,9 @@ func TestBindTo(t *testing.T) {
|
||||
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, int](30)
|
||||
ageEffect := Of[TestContext](30)
|
||||
|
||||
eff := ApS[TestContext](
|
||||
eff := ApS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -333,7 +333,7 @@ func TestApS(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
ageEffect := Fail[TestContext, int](expectedErr)
|
||||
|
||||
eff := ApS[TestContext](
|
||||
eff := ApS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -388,7 +388,7 @@ func TestBindIOEitherK(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) ioeither.IOEither[error, int] {
|
||||
return ioeither.Of[error, int](30)
|
||||
return ioeither.Of[error](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -411,7 +411,7 @@ func TestBindIOEitherK(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) ioeither.IOEither[error, int] {
|
||||
return ioeither.Left[int, error](expectedErr)
|
||||
return ioeither.Left[int](expectedErr)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -434,7 +434,7 @@ func TestBindIOResultK(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) ioresult.IOResult[int] {
|
||||
return ioresult.Of[int](30)
|
||||
return ioresult.Of(30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -450,7 +450,7 @@ func TestBindReaderK(t *testing.T) {
|
||||
t.Run("binds Reader operation to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindReaderK[TestContext](
|
||||
eff := BindReaderK(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -476,7 +476,7 @@ func TestBindReaderIOK(t *testing.T) {
|
||||
t.Run("binds ReaderIO operation to state", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
|
||||
eff := BindReaderIOK[TestContext](
|
||||
eff := BindReaderIOK(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -512,7 +512,7 @@ func TestBindEitherK(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) either.Either[error, int] {
|
||||
return either.Of[error, int](30)
|
||||
return either.Of[error](30)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -535,7 +535,7 @@ func TestBindEitherK(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s BindState) either.Either[error, int] {
|
||||
return either.Left[int, error](expectedErr)
|
||||
return either.Left[int](expectedErr)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -566,9 +566,9 @@ func TestLensOperations(t *testing.T) {
|
||||
|
||||
t.Run("ApSL applies effect using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
ageEffect := Of[TestContext, int](30)
|
||||
ageEffect := Of[TestContext](30)
|
||||
|
||||
eff := ApSL[TestContext](ageLens, ageEffect)(Do[TestContext](initial))
|
||||
eff := ApSL(ageLens, ageEffect)(Do[TestContext](initial))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
@@ -580,10 +580,10 @@ func TestLensOperations(t *testing.T) {
|
||||
t.Run("BindL binds effect using lens", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice", Age: 25}
|
||||
|
||||
eff := BindL[TestContext](
|
||||
eff := BindL(
|
||||
ageLens,
|
||||
func(age int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](age + 5)
|
||||
return Of[TestContext](age + 5)
|
||||
},
|
||||
)(Do[TestContext](initial))
|
||||
|
||||
@@ -667,7 +667,7 @@ func TestApOperations(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
readerEffect := func(ctx TestContext) int { return 30 }
|
||||
|
||||
eff := ApReaderS[TestContext](
|
||||
eff := ApReaderS(
|
||||
func(age int) func(BindState) BindState {
|
||||
return func(s BindState) BindState {
|
||||
s.Age = age
|
||||
@@ -685,7 +685,7 @@ func TestApOperations(t *testing.T) {
|
||||
|
||||
t.Run("ApEitherS applies Either effect", func(t *testing.T) {
|
||||
initial := BindState{Name: "Alice"}
|
||||
eitherEffect := either.Of[error, int](30)
|
||||
eitherEffect := either.Of[error](30)
|
||||
|
||||
eff := ApEitherS[TestContext](
|
||||
func(age int) func(BindState) BindState {
|
||||
@@ -742,7 +742,7 @@ func TestComplexBindChain(t *testing.T) {
|
||||
func(s ComplexState) string {
|
||||
return s.Name + "@example.com"
|
||||
},
|
||||
)(Bind[TestContext](
|
||||
)(Bind(
|
||||
func(age int) func(ComplexState) ComplexState {
|
||||
return func(s ComplexState) ComplexState {
|
||||
s.Age = age
|
||||
@@ -750,11 +750,11 @@ func TestComplexBindChain(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s ComplexState) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](25)
|
||||
return Of[TestContext](25)
|
||||
},
|
||||
)(BindTo[TestContext](func(name string) ComplexState {
|
||||
return ComplexState{Name: name}
|
||||
})(Of[TestContext, string]("Alice"))))))
|
||||
})(Of[TestContext]("Alice"))))))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ type InnerContext struct {
|
||||
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, string]("result")
|
||||
innerEffect := Of[InnerContext]("result")
|
||||
|
||||
// Transform OuterContext to InnerContext
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
@@ -52,7 +52,7 @@ func TestLocal(t *testing.T) {
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -61,9 +61,9 @@ func TestLocal(t *testing.T) {
|
||||
|
||||
t.Run("allows accessing outer context fields", func(t *testing.T) {
|
||||
// Create an effect that reads from InnerContext
|
||||
innerEffect := Chain[InnerContext](func(_ string) Effect[InnerContext, string] {
|
||||
return Of[InnerContext, string]("inner value")
|
||||
})(Of[InnerContext, string]("start"))
|
||||
innerEffect := Chain(func(_ string) Effect[InnerContext, string] {
|
||||
return Of[InnerContext]("inner value")
|
||||
})(Of[InnerContext]("start"))
|
||||
|
||||
// Transform context
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
@@ -78,7 +78,7 @@ func TestLocal(t *testing.T) {
|
||||
Value: "original",
|
||||
Number: 100,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -100,7 +100,7 @@ func TestLocal(t *testing.T) {
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -119,7 +119,7 @@ func TestLocal(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
level3Effect := Of[Level3, string]("deep result")
|
||||
level3Effect := Of[Level3]("deep result")
|
||||
|
||||
// Transform Level2 -> Level3
|
||||
local23 := Local[Level2, Level3, string](func(l2 Level2) Level3 {
|
||||
@@ -137,7 +137,7 @@ func TestLocal(t *testing.T) {
|
||||
|
||||
// Run with Level1 context
|
||||
ioResult := Provide[Level1, string](Level1{A: "a"})(level1Effect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -158,7 +158,7 @@ func TestLocal(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect that needs only DatabaseConfig
|
||||
dbEffect := Of[DatabaseConfig, string]("connected")
|
||||
dbEffect := Of[DatabaseConfig]("connected")
|
||||
|
||||
// Extract DB config from AppConfig
|
||||
accessor := func(app AppConfig) DatabaseConfig {
|
||||
@@ -178,7 +178,7 @@ func TestLocal(t *testing.T) {
|
||||
APIKey: "secret",
|
||||
Timeout: 30,
|
||||
})(appEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -188,7 +188,7 @@ func TestLocal(t *testing.T) {
|
||||
|
||||
func TestContramap(t *testing.T) {
|
||||
t.Run("is equivalent to Local", func(t *testing.T) {
|
||||
innerEffect := Of[InnerContext, int](42)
|
||||
innerEffect := Of[InnerContext](42)
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value}
|
||||
@@ -206,11 +206,11 @@ func TestContramap(t *testing.T) {
|
||||
|
||||
// Run both
|
||||
localIO := Provide[OuterContext, int](outerCtx)(localEffect)
|
||||
localReader := RunSync[int](localIO)
|
||||
localReader := RunSync(localIO)
|
||||
localResult, localErr := localReader(context.Background())
|
||||
|
||||
contramapIO := Provide[OuterContext, int](outerCtx)(contramapEffect)
|
||||
contramapReader := RunSync[int](contramapIO)
|
||||
contramapReader := RunSync(contramapIO)
|
||||
contramapResult, contramapErr := contramapReader(context.Background())
|
||||
|
||||
assert.NoError(t, localErr)
|
||||
@@ -219,7 +219,7 @@ func TestContramap(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("transforms context correctly", func(t *testing.T) {
|
||||
innerEffect := Of[InnerContext, string]("success")
|
||||
innerEffect := Of[InnerContext]("success")
|
||||
|
||||
accessor := func(outer OuterContext) InnerContext {
|
||||
return InnerContext{Value: outer.Value + " modified"}
|
||||
@@ -232,7 +232,7 @@ func TestContramap(t *testing.T) {
|
||||
Value: "original",
|
||||
Number: 50,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -254,7 +254,7 @@ func TestContramap(t *testing.T) {
|
||||
Value: "test",
|
||||
Number: 42,
|
||||
})(outerEffect)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -275,7 +275,7 @@ func TestLocalAndContramapInteroperability(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
effect3 := Of[Config3, string]("result")
|
||||
effect3 := Of[Config3]("result")
|
||||
|
||||
// Use Local for first transformation
|
||||
local23 := Local[Config2, Config3, string](func(c2 Config2) Config3 {
|
||||
@@ -293,7 +293,7 @@ func TestLocalAndContramapInteroperability(t *testing.T) {
|
||||
|
||||
// Run
|
||||
ioResult := Provide[Config1, string](Config1{Value: "test"})(effect1)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -312,24 +312,24 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect that needs DatabaseConfig
|
||||
dbEffect := Of[DatabaseConfig, string]("query result")
|
||||
dbEffect := Of[DatabaseConfig]("query result")
|
||||
|
||||
// Transform AppConfig to DatabaseConfig effectfully
|
||||
loadConfig := func(app AppConfig) Effect[AppConfig, DatabaseConfig] {
|
||||
return Of[AppConfig, DatabaseConfig](DatabaseConfig{
|
||||
return Of[AppConfig](DatabaseConfig{
|
||||
ConnectionString: "loaded from " + app.ConfigPath,
|
||||
})
|
||||
}
|
||||
|
||||
// Apply the transformation
|
||||
transform := LocalEffectK[string, DatabaseConfig, AppConfig](loadConfig)
|
||||
transform := LocalEffectK[string](loadConfig)
|
||||
appEffect := transform(dbEffect)
|
||||
|
||||
// Run with AppConfig
|
||||
ioResult := Provide[AppConfig, string](AppConfig{
|
||||
ConfigPath: "/etc/app.conf",
|
||||
})(appEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -345,7 +345,7 @@ func TestLocalEffectK(t *testing.T) {
|
||||
Path string
|
||||
}
|
||||
|
||||
innerEffect := Of[InnerCtx, string]("success")
|
||||
innerEffect := Of[InnerCtx]("success")
|
||||
|
||||
expectedErr := assert.AnError
|
||||
// Context transformation that fails
|
||||
@@ -353,11 +353,11 @@ func TestLocalEffectK(t *testing.T) {
|
||||
return Fail[OuterCtx, InnerCtx](expectedErr)
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string, InnerCtx, OuterCtx](failingTransform)
|
||||
transform := LocalEffectK[string](failingTransform)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
ioResult := Provide[OuterCtx, string](OuterCtx{Path: "test"})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -378,14 +378,14 @@ func TestLocalEffectK(t *testing.T) {
|
||||
|
||||
// Successful context transformation
|
||||
transform := func(outer OuterCtx) Effect[OuterCtx, InnerCtx] {
|
||||
return Of[OuterCtx, InnerCtx](InnerCtx{Value: outer.Path})
|
||||
return Of[OuterCtx](InnerCtx{Value: outer.Path})
|
||||
}
|
||||
|
||||
transformK := LocalEffectK[string, InnerCtx, OuterCtx](transform)
|
||||
transformK := LocalEffectK[string](transform)
|
||||
outerEffect := transformK(innerEffect)
|
||||
|
||||
ioResult := Provide[OuterCtx, string](OuterCtx{Path: "test"})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -402,25 +402,25 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect that uses Config
|
||||
configEffect := Chain[Config](func(cfg Config) Effect[Config, string] {
|
||||
return Of[Config, string]("processed: " + cfg.Data)
|
||||
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](Config{
|
||||
return Of[AppContext](Config{
|
||||
Data: "loaded from " + app.ConfigFile,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string, Config, AppContext](loadConfigEffect)
|
||||
transform := LocalEffectK[string](loadConfigEffect)
|
||||
appEffect := transform(configEffect)
|
||||
|
||||
ioResult := Provide[AppContext, string](AppContext{
|
||||
ConfigFile: "config.json",
|
||||
})(appEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -439,16 +439,16 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
level3Effect := Of[Level3, string]("deep result")
|
||||
level3Effect := Of[Level3]("deep result")
|
||||
|
||||
// Transform Level2 -> Level3 effectfully
|
||||
transform23 := LocalEffectK[string, Level3, Level2](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2, Level3](Level3{C: l2.B + "-c"})
|
||||
transform23 := LocalEffectK[string](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2](Level3{C: l2.B + "-c"})
|
||||
})
|
||||
|
||||
// Transform Level1 -> Level2 effectfully
|
||||
transform12 := LocalEffectK[string, Level2, Level1](func(l1 Level1) Effect[Level1, Level2] {
|
||||
return Of[Level1, Level2](Level2{B: l1.A + "-b"})
|
||||
transform12 := LocalEffectK[string](func(l1 Level1) Effect[Level1, Level2] {
|
||||
return Of[Level1](Level2{B: l1.A + "-b"})
|
||||
})
|
||||
|
||||
// Compose transformations
|
||||
@@ -457,7 +457,7 @@ func TestLocalEffectK(t *testing.T) {
|
||||
|
||||
// Run with Level1 context
|
||||
ioResult := Provide[Level1, string](Level1{A: "a"})(level1Effect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -477,8 +477,8 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect that needs DatabaseConfig
|
||||
dbEffect := Chain[DatabaseConfig](func(cfg DatabaseConfig) Effect[DatabaseConfig, string] {
|
||||
return Of[DatabaseConfig, string](fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
|
||||
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
|
||||
@@ -488,13 +488,13 @@ func TestLocalEffectK(t *testing.T) {
|
||||
if app.Environment == "prod" {
|
||||
prefix = "prod-"
|
||||
}
|
||||
return Of[AppConfig, DatabaseConfig](DatabaseConfig{
|
||||
return Of[AppConfig](DatabaseConfig{
|
||||
Host: prefix + app.DBHost,
|
||||
Port: app.DBPort,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string, DatabaseConfig, AppConfig](transformWithContext)
|
||||
transform := LocalEffectK[string](transformWithContext)
|
||||
appEffect := transform(dbEffect)
|
||||
|
||||
ioResult := Provide[AppConfig, string](AppConfig{
|
||||
@@ -502,7 +502,7 @@ func TestLocalEffectK(t *testing.T) {
|
||||
DBHost: "localhost",
|
||||
DBPort: 5432,
|
||||
})(appEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -518,31 +518,31 @@ func TestLocalEffectK(t *testing.T) {
|
||||
APIKey string
|
||||
}
|
||||
|
||||
innerEffect := Of[ValidatedConfig, string]("success")
|
||||
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](ValidatedConfig{
|
||||
return Of[RawConfig](ValidatedConfig{
|
||||
APIKey: raw.APIKey,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[string, ValidatedConfig, RawConfig](validateConfig)
|
||||
transform := LocalEffectK[string](validateConfig)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
// Test with invalid config
|
||||
ioResult := Provide[RawConfig, string](RawConfig{APIKey: ""})(outerEffect)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
|
||||
// Test with valid config
|
||||
ioResult2 := Provide[RawConfig, string](RawConfig{APIKey: "valid-key"})(outerEffect)
|
||||
readerResult2 := RunSync[string](ioResult2)
|
||||
readerResult2 := RunSync(ioResult2)
|
||||
result, err2 := readerResult2(context.Background())
|
||||
|
||||
assert.NoError(t, err2)
|
||||
@@ -561,11 +561,11 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect at deepest level
|
||||
effect3 := Of[Level3, string]("result")
|
||||
effect3 := Of[Level3]("result")
|
||||
|
||||
// Use LocalEffectK for first transformation (effectful)
|
||||
localEffectK23 := LocalEffectK[string, Level3, Level2](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2, Level3](Level3{Info: l2.Data})
|
||||
localEffectK23 := LocalEffectK[string](func(l2 Level2) Effect[Level2, Level3] {
|
||||
return Of[Level2](Level3{Info: l2.Data})
|
||||
})
|
||||
|
||||
// Use Local for second transformation (pure)
|
||||
@@ -579,7 +579,7 @@ func TestLocalEffectK(t *testing.T) {
|
||||
|
||||
// Run
|
||||
ioResult := Provide[Level1, string](Level1{Value: "test"})(effect1)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -596,22 +596,22 @@ func TestLocalEffectK(t *testing.T) {
|
||||
}
|
||||
|
||||
// Effect that uses InnerCtx
|
||||
innerEffect := Chain[InnerCtx](func(ctx InnerCtx) Effect[InnerCtx, int] {
|
||||
return Of[InnerCtx, int](ctx.Value * 2)
|
||||
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](InnerCtx{
|
||||
return Of[OuterCtx](InnerCtx{
|
||||
Value: outer.Multiplier * 10,
|
||||
})
|
||||
}
|
||||
|
||||
transform := LocalEffectK[int, InnerCtx, OuterCtx](complexTransform)
|
||||
transform := LocalEffectK[int](complexTransform)
|
||||
outerEffect := transform(innerEffect)
|
||||
|
||||
ioResult := Provide[OuterCtx, int](OuterCtx{Multiplier: 3})(outerEffect)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -31,13 +31,13 @@ type TestContext struct {
|
||||
|
||||
func runEffect[A any](eff Effect[TestContext, A], ctx TestContext) (A, error) {
|
||||
ioResult := Provide[TestContext, A](ctx)(eff)
|
||||
readerResult := RunSync[A](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
return readerResult(context.Background())
|
||||
}
|
||||
|
||||
func TestSucceed(t *testing.T) {
|
||||
t.Run("creates successful effect with value", func(t *testing.T) {
|
||||
eff := Succeed[TestContext, int](42)
|
||||
eff := Succeed[TestContext](42)
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -45,7 +45,7 @@ func TestSucceed(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("creates successful effect with string", func(t *testing.T) {
|
||||
eff := Succeed[TestContext, string]("hello")
|
||||
eff := Succeed[TestContext]("hello")
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -58,7 +58,7 @@ func TestSucceed(t *testing.T) {
|
||||
Age int
|
||||
}
|
||||
user := User{Name: "Alice", Age: 30}
|
||||
eff := Succeed[TestContext, User](user)
|
||||
eff := Succeed[TestContext](user)
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -88,7 +88,7 @@ func TestFail(t *testing.T) {
|
||||
|
||||
func TestOf(t *testing.T) {
|
||||
t.Run("lifts value into effect", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](100)
|
||||
eff := Of[TestContext](100)
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -97,8 +97,8 @@ func TestOf(t *testing.T) {
|
||||
|
||||
t.Run("is equivalent to Succeed", func(t *testing.T) {
|
||||
value := "test value"
|
||||
eff1 := Of[TestContext, string](value)
|
||||
eff2 := Succeed[TestContext, string](value)
|
||||
eff1 := Of[TestContext](value)
|
||||
eff2 := Succeed[TestContext](value)
|
||||
|
||||
result1, err1 := runEffect(eff1, TestContext{Value: "test"})
|
||||
result2, err2 := runEffect(eff2, TestContext{Value: "test"})
|
||||
@@ -111,7 +111,7 @@ func TestOf(t *testing.T) {
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
t.Run("maps over successful effect", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](10)
|
||||
eff := Of[TestContext](10)
|
||||
mapped := Map[TestContext](func(x int) int {
|
||||
return x * 2
|
||||
})(eff)
|
||||
@@ -123,7 +123,7 @@ func TestMap(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("maps to different type", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](42)
|
||||
eff := Of[TestContext](42)
|
||||
mapped := Map[TestContext](func(x int) string {
|
||||
return fmt.Sprintf("value: %d", x)
|
||||
})(eff)
|
||||
@@ -148,7 +148,7 @@ func TestMap(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("chains multiple maps", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](5)
|
||||
eff := Of[TestContext](5)
|
||||
result := Map[TestContext](func(x int) int {
|
||||
return x + 1
|
||||
})(Map[TestContext](func(x int) int {
|
||||
@@ -164,9 +164,9 @@ func TestMap(t *testing.T) {
|
||||
|
||||
func TestChain(t *testing.T) {
|
||||
t.Run("chains successful effects", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](10)
|
||||
chained := Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
eff := Of[TestContext](10)
|
||||
chained := Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(eff)
|
||||
|
||||
result, err := runEffect(chained, TestContext{Value: "test"})
|
||||
@@ -176,9 +176,9 @@ func TestChain(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("chains to different type", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](42)
|
||||
chained := Chain[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](fmt.Sprintf("number: %d", x))
|
||||
eff := Of[TestContext](42)
|
||||
chained := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](fmt.Sprintf("number: %d", x))
|
||||
})(eff)
|
||||
|
||||
result, err := runEffect(chained, TestContext{Value: "test"})
|
||||
@@ -190,8 +190,8 @@ func TestChain(t *testing.T) {
|
||||
t.Run("propagates first error", func(t *testing.T) {
|
||||
expectedErr := errors.New("first error")
|
||||
eff := Fail[TestContext, int](expectedErr)
|
||||
chained := Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
chained := Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(eff)
|
||||
|
||||
_, err := runEffect(chained, TestContext{Value: "test"})
|
||||
@@ -202,8 +202,8 @@ func TestChain(t *testing.T) {
|
||||
|
||||
t.Run("propagates second error", func(t *testing.T) {
|
||||
expectedErr := errors.New("second error")
|
||||
eff := Of[TestContext, int](10)
|
||||
chained := Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
eff := Of[TestContext](10)
|
||||
chained := Chain(func(x int) Effect[TestContext, int] {
|
||||
return Fail[TestContext, int](expectedErr)
|
||||
})(eff)
|
||||
|
||||
@@ -214,11 +214,11 @@ func TestChain(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("chains multiple operations", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](5)
|
||||
result := Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x + 10)
|
||||
})(Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
eff := Of[TestContext](5)
|
||||
result := Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x + 10)
|
||||
})(Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(eff))
|
||||
|
||||
value, err := runEffect(result, TestContext{Value: "test"})
|
||||
@@ -230,10 +230,10 @@ func TestChain(t *testing.T) {
|
||||
|
||||
func TestAp(t *testing.T) {
|
||||
t.Run("applies function effect to value effect", func(t *testing.T) {
|
||||
fn := Of[TestContext, func(int) int](func(x int) int {
|
||||
fn := Of[TestContext](func(x int) int {
|
||||
return x * 2
|
||||
})
|
||||
value := Of[TestContext, int](21)
|
||||
value := Of[TestContext](21)
|
||||
|
||||
result := Ap[int](value)(fn)
|
||||
val, err := runEffect(result, TestContext{Value: "test"})
|
||||
@@ -243,10 +243,10 @@ func TestAp(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("applies function to different type", func(t *testing.T) {
|
||||
fn := Of[TestContext, func(int) string](func(x int) string {
|
||||
fn := Of[TestContext](func(x int) string {
|
||||
return fmt.Sprintf("value: %d", x)
|
||||
})
|
||||
value := Of[TestContext, int](42)
|
||||
value := Of[TestContext](42)
|
||||
|
||||
result := Ap[string](value)(fn)
|
||||
val, err := runEffect(result, TestContext{Value: "test"})
|
||||
@@ -258,7 +258,7 @@ func TestAp(t *testing.T) {
|
||||
t.Run("propagates error from function effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("function error")
|
||||
fn := Fail[TestContext, func(int) int](expectedErr)
|
||||
value := Of[TestContext, int](42)
|
||||
value := Of[TestContext](42)
|
||||
|
||||
result := Ap[int](value)(fn)
|
||||
_, err := runEffect(result, TestContext{Value: "test"})
|
||||
@@ -269,7 +269,7 @@ func TestAp(t *testing.T) {
|
||||
|
||||
t.Run("propagates error from value effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("value error")
|
||||
fn := Of[TestContext, func(int) int](func(x int) int {
|
||||
fn := Of[TestContext](func(x int) int {
|
||||
return x * 2
|
||||
})
|
||||
value := Fail[TestContext, int](expectedErr)
|
||||
@@ -285,9 +285,9 @@ func TestAp(t *testing.T) {
|
||||
func TestSuspend(t *testing.T) {
|
||||
t.Run("suspends effect computation", func(t *testing.T) {
|
||||
callCount := 0
|
||||
eff := Suspend[TestContext, int](func() Effect[TestContext, int] {
|
||||
eff := Suspend(func() Effect[TestContext, int] {
|
||||
callCount++
|
||||
return Of[TestContext, int](42)
|
||||
return Of[TestContext](42)
|
||||
})
|
||||
|
||||
// Effect not executed yet
|
||||
@@ -302,7 +302,7 @@ func TestSuspend(t *testing.T) {
|
||||
|
||||
t.Run("suspends failing effect", func(t *testing.T) {
|
||||
expectedErr := errors.New("suspended error")
|
||||
eff := Suspend[TestContext, int](func() Effect[TestContext, int] {
|
||||
eff := Suspend(func() Effect[TestContext, int] {
|
||||
return Fail[TestContext, int](expectedErr)
|
||||
})
|
||||
|
||||
@@ -314,8 +314,8 @@ func TestSuspend(t *testing.T) {
|
||||
|
||||
t.Run("allows lazy evaluation", func(t *testing.T) {
|
||||
var value int
|
||||
eff := Suspend[TestContext, int](func() Effect[TestContext, int] {
|
||||
return Of[TestContext, int](value)
|
||||
eff := Suspend(func() Effect[TestContext, int] {
|
||||
return Of[TestContext](value)
|
||||
})
|
||||
|
||||
value = 10
|
||||
@@ -334,8 +334,8 @@ func TestSuspend(t *testing.T) {
|
||||
func TestTap(t *testing.T) {
|
||||
t.Run("executes side effect without changing value", func(t *testing.T) {
|
||||
sideEffectValue := 0
|
||||
eff := Of[TestContext, int](42)
|
||||
tapped := Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
eff := Of[TestContext](42)
|
||||
tapped := Tap(func(x int) Effect[TestContext, any] {
|
||||
sideEffectValue = x * 2
|
||||
return Of[TestContext, any](nil)
|
||||
})(eff)
|
||||
@@ -350,7 +350,7 @@ func TestTap(t *testing.T) {
|
||||
t.Run("propagates original error", func(t *testing.T) {
|
||||
expectedErr := errors.New("original error")
|
||||
eff := Fail[TestContext, int](expectedErr)
|
||||
tapped := Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
tapped := Tap(func(x int) Effect[TestContext, any] {
|
||||
return Of[TestContext, any](nil)
|
||||
})(eff)
|
||||
|
||||
@@ -362,8 +362,8 @@ func TestTap(t *testing.T) {
|
||||
|
||||
t.Run("propagates tap error", func(t *testing.T) {
|
||||
expectedErr := errors.New("tap error")
|
||||
eff := Of[TestContext, int](42)
|
||||
tapped := Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
eff := Of[TestContext](42)
|
||||
tapped := Tap(func(x int) Effect[TestContext, any] {
|
||||
return Fail[TestContext, any](expectedErr)
|
||||
})(eff)
|
||||
|
||||
@@ -375,11 +375,11 @@ func TestTap(t *testing.T) {
|
||||
|
||||
t.Run("chains multiple taps", func(t *testing.T) {
|
||||
values := []int{}
|
||||
eff := Of[TestContext, int](10)
|
||||
result := Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
eff := Of[TestContext](10)
|
||||
result := Tap(func(x int) Effect[TestContext, any] {
|
||||
values = append(values, x+2)
|
||||
return Of[TestContext, any](nil)
|
||||
})(Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
})(Tap(func(x int) Effect[TestContext, any] {
|
||||
values = append(values, x+1)
|
||||
return Of[TestContext, any](nil)
|
||||
})(eff))
|
||||
@@ -394,13 +394,13 @@ func TestTap(t *testing.T) {
|
||||
|
||||
func TestTernary(t *testing.T) {
|
||||
t.Run("executes onTrue when predicate is true", func(t *testing.T) {
|
||||
kleisli := Ternary[TestContext, int, string](
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("greater")
|
||||
return Of[TestContext]("greater")
|
||||
},
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("less or equal")
|
||||
return Of[TestContext]("less or equal")
|
||||
},
|
||||
)
|
||||
|
||||
@@ -411,13 +411,13 @@ func TestTernary(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("executes onFalse when predicate is false", func(t *testing.T) {
|
||||
kleisli := Ternary[TestContext, int, string](
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("greater")
|
||||
return Of[TestContext]("greater")
|
||||
},
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("less or equal")
|
||||
return Of[TestContext]("less or equal")
|
||||
},
|
||||
)
|
||||
|
||||
@@ -429,13 +429,13 @@ func TestTernary(t *testing.T) {
|
||||
|
||||
t.Run("handles errors in onTrue branch", func(t *testing.T) {
|
||||
expectedErr := errors.New("true branch error")
|
||||
kleisli := Ternary[TestContext, int, string](
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
},
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("less or equal")
|
||||
return Of[TestContext]("less or equal")
|
||||
},
|
||||
)
|
||||
|
||||
@@ -447,10 +447,10 @@ func TestTernary(t *testing.T) {
|
||||
|
||||
t.Run("handles errors in onFalse branch", func(t *testing.T) {
|
||||
expectedErr := errors.New("false branch error")
|
||||
kleisli := Ternary[TestContext, int, string](
|
||||
kleisli := Ternary(
|
||||
func(x int) bool { return x > 10 },
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("greater")
|
||||
return Of[TestContext]("greater")
|
||||
},
|
||||
func(x int) Effect[TestContext, string] {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
@@ -466,9 +466,9 @@ func TestTernary(t *testing.T) {
|
||||
|
||||
func TestEffectComposition(t *testing.T) {
|
||||
t.Run("composes Map and Chain", func(t *testing.T) {
|
||||
eff := Of[TestContext, int](5)
|
||||
result := Chain[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](fmt.Sprintf("result: %d", x))
|
||||
eff := Of[TestContext](5)
|
||||
result := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](fmt.Sprintf("result: %d", x))
|
||||
})(Map[TestContext](func(x int) int {
|
||||
return x * 2
|
||||
})(eff))
|
||||
@@ -481,12 +481,12 @@ func TestEffectComposition(t *testing.T) {
|
||||
|
||||
t.Run("composes Chain and Tap", func(t *testing.T) {
|
||||
sideEffect := 0
|
||||
eff := Of[TestContext, int](10)
|
||||
result := Tap[TestContext](func(x int) Effect[TestContext, any] {
|
||||
eff := Of[TestContext](10)
|
||||
result := Tap(func(x int) Effect[TestContext, any] {
|
||||
sideEffect = x
|
||||
return Of[TestContext, any](nil)
|
||||
})(Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
})(Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(eff))
|
||||
|
||||
value, err := runEffect(result, TestContext{Value: "test"})
|
||||
@@ -499,7 +499,7 @@ func TestEffectComposition(t *testing.T) {
|
||||
|
||||
func TestEffectWithResult(t *testing.T) {
|
||||
t.Run("converts result to effect", func(t *testing.T) {
|
||||
res := result.Of[int](42)
|
||||
res := result.Of(42)
|
||||
// This demonstrates integration with result package
|
||||
assert.True(t, result.IsRight(res))
|
||||
})
|
||||
|
||||
@@ -30,11 +30,11 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext, string]("Hello")
|
||||
eff2 := Of[TestContext, string](" ")
|
||||
eff3 := Of[TestContext, string]("World")
|
||||
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"})
|
||||
@@ -49,11 +49,11 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
0,
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, int](intMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Of[TestContext, int](10)
|
||||
eff2 := Of[TestContext, int](20)
|
||||
eff3 := Of[TestContext, int](30)
|
||||
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"})
|
||||
@@ -68,7 +68,7 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
"empty",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
result, err := runEffect(effectMonoid.Empty(), TestContext{Value: "test"})
|
||||
|
||||
@@ -83,10 +83,10 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](expectedErr)
|
||||
eff2 := Of[TestContext, string]("World")
|
||||
eff2 := Of[TestContext]("World")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
_, err := runEffect(combined, TestContext{Value: "test"})
|
||||
@@ -102,9 +102,9 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext, string]("Hello")
|
||||
eff1 := Of[TestContext]("Hello")
|
||||
eff2 := Fail[TestContext, string](expectedErr)
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
@@ -120,13 +120,13 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
1,
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, int](intMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
|
||||
effects := []Effect[TestContext, int]{
|
||||
Of[TestContext, int](2),
|
||||
Of[TestContext, int](3),
|
||||
Of[TestContext, int](4),
|
||||
Of[TestContext, int](5),
|
||||
Of[TestContext](2),
|
||||
Of[TestContext](3),
|
||||
Of[TestContext](4),
|
||||
Of[TestContext](5),
|
||||
}
|
||||
|
||||
combined := effectMonoid.Empty()
|
||||
@@ -152,11 +152,11 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
Counter{Count: 0},
|
||||
)
|
||||
|
||||
effectMonoid := ApplicativeMonoid[TestContext, Counter](counterMonoid)
|
||||
effectMonoid := ApplicativeMonoid[TestContext](counterMonoid)
|
||||
|
||||
eff1 := Of[TestContext, Counter](Counter{Count: 5})
|
||||
eff2 := Of[TestContext, Counter](Counter{Count: 10})
|
||||
eff3 := Of[TestContext, Counter](Counter{Count: 15})
|
||||
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"})
|
||||
@@ -173,10 +173,10 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext, string]("First")
|
||||
eff2 := Of[TestContext, string]("Second")
|
||||
eff1 := Of[TestContext]("First")
|
||||
eff2 := Of[TestContext]("Second")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
@@ -191,10 +191,10 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](errors.New("first failed"))
|
||||
eff2 := Of[TestContext, string]("Second")
|
||||
eff2 := Of[TestContext]("Second")
|
||||
|
||||
combined := effectMonoid.Concat(eff1, eff2)
|
||||
result, err := runEffect(combined, TestContext{Value: "test"})
|
||||
@@ -210,7 +210,7 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, string](errors.New("first error"))
|
||||
eff2 := Fail[TestContext, string](expectedErr)
|
||||
@@ -228,7 +228,7 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
"default",
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, string](stringMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
result, err := runEffect(effectMonoid.Empty(), TestContext{Value: "test"})
|
||||
|
||||
@@ -242,12 +242,12 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
0,
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, int](intMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, int](errors.New("error 1"))
|
||||
eff2 := Fail[TestContext, int](errors.New("error 2"))
|
||||
eff3 := Of[TestContext, int](42)
|
||||
eff4 := Of[TestContext, int](100)
|
||||
eff3 := Of[TestContext](42)
|
||||
eff4 := Of[TestContext](100)
|
||||
|
||||
combined := effectMonoid.Concat(
|
||||
effectMonoid.Concat(eff1, eff2),
|
||||
@@ -273,11 +273,11 @@ func TestAlternativeMonoid(t *testing.T) {
|
||||
Result{Value: "", Code: 0},
|
||||
)
|
||||
|
||||
effectMonoid := AlternativeMonoid[TestContext, Result](resultMonoid)
|
||||
effectMonoid := AlternativeMonoid[TestContext](resultMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, Result](errors.New("failed"))
|
||||
eff2 := Of[TestContext, Result](Result{Value: "success", Code: 200})
|
||||
eff3 := Of[TestContext, Result](Result{Value: "backup", Code: 201})
|
||||
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"})
|
||||
@@ -295,11 +295,11 @@ func TestMonoidComparison(t *testing.T) {
|
||||
"",
|
||||
)
|
||||
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext, string](stringMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext, string](stringMonoid)
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext](stringMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext](stringMonoid)
|
||||
|
||||
eff1 := Of[TestContext, string]("A")
|
||||
eff2 := Of[TestContext, string]("B")
|
||||
eff1 := Of[TestContext]("A")
|
||||
eff2 := Of[TestContext]("B")
|
||||
|
||||
// Applicative combines values
|
||||
applicativeResult, err1 := runEffect(
|
||||
@@ -325,11 +325,11 @@ func TestMonoidComparison(t *testing.T) {
|
||||
0,
|
||||
)
|
||||
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext, int](intMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext, int](intMonoid)
|
||||
applicativeMonoid := ApplicativeMonoid[TestContext](intMonoid)
|
||||
alternativeMonoid := AlternativeMonoid[TestContext](intMonoid)
|
||||
|
||||
eff1 := Fail[TestContext, int](errors.New("error 1"))
|
||||
eff2 := Of[TestContext, int](42)
|
||||
eff2 := Of[TestContext](42)
|
||||
|
||||
// Applicative fails on first error
|
||||
_, err1 := runEffect(
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestRetrying(t *testing.T) {
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
return Of[TestContext, string]("success")
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
@@ -59,7 +59,7 @@ func TestRetrying(t *testing.T) {
|
||||
if attemptCount < 3 {
|
||||
return Fail[TestContext, string](errors.New("temporary error"))
|
||||
}
|
||||
return Of[TestContext, string]("success after retries")
|
||||
return Of[TestContext]("success after retries")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
@@ -78,7 +78,7 @@ func TestRetrying(t *testing.T) {
|
||||
maxRetries := uint(3)
|
||||
policy := retry.LimitRetries(maxRetries)
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
eff := Retrying(
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
@@ -103,7 +103,7 @@ func TestRetrying(t *testing.T) {
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, int] {
|
||||
attemptCount++
|
||||
return Of[TestContext, int](42)
|
||||
return Of[TestContext](42)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
return result.IsLeft(res) // retry on error
|
||||
@@ -125,7 +125,7 @@ func TestRetrying(t *testing.T) {
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, int] {
|
||||
attemptCount++
|
||||
return Of[TestContext, int](attemptCount * 10)
|
||||
return Of[TestContext](attemptCount * 10)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
// Retry if value is less than 30
|
||||
@@ -155,7 +155,7 @@ func TestRetrying(t *testing.T) {
|
||||
if len(statuses) < 3 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, string]("done")
|
||||
return Of[TestContext]("done")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -188,7 +188,7 @@ func TestRetrying(t *testing.T) {
|
||||
if attemptCount < 3 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, string]("success")
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -218,7 +218,7 @@ func TestRetrying(t *testing.T) {
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, string]("success")
|
||||
return Of[TestContext]("success")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -250,7 +250,7 @@ func TestRetrying(t *testing.T) {
|
||||
return Fail[TestContext, string](err)
|
||||
}
|
||||
attemptCount++
|
||||
return Of[TestContext, string]("finally succeeded")
|
||||
return Of[TestContext]("finally succeeded")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -268,7 +268,7 @@ func TestRetrying(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(5)
|
||||
|
||||
eff := Retrying[TestContext, string](
|
||||
eff := Retrying(
|
||||
policy,
|
||||
func(status retry.RetryStatus) Effect[TestContext, string] {
|
||||
attemptCount++
|
||||
@@ -297,7 +297,7 @@ func TestRetrying(t *testing.T) {
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, string](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, string]("success with context")
|
||||
return Of[TestContext]("success with context")
|
||||
},
|
||||
func(res Result[string]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -335,7 +335,7 @@ func TestRetryingWithComplexScenarios(t *testing.T) {
|
||||
if status.IterNumber < 2 {
|
||||
return Fail[TestContext, State](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, State](state)
|
||||
return Of[TestContext](state)
|
||||
},
|
||||
func(res Result[State]) bool {
|
||||
return result.IsLeft(res)
|
||||
@@ -353,8 +353,8 @@ func TestRetryingWithComplexScenarios(t *testing.T) {
|
||||
attemptCount := 0
|
||||
policy := retry.LimitRetries(3)
|
||||
|
||||
eff := Chain[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("final: " + string(rune('0'+x)))
|
||||
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] {
|
||||
@@ -362,7 +362,7 @@ func TestRetryingWithComplexScenarios(t *testing.T) {
|
||||
if attemptCount < 2 {
|
||||
return Fail[TestContext, int](errors.New("retry"))
|
||||
}
|
||||
return Of[TestContext, int](attemptCount)
|
||||
return Of[TestContext](attemptCount)
|
||||
},
|
||||
func(res Result[int]) bool {
|
||||
return result.IsLeft(res)
|
||||
|
||||
@@ -26,10 +26,10 @@ import (
|
||||
func TestProvide(t *testing.T) {
|
||||
t.Run("provides context to effect", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test-value"}
|
||||
eff := Of[TestContext, string]("result")
|
||||
eff := Of[TestContext]("result")
|
||||
|
||||
ioResult := Provide[TestContext, string](ctx)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -43,10 +43,10 @@ func TestProvide(t *testing.T) {
|
||||
}
|
||||
|
||||
cfg := Config{Host: "localhost", Port: 8080}
|
||||
eff := Of[Config, string]("connected")
|
||||
eff := Of[Config]("connected")
|
||||
|
||||
ioResult := Provide[Config, string](cfg)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -59,7 +59,7 @@ func TestProvide(t *testing.T) {
|
||||
eff := Fail[TestContext, string](expectedErr)
|
||||
|
||||
ioResult := Provide[TestContext, string](ctx)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -72,10 +72,10 @@ func TestProvide(t *testing.T) {
|
||||
}
|
||||
|
||||
ctx := SimpleContext{ID: 42}
|
||||
eff := Of[SimpleContext, int](100)
|
||||
eff := Of[SimpleContext](100)
|
||||
|
||||
ioResult := Provide[SimpleContext, int](ctx)(eff)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -85,12 +85,12 @@ func TestProvide(t *testing.T) {
|
||||
t.Run("provides context to chained effects", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "base"}
|
||||
|
||||
eff := Chain[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string]("result")
|
||||
})(Of[TestContext, int](42))
|
||||
eff := Chain(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext]("result")
|
||||
})(Of[TestContext](42))
|
||||
|
||||
ioResult := Provide[TestContext, string](ctx)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -102,10 +102,10 @@ func TestProvide(t *testing.T) {
|
||||
|
||||
eff := Map[TestContext](func(x int) string {
|
||||
return "mapped"
|
||||
})(Of[TestContext, int](42))
|
||||
})(Of[TestContext](42))
|
||||
|
||||
ioResult := Provide[TestContext, string](ctx)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -116,10 +116,10 @@ func TestProvide(t *testing.T) {
|
||||
func TestRunSync(t *testing.T) {
|
||||
t.Run("runs effect synchronously", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext, int](42)
|
||||
eff := Of[TestContext](42)
|
||||
|
||||
ioResult := Provide[TestContext, int](ctx)(eff)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -128,10 +128,10 @@ func TestRunSync(t *testing.T) {
|
||||
|
||||
t.Run("runs effect with context.Context", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext, string]("hello")
|
||||
eff := Of[TestContext]("hello")
|
||||
|
||||
ioResult := Provide[TestContext, string](ctx)(eff)
|
||||
readerResult := RunSync[string](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
|
||||
bgCtx := context.Background()
|
||||
result, err := readerResult(bgCtx)
|
||||
@@ -146,7 +146,7 @@ func TestRunSync(t *testing.T) {
|
||||
eff := Fail[TestContext, int](expectedErr)
|
||||
|
||||
ioResult := Provide[TestContext, int](ctx)(eff)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
_, err := readerResult(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
@@ -156,14 +156,14 @@ func TestRunSync(t *testing.T) {
|
||||
t.Run("runs complex effect chains", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
})(Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x + 10)
|
||||
})(Of[TestContext, int](5)))
|
||||
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[TestContext, int](ctx)(eff)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -172,10 +172,10 @@ func TestRunSync(t *testing.T) {
|
||||
|
||||
t.Run("handles multiple sequential runs", func(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
eff := Of[TestContext, int](42)
|
||||
eff := Of[TestContext](42)
|
||||
|
||||
ioResult := Provide[TestContext, int](ctx)(eff)
|
||||
readerResult := RunSync[int](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
|
||||
// Run multiple times
|
||||
result1, err1 := readerResult(context.Background())
|
||||
@@ -198,10 +198,10 @@ func TestRunSync(t *testing.T) {
|
||||
|
||||
ctx := TestContext{Value: "test"}
|
||||
user := User{Name: "Alice", Age: 30}
|
||||
eff := Of[TestContext, User](user)
|
||||
eff := Of[TestContext](user)
|
||||
|
||||
ioResult := Provide[TestContext, User](ctx)(eff)
|
||||
readerResult := RunSync[User](ioResult)
|
||||
readerResult := RunSync(ioResult)
|
||||
result, err := readerResult(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
@@ -219,10 +219,10 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
cfg := AppConfig{APIKey: "secret", Timeout: 30}
|
||||
|
||||
// Create an effect that uses the config
|
||||
eff := Of[AppConfig, string]("API call successful")
|
||||
eff := Of[AppConfig]("API call successful")
|
||||
|
||||
// Provide config and run
|
||||
result, err := RunSync[string](Provide[AppConfig, string](cfg)(eff))(context.Background())
|
||||
result, err := RunSync(Provide[AppConfig, string](cfg)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "API call successful", result)
|
||||
@@ -238,7 +238,7 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
|
||||
eff := Fail[AppConfig, string](expectedErr)
|
||||
|
||||
_, err := RunSync[string](Provide[AppConfig, string](cfg)(eff))(context.Background())
|
||||
_, err := RunSync(Provide[AppConfig, string](cfg)(eff))(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, expectedErr, err)
|
||||
@@ -249,11 +249,11 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
|
||||
eff := Map[TestContext](func(x int) string {
|
||||
return "final"
|
||||
})(Chain[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
})(Of[TestContext, int](21)))
|
||||
})(Chain(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(Of[TestContext](21)))
|
||||
|
||||
result, err := RunSync[string](Provide[TestContext, string](ctx)(eff))(context.Background())
|
||||
result, err := RunSync(Provide[TestContext, string](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "final", result)
|
||||
@@ -267,7 +267,7 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
|
||||
ctx := TestContext{Value: "test"}
|
||||
|
||||
eff := Bind[TestContext](
|
||||
eff := Bind(
|
||||
func(y int) func(State) State {
|
||||
return func(s State) State {
|
||||
s.Y = y
|
||||
@@ -275,13 +275,13 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
}
|
||||
},
|
||||
func(s State) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](s.X * 2)
|
||||
return Of[TestContext](s.X * 2)
|
||||
},
|
||||
)(BindTo[TestContext](func(x int) State {
|
||||
return State{X: x}
|
||||
})(Of[TestContext, int](10)))
|
||||
})(Of[TestContext](10)))
|
||||
|
||||
result, err := RunSync[State](Provide[TestContext, State](ctx)(eff))(context.Background())
|
||||
result, err := RunSync(Provide[TestContext, State](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 10, result.X)
|
||||
@@ -297,14 +297,14 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
}
|
||||
|
||||
outerCtx := OuterCtx{Value: "outer"}
|
||||
innerEff := Of[InnerCtx, string]("inner result")
|
||||
innerEff := Of[InnerCtx]("inner result")
|
||||
|
||||
// Transform context
|
||||
transformedEff := Local[OuterCtx, InnerCtx, string](func(outer OuterCtx) InnerCtx {
|
||||
return InnerCtx{Data: outer.Value + "-transformed"}
|
||||
})(innerEff)
|
||||
|
||||
result, err := RunSync[string](Provide[OuterCtx, string](outerCtx)(transformedEff))(context.Background())
|
||||
result, err := RunSync(Provide[OuterCtx, string](outerCtx)(transformedEff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "inner result", result)
|
||||
@@ -314,11 +314,11 @@ func TestProvideAndRunSyncIntegration(t *testing.T) {
|
||||
ctx := TestContext{Value: "test"}
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
|
||||
eff := TraverseArray[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
eff := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})(input)
|
||||
|
||||
result, err := RunSync[[]int](Provide[TestContext, []int](ctx)(eff))(context.Background())
|
||||
result, err := RunSync(Provide[TestContext, []int](ctx)(eff))(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []int{2, 4, 6, 8, 10}, result)
|
||||
|
||||
@@ -27,8 +27,8 @@ import (
|
||||
func TestTraverseArray(t *testing.T) {
|
||||
t.Run("traverses empty array", func(t *testing.T) {
|
||||
input := []int{}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -39,8 +39,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("traverses array with single element", func(t *testing.T) {
|
||||
input := []int{42}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -51,8 +51,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("traverses array with multiple elements", func(t *testing.T) {
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -63,8 +63,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("transforms to different type", func(t *testing.T) {
|
||||
input := []string{"hello", "world", "test"}
|
||||
kleisli := TraverseArray[TestContext](func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](len(s))
|
||||
kleisli := TraverseArray(func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext](len(s))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -76,11 +76,11 @@ func TestTraverseArray(t *testing.T) {
|
||||
t.Run("stops on first error", func(t *testing.T) {
|
||||
expectedErr := errors.New("traverse error")
|
||||
input := []int{1, 2, 3, 4, 5}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 3 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -96,8 +96,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
}
|
||||
|
||||
input := []int{1, 2, 3}
|
||||
kleisli := TraverseArray[TestContext](func(id int) Effect[TestContext, User] {
|
||||
return Of[TestContext, User](User{
|
||||
kleisli := TraverseArray(func(id int) Effect[TestContext, User] {
|
||||
return Of[TestContext](User{
|
||||
ID: id,
|
||||
Name: fmt.Sprintf("User%d", id),
|
||||
})
|
||||
@@ -118,15 +118,15 @@ func TestTraverseArray(t *testing.T) {
|
||||
t.Run("chains with other operations", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
|
||||
eff := Chain[TestContext](func(strings []string) Effect[TestContext, int] {
|
||||
eff := Chain(func(strings []string) Effect[TestContext, int] {
|
||||
total := 0
|
||||
for _, s := range strings {
|
||||
val, _ := strconv.Atoi(s)
|
||||
total += val
|
||||
}
|
||||
return Of[TestContext, int](total)
|
||||
})(TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x * 2))
|
||||
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"})
|
||||
@@ -137,10 +137,10 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("uses context in transformation", func(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Chain[TestContext](func(ctx TestContext) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](fmt.Sprintf("%s-%d", ctx.Value, x))
|
||||
})(Of[TestContext, TestContext](TestContext{Value: "prefix"}))
|
||||
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"})
|
||||
@@ -151,8 +151,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("preserves order", func(t *testing.T) {
|
||||
input := []int{5, 3, 8, 1, 9, 2}
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 10)
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 10)
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -168,8 +168,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
input[i] = i
|
||||
}
|
||||
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](x * 2)
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, int] {
|
||||
return Of[TestContext](x * 2)
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -184,16 +184,16 @@ func TestTraverseArray(t *testing.T) {
|
||||
input := []int{1, 2, 3}
|
||||
|
||||
// First traversal: int -> string
|
||||
kleisli1 := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
kleisli1 := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
// Second traversal: string -> int (length)
|
||||
kleisli2 := TraverseArray[TestContext](func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext, int](len(s))
|
||||
kleisli2 := TraverseArray(func(s string) Effect[TestContext, int] {
|
||||
return Of[TestContext](len(s))
|
||||
})
|
||||
|
||||
eff := Chain[TestContext](kleisli2)(kleisli1(input))
|
||||
eff := Chain(kleisli2)(kleisli1(input))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
|
||||
@@ -203,8 +203,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
|
||||
t.Run("handles nil array", func(t *testing.T) {
|
||||
var input []int
|
||||
kleisli := TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
result, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -222,8 +222,8 @@ func TestTraverseArray(t *testing.T) {
|
||||
result += s + ","
|
||||
}
|
||||
return result
|
||||
})(TraverseArray[TestContext](func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
})(TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})(input))
|
||||
|
||||
result, err := runEffect(eff, TestContext{Value: "test"})
|
||||
@@ -235,11 +235,11 @@ func TestTraverseArray(t *testing.T) {
|
||||
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[TestContext](func(x int) Effect[TestContext, string] {
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 5 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
@@ -251,11 +251,11 @@ func TestTraverseArray(t *testing.T) {
|
||||
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[TestContext](func(x int) Effect[TestContext, string] {
|
||||
kleisli := TraverseArray(func(x int) Effect[TestContext, string] {
|
||||
if x == 5 {
|
||||
return Fail[TestContext, string](expectedErr)
|
||||
}
|
||||
return Of[TestContext, string](strconv.Itoa(x))
|
||||
return Of[TestContext](strconv.Itoa(x))
|
||||
})
|
||||
|
||||
_, err := runEffect(kleisli(input), TestContext{Value: "test"})
|
||||
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
@@ -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[error, string](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[error, string](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[int, string](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[error, string](func(err error) Either[string, int] {
|
||||
return Left[int](err.Error())
|
||||
})
|
||||
|
||||
// Second handler: add prefix to string error
|
||||
handler2 := ChainLeft[string, string](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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -489,6 +489,8 @@ func After[E, A any](timestamp time.Time) Operator[E, A, A] {
|
||||
// If the input is a Left value, it applies the function f to transform the error and potentially
|
||||
// change the error type from EA to EB. If the input is a Right value, it passes through unchanged.
|
||||
//
|
||||
// Note: MonadChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// This is useful for error recovery or error transformation scenarios where you want to handle
|
||||
// errors by performing another computation that may also fail.
|
||||
//
|
||||
@@ -523,6 +525,8 @@ func MonadChainLeft[EA, EB, A any](fa IOEither[EA, A], f Kleisli[EB, EA, A]) IOE
|
||||
// ChainLeft is the curried version of [MonadChainLeft].
|
||||
// It returns a function that chains a computation on the left (error) side of an [IOEither].
|
||||
//
|
||||
// Note: ChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// This is particularly useful in functional composition pipelines where you want to handle
|
||||
// errors by performing another computation that may also fail.
|
||||
//
|
||||
@@ -644,6 +648,8 @@ func TapLeft[A, EA, EB, B any](f Kleisli[EB, EA, B]) Operator[EA, A, A] {
|
||||
// If the IOEither is Left, it applies the provided function to the error value,
|
||||
// which returns a new IOEither 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 IO computations.
|
||||
// The error type can be widened from E1 to E2, allowing transformation of error types.
|
||||
//
|
||||
|
||||
@@ -490,3 +490,148 @@ func TestOrElseW(t *testing.T) {
|
||||
preserved := preserveRecover(preservedRight)()
|
||||
assert.Equal(t, E.Right[AppError](42), preserved)
|
||||
}
|
||||
|
||||
// TestChainLeftIdenticalToOrElse proves that ChainLeft and OrElse are identical functions.
|
||||
// This test verifies that both functions produce the same results for all scenarios:
|
||||
// - Left values with error recovery
|
||||
// - Left values with error transformation
|
||||
// - Right values passing through unchanged
|
||||
func TestChainLeftIdenticalToOrElse(t *testing.T) {
|
||||
// Test 1: Left value with error recovery - both should recover to Right
|
||||
t.Run("Left value recovery - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
recoveryFn := func(e string) IOEither[string, int] {
|
||||
if e == "recoverable" {
|
||||
return Right[string](42)
|
||||
}
|
||||
return Left[int](e)
|
||||
}
|
||||
|
||||
input := Left[int]("recoverable")
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(recoveryFn)(input)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(recoveryFn)(input)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](42), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 2: Left value with error transformation - both should transform error
|
||||
t.Run("Left value transformation - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
transformFn := func(e string) IOEither[string, int] {
|
||||
return Left[int]("transformed: " + e)
|
||||
}
|
||||
|
||||
input := Left[int]("original error")
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(transformFn)(input)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(transformFn)(input)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Left[int]("transformed: original error"), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 3: Right value - both should pass through unchanged
|
||||
t.Run("Right value passthrough - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
handlerFn := func(e string) IOEither[string, int] {
|
||||
return Left[int]("should not be called")
|
||||
}
|
||||
|
||||
input := Right[string](100)
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(handlerFn)(input)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(handlerFn)(input)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](100), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 4: Error type widening - both should handle type transformation
|
||||
t.Run("Error type widening - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
widenFn := func(e string) IOEither[int, int] {
|
||||
return Left[int](404)
|
||||
}
|
||||
|
||||
input := Left[int]("not found")
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(widenFn)(input)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(widenFn)(input)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Left[int](404), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 5: Composition in pipeline - both should work identically in F.Pipe
|
||||
t.Run("Pipeline composition - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
recoveryFn := func(e string) IOEither[string, int] {
|
||||
if e == "network error" {
|
||||
return Right[string](0)
|
||||
}
|
||||
return Left[int](e)
|
||||
}
|
||||
|
||||
input := Left[int]("network error")
|
||||
|
||||
// Using ChainLeft in pipeline
|
||||
resultChainLeft := F.Pipe1(input, ChainLeft(recoveryFn))()
|
||||
|
||||
// Using OrElse in pipeline
|
||||
resultOrElse := F.Pipe1(input, OrElse(recoveryFn))()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](0), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 6: Multiple chained operations - both should behave identically
|
||||
t.Run("Multiple operations - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
handler1 := func(e string) IOEither[string, int] {
|
||||
if e == "error1" {
|
||||
return Right[string](1)
|
||||
}
|
||||
return Left[int](e)
|
||||
}
|
||||
|
||||
handler2 := func(e string) IOEither[string, int] {
|
||||
if e == "error2" {
|
||||
return Right[string](2)
|
||||
}
|
||||
return Left[int](e)
|
||||
}
|
||||
|
||||
input := Left[int]("error2")
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := F.Pipe2(
|
||||
input,
|
||||
ChainLeft(handler1),
|
||||
ChainLeft(handler2),
|
||||
)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := F.Pipe2(
|
||||
input,
|
||||
OrElse(handler1),
|
||||
OrElse(handler2),
|
||||
)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](2), resultChainLeft)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
)
|
||||
|
||||
// MakeIORef creates a new IORef containing the given initial value.
|
||||
@@ -50,6 +51,32 @@ func MakeIORef[A any](a A) IO[IORef[A]] {
|
||||
}
|
||||
}
|
||||
|
||||
// Write atomically writes a new value to an IORef and returns the written value.
|
||||
//
|
||||
// This function returns a Kleisli arrow that takes an IORef and produces an IO
|
||||
// computation that writes the given value to the reference. The write operation
|
||||
// is atomic and thread-safe, using a write lock to ensure exclusive access.
|
||||
//
|
||||
// Parameters:
|
||||
// - a: The new value to write to the IORef
|
||||
//
|
||||
// Returns:
|
||||
// - A Kleisli arrow from IORef[A] to IO[A] that writes the value and returns it
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// ref := ioref.MakeIORef(42)()
|
||||
//
|
||||
// // Write a new value
|
||||
// newValue := ioref.Write(100)(ref)() // Returns 100, ref now contains 100
|
||||
//
|
||||
// // Chain writes
|
||||
// pipe.Pipe2(
|
||||
// ref,
|
||||
// ioref.Write(50),
|
||||
// io.Chain(ioref.Write(75)),
|
||||
// )() // ref now contains 75
|
||||
//
|
||||
//go:inline
|
||||
func Write[A any](a A) io.Kleisli[IORef[A], A] {
|
||||
return func(ref IORef[A]) IO[A] {
|
||||
@@ -180,6 +207,57 @@ func ModifyIOK[A any](f io.Kleisli[A, A]) io.Kleisli[IORef[A], A] {
|
||||
}
|
||||
}
|
||||
|
||||
// ModifyReaderIOK atomically modifies the value in an IORef using a ReaderIO-based transformation.
|
||||
//
|
||||
// This is a variant of ModifyIOK that works with ReaderIO computations, allowing the
|
||||
// transformation function to access an environment of type R while performing IO effects.
|
||||
// This is useful when the modification logic needs access to configuration, context,
|
||||
// or other shared resources.
|
||||
//
|
||||
// The modification is atomic and thread-safe, using a write lock to ensure exclusive
|
||||
// access during the read-modify-write cycle. The ReaderIO effect in the transformation
|
||||
// function is executed while holding the lock.
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A ReaderIO Kleisli arrow (readerio.Kleisli[R, A, A]) that takes the current value
|
||||
// and an environment R, and returns an IO computation producing the new value
|
||||
//
|
||||
// Returns:
|
||||
// - A ReaderIO Kleisli arrow from IORef[A] to ReaderIO[R, A] that returns the new value
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// multiplier int
|
||||
// }
|
||||
//
|
||||
// ref := ioref.MakeIORef(10)()
|
||||
//
|
||||
// // Modify using environment
|
||||
// modifyWithConfig := ioref.ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
// return func(cfg Config) io.IO[int] {
|
||||
// return func() int {
|
||||
// return x * cfg.multiplier
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// config := Config{multiplier: 5}
|
||||
// newValue := modifyWithConfig(ref)(config)() // Returns 50, ref now contains 50
|
||||
func ModifyReaderIOK[R, A any](f readerio.Kleisli[R, A, A]) readerio.Kleisli[R, IORef[A], A] {
|
||||
return func(ref IORef[A]) ReaderIO[R, A] {
|
||||
return func(r R) readerio.IO[A] {
|
||||
return func() A {
|
||||
ref.mu.Lock()
|
||||
defer ref.mu.Unlock()
|
||||
|
||||
ref.a = f(ref.a)(r)()
|
||||
return ref.a
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ModifyWithResult atomically modifies the value in an IORef and returns both
|
||||
// the new value and an additional result computed from the old value.
|
||||
//
|
||||
@@ -269,3 +347,62 @@ func ModifyIOKWithResult[A, B any](f io.Kleisli[A, Pair[A, B]]) io.Kleisli[IORef
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ModifyReaderIOKWithResult atomically modifies the value in an IORef and returns a result,
|
||||
// using a ReaderIO-based transformation function.
|
||||
//
|
||||
// This combines the capabilities of ModifyIOKWithResult and ModifyReaderIOK, allowing the
|
||||
// transformation function to:
|
||||
// - Access an environment of type R (like configuration or context)
|
||||
// - Perform IO effects during the transformation
|
||||
// - Both update the stored value and compute a result based on the old value
|
||||
// - Ensure atomicity of the entire read-transform-write-compute cycle
|
||||
//
|
||||
// The modification is atomic and thread-safe, using a write lock to ensure exclusive
|
||||
// access. The ReaderIO effect in the transformation function is executed while holding the lock.
|
||||
//
|
||||
// Parameters:
|
||||
// - f: A ReaderIO Kleisli arrow (readerio.Kleisli[R, A, Pair[A, B]]) that takes the old value
|
||||
// and an environment R, and returns an IO computation producing a Pair of (new value, result)
|
||||
//
|
||||
// Returns:
|
||||
// - A ReaderIO Kleisli arrow from IORef[A] to ReaderIO[R, B] that produces the result
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// logEnabled bool
|
||||
// }
|
||||
//
|
||||
// ref := ioref.MakeIORef(42)()
|
||||
//
|
||||
// // Increment with conditional logging, return old value
|
||||
// incrementWithLog := ioref.ModifyReaderIOKWithResult(
|
||||
// func(x int) readerio.ReaderIO[Config, pair.Pair[int, int]] {
|
||||
// return func(cfg Config) io.IO[pair.Pair[int, int]] {
|
||||
// return func() pair.Pair[int, int] {
|
||||
// if cfg.logEnabled {
|
||||
// fmt.Printf("Incrementing from %d\n", x)
|
||||
// }
|
||||
// return pair.MakePair(x+1, x)
|
||||
// }
|
||||
// }
|
||||
// },
|
||||
// )
|
||||
//
|
||||
// config := Config{logEnabled: true}
|
||||
// oldValue := incrementWithLog(ref)(config)() // Logs and returns 42, ref now contains 43
|
||||
func ModifyReaderIOKWithResult[R, A, B any](f readerio.Kleisli[R, A, Pair[A, B]]) readerio.Kleisli[R, IORef[A], B] {
|
||||
return func(ref IORef[A]) ReaderIO[R, B] {
|
||||
return func(r R) readerio.IO[B] {
|
||||
return func() B {
|
||||
ref.mu.Lock()
|
||||
defer ref.mu.Unlock()
|
||||
|
||||
result := f(ref.a)(r)()
|
||||
ref.a = pair.Head(result)
|
||||
return pair.Tail(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,9 +24,588 @@ import (
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
N "github.com/IBM/fp-go/v2/number"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMakeIORef(t *testing.T) {
|
||||
t.Run("creates IORef with integer value", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Equal(t, 42, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("creates IORef with string value", func(t *testing.T) {
|
||||
ref := MakeIORef("hello")()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Equal(t, "hello", Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("creates IORef with slice value", func(t *testing.T) {
|
||||
slice := []int{1, 2, 3}
|
||||
ref := MakeIORef(slice)()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Equal(t, slice, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("creates IORef with struct value", func(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
person := Person{Name: "Alice", Age: 30}
|
||||
ref := MakeIORef(person)()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Equal(t, person, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("creates IORef with zero value", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Equal(t, 0, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("creates IORef with nil pointer", func(t *testing.T) {
|
||||
var ptr *int
|
||||
ref := MakeIORef(ptr)()
|
||||
assert.NotNil(t, ref)
|
||||
assert.Nil(t, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("multiple IORefs are independent", func(t *testing.T) {
|
||||
ref1 := MakeIORef(10)()
|
||||
ref2 := MakeIORef(20)()
|
||||
|
||||
assert.Equal(t, 10, Read(ref1)())
|
||||
assert.Equal(t, 20, Read(ref2)())
|
||||
|
||||
Write(30)(ref1)()
|
||||
assert.Equal(t, 30, Read(ref1)())
|
||||
assert.Equal(t, 20, Read(ref2)()) // ref2 unchanged
|
||||
})
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
t.Run("reads initial value", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
value := Read(ref)()
|
||||
assert.Equal(t, 42, value)
|
||||
})
|
||||
|
||||
t.Run("reads updated value", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
Write(20)(ref)()
|
||||
value := Read(ref)()
|
||||
assert.Equal(t, 20, value)
|
||||
})
|
||||
|
||||
t.Run("multiple reads return same value", func(t *testing.T) {
|
||||
ref := MakeIORef(100)()
|
||||
value1 := Read(ref)()
|
||||
value2 := Read(ref)()
|
||||
value3 := Read(ref)()
|
||||
assert.Equal(t, 100, value1)
|
||||
assert.Equal(t, 100, value2)
|
||||
assert.Equal(t, 100, value3)
|
||||
})
|
||||
|
||||
t.Run("concurrent reads are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
results := make([]int, iterations)
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
results[idx] = Read(ref)()
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// All reads should return the same value
|
||||
for _, v := range results {
|
||||
assert.Equal(t, 42, v)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reads during concurrent writes", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
var wg sync.WaitGroup
|
||||
iterations := 50
|
||||
|
||||
// Start concurrent writes
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func(val int) {
|
||||
defer wg.Done()
|
||||
Write(val)(ref)()
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Start concurrent reads
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
value := Read(ref)()
|
||||
// Value should be valid (between 0 and iterations-1)
|
||||
assert.GreaterOrEqual(t, value, 0)
|
||||
assert.Less(t, value, iterations)
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
})
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
t.Run("writes new value", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
result := Write(100)(ref)()
|
||||
assert.Equal(t, 100, result)
|
||||
assert.Equal(t, 100, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("overwrites existing value", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
Write(20)(ref)()
|
||||
Write(30)(ref)()
|
||||
assert.Equal(t, 30, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("returns written value", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
result := Write(42)(ref)()
|
||||
assert.Equal(t, 42, result)
|
||||
})
|
||||
|
||||
t.Run("writes string value", func(t *testing.T) {
|
||||
ref := MakeIORef("hello")()
|
||||
result := Write("world")(ref)()
|
||||
assert.Equal(t, "world", result)
|
||||
assert.Equal(t, "world", Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("chained writes", func(t *testing.T) {
|
||||
ref := MakeIORef(1)()
|
||||
Write(2)(ref)()
|
||||
Write(3)(ref)()
|
||||
result := Write(4)(ref)()
|
||||
assert.Equal(t, 4, result)
|
||||
assert.Equal(t, 4, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("concurrent writes are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func(val int) {
|
||||
defer wg.Done()
|
||||
Write(val)(ref)()
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Final value should be one of the written values
|
||||
finalValue := Read(ref)()
|
||||
assert.GreaterOrEqual(t, finalValue, 0)
|
||||
assert.Less(t, finalValue, iterations)
|
||||
})
|
||||
|
||||
t.Run("write with zero value", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
Write(0)(ref)()
|
||||
assert.Equal(t, 0, Read(ref)())
|
||||
})
|
||||
}
|
||||
|
||||
func TestModify(t *testing.T) {
|
||||
t.Run("modifies value with simple function", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
result := Modify(func(x int) int { return x * 2 })(ref)()
|
||||
assert.Equal(t, 20, result)
|
||||
assert.Equal(t, 20, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("modifies with addition", func(t *testing.T) {
|
||||
ref := MakeIORef(5)()
|
||||
Modify(func(x int) int { return x + 10 })(ref)()
|
||||
assert.Equal(t, 15, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("modifies string value", func(t *testing.T) {
|
||||
ref := MakeIORef("hello")()
|
||||
result := Modify(func(s string) string { return s + " world" })(ref)()
|
||||
assert.Equal(t, "hello world", result)
|
||||
assert.Equal(t, "hello world", Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("chained modifications", func(t *testing.T) {
|
||||
ref := MakeIORef(2)()
|
||||
Modify(func(x int) int { return x * 3 })(ref)() // 6
|
||||
Modify(func(x int) int { return x + 4 })(ref)() // 10
|
||||
result := Modify(func(x int) int { return x / 2 })(ref)()
|
||||
assert.Equal(t, 5, result)
|
||||
assert.Equal(t, 5, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("concurrent modifications are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
Modify(func(x int) int { return x + 1 })(ref)()
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(t, iterations, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("modify with identity function", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
result := Modify(func(x int) int { return x })(ref)()
|
||||
assert.Equal(t, 42, result)
|
||||
assert.Equal(t, 42, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("modify returns new value", func(t *testing.T) {
|
||||
ref := MakeIORef(100)()
|
||||
result := Modify(func(x int) int { return x - 50 })(ref)()
|
||||
assert.Equal(t, 50, result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestModifyWithResult(t *testing.T) {
|
||||
t.Run("modifies and returns old value", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
oldValue := ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
return pair.MakePair(x+1, x)
|
||||
})(ref)()
|
||||
assert.Equal(t, 42, oldValue)
|
||||
assert.Equal(t, 43, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("swaps value and returns old", func(t *testing.T) {
|
||||
ref := MakeIORef(100)()
|
||||
oldValue := ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
return pair.MakePair(200, x)
|
||||
})(ref)()
|
||||
assert.Equal(t, 100, oldValue)
|
||||
assert.Equal(t, 200, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("returns different type", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
message := ModifyWithResult(func(x int) pair.Pair[int, string] {
|
||||
return pair.MakePair(x*2, fmt.Sprintf("doubled from %d", x))
|
||||
})(ref)()
|
||||
assert.Equal(t, "doubled from 42", message)
|
||||
assert.Equal(t, 84, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("computes result based on old value", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
wasPositive := ModifyWithResult(func(x int) pair.Pair[int, bool] {
|
||||
return pair.MakePair(x+5, x > 0)
|
||||
})(ref)()
|
||||
assert.True(t, wasPositive)
|
||||
assert.Equal(t, 15, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("chained modifications with results", func(t *testing.T) {
|
||||
ref := MakeIORef(5)()
|
||||
result1 := ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
return pair.MakePair(x*2, x)
|
||||
})(ref)()
|
||||
result2 := ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
return pair.MakePair(x+10, x)
|
||||
})(ref)()
|
||||
assert.Equal(t, 5, result1)
|
||||
assert.Equal(t, 10, result2)
|
||||
assert.Equal(t, 20, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("concurrent modifications with results are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
results := make([]int, iterations)
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
oldValue := ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
return pair.MakePair(x+1, x)
|
||||
})(ref)()
|
||||
results[idx] = oldValue
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(t, iterations, Read(ref)())
|
||||
|
||||
// All old values should be unique
|
||||
seen := make(map[int]bool)
|
||||
for _, v := range results {
|
||||
assert.False(t, seen[v])
|
||||
seen[v] = true
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("extract and replace pattern", func(t *testing.T) {
|
||||
ref := MakeIORef([]int{1, 2, 3})()
|
||||
first := ModifyWithResult(func(xs []int) pair.Pair[[]int, int] {
|
||||
if len(xs) == 0 {
|
||||
return pair.MakePair(xs, 0)
|
||||
}
|
||||
return pair.MakePair(xs[1:], xs[0])
|
||||
})(ref)()
|
||||
assert.Equal(t, 1, first)
|
||||
assert.Equal(t, []int{2, 3}, Read(ref)())
|
||||
})
|
||||
}
|
||||
|
||||
func TestModifyReaderIOK(t *testing.T) {
|
||||
type Config struct {
|
||||
multiplier int
|
||||
}
|
||||
|
||||
t.Run("modifies with environment", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
config := Config{multiplier: 5}
|
||||
|
||||
result := ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
return func(cfg Config) io.IO[int] {
|
||||
return io.Of(x * cfg.multiplier)
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, 50, result)
|
||||
assert.Equal(t, 50, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("uses environment for computation", func(t *testing.T) {
|
||||
ref := MakeIORef(100)()
|
||||
config := Config{multiplier: 2}
|
||||
|
||||
result := ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
return func(cfg Config) io.IO[int] {
|
||||
return func() int {
|
||||
return x / cfg.multiplier
|
||||
}
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, 50, result)
|
||||
assert.Equal(t, 50, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("chained modifications with different configs", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
config1 := Config{multiplier: 2}
|
||||
config2 := Config{multiplier: 3}
|
||||
|
||||
ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
return func(cfg Config) io.IO[int] {
|
||||
return io.Of(x * cfg.multiplier)
|
||||
}
|
||||
})(ref)(config1)()
|
||||
|
||||
result := ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
return func(cfg Config) io.IO[int] {
|
||||
return io.Of(x + cfg.multiplier)
|
||||
}
|
||||
})(ref)(config2)()
|
||||
|
||||
assert.Equal(t, 23, result) // (10 * 2) + 3
|
||||
assert.Equal(t, 23, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("concurrent modifications with environment are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
config := Config{multiplier: 1}
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ModifyReaderIOK(func(x int) readerio.ReaderIO[Config, int] {
|
||||
return func(cfg Config) io.IO[int] {
|
||||
return io.Of(x + cfg.multiplier)
|
||||
}
|
||||
})(ref)(config)()
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(t, iterations, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("environment provides configuration", func(t *testing.T) {
|
||||
type Settings struct {
|
||||
prefix string
|
||||
}
|
||||
ref := MakeIORef("world")()
|
||||
settings := Settings{prefix: "hello "}
|
||||
|
||||
result := ModifyReaderIOK(func(s string) readerio.ReaderIO[Settings, string] {
|
||||
return func(cfg Settings) io.IO[string] {
|
||||
return io.Of(cfg.prefix + s)
|
||||
}
|
||||
})(ref)(settings)()
|
||||
|
||||
assert.Equal(t, "hello world", result)
|
||||
assert.Equal(t, "hello world", Read(ref)())
|
||||
})
|
||||
}
|
||||
|
||||
func TestModifyReaderIOKWithResult(t *testing.T) {
|
||||
type Config struct {
|
||||
logEnabled bool
|
||||
multiplier int
|
||||
}
|
||||
|
||||
t.Run("modifies with environment and returns result", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
config := Config{logEnabled: false, multiplier: 2}
|
||||
|
||||
oldValue := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, int]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, int]] {
|
||||
return io.Of(pair.MakePair(x*cfg.multiplier, x))
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, 42, oldValue)
|
||||
assert.Equal(t, 84, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("returns different type based on environment", func(t *testing.T) {
|
||||
ref := MakeIORef(10)()
|
||||
config := Config{logEnabled: true, multiplier: 3}
|
||||
|
||||
message := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, string]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, string]] {
|
||||
return func() pair.Pair[int, string] {
|
||||
newVal := x * cfg.multiplier
|
||||
msg := fmt.Sprintf("multiplied %d by %d", x, cfg.multiplier)
|
||||
return pair.MakePair(newVal, msg)
|
||||
}
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, "multiplied 10 by 3", message)
|
||||
assert.Equal(t, 30, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("conditional logic based on environment", func(t *testing.T) {
|
||||
ref := MakeIORef(-10)()
|
||||
config := Config{logEnabled: true, multiplier: 2}
|
||||
|
||||
message := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, string]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, string]] {
|
||||
return func() pair.Pair[int, string] {
|
||||
if x < 0 {
|
||||
return pair.MakePair(0, "reset negative value")
|
||||
}
|
||||
return pair.MakePair(x*cfg.multiplier, "multiplied positive value")
|
||||
}
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, "reset negative value", message)
|
||||
assert.Equal(t, 0, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("chained modifications with results", func(t *testing.T) {
|
||||
ref := MakeIORef(5)()
|
||||
config := Config{logEnabled: false, multiplier: 2}
|
||||
|
||||
result1 := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, int]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, int]] {
|
||||
return io.Of(pair.MakePair(x*cfg.multiplier, x))
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
result2 := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, int]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, int]] {
|
||||
return io.Of(pair.MakePair(x+cfg.multiplier, x))
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, 5, result1)
|
||||
assert.Equal(t, 10, result2)
|
||||
assert.Equal(t, 12, Read(ref)())
|
||||
})
|
||||
|
||||
t.Run("concurrent modifications with environment are thread-safe", func(t *testing.T) {
|
||||
ref := MakeIORef(0)()
|
||||
config := Config{logEnabled: false, multiplier: 1}
|
||||
var wg sync.WaitGroup
|
||||
iterations := 100
|
||||
results := make([]int, iterations)
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
oldValue := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[Config, pair.Pair[int, int]] {
|
||||
return func(cfg Config) io.IO[pair.Pair[int, int]] {
|
||||
return io.Of(pair.MakePair(x+cfg.multiplier, x))
|
||||
}
|
||||
})(ref)(config)()
|
||||
results[idx] = oldValue
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
assert.Equal(t, iterations, Read(ref)())
|
||||
|
||||
// All old values should be unique
|
||||
seen := make(map[int]bool)
|
||||
for _, v := range results {
|
||||
assert.False(t, seen[v])
|
||||
seen[v] = true
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("environment provides validation rules", func(t *testing.T) {
|
||||
type ValidationConfig struct {
|
||||
maxValue int
|
||||
}
|
||||
ref := MakeIORef(100)()
|
||||
config := ValidationConfig{maxValue: 50}
|
||||
|
||||
message := ModifyReaderIOKWithResult(func(x int) readerio.ReaderIO[ValidationConfig, pair.Pair[int, string]] {
|
||||
return func(cfg ValidationConfig) io.IO[pair.Pair[int, string]] {
|
||||
return func() pair.Pair[int, string] {
|
||||
if x > cfg.maxValue {
|
||||
return pair.MakePair(cfg.maxValue, fmt.Sprintf("capped at %d", cfg.maxValue))
|
||||
}
|
||||
return pair.MakePair(x, "value within limits")
|
||||
}
|
||||
}
|
||||
})(ref)(config)()
|
||||
|
||||
assert.Equal(t, "capped at 50", message)
|
||||
assert.Equal(t, 50, Read(ref)())
|
||||
})
|
||||
}
|
||||
|
||||
func TestModifyIOK(t *testing.T) {
|
||||
t.Run("basic modification with IO effect", func(t *testing.T) {
|
||||
ref := MakeIORef(42)()
|
||||
|
||||
@@ -45,29 +45,110 @@ import (
|
||||
"github.com/IBM/fp-go/v2/endomorphism"
|
||||
"github.com/IBM/fp-go/v2/io"
|
||||
"github.com/IBM/fp-go/v2/pair"
|
||||
"github.com/IBM/fp-go/v2/readerio"
|
||||
)
|
||||
|
||||
type (
|
||||
// ioRef is the internal implementation of a mutable reference.
|
||||
// It uses a read-write mutex to ensure thread-safe access.
|
||||
// It uses a read-write mutex to ensure thread-safe access to the stored value.
|
||||
//
|
||||
// The mutex allows multiple concurrent readers (using RLock) but ensures
|
||||
// exclusive access for writers (using Lock), preventing race conditions
|
||||
// when reading or modifying the stored value.
|
||||
//
|
||||
// This type is not exported; users interact with it through the IORef type alias.
|
||||
ioRef[A any] struct {
|
||||
mu sync.RWMutex
|
||||
a A
|
||||
mu sync.RWMutex // Protects concurrent access to the stored value
|
||||
a A // The stored value
|
||||
}
|
||||
|
||||
// IO represents a synchronous computation that may have side effects.
|
||||
// It's a function that takes no arguments and returns a value of type A.
|
||||
//
|
||||
// IO computations are lazy - they don't execute until explicitly invoked
|
||||
// by calling the function. This allows for composing and chaining effects
|
||||
// before execution.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Define an IO computation
|
||||
// computation := func() int {
|
||||
// fmt.Println("Computing...")
|
||||
// return 42
|
||||
// }
|
||||
//
|
||||
// // Nothing happens yet - the computation is lazy
|
||||
// result := computation() // Now it executes and prints "Computing..."
|
||||
IO[A any] = io.IO[A]
|
||||
|
||||
// ReaderIO represents a computation that requires an environment of type R
|
||||
// and produces an IO effect that yields a value of type A.
|
||||
//
|
||||
// This combines the Reader pattern (dependency injection) with IO effects,
|
||||
// allowing computations to access shared configuration or context while
|
||||
// performing side effects.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// multiplier int
|
||||
// }
|
||||
//
|
||||
// // A ReaderIO that uses config to compute a value
|
||||
// computation := func(cfg Config) io.IO[int] {
|
||||
// return func() int {
|
||||
// return 42 * cfg.multiplier
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Execute with specific config
|
||||
// result := computation(Config{multiplier: 2})() // Returns 84
|
||||
ReaderIO[R, A any] = readerio.ReaderIO[R, A]
|
||||
|
||||
// IORef represents a mutable reference to a value of type A.
|
||||
// Operations on IORef are thread-safe and performed within the IO monad.
|
||||
//
|
||||
// IORef provides a way to work with mutable state in a functional style,
|
||||
// where mutations are explicit and contained within IO computations.
|
||||
// This makes side effects visible in the type system and allows for
|
||||
// better reasoning about code that uses mutable state.
|
||||
//
|
||||
// All operations on IORef (Read, Write, Modify, etc.) are atomic and
|
||||
// thread-safe, making it safe to share IORefs across goroutines.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Create a new IORef
|
||||
// ref := ioref.MakeIORef(42)()
|
||||
//
|
||||
// // Read the current value
|
||||
// value := ioref.Read(ref)() // 42
|
||||
//
|
||||
// // Write a new value
|
||||
// ioref.Write(100)(ref)()
|
||||
//
|
||||
// // Modify the value atomically
|
||||
// ioref.Modify(func(x int) int { return x * 2 })(ref)()
|
||||
IORef[A any] = *ioRef[A]
|
||||
|
||||
// Endomorphism represents a function from A to A.
|
||||
// It's commonly used with Modify to transform the value in an IORef.
|
||||
//
|
||||
// An endomorphism is a morphism (structure-preserving map) from a
|
||||
// mathematical object to itself. In programming terms, it's simply
|
||||
// a function that takes a value and returns a value of the same type.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // An endomorphism that doubles an integer
|
||||
// double := func(x int) int { return x * 2 }
|
||||
//
|
||||
// // An endomorphism that uppercases a string
|
||||
// upper := func(s string) string { return strings.ToUpper(s) }
|
||||
//
|
||||
// // Use with IORef
|
||||
// ref := ioref.MakeIORef(21)()
|
||||
// ioref.Modify(double)(ref)() // ref now contains 42
|
||||
Endomorphism[A any] = endomorphism.Endomorphism[A]
|
||||
|
||||
// Pair represents a tuple of two values of types A and B.
|
||||
@@ -76,6 +157,8 @@ type (
|
||||
//
|
||||
// The head of the pair contains the new value to store in the IORef,
|
||||
// while the tail contains the result to return from the operation.
|
||||
// This allows atomic operations that both update the reference and
|
||||
// compute a result based on the old value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
@@ -85,5 +168,11 @@ type (
|
||||
// // Extract values
|
||||
// newVal := pair.Head(p) // Gets the head (new value)
|
||||
// oldVal := pair.Tail(p) // Gets the tail (old value)
|
||||
//
|
||||
// // Use with ModifyWithResult to swap and return old value
|
||||
// ref := ioref.MakeIORef(42)()
|
||||
// oldValue := ioref.ModifyWithResult(func(x int) pair.Pair[int, int] {
|
||||
// return pair.MakePair(100, x) // Store 100, return old value
|
||||
// })(ref)() // oldValue is 42, ref now contains 100
|
||||
Pair[A, B any] = pair.Pair[A, B]
|
||||
)
|
||||
|
||||
@@ -710,6 +710,146 @@ func TestTranscodeEither(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestTranscodeEitherValidation(t *testing.T) {
|
||||
t.Run("validates Left value with context", func(t *testing.T) {
|
||||
eitherCodec := TranscodeEither(String(), Int())
|
||||
result := eitherCodec.Decode(either.Left[any, any](123)) // Invalid: should be string
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(either.Either[string, int]) validation.Errors { return nil },
|
||||
)
|
||||
assert.NotEmpty(t, errors)
|
||||
// Verify error contains type information
|
||||
assert.Contains(t, fmt.Sprintf("%v", errors[0]), "string")
|
||||
})
|
||||
|
||||
t.Run("validates Right value with context", func(t *testing.T) {
|
||||
eitherCodec := TranscodeEither(String(), Int())
|
||||
result := eitherCodec.Decode(either.Right[any, any]("not a number"))
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(either.Either[string, int]) validation.Errors { return nil },
|
||||
)
|
||||
assert.NotEmpty(t, errors)
|
||||
// Verify error contains type information
|
||||
assert.Contains(t, fmt.Sprintf("%v", errors[0]), "int")
|
||||
})
|
||||
|
||||
t.Run("preserves Either structure on validation failure", func(t *testing.T) {
|
||||
eitherCodec := TranscodeEither(String(), Int())
|
||||
|
||||
// Left with wrong type
|
||||
leftResult := eitherCodec.Decode(either.Left[any, any]([]int{1, 2, 3}))
|
||||
assert.True(t, either.IsLeft(leftResult))
|
||||
|
||||
// Right with wrong type
|
||||
rightResult := eitherCodec.Decode(either.Right[any, any](true))
|
||||
assert.True(t, either.IsLeft(rightResult))
|
||||
})
|
||||
|
||||
t.Run("validates with custom codec that can fail", func(t *testing.T) {
|
||||
// Create a codec that only accepts positive integers
|
||||
positiveInt := MakeType(
|
||||
"PositiveInt",
|
||||
func(u any) either.Either[error, int] {
|
||||
i, ok := u.(int)
|
||||
if !ok || i <= 0 {
|
||||
return either.Left[int](fmt.Errorf("not a positive integer"))
|
||||
}
|
||||
return either.Of[error](i)
|
||||
},
|
||||
func(i int) Decode[Context, int] {
|
||||
return func(c Context) Validation[int] {
|
||||
if i <= 0 {
|
||||
return validation.FailureWithMessage[int](i, "must be positive")(c)
|
||||
}
|
||||
return validation.Success(i)
|
||||
}
|
||||
},
|
||||
F.Identity[int],
|
||||
)
|
||||
|
||||
eitherCodec := TranscodeEither(String(), positiveInt)
|
||||
|
||||
// Valid positive integer
|
||||
validResult := eitherCodec.Decode(either.Right[any](42))
|
||||
assert.True(t, either.IsRight(validResult))
|
||||
|
||||
// Invalid: zero
|
||||
zeroResult := eitherCodec.Decode(either.Right[any](0))
|
||||
assert.True(t, either.IsLeft(zeroResult))
|
||||
|
||||
// Invalid: negative
|
||||
negativeResult := eitherCodec.Decode(either.Right[any](-5))
|
||||
assert.True(t, either.IsLeft(negativeResult))
|
||||
})
|
||||
|
||||
t.Run("validates both branches independently", func(t *testing.T) {
|
||||
// Create codecs with specific validation rules
|
||||
nonEmptyString := MakeType(
|
||||
"NonEmptyString",
|
||||
func(u any) either.Either[error, string] {
|
||||
s, ok := u.(string)
|
||||
if !ok || len(s) == 0 {
|
||||
return either.Left[string](fmt.Errorf("not a non-empty string"))
|
||||
}
|
||||
return either.Of[error](s)
|
||||
},
|
||||
func(s string) Decode[Context, string] {
|
||||
return func(c Context) Validation[string] {
|
||||
if len(s) == 0 {
|
||||
return validation.FailureWithMessage[string](s, "must not be empty")(c)
|
||||
}
|
||||
return validation.Success(s)
|
||||
}
|
||||
},
|
||||
F.Identity[string],
|
||||
)
|
||||
|
||||
evenInt := MakeType(
|
||||
"EvenInt",
|
||||
func(u any) either.Either[error, int] {
|
||||
i, ok := u.(int)
|
||||
if !ok || i%2 != 0 {
|
||||
return either.Left[int](fmt.Errorf("not an even integer"))
|
||||
}
|
||||
return either.Of[error](i)
|
||||
},
|
||||
func(i int) Decode[Context, int] {
|
||||
return func(c Context) Validation[int] {
|
||||
if i%2 != 0 {
|
||||
return validation.FailureWithMessage[int](i, "must be even")(c)
|
||||
}
|
||||
return validation.Success(i)
|
||||
}
|
||||
},
|
||||
F.Identity[int],
|
||||
)
|
||||
|
||||
eitherCodec := TranscodeEither(nonEmptyString, evenInt)
|
||||
|
||||
// Valid Left: non-empty string
|
||||
validLeft := eitherCodec.Decode(either.Left[int]("hello"))
|
||||
assert.True(t, either.IsRight(validLeft))
|
||||
|
||||
// Invalid Left: empty string
|
||||
invalidLeft := eitherCodec.Decode(either.Left[int](""))
|
||||
assert.True(t, either.IsLeft(invalidLeft))
|
||||
|
||||
// Valid Right: even integer
|
||||
validRight := eitherCodec.Decode(either.Right[string](42))
|
||||
assert.True(t, either.IsRight(validRight))
|
||||
|
||||
// Invalid Right: odd integer
|
||||
invalidRight := eitherCodec.Decode(either.Right[string](43))
|
||||
assert.True(t, either.IsLeft(invalidRight))
|
||||
})
|
||||
}
|
||||
|
||||
func TestTranscodeEitherWithTransformation(t *testing.T) {
|
||||
// Create a codec that transforms strings to their lengths
|
||||
stringToLength := MakeType(
|
||||
|
||||
321
v2/optics/codec/decode/OrElse_ChainLeft_explanation.md
Normal file
321
v2/optics/codec/decode/OrElse_ChainLeft_explanation.md
Normal file
@@ -0,0 +1,321 @@
|
||||
# ChainLeft and OrElse in the Decode Package
|
||||
|
||||
## Overview
|
||||
|
||||
In [`optics/codec/decode/monad.go`](monad.go:53-62), the [`ChainLeft`](monad.go:53) and [`OrElse`](monad.go:60) functions work with decoders that may fail during decoding operations.
|
||||
|
||||
```go
|
||||
func ChainLeft[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return readert.Chain[Decode[I, A]](
|
||||
validation.ChainLeft,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
func OrElse[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return ChainLeft(f)
|
||||
}
|
||||
```
|
||||
|
||||
## Key Insight: OrElse is ChainLeft
|
||||
|
||||
**`OrElse` is exactly the same as `ChainLeft`** - they are aliases with identical implementations and behavior. The choice between them is purely about **code readability and semantic intent**.
|
||||
|
||||
## Understanding the Types
|
||||
|
||||
### Decode[I, A]
|
||||
A decoder that takes input of type `I` and produces a `Validation[A]`:
|
||||
```go
|
||||
type Decode[I, A any] = func(I) Validation[A]
|
||||
```
|
||||
|
||||
### Kleisli[I, Errors, A]
|
||||
A function that takes `Errors` and produces a `Decode[I, A]`:
|
||||
```go
|
||||
type Kleisli[I, Errors, A] = func(Errors) Decode[I, A]
|
||||
```
|
||||
|
||||
This allows error handlers to:
|
||||
1. Access the validation errors that occurred
|
||||
2. Access the original input (via the returned Decode function)
|
||||
3. Either recover with a success value or produce new errors
|
||||
|
||||
### Operator[I, A, A]
|
||||
A function that transforms one decoder into another:
|
||||
```go
|
||||
type Operator[I, A, A] = func(Decode[I, A]) Decode[I, A]
|
||||
```
|
||||
|
||||
## Core Behavior
|
||||
|
||||
Both [`ChainLeft`](monad.go:53) and [`OrElse`](monad.go:60) delegate to [`validation.ChainLeft`](../validation/monad.go:304), which provides:
|
||||
|
||||
### 1. Error Aggregation
|
||||
When the transformation function returns a failure, **both the original errors AND the new errors are combined** using the Errors monoid:
|
||||
|
||||
```go
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "original error"},
|
||||
})
|
||||
}
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "additional error"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := handler(failingDecoder)
|
||||
result := decoder("input")
|
||||
// Result contains BOTH errors: ["original error", "additional error"]
|
||||
```
|
||||
|
||||
### 2. Success Pass-Through
|
||||
Success values pass through unchanged - the handler is never called:
|
||||
|
||||
```go
|
||||
successDecoder := Of[string](42)
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "never called"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := handler(successDecoder)
|
||||
result := decoder("input")
|
||||
// Result: Success(42) - unchanged
|
||||
```
|
||||
|
||||
### 3. Error Recovery
|
||||
The handler can recover from failures by returning a successful decoder:
|
||||
|
||||
```go
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "not found"},
|
||||
})
|
||||
}
|
||||
|
||||
recoverFromNotFound := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "not found" {
|
||||
return Of[string](0) // recover with default
|
||||
}
|
||||
}
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
})
|
||||
|
||||
decoder := recoverFromNotFound(failingDecoder)
|
||||
result := decoder("input")
|
||||
// Result: Success(0) - recovered from failure
|
||||
```
|
||||
|
||||
### 4. Access to Original Input
|
||||
The handler returns a `Decode[I, A]` function, giving it access to the original input:
|
||||
|
||||
```go
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
// Can access both errs and input here
|
||||
if input == "special" {
|
||||
return validation.Of(999)
|
||||
}
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Fallback Decoding (OrElse reads better)
|
||||
|
||||
```go
|
||||
// Primary decoder that may fail
|
||||
primaryDecoder := func(input string) Validation[int] {
|
||||
n, err := strconv.Atoi(input)
|
||||
if err != nil {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "not a valid integer"},
|
||||
})
|
||||
}
|
||||
return validation.Of(n)
|
||||
}
|
||||
|
||||
// Use OrElse for semantic clarity - "try primary, or else use default"
|
||||
withDefault := OrElse(func(errs Errors) Decode[string, int] {
|
||||
return Of[string](0) // default to 0 if decoding fails
|
||||
})
|
||||
|
||||
decoder := withDefault(primaryDecoder)
|
||||
|
||||
result1 := decoder("42") // Success(42)
|
||||
result2 := decoder("abc") // Success(0) - fallback
|
||||
```
|
||||
|
||||
### 2. Error Context Addition (ChainLeft reads better)
|
||||
|
||||
```go
|
||||
decodeUserAge := func(data map[string]any) Validation[int] {
|
||||
age, ok := data["age"].(int)
|
||||
if !ok {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: data["age"], Messsage: "invalid type"},
|
||||
})
|
||||
}
|
||||
return validation.Of(age)
|
||||
}
|
||||
|
||||
// Use ChainLeft when emphasizing error transformation
|
||||
addContext := ChainLeft(func(errs Errors) Decode[map[string]any, int] {
|
||||
return func(data map[string]any) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{
|
||||
Context: validation.Context{{Key: "user", Type: "User"}, {Key: "age", Type: "int"}},
|
||||
Messsage: "failed to decode user age",
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := addContext(decodeUserAge)
|
||||
// Errors will include both original error and context
|
||||
```
|
||||
|
||||
### 3. Conditional Recovery Based on Input
|
||||
|
||||
```go
|
||||
decodePort := func(input string) Validation[int] {
|
||||
port, err := strconv.Atoi(input)
|
||||
if err != nil {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "invalid port"},
|
||||
})
|
||||
}
|
||||
return validation.Of(port)
|
||||
}
|
||||
|
||||
// Recover with different defaults based on input
|
||||
smartDefault := OrElse(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
// Check input to determine appropriate default
|
||||
if strings.Contains(input, "http") {
|
||||
return validation.Of(80)
|
||||
}
|
||||
if strings.Contains(input, "https") {
|
||||
return validation.Of(443)
|
||||
}
|
||||
return validation.Of(8080)
|
||||
}
|
||||
})
|
||||
|
||||
decoder := smartDefault(decodePort)
|
||||
result1 := decoder("http-server") // Success(80)
|
||||
result2 := decoder("https-server") // Success(443)
|
||||
result3 := decoder("other") // Success(8080)
|
||||
```
|
||||
|
||||
### 4. Pipeline Composition
|
||||
|
||||
```go
|
||||
type Config struct {
|
||||
DatabaseURL string
|
||||
}
|
||||
|
||||
decodeConfig := func(data map[string]any) Validation[Config] {
|
||||
url, ok := data["db_url"].(string)
|
||||
if !ok {
|
||||
return either.Left[Config](validation.Errors{
|
||||
{Messsage: "missing db_url"},
|
||||
})
|
||||
}
|
||||
return validation.Of(Config{DatabaseURL: url})
|
||||
}
|
||||
|
||||
// Build a pipeline with multiple error handlers
|
||||
decoder := F.Pipe2(
|
||||
decodeConfig,
|
||||
OrElse(func(errs Errors) Decode[map[string]any, Config] {
|
||||
// Try environment variable as fallback
|
||||
return func(data map[string]any) Validation[Config] {
|
||||
if url := os.Getenv("DATABASE_URL"); url != "" {
|
||||
return validation.Of(Config{DatabaseURL: url})
|
||||
}
|
||||
return either.Left[Config](errs)
|
||||
}
|
||||
}),
|
||||
OrElse(func(errs Errors) Decode[map[string]any, Config] {
|
||||
// Final fallback to default
|
||||
return Of[map[string]any](Config{
|
||||
DatabaseURL: "localhost:5432",
|
||||
})
|
||||
}),
|
||||
)
|
||||
```
|
||||
|
||||
## Comparison with validation.ChainLeft
|
||||
|
||||
The decode package's [`ChainLeft`](monad.go:53) wraps [`validation.ChainLeft`](../validation/monad.go:304) using the Reader transformer pattern:
|
||||
|
||||
| Aspect | validation.ChainLeft | decode.ChainLeft |
|
||||
|--------|---------------------|------------------|
|
||||
| **Input** | `Validation[A]` | `Decode[I, A]` (function) |
|
||||
| **Handler** | `func(Errors) Validation[A]` | `func(Errors) Decode[I, A]` |
|
||||
| **Output** | `Validation[A]` | `Decode[I, A]` (function) |
|
||||
| **Context** | No input access | Access to original input `I` |
|
||||
| **Use Case** | Pure validation logic | Decoding with input-dependent recovery |
|
||||
|
||||
The key difference is that decode's version gives handlers access to the original input through the returned `Decode[I, A]` function.
|
||||
|
||||
## When to Use Which Name
|
||||
|
||||
### Use **OrElse** when:
|
||||
- Emphasizing fallback/alternative decoding logic
|
||||
- Providing default values on decode failure
|
||||
- The intent is "try this, or else try that"
|
||||
- Code reads more naturally with "or else"
|
||||
|
||||
### Use **ChainLeft** when:
|
||||
- Emphasizing technical error channel transformation
|
||||
- Adding context or enriching error information
|
||||
- The focus is on error handling mechanics
|
||||
- Working with other functional programming concepts
|
||||
|
||||
## Verification
|
||||
|
||||
The test suite in [`monad_test.go`](monad_test.go:385) includes comprehensive tests proving that `OrElse` and `ChainLeft` are equivalent:
|
||||
|
||||
- ✅ Identical behavior for Success values
|
||||
- ✅ Identical behavior for error recovery
|
||||
- ✅ Identical behavior for error aggregation
|
||||
- ✅ Identical behavior in pipeline composition
|
||||
- ✅ Identical behavior for multiple error scenarios
|
||||
- ✅ Both provide access to original input
|
||||
|
||||
Run the tests:
|
||||
```bash
|
||||
go test -v -run "TestChainLeft|TestOrElse" ./optics/codec/decode
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
**`OrElse` is exactly the same as `ChainLeft`** in the decode package - they are aliases with identical implementations and behavior. Both:
|
||||
|
||||
1. **Delegate to validation.ChainLeft** for error handling logic
|
||||
2. **Aggregate errors** when transformations fail
|
||||
3. **Preserve successes** unchanged
|
||||
4. **Enable recovery** from decode failures
|
||||
5. **Provide access** to the original input
|
||||
|
||||
The choice between them is purely about **code readability and semantic intent**:
|
||||
- Use **`OrElse`** when emphasizing fallback/alternative decoding
|
||||
- Use **`ChainLeft`** when emphasizing error transformation
|
||||
|
||||
Both maintain the critical property of **error aggregation**, ensuring all validation failures are preserved and reported together.
|
||||
335
v2/optics/codec/decode/bind.go
Normal file
335
v2/optics/codec/decode/bind.go
Normal file
@@ -0,0 +1,335 @@
|
||||
// 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 decode
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
A "github.com/IBM/fp-go/v2/internal/apply"
|
||||
C "github.com/IBM/fp-go/v2/internal/chain"
|
||||
F "github.com/IBM/fp-go/v2/internal/functor"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
)
|
||||
|
||||
// Do creates an empty context of type S to be used with the Bind operation.
|
||||
// This is the starting point for building up a context using do-notation style.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Result struct {
|
||||
// x int
|
||||
// y string
|
||||
// }
|
||||
// result := Do(Result{})
|
||||
func Do[I, S any](
|
||||
empty S,
|
||||
) Decode[I, S] {
|
||||
return Of[I](empty)
|
||||
}
|
||||
|
||||
// Bind attaches the result of a computation to a context S1 to produce a context S2.
|
||||
// This is used in do-notation style to sequentially build up a context.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{}),
|
||||
// Bind(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, func(s State) Decode[string, int] {
|
||||
// return Of[string](42)
|
||||
// }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 42})
|
||||
func Bind[I, S1, S2, A any](
|
||||
setter func(A) func(S1) S2,
|
||||
f Kleisli[I, S1, A],
|
||||
) Operator[I, S1, S2] {
|
||||
return C.Bind(
|
||||
Chain[I, S1, S2],
|
||||
Map[I, A, S2],
|
||||
setter,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// 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, not wrapped in Decode.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; computed int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{x: 5}),
|
||||
// Let[string](func(c int) func(State) State {
|
||||
// return func(s State) State { s.computed = c; return s }
|
||||
// }, func(s State) int { return s.x * 2 }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 5, computed: 10})
|
||||
func Let[I, S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
f func(S1) B,
|
||||
) Operator[I, S1, S2] {
|
||||
return F.Let(
|
||||
Map[I, S1, S2],
|
||||
key,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// LetTo attaches a constant value to a context S1 to produce a context S2.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; name string }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{x: 5}),
|
||||
// LetTo(func(n string) func(State) State {
|
||||
// return func(s State) State { s.name = n; return s }
|
||||
// }, "example"),
|
||||
// )
|
||||
func LetTo[I, S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
b B,
|
||||
) Operator[I, S1, S2] {
|
||||
return F.LetTo(
|
||||
Map[I, S1, S2],
|
||||
key,
|
||||
b,
|
||||
)
|
||||
}
|
||||
|
||||
// BindTo initializes a new state S1 from a value T.
|
||||
// This is typically used as the first operation after creating a Decode value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { value int }
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](42),
|
||||
// BindTo[string](func(x int) State { return State{value: x} }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{value: 42})
|
||||
func BindTo[I, S1, T any](
|
||||
setter func(T) S1,
|
||||
) Operator[I, T, S1] {
|
||||
return C.BindTo(
|
||||
Map[I, T, S1],
|
||||
setter,
|
||||
)
|
||||
}
|
||||
|
||||
// ApS attaches a value to a context S1 to produce a context S2 by considering the context and the value concurrently.
|
||||
// This uses the applicative functor pattern, allowing parallel composition.
|
||||
//
|
||||
// IMPORTANT: Unlike Bind which fails fast, ApS aggregates ALL validation errors from both the context
|
||||
// and the value. If both validations fail, all errors are collected and returned together.
|
||||
// This is useful for validating multiple independent fields and reporting all errors at once.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{}),
|
||||
// ApS(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, Of[string](42)),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 42})
|
||||
//
|
||||
// Error aggregation example:
|
||||
//
|
||||
// // Both decoders fail - errors are aggregated
|
||||
// decoder1 := func(input string) Validation[State] {
|
||||
// return validation.Failures[State](/* errors */)
|
||||
// }
|
||||
// decoder2 := func(input string) Validation[int] {
|
||||
// return validation.Failures[int](/* errors */)
|
||||
// }
|
||||
// combined := ApS(setter, decoder2)(decoder1)
|
||||
// result := combined("input") // Contains BOTH sets of errors
|
||||
func ApS[I, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Decode[I, T],
|
||||
) Operator[I, S1, S2] {
|
||||
return A.ApS(
|
||||
Ap[S2, I, T],
|
||||
Map[I, S1, func(T) S2],
|
||||
setter,
|
||||
fa,
|
||||
)
|
||||
}
|
||||
|
||||
// ApSL attaches a value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines ApS with a lens, allowing you to use
|
||||
// optics to update nested structures in a more composable way.
|
||||
//
|
||||
// IMPORTANT: Like ApS, this function aggregates ALL validation errors. If both the context
|
||||
// and the value fail validation, all errors are collected and returned together.
|
||||
// This enables comprehensive error reporting for complex nested structures.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// This eliminates the need to manually write setter functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Address struct {
|
||||
// Street string
|
||||
// City string
|
||||
// }
|
||||
//
|
||||
// type Person struct {
|
||||
// Name string
|
||||
// Address Address
|
||||
// }
|
||||
//
|
||||
// // Create a lens for the Address field
|
||||
// addressLens := lens.MakeLens(
|
||||
// func(p Person) Address { return p.Address },
|
||||
// func(p Person, a Address) Person { p.Address = a; return p },
|
||||
// )
|
||||
//
|
||||
// // Use ApSL to update the address
|
||||
// decoder := F.Pipe2(
|
||||
// Of[string](Person{Name: "Alice"}),
|
||||
// ApSL(
|
||||
// addressLens,
|
||||
// Of[string](Address{Street: "Main St", City: "NYC"}),
|
||||
// ),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Person{...})
|
||||
func ApSL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
fa Decode[I, T],
|
||||
) Operator[I, S, S] {
|
||||
return ApS(lens.Set, fa)
|
||||
}
|
||||
|
||||
// BindL attaches the result of a computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Bind with a lens, allowing you to use
|
||||
// optics to update nested structures based on their current values.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The computation function f receives the current value of the focused field and returns
|
||||
// a Validation that produces the new value.
|
||||
//
|
||||
// Unlike ApSL, BindL uses monadic sequencing, meaning the computation f can depend on
|
||||
// the current value of the focused field.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Increment the counter, but fail if it would exceed 100
|
||||
// increment := func(v int) Decode[string, int] {
|
||||
// return func(input string) Validation[int] {
|
||||
// if v >= 100 {
|
||||
// return validation.Failures[int](/* errors */)
|
||||
// }
|
||||
// return validation.Success(v + 1)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Counter{Value: 42}),
|
||||
// BindL(valueLens, increment),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Counter{Value: 43})
|
||||
func BindL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Kleisli[I, T, T],
|
||||
) Operator[I, S, S] {
|
||||
return Bind(lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetL attaches the result of a pure computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Let with a lens, allowing you to use
|
||||
// optics to update nested structures with pure transformations.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The transformation function f receives the current value of the focused field and returns
|
||||
// the new value directly (not wrapped in Validation).
|
||||
//
|
||||
// This is useful for pure transformations that cannot fail, such as mathematical operations,
|
||||
// string manipulations, or other deterministic updates.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Double the counter value
|
||||
// double := func(v int) int { return v * 2 }
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Counter{Value: 21}),
|
||||
// LetL(valueLens, double),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Counter{Value: 42})
|
||||
func LetL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Endomorphism[T],
|
||||
) Operator[I, S, S] {
|
||||
return Let[I](lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetToL attaches a constant value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines LetTo with a lens, allowing you to use
|
||||
// optics to set nested fields to specific values.
|
||||
//
|
||||
// The lens parameter provides the setter for a field within the structure S.
|
||||
// Unlike LetL which transforms the current value, LetToL simply replaces it with
|
||||
// the provided constant value b.
|
||||
//
|
||||
// This is useful for resetting fields, initializing values, or setting fields to
|
||||
// predetermined constants.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// Debug bool
|
||||
// Timeout int
|
||||
// }
|
||||
//
|
||||
// debugLens := lens.MakeLens(
|
||||
// func(c Config) bool { return c.Debug },
|
||||
// func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
// )
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Config{Debug: true, Timeout: 30}),
|
||||
// LetToL(debugLens, false),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Config{Debug: false, Timeout: 30})
|
||||
func LetToL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
b T,
|
||||
) Operator[I, S, S] {
|
||||
return LetTo[I](lens.Set, b)
|
||||
}
|
||||
665
v2/optics/codec/decode/bind_test.go
Normal file
665
v2/optics/codec/decode/bind_test.go
Normal file
@@ -0,0 +1,665 @@
|
||||
package decode
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDo(t *testing.T) {
|
||||
t.Run("creates decoder with empty state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
decoder := Do[string](State{})
|
||||
result := decoder("input")
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{}, value)
|
||||
})
|
||||
|
||||
t.Run("creates decoder with initialized state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
initial := State{x: 42, y: "hello"}
|
||||
decoder := Do[string](initial)
|
||||
result := decoder("input")
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, initial, value)
|
||||
})
|
||||
|
||||
t.Run("works with different input types", func(t *testing.T) {
|
||||
intDecoder := Do[int](0)
|
||||
assert.True(t, either.IsRight(intDecoder(42)))
|
||||
|
||||
strDecoder := Do[string]("")
|
||||
assert.True(t, either.IsRight(strDecoder("test")))
|
||||
|
||||
type Custom struct{ Value int }
|
||||
customDecoder := Do[[]byte](Custom{Value: 100})
|
||||
assert.True(t, either.IsRight(customDecoder([]byte("data"))))
|
||||
})
|
||||
}
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("binds successful decode to state", func(t *testing.T) {
|
||||
decoder := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return Of[string](42)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return Of[string](10)
|
||||
}),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 42, y: 10}, value)
|
||||
})
|
||||
|
||||
t.Run("propagates failure", func(t *testing.T) {
|
||||
decoder := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return Of[string](42)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return func(input string) validation.Validation[int] {
|
||||
return validation.Failures[int](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "y failed"},
|
||||
})
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(State) validation.Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "y failed", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("can access previous state values", func(t *testing.T) {
|
||||
decoder := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return Of[string](10)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
// y depends on x
|
||||
return Of[string](s.x * 2)
|
||||
}),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 10, y: 20}, value)
|
||||
})
|
||||
|
||||
t.Run("can access input in decoder", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Decode[string, int] {
|
||||
return func(input string) validation.Validation[int] {
|
||||
// Use input to determine value
|
||||
if input == "large" {
|
||||
return validation.Success(100)
|
||||
}
|
||||
return validation.Success(10)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result1 := decoder("large")
|
||||
value1 := either.MonadFold(result1,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, 100, value1.x)
|
||||
|
||||
result2 := decoder("small")
|
||||
value2 := either.MonadFold(result2,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, 10, value2.x)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLet(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
computed int
|
||||
}
|
||||
|
||||
t.Run("attaches pure computation result to state", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Do[string](State{x: 5}),
|
||||
Let[string](func(c int) func(State) State {
|
||||
return func(s State) State { s.computed = c; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 5, computed: 10}, value)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Let operations", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
z int
|
||||
}
|
||||
decoder := F.Pipe3(
|
||||
Do[string](State{x: 5}),
|
||||
Let[string](func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
Let[string](func(z int) func(State) State {
|
||||
return func(s State) State { s.z = z; return s }
|
||||
}, func(s State) int { return s.y + 10 }),
|
||||
Let[string](func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) int { return s.z * 3 }),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 60, y: 10, z: 20}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetTo(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
name string
|
||||
}
|
||||
|
||||
t.Run("attaches constant value to state", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Do[string](State{x: 5}),
|
||||
LetTo[string](func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "example"),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 5, name: "example"}, value)
|
||||
})
|
||||
|
||||
t.Run("sets multiple constant values", func(t *testing.T) {
|
||||
type State struct {
|
||||
name string
|
||||
version int
|
||||
active bool
|
||||
}
|
||||
decoder := F.Pipe3(
|
||||
Do[string](State{}),
|
||||
LetTo[string](func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "app"),
|
||||
LetTo[string](func(v int) func(State) State {
|
||||
return func(s State) State { s.version = v; return s }
|
||||
}, 2),
|
||||
LetTo[string](func(a bool) func(State) State {
|
||||
return func(s State) State { s.active = a; return s }
|
||||
}, true),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{name: "app", version: 2, active: true}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindTo(t *testing.T) {
|
||||
type State struct {
|
||||
value int
|
||||
}
|
||||
|
||||
t.Run("initializes state from value", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Of[string](42),
|
||||
BindTo[string](func(x int) State { return State{value: x} }),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{value: 42}, value)
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
type StringState struct {
|
||||
text string
|
||||
}
|
||||
decoder := F.Pipe1(
|
||||
Of[string]("hello"),
|
||||
BindTo[string](func(s string) StringState { return StringState{text: s} }),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) StringState { return StringState{} },
|
||||
F.Identity[StringState],
|
||||
)
|
||||
assert.Equal(t, StringState{text: "hello"}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApS(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("attaches value using applicative pattern", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Do[string](State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Of[string](42)),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 42}, value)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors from both decoders", func(t *testing.T) {
|
||||
stateDecoder := func(input string) validation.Validation[State] {
|
||||
return validation.Failures[State](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "state error"},
|
||||
})
|
||||
}
|
||||
valueDecoder := func(input string) validation.Validation[int] {
|
||||
return validation.Failures[int](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "value error"},
|
||||
})
|
||||
}
|
||||
|
||||
decoder := ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, valueDecoder)(stateDecoder)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(State) validation.Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
messages := []string{errors[0].Messsage, errors[1].Messsage}
|
||||
assert.Contains(t, messages, "state error")
|
||||
assert.Contains(t, messages, "value error")
|
||||
})
|
||||
|
||||
t.Run("combines multiple ApS operations", func(t *testing.T) {
|
||||
decoder := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Of[string](10)),
|
||||
ApS(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, Of[string](20)),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 10, y: 20}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApSL(t *testing.T) {
|
||||
type Address struct {
|
||||
Street string
|
||||
City string
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Address Address
|
||||
}
|
||||
|
||||
t.Run("updates nested structure using lens", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
decoder := F.Pipe1(
|
||||
Of[string](Person{Name: "Alice"}),
|
||||
ApSL(
|
||||
addressLens,
|
||||
Of[string](Address{Street: "Main St", City: "NYC"}),
|
||||
),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Person { return Person{} },
|
||||
F.Identity[Person],
|
||||
)
|
||||
assert.Equal(t, "Alice", value.Name)
|
||||
assert.Equal(t, "Main St", value.Address.Street)
|
||||
assert.Equal(t, "NYC", value.Address.City)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
personDecoder := func(input string) validation.Validation[Person] {
|
||||
return validation.Failures[Person](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "person error"},
|
||||
})
|
||||
}
|
||||
addressDecoder := func(input string) validation.Validation[Address] {
|
||||
return validation.Failures[Address](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "address error"},
|
||||
})
|
||||
}
|
||||
|
||||
decoder := ApSL(addressLens, addressDecoder)(personDecoder)
|
||||
result := decoder("input")
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(Person) validation.Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("updates field based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Decode[string, int] {
|
||||
return Of[string](v + 1)
|
||||
}
|
||||
|
||||
decoder := F.Pipe1(
|
||||
Of[string](Counter{Value: 42}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Counter { return Counter{} },
|
||||
F.Identity[Counter],
|
||||
)
|
||||
assert.Equal(t, Counter{Value: 43}, value)
|
||||
})
|
||||
|
||||
t.Run("fails validation based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Decode[string, int] {
|
||||
return func(input string) validation.Validation[int] {
|
||||
if v >= 100 {
|
||||
return validation.Failures[int](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "exceeds limit"},
|
||||
})
|
||||
}
|
||||
return validation.Success(v + 1)
|
||||
}
|
||||
}
|
||||
|
||||
decoder := F.Pipe1(
|
||||
Of[string](Counter{Value: 100}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(Counter) validation.Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "exceeds limit", errors[0].Messsage)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("transforms field with pure function", func(t *testing.T) {
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
decoder := F.Pipe1(
|
||||
Of[string](Counter{Value: 21}),
|
||||
LetL[string](valueLens, double),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Counter { return Counter{} },
|
||||
F.Identity[Counter],
|
||||
)
|
||||
assert.Equal(t, Counter{Value: 42}, value)
|
||||
})
|
||||
|
||||
t.Run("chains multiple transformations", func(t *testing.T) {
|
||||
add10 := func(v int) int { return v + 10 }
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
decoder := F.Pipe2(
|
||||
Of[string](Counter{Value: 5}),
|
||||
LetL[string](valueLens, add10),
|
||||
LetL[string](valueLens, double),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Counter { return Counter{} },
|
||||
F.Identity[Counter],
|
||||
)
|
||||
assert.Equal(t, Counter{Value: 30}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetToL(t *testing.T) {
|
||||
type Config struct {
|
||||
Debug bool
|
||||
Timeout int
|
||||
}
|
||||
|
||||
debugLens := L.MakeLens(
|
||||
func(c Config) bool { return c.Debug },
|
||||
func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
)
|
||||
|
||||
t.Run("sets field to constant value", func(t *testing.T) {
|
||||
decoder := F.Pipe1(
|
||||
Of[string](Config{Debug: true, Timeout: 30}),
|
||||
LetToL[string](debugLens, false),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Config { return Config{} },
|
||||
F.Identity[Config],
|
||||
)
|
||||
assert.Equal(t, Config{Debug: false, Timeout: 30}, value)
|
||||
})
|
||||
|
||||
t.Run("sets multiple fields", func(t *testing.T) {
|
||||
timeoutLens := L.MakeLens(
|
||||
func(c Config) int { return c.Timeout },
|
||||
func(c Config, t int) Config { c.Timeout = t; return c },
|
||||
)
|
||||
|
||||
decoder := F.Pipe2(
|
||||
Of[string](Config{Debug: true, Timeout: 30}),
|
||||
LetToL[string](debugLens, false),
|
||||
LetToL[string](timeoutLens, 60),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) Config { return Config{} },
|
||||
F.Identity[Config],
|
||||
)
|
||||
assert.Equal(t, Config{Debug: false, Timeout: 60}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindOperationsComposition(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
t.Run("combines Do, Bind, Let, and LetTo", func(t *testing.T) {
|
||||
decoder := F.Pipe4(
|
||||
Do[string](User{}),
|
||||
LetTo[string](func(n string) func(User) User {
|
||||
return func(u User) User { u.Name = n; return u }
|
||||
}, "Alice"),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Decode[string, int] {
|
||||
// Age validation
|
||||
if len(u.Name) > 0 {
|
||||
return Of[string](25)
|
||||
}
|
||||
return func(input string) validation.Validation[int] {
|
||||
return validation.Failures[int](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "name required"},
|
||||
})
|
||||
}
|
||||
}),
|
||||
Let[string](func(e string) func(User) User {
|
||||
return func(u User) User { u.Email = e; return u }
|
||||
}, func(u User) string {
|
||||
// Derive email from name
|
||||
return u.Name + "@example.com"
|
||||
}),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Decode[string, int] {
|
||||
// Validate age is positive
|
||||
if u.Age > 0 {
|
||||
return Of[string](u.Age)
|
||||
}
|
||||
return func(input string) validation.Validation[int] {
|
||||
return validation.Failures[int](validation.Errors{
|
||||
&validation.ValidationError{Messsage: "age must be positive"},
|
||||
})
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := decoder("input")
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) User { return User{} },
|
||||
F.Identity[User],
|
||||
)
|
||||
assert.Equal(t, "Alice", value.Name)
|
||||
assert.Equal(t, 25, value.Age)
|
||||
assert.Equal(t, "Alice@example.com", value.Email)
|
||||
})
|
||||
}
|
||||
@@ -50,6 +50,17 @@ func Chain[I, A, B any](f Kleisli[I, A, B]) Operator[I, A, B] {
|
||||
)
|
||||
}
|
||||
|
||||
func ChainLeft[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return readert.Chain[Decode[I, A]](
|
||||
validation.ChainLeft,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
func OrElse[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return ChainLeft(f)
|
||||
}
|
||||
|
||||
// MonadMap transforms the decoded value using the provided function.
|
||||
// This is the functor map operation that applies a transformation to successful decode results.
|
||||
//
|
||||
|
||||
@@ -382,3 +382,400 @@ func TestFunctorLaws(t *testing.T) {
|
||||
assert.Equal(t, right(input), left(input))
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainLeft tests the ChainLeft function
|
||||
func TestChainLeft(t *testing.T) {
|
||||
t.Run("transforms failures while preserving successes", func(t *testing.T) {
|
||||
// Create a failing decoder
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "decode failed"},
|
||||
})
|
||||
}
|
||||
|
||||
// Handler that recovers from specific errors
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "decode failed" {
|
||||
return Of[string](0) // recover with default
|
||||
}
|
||||
}
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
})
|
||||
|
||||
decoder := handler(failingDecoder)
|
||||
res := decoder("input")
|
||||
|
||||
assert.Equal(t, validation.Of(0), res, "Should recover from failure")
|
||||
})
|
||||
|
||||
t.Run("preserves success values unchanged", func(t *testing.T) {
|
||||
successDecoder := Of[string](42)
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "should not be called"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := handler(successDecoder)
|
||||
res := decoder("input")
|
||||
|
||||
assert.Equal(t, validation.Of(42), res, "Success should pass through unchanged")
|
||||
})
|
||||
|
||||
t.Run("aggregates errors when transformation also fails", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[string] {
|
||||
return either.Left[string](validation.Errors{
|
||||
{Value: input, Messsage: "original error"},
|
||||
})
|
||||
}
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Decode[string, string] {
|
||||
return func(input string) Validation[string] {
|
||||
return either.Left[string](validation.Errors{
|
||||
{Messsage: "additional error"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := handler(failingDecoder)
|
||||
res := decoder("input")
|
||||
|
||||
assert.True(t, either.IsLeft(res))
|
||||
errors := either.MonadFold(res,
|
||||
func(e Errors) Errors { return e },
|
||||
func(string) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2, "Should aggregate both errors")
|
||||
|
||||
messages := make([]string, len(errors))
|
||||
for i, err := range errors {
|
||||
messages[i] = err.Messsage
|
||||
}
|
||||
assert.Contains(t, messages, "original error")
|
||||
assert.Contains(t, messages, "additional error")
|
||||
})
|
||||
|
||||
t.Run("adds context to errors", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "invalid format"},
|
||||
})
|
||||
}
|
||||
|
||||
addContext := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{
|
||||
Context: validation.Context{{Key: "user", Type: "User"}, {Key: "age", Type: "int"}},
|
||||
Messsage: "failed to decode user age",
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
decoder := addContext(failingDecoder)
|
||||
res := decoder("abc")
|
||||
|
||||
assert.True(t, either.IsLeft(res))
|
||||
errors := either.MonadFold(res,
|
||||
func(e Errors) Errors { return e },
|
||||
func(int) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2, "Should have both original and context errors")
|
||||
})
|
||||
|
||||
t.Run("can be composed in pipeline", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "error1"},
|
||||
})
|
||||
}
|
||||
|
||||
handler1 := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "error2"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
handler2 := ChainLeft(func(errs Errors) Decode[string, int] {
|
||||
// Check if we can recover
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "error1" {
|
||||
return Of[string](100) // recover
|
||||
}
|
||||
}
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
})
|
||||
|
||||
// Compose handlers
|
||||
decoder := handler2(handler1(failingDecoder))
|
||||
res := decoder("input")
|
||||
|
||||
// Should recover because error1 is present
|
||||
assert.Equal(t, validation.Of(100), res)
|
||||
})
|
||||
|
||||
t.Run("works with different input types", func(t *testing.T) {
|
||||
type Config struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
failingDecoder := func(cfg Config) Validation[string] {
|
||||
return either.Left[string](validation.Errors{
|
||||
{Value: cfg.Port, Messsage: "invalid port"},
|
||||
})
|
||||
}
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Decode[Config, string] {
|
||||
return Of[Config]("default-value")
|
||||
})
|
||||
|
||||
decoder := handler(failingDecoder)
|
||||
res := decoder(Config{Port: 9999})
|
||||
|
||||
assert.Equal(t, validation.Of("default-value"), res)
|
||||
})
|
||||
}
|
||||
|
||||
// TestOrElse tests the OrElse function
|
||||
func TestOrElse(t *testing.T) {
|
||||
t.Run("OrElse is equivalent to ChainLeft - Success case", func(t *testing.T) {
|
||||
successDecoder := Of[string](42)
|
||||
|
||||
handler := func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "should not be called"},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test with OrElse
|
||||
resultOrElse := OrElse(handler)(successDecoder)("input")
|
||||
// Test with ChainLeft
|
||||
resultChainLeft := ChainLeft(handler)(successDecoder)("input")
|
||||
|
||||
assert.Equal(t, resultChainLeft, resultOrElse, "OrElse and ChainLeft should produce identical results for Success")
|
||||
assert.Equal(t, validation.Of(42), resultOrElse)
|
||||
})
|
||||
|
||||
t.Run("OrElse is equivalent to ChainLeft - Failure recovery", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "not found"},
|
||||
})
|
||||
}
|
||||
|
||||
handler := func(errs Errors) Decode[string, int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "not found" {
|
||||
return Of[string](0) // recover with default
|
||||
}
|
||||
}
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
}
|
||||
|
||||
// Test with OrElse
|
||||
resultOrElse := OrElse(handler)(failingDecoder)("input")
|
||||
// Test with ChainLeft
|
||||
resultChainLeft := ChainLeft(handler)(failingDecoder)("input")
|
||||
|
||||
assert.Equal(t, resultChainLeft, resultOrElse, "OrElse and ChainLeft should produce identical results for recovery")
|
||||
assert.Equal(t, validation.Of(0), resultOrElse)
|
||||
})
|
||||
|
||||
t.Run("OrElse is equivalent to ChainLeft - Error aggregation", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[string] {
|
||||
return either.Left[string](validation.Errors{
|
||||
{Value: input, Messsage: "original error"},
|
||||
})
|
||||
}
|
||||
|
||||
handler := func(errs Errors) Decode[string, string] {
|
||||
return func(input string) Validation[string] {
|
||||
return either.Left[string](validation.Errors{
|
||||
{Messsage: "additional error"},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test with OrElse
|
||||
resultOrElse := OrElse(handler)(failingDecoder)("input")
|
||||
// Test with ChainLeft
|
||||
resultChainLeft := ChainLeft(handler)(failingDecoder)("input")
|
||||
|
||||
assert.Equal(t, resultChainLeft, resultOrElse, "OrElse and ChainLeft should produce identical results for error aggregation")
|
||||
|
||||
// Verify both aggregate errors
|
||||
assert.True(t, either.IsLeft(resultOrElse))
|
||||
errors := either.MonadFold(resultOrElse,
|
||||
func(e Errors) Errors { return e },
|
||||
func(string) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2, "Should aggregate both errors")
|
||||
messages := make([]string, len(errors))
|
||||
for i, err := range errors {
|
||||
messages[i] = err.Messsage
|
||||
}
|
||||
assert.Contains(t, messages, "original error")
|
||||
assert.Contains(t, messages, "additional error")
|
||||
})
|
||||
|
||||
t.Run("OrElse semantic meaning - fallback decoder", func(t *testing.T) {
|
||||
// OrElse provides a semantic name for fallback/alternative decoding
|
||||
// It reads naturally: "try this decoder, or else try this alternative"
|
||||
|
||||
primaryDecoder := func(input string) Validation[int] {
|
||||
if input == "valid" {
|
||||
return validation.Of(42)
|
||||
}
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "primary decode failed"},
|
||||
})
|
||||
}
|
||||
|
||||
// Use OrElse to provide a fallback: if decoding fails, use default value
|
||||
withDefault := OrElse(func(errs Errors) Decode[string, int] {
|
||||
return Of[string](0) // default to 0 if decoding fails
|
||||
})
|
||||
|
||||
decoder := withDefault(primaryDecoder)
|
||||
|
||||
// Test success case
|
||||
resSuccess := decoder("valid")
|
||||
assert.Equal(t, validation.Of(42), resSuccess, "Should use primary decoder on success")
|
||||
|
||||
// Test fallback case
|
||||
resFallback := decoder("invalid")
|
||||
assert.Equal(t, validation.Of(0), resFallback, "OrElse provides fallback value")
|
||||
})
|
||||
|
||||
t.Run("OrElse in pipeline composition", func(t *testing.T) {
|
||||
failingDecoder := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "database error"},
|
||||
})
|
||||
}
|
||||
|
||||
addContext := OrElse(func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "context added"},
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
recoverFromNotFound := OrElse(func(errs Errors) Decode[string, int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "not found" {
|
||||
return Of[string](0)
|
||||
}
|
||||
}
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](errs)
|
||||
}
|
||||
})
|
||||
|
||||
// Test error aggregation in pipeline
|
||||
decoder1 := recoverFromNotFound(addContext(failingDecoder))
|
||||
res1 := decoder1("input")
|
||||
|
||||
assert.True(t, either.IsLeft(res1))
|
||||
errors := either.MonadFold(res1,
|
||||
func(e Errors) Errors { return e },
|
||||
func(int) Errors { return nil },
|
||||
)
|
||||
// Errors accumulate through the pipeline
|
||||
assert.Greater(t, len(errors), 1, "Should aggregate errors from pipeline")
|
||||
|
||||
// Test recovery in pipeline
|
||||
failingDecoder2 := func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Value: input, Messsage: "not found"},
|
||||
})
|
||||
}
|
||||
|
||||
decoder2 := recoverFromNotFound(addContext(failingDecoder2))
|
||||
res2 := decoder2("input")
|
||||
|
||||
assert.Equal(t, validation.Of(0), res2, "Should recover from 'not found' error")
|
||||
})
|
||||
|
||||
t.Run("OrElse vs ChainLeft - identical behavior verification", func(t *testing.T) {
|
||||
// Create various test scenarios
|
||||
scenarios := []struct {
|
||||
name string
|
||||
decoder Decode[string, int]
|
||||
handler func(Errors) Decode[string, int]
|
||||
}{
|
||||
{
|
||||
name: "Success value",
|
||||
decoder: Of[string](42),
|
||||
handler: func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{{Messsage: "error"}})
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Failure with recovery",
|
||||
decoder: func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{{Messsage: "error"}})
|
||||
},
|
||||
handler: func(errs Errors) Decode[string, int] {
|
||||
return Of[string](0)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Failure with error transformation",
|
||||
decoder: func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{{Messsage: "error1"}})
|
||||
},
|
||||
handler: func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{{Messsage: "error2"}})
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Multiple errors aggregation",
|
||||
decoder: func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "error1"},
|
||||
{Messsage: "error2"},
|
||||
})
|
||||
},
|
||||
handler: func(errs Errors) Decode[string, int] {
|
||||
return func(input string) Validation[int] {
|
||||
return either.Left[int](validation.Errors{
|
||||
{Messsage: "error3"},
|
||||
{Messsage: "error4"},
|
||||
})
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
resultOrElse := OrElse(scenario.handler)(scenario.decoder)("test-input")
|
||||
resultChainLeft := ChainLeft(scenario.handler)(scenario.decoder)("test-input")
|
||||
|
||||
assert.Equal(t, resultChainLeft, resultOrElse,
|
||||
"OrElse and ChainLeft must produce identical results for: %s", scenario.name)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,30 +1,222 @@
|
||||
// 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 decode
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/endomorphism"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
)
|
||||
|
||||
type (
|
||||
Errors = validation.Errors
|
||||
|
||||
// Validation represents the result of a validation operation that may contain
|
||||
// validation errors or a successfully validated value of type A.
|
||||
// This is an alias for validation.Validation[A], which is Either[Errors, A].
|
||||
//
|
||||
// In the decode context:
|
||||
// - Left(Errors): Decoding failed with one or more validation errors
|
||||
// - Right(A): Successfully decoded value of type A
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Success case
|
||||
// valid := validation.Success(42) // Right(42)
|
||||
//
|
||||
// // Failure case
|
||||
// invalid := validation.Failures[int](validation.Errors{
|
||||
// &validation.ValidationError{Messsage: "invalid format"},
|
||||
// }) // Left([...])
|
||||
Validation[A any] = validation.Validation[A]
|
||||
|
||||
// Reader represents a computation that depends on an environment R and produces a value A.
|
||||
// This is an alias for reader.Reader[R, A], which is func(R) A.
|
||||
//
|
||||
// In the decode context, Reader is used to access the input data being decoded.
|
||||
// The environment R is typically the raw input (e.g., JSON, string, bytes) that
|
||||
// needs to be decoded into a structured type A.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // A reader that extracts a field from a map
|
||||
// getField := func(data map[string]any) string {
|
||||
// return data["name"].(string)
|
||||
// } // Reader[map[string]any, string]
|
||||
Reader[R, A any] = reader.Reader[R, A]
|
||||
|
||||
// Decode is a function that decodes input I to type A with validation.
|
||||
// It returns a Validation result directly.
|
||||
// It combines the Reader pattern (for accessing input) with Validation (for error handling).
|
||||
//
|
||||
// Type: func(I) Validation[A]
|
||||
//
|
||||
// A Decode function:
|
||||
// 1. Takes raw input of type I (e.g., JSON, string, bytes)
|
||||
// 2. Attempts to decode/parse it into type A
|
||||
// 3. Returns a Validation[A] with either:
|
||||
// - Success(A): Successfully decoded value
|
||||
// - Failures(Errors): Validation errors describing what went wrong
|
||||
//
|
||||
// This type is the foundation of the decode package, enabling composable,
|
||||
// type-safe decoding with comprehensive error reporting.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Decode a string to an integer
|
||||
// decodeInt := func(input string) Validation[int] {
|
||||
// n, err := strconv.Atoi(input)
|
||||
// if err != nil {
|
||||
// return validation.Failures[int](validation.Errors{
|
||||
// &validation.ValidationError{
|
||||
// Value: input,
|
||||
// Messsage: "not a valid integer",
|
||||
// Cause: err,
|
||||
// },
|
||||
// })
|
||||
// }
|
||||
// return validation.Success(n)
|
||||
// } // Decode[string, int]
|
||||
//
|
||||
// result := decodeInt("42") // Success(42)
|
||||
// result := decodeInt("abc") // Failures([...])
|
||||
Decode[I, A any] = Reader[I, Validation[A]]
|
||||
|
||||
// Kleisli represents a function from A to a decoded B given input type I.
|
||||
// It's a Reader that takes an input A and produces a Decode[I, B] function.
|
||||
// This enables composition of decoding operations in a functional style.
|
||||
//
|
||||
// Type: func(A) Decode[I, B]
|
||||
// which expands to: func(A) func(I) Validation[B]
|
||||
//
|
||||
// Kleisli arrows are the fundamental building blocks for composing decoders.
|
||||
// They allow you to chain decoding operations where each step can:
|
||||
// 1. Depend on the result of the previous step (the A parameter)
|
||||
// 2. Access the original input (the I parameter via Decode)
|
||||
// 3. Fail with validation errors (via Validation[B])
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Conditional decoding based on previously decoded values
|
||||
// - Multi-stage decoding pipelines
|
||||
// - Dependent field validation
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Decode a user, then decode their age based on their type
|
||||
// decodeAge := func(userType string) Decode[map[string]any, int] {
|
||||
// return func(data map[string]any) Validation[int] {
|
||||
// if userType == "admin" {
|
||||
// // Admins must be 18+
|
||||
// age := data["age"].(int)
|
||||
// if age < 18 {
|
||||
// return validation.Failures[int](/* error */)
|
||||
// }
|
||||
// return validation.Success(age)
|
||||
// }
|
||||
// // Regular users can be any age
|
||||
// return validation.Success(data["age"].(int))
|
||||
// }
|
||||
// } // Kleisli[map[string]any, string, int]
|
||||
//
|
||||
// // Use with Chain to compose decoders
|
||||
// decoder := F.Pipe2(
|
||||
// decodeUserType, // Decode[map[string]any, string]
|
||||
// Chain(decodeAge), // Chains with Kleisli
|
||||
// Map(func(age int) User { // Transform to final type
|
||||
// return User{Age: age}
|
||||
// }),
|
||||
// )
|
||||
Kleisli[I, A, B any] = Reader[A, Decode[I, B]]
|
||||
|
||||
// Operator represents a decoding transformation that takes a decoded A and produces a decoded B.
|
||||
// It's a specialized Kleisli arrow for composing decode operations where the input is already decoded.
|
||||
// This allows chaining multiple decode transformations together.
|
||||
//
|
||||
// Type: func(Decode[I, A]) Decode[I, B]
|
||||
//
|
||||
// Operators are higher-order functions that transform one decoder into another.
|
||||
// They are the result of partially applying functions like Map, Chain, and Ap,
|
||||
// making them ideal for use in composition pipelines with F.Pipe.
|
||||
//
|
||||
// Key characteristics:
|
||||
// - Takes a Decode[I, A] as input
|
||||
// - Returns a Decode[I, B] as output
|
||||
// - Preserves the input type I (the raw data being decoded)
|
||||
// - Transforms the output type from A to B
|
||||
//
|
||||
// Common operators:
|
||||
// - Map(f): Transforms successful decode results
|
||||
// - Chain(f): Sequences dependent decode operations
|
||||
// - Ap(fa): Applies function decoders to value decoders
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Create reusable operators
|
||||
// toString := Map(func(n int) string {
|
||||
// return strconv.Itoa(n)
|
||||
// }) // Operator[string, int, string]
|
||||
//
|
||||
// validatePositive := Chain(func(n int) Decode[string, int] {
|
||||
// return func(input string) Validation[int] {
|
||||
// if n <= 0 {
|
||||
// return validation.Failures[int](/* error */)
|
||||
// }
|
||||
// return validation.Success(n)
|
||||
// }
|
||||
// }) // Operator[string, int, int]
|
||||
//
|
||||
// // Compose operators in a pipeline
|
||||
// decoder := F.Pipe2(
|
||||
// decodeInt, // Decode[string, int]
|
||||
// validatePositive, // Operator[string, int, int]
|
||||
// toString, // Operator[string, int, string]
|
||||
// ) // Decode[string, string]
|
||||
//
|
||||
// result := decoder("42") // Success("42")
|
||||
// result := decoder("-5") // Failures([...])
|
||||
Operator[I, A, B any] = Kleisli[I, Decode[I, A], B]
|
||||
|
||||
// Endomorphism represents a function from a type to itself: func(A) A.
|
||||
// This is an alias for endomorphism.Endomorphism[A].
|
||||
//
|
||||
// In the decode context, endomorphisms are used with LetL to transform
|
||||
// decoded values using pure functions that don't change the type.
|
||||
//
|
||||
// Endomorphisms are useful for:
|
||||
// - Normalizing data (e.g., trimming strings, rounding numbers)
|
||||
// - Applying business rules (e.g., clamping values to ranges)
|
||||
// - Data sanitization (e.g., removing special characters)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Normalize a string by trimming and lowercasing
|
||||
// normalize := func(s string) string {
|
||||
// return strings.ToLower(strings.TrimSpace(s))
|
||||
// } // Endomorphism[string]
|
||||
//
|
||||
// // Clamp an integer to a range
|
||||
// clamp := func(n int) int {
|
||||
// if n < 0 { return 0 }
|
||||
// if n > 100 { return 100 }
|
||||
// return n
|
||||
// } // Endomorphism[int]
|
||||
//
|
||||
// // Use with LetL to transform decoded values
|
||||
// decoder := F.Pipe1(
|
||||
// decodeString,
|
||||
// LetL(nameLens, normalize),
|
||||
// )
|
||||
Endomorphism[A any] = endomorphism.Endomorphism[A]
|
||||
)
|
||||
|
||||
277
v2/optics/codec/either.go
Normal file
277
v2/optics/codec/either.go
Normal file
@@ -0,0 +1,277 @@
|
||||
// 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 codec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/IBM/fp-go/v2/array"
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validate"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
)
|
||||
|
||||
// encodeEither creates an encoder for Either[A, B] values.
|
||||
//
|
||||
// This function produces an encoder that handles both Left and Right cases of an Either value.
|
||||
// It uses the provided codecs to encode the Left (A) and Right (B) values respectively.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The type of the Left value
|
||||
// - B: The type of the Right value
|
||||
// - O: The output type after encoding
|
||||
// - I: The input type for validation (not used in encoding)
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - leftItem: The codec for encoding Left values of type A
|
||||
// - rightItem: The codec for encoding Right values of type B
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// An Encode function that takes an Either[A, B] and returns O by encoding
|
||||
// either the Left or Right value using the appropriate codec.
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// stringCodec := String()
|
||||
// intCodec := Int()
|
||||
// encoder := encodeEither(stringCodec, intCodec)
|
||||
//
|
||||
// // Encode a Left value
|
||||
// leftResult := encoder(either.Left[int]("error"))
|
||||
// // leftResult contains the encoded string "error"
|
||||
//
|
||||
// // Encode a Right value
|
||||
// rightResult := encoder(either.Right[string](42))
|
||||
// // rightResult contains the encoded int 42
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - Uses either.Fold to pattern match on the Either value
|
||||
// - Left values are encoded using leftItem.Encode
|
||||
// - Right values are encoded using rightItem.Encode
|
||||
func encodeEither[A, B, O, I any](
|
||||
leftItem Type[A, O, I],
|
||||
rightItem Type[B, O, I],
|
||||
) Encode[either.Either[A, B], O] {
|
||||
return either.Fold(
|
||||
leftItem.Encode,
|
||||
rightItem.Encode,
|
||||
)
|
||||
}
|
||||
|
||||
// validateEither creates a validator for Either[A, B] values.
|
||||
//
|
||||
// This function produces a validator that attempts to validate the input as both
|
||||
// a Left (A) and Right (B) value. The validation strategy is:
|
||||
// 1. First, try to validate as a Right value (B)
|
||||
// 2. If Right validation succeeds, return Either.Right[A](B)
|
||||
// 3. If Right validation fails, try to validate as a Left value (A)
|
||||
// 4. If Left validation succeeds, return Either.Left[B](A)
|
||||
// 5. If both validations fail, concatenate all errors from both attempts
|
||||
//
|
||||
// This approach ensures that the validator tries both branches and provides
|
||||
// comprehensive error information when both fail.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The type of the Left value
|
||||
// - B: The type of the Right value
|
||||
// - O: The output type after encoding (not used in validation)
|
||||
// - I: The input type to validate
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - leftItem: The codec for validating Left values of type A
|
||||
// - rightItem: The codec for validating Right values of type B
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// A Validate function that takes an input I and returns a Decode function.
|
||||
// The Decode function takes a Context and returns a Validation[Either[A, B]].
|
||||
//
|
||||
// # Validation Logic
|
||||
//
|
||||
// The validator follows this decision tree:
|
||||
//
|
||||
// Input I
|
||||
// |
|
||||
// +--> Validate as Right (B)
|
||||
// |
|
||||
// +-- Success --> Return Either.Right[A](B)
|
||||
// |
|
||||
// +-- Failure --> Validate as Left (A)
|
||||
// |
|
||||
// +-- Success --> Return Either.Left[B](A)
|
||||
// |
|
||||
// +-- Failure --> Return all errors (Left + Right)
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// stringCodec := String()
|
||||
// intCodec := Int()
|
||||
// validator := validateEither(stringCodec, intCodec)
|
||||
//
|
||||
// // Validate a string (will succeed as Left)
|
||||
// result1 := validator("hello")(validation.Context{})
|
||||
// // result1 is Success(Either.Left[int]("hello"))
|
||||
//
|
||||
// // Validate an int (will succeed as Right)
|
||||
// result2 := validator(42)(validation.Context{})
|
||||
// // result2 is Success(Either.Right[string](42))
|
||||
//
|
||||
// // Validate something that's neither (will fail with both errors)
|
||||
// result3 := validator([]int{1, 2, 3})(validation.Context{})
|
||||
// // result3 is Failure with errors from both string and int validation
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - Prioritizes Right validation over Left validation
|
||||
// - Accumulates errors from both branches when both fail
|
||||
// - Uses the validation context to provide detailed error messages
|
||||
// - The validator is lazy: it only evaluates Left if Right fails
|
||||
func validateEither[A, B, O, I any](
|
||||
leftItem Type[A, O, I],
|
||||
rightItem Type[B, O, I],
|
||||
) Validate[I, either.Either[A, B]] {
|
||||
|
||||
return func(i I) Decode[Context, either.Either[A, B]] {
|
||||
valRight := rightItem.Validate(i)
|
||||
valLeft := leftItem.Validate(i)
|
||||
|
||||
return func(ctx Context) Validation[either.Either[A, B]] {
|
||||
|
||||
resRight := valRight(ctx)
|
||||
|
||||
return either.Fold(
|
||||
func(rightErrors validate.Errors) Validation[either.Either[A, B]] {
|
||||
resLeft := valLeft(ctx)
|
||||
return either.Fold(
|
||||
func(leftErrors validate.Errors) Validation[either.Either[A, B]] {
|
||||
return validation.Failures[either.Either[A, B]](array.Concat(leftErrors)(rightErrors))
|
||||
},
|
||||
F.Flow2(either.Left[B, A], validation.Of),
|
||||
)(resLeft)
|
||||
},
|
||||
F.Flow2(either.Right[A, B], validation.Of),
|
||||
)(resRight)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Either creates a codec for Either[A, B] values.
|
||||
//
|
||||
// This function constructs a complete codec that can encode, decode, and validate
|
||||
// Either values. An Either represents a value that can be one of two types: Left (A)
|
||||
// or Right (B). This is commonly used for error handling, where Left represents an
|
||||
// error and Right represents a success value.
|
||||
//
|
||||
// The codec handles both branches of the Either type using the provided codecs for
|
||||
// each branch. During validation, it attempts to validate the input as both types
|
||||
// and succeeds if either validation passes.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - A: The type of the Left value
|
||||
// - B: The type of the Right value
|
||||
// - O: The output type after encoding
|
||||
// - I: The input type for validation
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - leftItem: The codec for handling Left values of type A
|
||||
// - rightItem: The codec for handling Right values of type B
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// A Type[either.Either[A, B], O, I] that can encode, decode, and validate Either values.
|
||||
//
|
||||
// # Codec Behavior
|
||||
//
|
||||
// Encoding:
|
||||
// - Left values are encoded using leftItem.Encode
|
||||
// - Right values are encoded using rightItem.Encode
|
||||
//
|
||||
// Validation:
|
||||
// - First attempts to validate as Right (B)
|
||||
// - If Right fails, attempts to validate as Left (A)
|
||||
// - If both fail, returns all accumulated errors
|
||||
// - If either succeeds, returns the corresponding Either value
|
||||
//
|
||||
// Type Checking:
|
||||
// - Uses Is[either.Either[A, B]]() to verify the value is an Either
|
||||
//
|
||||
// Naming:
|
||||
// - The codec name is "Either[<leftName>, <rightName>]"
|
||||
// - Example: "Either[string, int]"
|
||||
//
|
||||
// # Example
|
||||
//
|
||||
// // Create a codec for Either[string, int]
|
||||
// stringCodec := String()
|
||||
// intCodec := Int()
|
||||
// eitherCodec := Either(stringCodec, intCodec)
|
||||
//
|
||||
// // Encode a Left value
|
||||
// leftEncoded := eitherCodec.Encode(either.Left[int]("error"))
|
||||
// // leftEncoded contains the encoded string
|
||||
//
|
||||
// // Encode a Right value
|
||||
// rightEncoded := eitherCodec.Encode(either.Right[string](42))
|
||||
// // rightEncoded contains the encoded int
|
||||
//
|
||||
// // Decode/validate an input
|
||||
// result := eitherCodec.Decode("hello")
|
||||
// // result is Success(Either.Left[int]("hello"))
|
||||
//
|
||||
// result2 := eitherCodec.Decode(42)
|
||||
// // result2 is Success(Either.Right[string](42))
|
||||
//
|
||||
// // Get the codec name
|
||||
// name := eitherCodec.Name()
|
||||
// // name is "Either[string, int]"
|
||||
//
|
||||
// # Use Cases
|
||||
//
|
||||
// - Error handling: Either[Error, Value]
|
||||
// - Alternative values: Either[DefaultValue, CustomValue]
|
||||
// - Union types: Either[TypeA, TypeB]
|
||||
// - Validation results: Either[ValidationError, ValidatedValue]
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - The codec prioritizes Right validation over Left validation
|
||||
// - Both branches must have compatible encoding output types (O)
|
||||
// - Both branches must have compatible validation input types (I)
|
||||
// - The codec name includes the names of both branch codecs
|
||||
// - This is a building block for more complex sum types
|
||||
func Either[A, B, O, I any](
|
||||
leftItem Type[A, O, I],
|
||||
rightItem Type[B, O, I],
|
||||
) Type[either.Either[A, B], O, I] {
|
||||
name := fmt.Sprintf("Either[%s, %s]", leftItem.Name(), rightItem.Name())
|
||||
isEither := Is[either.Either[A, B]]()
|
||||
|
||||
return MakeType(
|
||||
name,
|
||||
isEither,
|
||||
validateEither(leftItem, rightItem),
|
||||
encodeEither(leftItem, rightItem),
|
||||
)
|
||||
}
|
||||
347
v2/optics/codec/either_test.go
Normal file
347
v2/optics/codec/either_test.go
Normal file
@@ -0,0 +1,347 @@
|
||||
// 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 codec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestEitherWithIdentityCodecs tests the Either function with identity codecs
|
||||
// where both branches have the same output and input types
|
||||
func TestEitherWithIdentityCodecs(t *testing.T) {
|
||||
t.Run("creates codec with correct name", func(t *testing.T) {
|
||||
// The Either function is designed for cases where both branches encode to the same type
|
||||
// For example, both encode to string or both encode to JSON
|
||||
|
||||
// Create codecs that both encode to string
|
||||
stringToString := Id[string]()
|
||||
intToString := IntFromString()
|
||||
|
||||
eitherCodec := Either(stringToString, intToString)
|
||||
|
||||
assert.Equal(t, "Either[string, IntFromString]", eitherCodec.Name())
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherEncode tests encoding of Either values
|
||||
func TestEitherEncode(t *testing.T) {
|
||||
// Create codecs that both encode to string
|
||||
stringToString := Id[string]()
|
||||
intToString := IntFromString()
|
||||
|
||||
eitherCodec := Either(stringToString, intToString)
|
||||
|
||||
t.Run("encodes Left value", func(t *testing.T) {
|
||||
leftValue := either.Left[int]("hello")
|
||||
encoded := eitherCodec.Encode(leftValue)
|
||||
|
||||
assert.Equal(t, "hello", encoded)
|
||||
})
|
||||
|
||||
t.Run("encodes Right value", func(t *testing.T) {
|
||||
rightValue := either.Right[string](42)
|
||||
encoded := eitherCodec.Encode(rightValue)
|
||||
|
||||
assert.Equal(t, "42", encoded)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherDecode tests decoding/validation of Either values
|
||||
func TestEitherDecode(t *testing.T) {
|
||||
getOrElseNull := either.GetOrElse(reader.Of[validation.Errors, either.Either[string, int]](either.Left[int]("")))
|
||||
|
||||
// Create codecs that both work with string input
|
||||
stringCodec := Id[string]()
|
||||
intFromString := IntFromString()
|
||||
|
||||
eitherCodec := Either(stringCodec, intFromString)
|
||||
|
||||
t.Run("decodes integer string as Right", func(t *testing.T) {
|
||||
result := eitherCodec.Decode("42")
|
||||
|
||||
assert.True(t, either.IsRight(result), "should successfully decode integer string")
|
||||
|
||||
value := getOrElseNull(result)
|
||||
assert.True(t, either.IsRight(value), "should be Right")
|
||||
|
||||
rightValue := either.MonadFold(value,
|
||||
func(string) int { return 0 },
|
||||
F.Identity[int],
|
||||
)
|
||||
assert.Equal(t, 42, rightValue)
|
||||
})
|
||||
|
||||
t.Run("decodes non-integer string as Left", func(t *testing.T) {
|
||||
result := eitherCodec.Decode("hello")
|
||||
|
||||
assert.True(t, either.IsRight(result), "should successfully decode string")
|
||||
|
||||
value := getOrElseNull(result)
|
||||
assert.True(t, either.IsLeft(value), "should be Left")
|
||||
|
||||
leftValue := either.MonadFold(value,
|
||||
F.Identity[string],
|
||||
func(int) string { return "" },
|
||||
)
|
||||
assert.Equal(t, "hello", leftValue)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherValidation tests validation behavior
|
||||
func TestEitherValidation(t *testing.T) {
|
||||
t.Run("validates with custom codecs", func(t *testing.T) {
|
||||
// Create a codec that only accepts non-empty strings
|
||||
nonEmptyString := MakeType(
|
||||
"NonEmptyString",
|
||||
func(u any) either.Either[error, string] {
|
||||
s, ok := u.(string)
|
||||
if !ok || len(s) == 0 {
|
||||
return either.Left[string](fmt.Errorf("not a non-empty string"))
|
||||
}
|
||||
return either.Of[error](s)
|
||||
},
|
||||
func(s string) Decode[Context, string] {
|
||||
return func(c Context) Validation[string] {
|
||||
if len(s) == 0 {
|
||||
return validation.FailureWithMessage[string](s, "must not be empty")(c)
|
||||
}
|
||||
return validation.Success(s)
|
||||
}
|
||||
},
|
||||
F.Identity[string],
|
||||
)
|
||||
|
||||
// Create a codec that only accepts positive integers from strings
|
||||
positiveIntFromString := MakeType(
|
||||
"PositiveInt",
|
||||
func(u any) either.Either[error, int] {
|
||||
i, ok := u.(int)
|
||||
if !ok || i <= 0 {
|
||||
return either.Left[int](fmt.Errorf("not a positive integer"))
|
||||
}
|
||||
return either.Of[error](i)
|
||||
},
|
||||
func(s string) Decode[Context, int] {
|
||||
return func(c Context) Validation[int] {
|
||||
var n int
|
||||
_, err := fmt.Sscanf(s, "%d", &n)
|
||||
if err != nil {
|
||||
return validation.FailureWithError[int](s, "expected integer string")(err)(c)
|
||||
}
|
||||
if n <= 0 {
|
||||
return validation.FailureWithMessage[int](n, "must be positive")(c)
|
||||
}
|
||||
return validation.Success(n)
|
||||
}
|
||||
},
|
||||
func(n int) string {
|
||||
return fmt.Sprintf("%d", n)
|
||||
},
|
||||
)
|
||||
|
||||
eitherCodec := Either(nonEmptyString, positiveIntFromString)
|
||||
|
||||
// Valid non-empty string
|
||||
validLeft := eitherCodec.Decode("hello")
|
||||
assert.True(t, either.IsRight(validLeft))
|
||||
|
||||
// Valid positive integer
|
||||
validRight := eitherCodec.Decode("42")
|
||||
assert.True(t, either.IsRight(validRight))
|
||||
|
||||
// Invalid empty string - should fail both validations
|
||||
invalidEmpty := eitherCodec.Decode("")
|
||||
assert.True(t, either.IsLeft(invalidEmpty))
|
||||
|
||||
// Invalid zero - should fail Right validation, succeed as Left
|
||||
zeroResult := eitherCodec.Decode("0")
|
||||
// "0" is a valid non-empty string, so it should succeed as Left
|
||||
assert.True(t, either.IsRight(zeroResult))
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherRoundTrip tests encoding and decoding round trips
|
||||
func TestEitherRoundTrip(t *testing.T) {
|
||||
stringCodec := Id[string]()
|
||||
intFromString := IntFromString()
|
||||
|
||||
eitherCodec := Either(stringCodec, intFromString)
|
||||
|
||||
t.Run("round-trip Left value", func(t *testing.T) {
|
||||
original := "hello"
|
||||
|
||||
// Decode
|
||||
decodeResult := eitherCodec.Decode(original)
|
||||
require.True(t, either.IsRight(decodeResult))
|
||||
|
||||
decoded := either.MonadFold(decodeResult,
|
||||
func(validation.Errors) either.Either[string, int] { return either.Left[int]("") },
|
||||
F.Identity[either.Either[string, int]],
|
||||
)
|
||||
|
||||
// Encode
|
||||
encoded := eitherCodec.Encode(decoded)
|
||||
|
||||
// Verify
|
||||
assert.Equal(t, original, encoded)
|
||||
})
|
||||
|
||||
t.Run("round-trip Right value", func(t *testing.T) {
|
||||
original := "42"
|
||||
|
||||
// Decode
|
||||
decodeResult := eitherCodec.Decode(original)
|
||||
require.True(t, either.IsRight(decodeResult))
|
||||
|
||||
decoded := either.MonadFold(decodeResult,
|
||||
func(validation.Errors) either.Either[string, int] { return either.Right[string](0) },
|
||||
F.Identity[either.Either[string, int]],
|
||||
)
|
||||
|
||||
// Encode
|
||||
encoded := eitherCodec.Encode(decoded)
|
||||
|
||||
// Verify
|
||||
assert.Equal(t, original, encoded)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherPrioritization tests that Right validation is prioritized over Left
|
||||
func TestEitherPrioritization(t *testing.T) {
|
||||
stringCodec := Id[string]()
|
||||
intFromString := IntFromString()
|
||||
|
||||
eitherCodec := Either(stringCodec, intFromString)
|
||||
|
||||
t.Run("prioritizes Right over Left when both could succeed", func(t *testing.T) {
|
||||
// "42" can be validated as both string (Left) and int (Right)
|
||||
// The codec should prioritize Right
|
||||
result := eitherCodec.Decode("42")
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) either.Either[string, int] { return either.Left[int]("") },
|
||||
F.Identity[either.Either[string, int]],
|
||||
)
|
||||
|
||||
// Should be Right because int validation succeeds and is prioritized
|
||||
assert.True(t, either.IsRight(value))
|
||||
|
||||
rightValue := either.MonadFold(value,
|
||||
func(string) int { return 0 },
|
||||
F.Identity[int],
|
||||
)
|
||||
assert.Equal(t, 42, rightValue)
|
||||
})
|
||||
|
||||
t.Run("falls back to Left when Right fails", func(t *testing.T) {
|
||||
// "hello" can only be validated as string (Left), not as int (Right)
|
||||
result := eitherCodec.Decode("hello")
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
|
||||
value := either.MonadFold(result,
|
||||
func(validation.Errors) either.Either[string, int] { return either.Left[int]("") },
|
||||
F.Identity[either.Either[string, int]],
|
||||
)
|
||||
|
||||
// Should be Left because int validation failed
|
||||
assert.True(t, either.IsLeft(value))
|
||||
|
||||
leftValue := either.MonadFold(value,
|
||||
F.Identity[string],
|
||||
func(int) string { return "" },
|
||||
)
|
||||
assert.Equal(t, "hello", leftValue)
|
||||
})
|
||||
}
|
||||
|
||||
// TestEitherErrorAccumulation tests that errors from both branches are accumulated
|
||||
func TestEitherErrorAccumulation(t *testing.T) {
|
||||
// Create codecs with specific validation rules that will both fail
|
||||
nonEmptyString := MakeType(
|
||||
"NonEmptyString",
|
||||
func(u any) either.Either[error, string] {
|
||||
s, ok := u.(string)
|
||||
if !ok || len(s) == 0 {
|
||||
return either.Left[string](fmt.Errorf("not a non-empty string"))
|
||||
}
|
||||
return either.Of[error](s)
|
||||
},
|
||||
func(s string) Decode[Context, string] {
|
||||
return func(c Context) Validation[string] {
|
||||
if len(s) == 0 {
|
||||
return validation.FailureWithMessage[string](s, "must not be empty")(c)
|
||||
}
|
||||
return validation.Success(s)
|
||||
}
|
||||
},
|
||||
F.Identity[string],
|
||||
)
|
||||
|
||||
positiveIntFromString := MakeType(
|
||||
"PositiveInt",
|
||||
func(u any) either.Either[error, int] {
|
||||
i, ok := u.(int)
|
||||
if !ok || i <= 0 {
|
||||
return either.Left[int](fmt.Errorf("not a positive integer"))
|
||||
}
|
||||
return either.Of[error](i)
|
||||
},
|
||||
func(s string) Decode[Context, int] {
|
||||
return func(c Context) Validation[int] {
|
||||
var n int
|
||||
_, err := fmt.Sscanf(s, "%d", &n)
|
||||
if err != nil {
|
||||
return validation.FailureWithError[int](s, "expected integer string")(err)(c)
|
||||
}
|
||||
if n <= 0 {
|
||||
return validation.FailureWithMessage[int](n, "must be positive")(c)
|
||||
}
|
||||
return validation.Success(n)
|
||||
}
|
||||
},
|
||||
strconv.Itoa,
|
||||
)
|
||||
|
||||
eitherCodec := Either(nonEmptyString, positiveIntFromString)
|
||||
|
||||
t.Run("accumulates errors from both branches when both fail", func(t *testing.T) {
|
||||
// Empty string will fail both validations
|
||||
result := eitherCodec.Decode("")
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[validation.Errors],
|
||||
func(either.Either[string, int]) validation.Errors { return nil },
|
||||
)
|
||||
|
||||
require.NotNil(t, errors)
|
||||
// Should have errors from both string and int validation attempts
|
||||
assert.NotEmpty(t, errors)
|
||||
})
|
||||
}
|
||||
335
v2/optics/codec/validate/bind.go
Normal file
335
v2/optics/codec/validate/bind.go
Normal file
@@ -0,0 +1,335 @@
|
||||
// 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 validate
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
A "github.com/IBM/fp-go/v2/internal/apply"
|
||||
C "github.com/IBM/fp-go/v2/internal/chain"
|
||||
F "github.com/IBM/fp-go/v2/internal/functor"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
)
|
||||
|
||||
// Do creates an empty context of type S to be used with the Bind operation.
|
||||
// This is the starting point for building up a context using do-notation style.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Result struct {
|
||||
// x int
|
||||
// y string
|
||||
// }
|
||||
// result := Do(Result{})
|
||||
func Do[I, S any](
|
||||
empty S,
|
||||
) Validate[I, S] {
|
||||
return Of[I](empty)
|
||||
}
|
||||
|
||||
// Bind attaches the result of a computation to a context S1 to produce a context S2.
|
||||
// This is used in do-notation style to sequentially build up a context.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{}),
|
||||
// Bind(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, func(s State) Validate[string, int] {
|
||||
// return Of[string](42)
|
||||
// }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 42})
|
||||
func Bind[I, S1, S2, A any](
|
||||
setter func(A) func(S1) S2,
|
||||
f Kleisli[I, S1, A],
|
||||
) Operator[I, S1, S2] {
|
||||
return C.Bind(
|
||||
Chain[I, S1, S2],
|
||||
Map[I, A, S2],
|
||||
setter,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// 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, not wrapped in Validate.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; computed int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{x: 5}),
|
||||
// Let[string](func(c int) func(State) State {
|
||||
// return func(s State) State { s.computed = c; return s }
|
||||
// }, func(s State) int { return s.x * 2 }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 5, computed: 10})
|
||||
func Let[I, S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
f func(S1) B,
|
||||
) Operator[I, S1, S2] {
|
||||
return F.Let(
|
||||
Map[I, S1, S2],
|
||||
key,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// LetTo attaches a constant value to a context S1 to produce a context S2.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; name string }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{x: 5}),
|
||||
// LetTo(func(n string) func(State) State {
|
||||
// return func(s State) State { s.name = n; return s }
|
||||
// }, "example"),
|
||||
// )
|
||||
func LetTo[I, S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
b B,
|
||||
) Operator[I, S1, S2] {
|
||||
return F.LetTo(
|
||||
Map[I, S1, S2],
|
||||
key,
|
||||
b,
|
||||
)
|
||||
}
|
||||
|
||||
// BindTo initializes a new state S1 from a value T.
|
||||
// This is typically used as the first operation after creating a Validate value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { value int }
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](42),
|
||||
// BindTo[string](func(x int) State { return State{value: x} }),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{value: 42})
|
||||
func BindTo[I, S1, T any](
|
||||
setter func(T) S1,
|
||||
) Operator[I, T, S1] {
|
||||
return C.BindTo(
|
||||
Map[I, T, S1],
|
||||
setter,
|
||||
)
|
||||
}
|
||||
|
||||
// ApS attaches a value to a context S1 to produce a context S2 by considering the context and the value concurrently.
|
||||
// This uses the applicative functor pattern, allowing parallel composition.
|
||||
//
|
||||
// IMPORTANT: Unlike Bind which fails fast, ApS aggregates ALL validation errors from both the context
|
||||
// and the value. If both validations fail, all errors are collected and returned together.
|
||||
// This is useful for validating multiple independent fields and reporting all errors at once.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// decoder := F.Pipe2(
|
||||
// Do[string](State{}),
|
||||
// ApS(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, Of[string](42)),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(State{x: 42})
|
||||
//
|
||||
// Error aggregation example:
|
||||
//
|
||||
// // Both decoders fail - errors are aggregated
|
||||
// decoder1 := func(input string) Validation[State] {
|
||||
// return validation.Failures[State](/* errors */)
|
||||
// }
|
||||
// decoder2 := func(input string) Validation[int] {
|
||||
// return validation.Failures[int](/* errors */)
|
||||
// }
|
||||
// combined := ApS(setter, decoder2)(decoder1)
|
||||
// result := combined("input") // Contains BOTH sets of errors
|
||||
func ApS[I, S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Validate[I, T],
|
||||
) Operator[I, S1, S2] {
|
||||
return A.ApS(
|
||||
Ap[S2, I, T],
|
||||
Map[I, S1, func(T) S2],
|
||||
setter,
|
||||
fa,
|
||||
)
|
||||
}
|
||||
|
||||
// ApSL attaches a value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines ApS with a lens, allowing you to use
|
||||
// optics to update nested structures in a more composable way.
|
||||
//
|
||||
// IMPORTANT: Like ApS, this function aggregates ALL validation errors. If both the context
|
||||
// and the value fail validation, all errors are collected and returned together.
|
||||
// This enables comprehensive error reporting for complex nested structures.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// This eliminates the need to manually write setter functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Address struct {
|
||||
// Street string
|
||||
// City string
|
||||
// }
|
||||
//
|
||||
// type Person struct {
|
||||
// Name string
|
||||
// Address Address
|
||||
// }
|
||||
//
|
||||
// // Create a lens for the Address field
|
||||
// addressLens := lens.MakeLens(
|
||||
// func(p Person) Address { return p.Address },
|
||||
// func(p Person, a Address) Person { p.Address = a; return p },
|
||||
// )
|
||||
//
|
||||
// // Use ApSL to update the address
|
||||
// decoder := F.Pipe2(
|
||||
// Of[string](Person{Name: "Alice"}),
|
||||
// ApSL(
|
||||
// addressLens,
|
||||
// Of[string](Address{Street: "Main St", City: "NYC"}),
|
||||
// ),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Person{...})
|
||||
func ApSL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
fa Validate[I, T],
|
||||
) Operator[I, S, S] {
|
||||
return ApS(lens.Set, fa)
|
||||
}
|
||||
|
||||
// BindL attaches the result of a computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Bind with a lens, allowing you to use
|
||||
// optics to update nested structures based on their current values.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The computation function f receives the current value of the focused field and returns
|
||||
// a Validation that produces the new value.
|
||||
//
|
||||
// Unlike ApSL, BindL uses monadic sequencing, meaning the computation f can depend on
|
||||
// the current value of the focused field.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Increment the counter, but fail if it would exceed 100
|
||||
// increment := func(v int) Validate[string, int] {
|
||||
// return func(input string) Validation[int] {
|
||||
// if v >= 100 {
|
||||
// return validation.Failures[int](/* errors */)
|
||||
// }
|
||||
// return validation.Success(v + 1)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Counter{Value: 42}),
|
||||
// BindL(valueLens, increment),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Counter{Value: 43})
|
||||
func BindL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Kleisli[I, T, T],
|
||||
) Operator[I, S, S] {
|
||||
return Bind(lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetL attaches the result of a pure computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Let with a lens, allowing you to use
|
||||
// optics to update nested structures with pure transformations.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The transformation function f receives the current value of the focused field and returns
|
||||
// the new value directly (not wrapped in Validation).
|
||||
//
|
||||
// This is useful for pure transformations that cannot fail, such as mathematical operations,
|
||||
// string manipulations, or other deterministic updates.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Double the counter value
|
||||
// double := func(v int) int { return v * 2 }
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Counter{Value: 21}),
|
||||
// LetL(valueLens, double),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Counter{Value: 42})
|
||||
func LetL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Endomorphism[T],
|
||||
) Operator[I, S, S] {
|
||||
return Let[I](lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetToL attaches a constant value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines LetTo with a lens, allowing you to use
|
||||
// optics to set nested fields to specific values.
|
||||
//
|
||||
// The lens parameter provides the setter for a field within the structure S.
|
||||
// Unlike LetL which transforms the current value, LetToL simply replaces it with
|
||||
// the provided constant value b.
|
||||
//
|
||||
// This is useful for resetting fields, initializing values, or setting fields to
|
||||
// predetermined constants.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// Debug bool
|
||||
// Timeout int
|
||||
// }
|
||||
//
|
||||
// debugLens := lens.MakeLens(
|
||||
// func(c Config) bool { return c.Debug },
|
||||
// func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
// )
|
||||
//
|
||||
// decoder := F.Pipe1(
|
||||
// Of[string](Config{Debug: true, Timeout: 30}),
|
||||
// LetToL(debugLens, false),
|
||||
// )
|
||||
// result := decoder("input") // Returns validation.Success(Config{Debug: false, Timeout: 30})
|
||||
func LetToL[I, S, T any](
|
||||
lens L.Lens[S, T],
|
||||
b T,
|
||||
) Operator[I, S, S] {
|
||||
return LetTo[I](lens.Set, b)
|
||||
}
|
||||
733
v2/optics/codec/validate/bind_test.go
Normal file
733
v2/optics/codec/validate/bind_test.go
Normal file
@@ -0,0 +1,733 @@
|
||||
// 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 validate
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDo(t *testing.T) {
|
||||
t.Run("creates successful validation with empty state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
validator := Do[string](State{})
|
||||
result := validator("input")(nil)
|
||||
|
||||
assert.Equal(t, either.Of[Errors](State{}), result)
|
||||
})
|
||||
|
||||
t.Run("creates successful validation with initialized state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
initial := State{x: 42, y: "hello"}
|
||||
validator := Do[string](initial)
|
||||
result := validator("input")(nil)
|
||||
|
||||
assert.Equal(t, either.Of[Errors](initial), result)
|
||||
})
|
||||
|
||||
t.Run("works with different input types", func(t *testing.T) {
|
||||
intValidator := Do[int](0)
|
||||
assert.Equal(t, either.Of[Errors](0), intValidator(42)(nil))
|
||||
|
||||
strValidator := Do[string]("")
|
||||
assert.Equal(t, either.Of[Errors](""), strValidator("test")(nil))
|
||||
|
||||
type Custom struct{ Value int }
|
||||
customValidator := Do[[]byte](Custom{Value: 100})
|
||||
assert.Equal(t, either.Of[Errors](Custom{Value: 100}), customValidator([]byte("data"))(nil))
|
||||
})
|
||||
}
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("binds successful validation to state", func(t *testing.T) {
|
||||
validator := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
return Of[string](42)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
return Of[string](10)
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](State{x: 42, y: 10}), result)
|
||||
})
|
||||
|
||||
t.Run("propagates failure", func(t *testing.T) {
|
||||
validator := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
return Of[string](42)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
return func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "y failed"}})
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "y failed", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("can access previous state values", func(t *testing.T) {
|
||||
validator := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
return Of[string](10)
|
||||
}),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validate[string, int] {
|
||||
// y depends on x
|
||||
return Of[string](s.x * 2)
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](State{x: 10, y: 20}), result)
|
||||
})
|
||||
|
||||
t.Run("can access input value", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Do[int](State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validate[int, int] {
|
||||
return func(input int) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Success(input * 2)
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator(21)(nil)
|
||||
assert.Equal(t, either.Of[Errors](State{x: 42}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLet(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
computed int
|
||||
}
|
||||
|
||||
t.Run("attaches pure computation result to state", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Do[string](State{x: 5}),
|
||||
Let[string](func(c int) func(State) State {
|
||||
return func(s State) State { s.computed = c; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 5, computed: 10}, value)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := func(input string) Reader[Context, Validation[State]] {
|
||||
return func(ctx Context) Validation[State] {
|
||||
return validation.Failures[State](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := Let[string](func(c int) func(State) State {
|
||||
return func(s State) State { s.computed = c; return s }
|
||||
}, func(s State) int { return s.x * 2 })
|
||||
|
||||
result := validator(failure)("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "error", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Let operations", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
z int
|
||||
}
|
||||
validator := F.Pipe3(
|
||||
Do[string](State{x: 5}),
|
||||
Let[string](func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
Let[string](func(z int) func(State) State {
|
||||
return func(s State) State { s.z = z; return s }
|
||||
}, func(s State) int { return s.y + 10 }),
|
||||
Let[string](func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) int { return s.z * 3 }),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 60, y: 10, z: 20}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetTo(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
name string
|
||||
}
|
||||
|
||||
t.Run("attaches constant value to state", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Do[string](State{x: 5}),
|
||||
LetTo[string](func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "example"),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{x: 5, name: "example"}, value)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := func(input string) Reader[Context, Validation[State]] {
|
||||
return func(ctx Context) Validation[State] {
|
||||
return validation.Failures[State](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := LetTo[string](func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "example")
|
||||
|
||||
result := validator(failure)("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("sets multiple constant values", func(t *testing.T) {
|
||||
type State struct {
|
||||
name string
|
||||
version int
|
||||
active bool
|
||||
}
|
||||
validator := F.Pipe3(
|
||||
Do[string](State{}),
|
||||
LetTo[string](func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "app"),
|
||||
LetTo[string](func(v int) func(State) State {
|
||||
return func(s State) State { s.version = v; return s }
|
||||
}, 2),
|
||||
LetTo[string](func(a bool) func(State) State {
|
||||
return func(s State) State { s.active = a; return s }
|
||||
}, true),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{name: "app", version: 2, active: true}, value)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindTo(t *testing.T) {
|
||||
type State struct {
|
||||
value int
|
||||
}
|
||||
|
||||
t.Run("initializes state from value", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Of[string](42),
|
||||
BindTo[string](func(x int) State { return State{value: x} }),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) State { return State{} },
|
||||
F.Identity[State],
|
||||
)
|
||||
assert.Equal(t, State{value: 42}, value)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := BindTo[string](func(x int) State { return State{value: x} })
|
||||
|
||||
result := validator(failure)("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "error", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
type StringState struct {
|
||||
text string
|
||||
}
|
||||
validator := F.Pipe1(
|
||||
Of[int]("hello"),
|
||||
BindTo[int](func(s string) StringState { return StringState{text: s} }),
|
||||
)
|
||||
|
||||
result := validator(42)(nil)
|
||||
assert.Equal(t, either.Of[Errors](StringState{text: "hello"}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApS(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("attaches value using applicative pattern", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Do[string](State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Of[string](42)),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](State{x: 42}), result)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors from both validations", func(t *testing.T) {
|
||||
stateFailure := func(input string) Reader[Context, Validation[State]] {
|
||||
return func(ctx Context) Validation[State] {
|
||||
return validation.Failures[State](Errors{&validation.ValidationError{Messsage: "state error"}})
|
||||
}
|
||||
}
|
||||
valueFailure := func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "value error"}})
|
||||
}
|
||||
}
|
||||
|
||||
validator := ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, valueFailure)
|
||||
|
||||
result := validator(stateFailure)("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
messages := []string{errors[0].Messsage, errors[1].Messsage}
|
||||
assert.Contains(t, messages, "state error")
|
||||
assert.Contains(t, messages, "value error")
|
||||
})
|
||||
|
||||
t.Run("combines multiple ApS operations", func(t *testing.T) {
|
||||
validator := F.Pipe2(
|
||||
Do[string](State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Of[string](10)),
|
||||
ApS(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, Of[string](20)),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](State{x: 10, y: 20}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApSL(t *testing.T) {
|
||||
type Address struct {
|
||||
Street string
|
||||
City string
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Address Address
|
||||
}
|
||||
|
||||
t.Run("updates nested structure using lens", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
validator := F.Pipe1(
|
||||
Of[string](Person{Name: "Alice"}),
|
||||
ApSL(
|
||||
addressLens,
|
||||
Of[string](Address{Street: "Main St", City: "NYC"}),
|
||||
),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
expected := Person{
|
||||
Name: "Alice",
|
||||
Address: Address{Street: "Main St", City: "NYC"},
|
||||
}
|
||||
assert.Equal(t, either.Of[Errors](expected), result)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
personFailure := func(input string) Reader[Context, Validation[Person]] {
|
||||
return func(ctx Context) Validation[Person] {
|
||||
return validation.Failures[Person](Errors{&validation.ValidationError{Messsage: "person error"}})
|
||||
}
|
||||
}
|
||||
addressFailure := func(input string) Reader[Context, Validation[Address]] {
|
||||
return func(ctx Context) Validation[Address] {
|
||||
return validation.Failures[Address](Errors{&validation.ValidationError{Messsage: "address error"}})
|
||||
}
|
||||
}
|
||||
|
||||
validator := ApSL(addressLens, addressFailure)
|
||||
result := validator(personFailure)("input")(nil)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(Person) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("updates field based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Validate[string, int] {
|
||||
return Of[string](v + 1)
|
||||
}
|
||||
|
||||
validator := F.Pipe1(
|
||||
Of[string](Counter{Value: 42}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](Counter{Value: 43}), result)
|
||||
})
|
||||
|
||||
t.Run("fails validation based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Validate[string, int] {
|
||||
return func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
if v >= 100 {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "exceeds limit"}})
|
||||
}
|
||||
return validation.Success(v + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validator := F.Pipe1(
|
||||
Of[string](Counter{Value: 100}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(Counter) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "exceeds limit", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
increment := func(v int) Validate[string, int] {
|
||||
return Of[string](v + 1)
|
||||
}
|
||||
|
||||
failure := func(input string) Reader[Context, Validation[Counter]] {
|
||||
return func(ctx Context) Validation[Counter] {
|
||||
return validation.Failures[Counter](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := BindL(valueLens, increment)
|
||||
result := validator(failure)("input")(nil)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("transforms field with pure function", func(t *testing.T) {
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
validator := F.Pipe1(
|
||||
Of[string](Counter{Value: 21}),
|
||||
LetL[string](valueLens, double),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](Counter{Value: 42}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
failure := func(input string) Reader[Context, Validation[Counter]] {
|
||||
return func(ctx Context) Validation[Counter] {
|
||||
return validation.Failures[Counter](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := LetL[string](valueLens, double)
|
||||
result := validator(failure)("input")(nil)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("chains multiple transformations", func(t *testing.T) {
|
||||
add10 := func(v int) int { return v + 10 }
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
validator := F.Pipe2(
|
||||
Of[string](Counter{Value: 5}),
|
||||
LetL[string](valueLens, add10),
|
||||
LetL[string](valueLens, double),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](Counter{Value: 30}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetToL(t *testing.T) {
|
||||
type Config struct {
|
||||
Debug bool
|
||||
Timeout int
|
||||
}
|
||||
|
||||
debugLens := L.MakeLens(
|
||||
func(c Config) bool { return c.Debug },
|
||||
func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
)
|
||||
|
||||
t.Run("sets field to constant value", func(t *testing.T) {
|
||||
validator := F.Pipe1(
|
||||
Of[string](Config{Debug: true, Timeout: 30}),
|
||||
LetToL[string](debugLens, false),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](Config{Debug: false, Timeout: 30}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := func(input string) Reader[Context, Validation[Config]] {
|
||||
return func(ctx Context) Validation[Config] {
|
||||
return validation.Failures[Config](Errors{&validation.ValidationError{Messsage: "error"}})
|
||||
}
|
||||
}
|
||||
validator := LetToL[string](debugLens, false)
|
||||
result := validator(failure)("input")(nil)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("sets multiple fields", func(t *testing.T) {
|
||||
timeoutLens := L.MakeLens(
|
||||
func(c Config) int { return c.Timeout },
|
||||
func(c Config, t int) Config { c.Timeout = t; return c },
|
||||
)
|
||||
|
||||
validator := F.Pipe2(
|
||||
Of[string](Config{Debug: true, Timeout: 30}),
|
||||
LetToL[string](debugLens, false),
|
||||
LetToL[string](timeoutLens, 60),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
assert.Equal(t, either.Of[Errors](Config{Debug: false, Timeout: 60}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindOperationsComposition(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
t.Run("combines Do, Bind, Let, and LetTo", func(t *testing.T) {
|
||||
validator := F.Pipe4(
|
||||
Do[string](User{}),
|
||||
LetTo[string](func(n string) func(User) User {
|
||||
return func(u User) User { u.Name = n; return u }
|
||||
}, "Alice"),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Validate[string, int] {
|
||||
// Age validation
|
||||
if len(u.Name) > 0 {
|
||||
return Of[string](25)
|
||||
}
|
||||
return func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "name required"}})
|
||||
}
|
||||
}
|
||||
}),
|
||||
Let[string](func(e string) func(User) User {
|
||||
return func(u User) User { u.Email = e; return u }
|
||||
}, func(u User) string {
|
||||
// Derive email from name
|
||||
return u.Name + "@example.com"
|
||||
}),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Validate[string, int] {
|
||||
// Validate age is positive
|
||||
if u.Age > 0 {
|
||||
return Of[string](u.Age)
|
||||
}
|
||||
return func(input string) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "age must be positive"}})
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator("input")(nil)
|
||||
expected := User{
|
||||
Name: "Alice",
|
||||
Age: 25,
|
||||
Email: "Alice@example.com",
|
||||
}
|
||||
assert.Equal(t, either.Of[Errors](expected), result)
|
||||
})
|
||||
|
||||
t.Run("validates with input-dependent logic", func(t *testing.T) {
|
||||
type Config struct {
|
||||
MaxValue int
|
||||
Value int
|
||||
}
|
||||
|
||||
validator := F.Pipe2(
|
||||
Do[int](Config{}),
|
||||
Bind(func(max int) func(Config) Config {
|
||||
return func(c Config) Config { c.MaxValue = max; return c }
|
||||
}, func(c Config) Validate[int, int] {
|
||||
// Extract max from input
|
||||
return func(input int) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
return validation.Success(input)
|
||||
}
|
||||
}
|
||||
}),
|
||||
Bind(func(val int) func(Config) Config {
|
||||
return func(c Config) Config { c.Value = val; return c }
|
||||
}, func(c Config) Validate[int, int] {
|
||||
// Validate value against max
|
||||
return func(input int) Reader[Context, Validation[int]] {
|
||||
return func(ctx Context) Validation[int] {
|
||||
if input/2 <= c.MaxValue {
|
||||
return validation.Success(input / 2)
|
||||
}
|
||||
return validation.Failures[int](Errors{&validation.ValidationError{Messsage: "value exceeds max"}})
|
||||
}
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
result := validator(100)(nil)
|
||||
assert.Equal(t, either.Of[Errors](Config{MaxValue: 100, Value: 50}), result)
|
||||
})
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/endomorphism"
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/decode"
|
||||
"github.com/IBM/fp-go/v2/optics/codec/validation"
|
||||
@@ -90,25 +91,72 @@ type (
|
||||
// "at user.address.zipCode: expected string, got number"
|
||||
Context = validation.Context
|
||||
|
||||
Decode[I, A any] = decode.Decode[I, A]
|
||||
|
||||
// Validate is a function that validates input I to produce type A with full context tracking.
|
||||
// Decode represents a decoding operation that transforms input I into output A
|
||||
// within a validation context.
|
||||
//
|
||||
// Type structure:
|
||||
// Validate[I, A] = Reader[I, Decode[Context, A]]
|
||||
// Decode[I, A] = Reader[Context, Validation[A]]
|
||||
//
|
||||
// This means:
|
||||
// 1. Takes an input of type I
|
||||
// 2. Returns a Reader that depends on validation Context
|
||||
// 3. That Reader produces a Validation[A] (Either[Errors, A])
|
||||
// 1. Takes a validation Context (path through nested structures)
|
||||
// 2. Returns a Validation[A] (Either[Errors, A])
|
||||
//
|
||||
// The layered structure enables:
|
||||
// - Access to the input value being validated
|
||||
// - Context tracking through nested structures
|
||||
// - Error accumulation with detailed paths
|
||||
// - Composition with other validators
|
||||
// Decode is used as the foundation for validation operations, providing:
|
||||
// - Context-aware error reporting with detailed paths
|
||||
// - Error accumulation across multiple validations
|
||||
// - Composable validation logic
|
||||
//
|
||||
// The Decode type is typically not used directly but through the Validate type,
|
||||
// which adds an additional Reader layer for accessing the input value.
|
||||
//
|
||||
// Example:
|
||||
// decoder := func(ctx Context) Validation[int] {
|
||||
// // Perform validation and return result
|
||||
// return validation.Success(42)
|
||||
// }
|
||||
// // decoder is a Decode[any, int]
|
||||
Decode[I, A any] = decode.Decode[I, A]
|
||||
|
||||
// Validate represents a composable validator that transforms input I to output A
|
||||
// with comprehensive error tracking and context propagation.
|
||||
//
|
||||
// # Type Structure
|
||||
//
|
||||
// Validate[I, A] = Reader[I, Decode[Context, A]]
|
||||
// = Reader[I, Reader[Context, Validation[A]]]
|
||||
// = func(I) func(Context) Either[Errors, A]
|
||||
//
|
||||
// This three-layer structure provides:
|
||||
// 1. Input access: The outer Reader[I, ...] gives access to the input value I
|
||||
// 2. Context tracking: The middle Reader[Context, ...] tracks the validation path
|
||||
// 3. Error handling: The inner Validation[A] accumulates errors or produces value A
|
||||
//
|
||||
// # Purpose
|
||||
//
|
||||
// Validate is the core type for building type-safe, composable validators that:
|
||||
// - Transform and validate data from one type to another
|
||||
// - Track the path through nested structures for detailed error messages
|
||||
// - Accumulate multiple validation errors instead of failing fast
|
||||
// - Compose with other validators using functional patterns
|
||||
//
|
||||
// # Key Features
|
||||
//
|
||||
// - Context-aware: Automatically tracks validation path (e.g., "user.address.zipCode")
|
||||
// - Error accumulation: Collects all validation errors, not just the first one
|
||||
// - Type-safe: Leverages Go's type system to ensure correctness
|
||||
// - Composable: Validators can be combined using Map, Chain, Ap, and other operators
|
||||
//
|
||||
// # Algebraic Structure
|
||||
//
|
||||
// Validate forms several algebraic structures:
|
||||
// - Functor: Transform successful results with Map
|
||||
// - Applicative: Combine independent validators in parallel with Ap
|
||||
// - Monad: Chain dependent validators sequentially with Chain
|
||||
//
|
||||
// # Example Usage
|
||||
//
|
||||
// Basic validator:
|
||||
//
|
||||
// Example usage:
|
||||
// validatePositive := func(n int) Reader[Context, Validation[int]] {
|
||||
// return func(ctx Context) Validation[int] {
|
||||
// if n > 0 {
|
||||
@@ -119,10 +167,33 @@ type (
|
||||
// }
|
||||
// // validatePositive is a Validate[int, int]
|
||||
//
|
||||
// The Validate type forms:
|
||||
// - A Functor: Can map over successful results
|
||||
// - An Applicative: Can combine validators in parallel
|
||||
// - A Monad: Can chain dependent validations
|
||||
// Composing validators:
|
||||
//
|
||||
// // Transform the result of a validator
|
||||
// doubled := Map[int, int, int](func(x int) int { return x * 2 })(validatePositive)
|
||||
//
|
||||
// // Chain dependent validations
|
||||
// validateRange := func(n int) Validate[int, int] {
|
||||
// return func(input int) Reader[Context, Validation[int]] {
|
||||
// return func(ctx Context) Validation[int] {
|
||||
// if n <= 100 {
|
||||
// return validation.Success(n)
|
||||
// }
|
||||
// return validation.FailureWithMessage[int](n, "must be <= 100")(ctx)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// combined := Chain(validateRange)(validatePositive)
|
||||
//
|
||||
// # Integration
|
||||
//
|
||||
// Validate integrates with the broader optics/codec ecosystem:
|
||||
// - Works with Decode for decoding operations
|
||||
// - Uses Validation for error handling
|
||||
// - Leverages Context for detailed error reporting
|
||||
// - Composes with other codec types for complete encode/decode pipelines
|
||||
//
|
||||
// See the package documentation for more examples and patterns.
|
||||
Validate[I, A any] = Reader[I, Decode[Context, A]]
|
||||
|
||||
// Errors is a collection of validation errors that occurred during validation.
|
||||
@@ -174,4 +245,30 @@ type (
|
||||
// // toUpper is an Operator[string, string, string]
|
||||
// // It can be applied to any string validator to uppercase the result
|
||||
Operator[I, A, B any] = Kleisli[I, Validate[I, A], B]
|
||||
|
||||
// Endomorphism represents a function from a type to itself.
|
||||
//
|
||||
// Type: Endomorphism[A] = func(A) A
|
||||
//
|
||||
// An endomorphism is a morphism (structure-preserving map) where the source
|
||||
// and target are the same type. In simpler terms, it's a function that takes
|
||||
// a value of type A and returns a value of the same type A.
|
||||
//
|
||||
// Endomorphisms are useful for:
|
||||
// - Transformations that preserve type (e.g., string normalization)
|
||||
// - Composable updates and modifications
|
||||
// - Building pipelines of same-type transformations
|
||||
// - Implementing the Monoid pattern (composition as binary operation)
|
||||
//
|
||||
// Endomorphisms form a Monoid under function composition:
|
||||
// - Identity: func(a A) A { return a }
|
||||
// - Concat: func(f, g Endomorphism[A]) Endomorphism[A] {
|
||||
// return func(a A) A { return f(g(a)) }
|
||||
// }
|
||||
//
|
||||
// Example:
|
||||
// trim := strings.TrimSpace // Endomorphism[string]
|
||||
// lower := strings.ToLower // Endomorphism[string]
|
||||
// normalize := compose(trim, lower) // Endomorphism[string]
|
||||
Endomorphism[A any] = endomorphism.Endomorphism[A]
|
||||
)
|
||||
|
||||
@@ -309,6 +309,225 @@ func Chain[I, A, B any](f Kleisli[I, A, B]) Operator[I, A, B] {
|
||||
)
|
||||
}
|
||||
|
||||
// ChainLeft sequences a computation on the failure (Left) channel of a validation.
|
||||
//
|
||||
// This function operates on the error path of validation, allowing you to transform,
|
||||
// enrich, or recover from validation failures. It's the dual of Chain - while Chain
|
||||
// operates on success values, ChainLeft operates on error values.
|
||||
//
|
||||
// # Key Behavior
|
||||
//
|
||||
// **Critical difference from standard Either operations**: This validation-specific
|
||||
// implementation **aggregates errors** using the Errors monoid. When the transformation
|
||||
// function returns a failure, both the original errors AND the new errors are combined,
|
||||
// ensuring comprehensive error reporting.
|
||||
//
|
||||
// 1. **Success Pass-Through**: If validation succeeds, the handler is never called and
|
||||
// the success value passes through unchanged.
|
||||
//
|
||||
// 2. **Error Recovery**: The handler can recover from failures by returning a successful
|
||||
// validation, converting Left to Right.
|
||||
//
|
||||
// 3. **Error Aggregation**: When the handler also returns a failure, both the original
|
||||
// errors and the new errors are combined using the Errors monoid.
|
||||
//
|
||||
// 4. **Input Access**: The handler returns a Validate[I, A] function, giving it access
|
||||
// to the original input value I for context-aware error handling.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - I: The input type
|
||||
// - A: The type of the validation result
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A Kleisli arrow that takes Errors and returns a Validate[I, A]. This function
|
||||
// is called only when validation fails, receiving the accumulated errors.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// An Operator[I, A, A] that transforms validators by handling their error cases.
|
||||
//
|
||||
// # Example: Error Recovery
|
||||
//
|
||||
// // Validator that may fail
|
||||
// validatePositive := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
// return func(ctx validation.Context) validation.Validation[int] {
|
||||
// if n > 0 {
|
||||
// return validation.Success(n)
|
||||
// }
|
||||
// return validation.FailureWithMessage[int](n, "must be positive")(ctx)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Recover from specific errors with a default value
|
||||
// withDefault := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
// for _, err := range errs {
|
||||
// if err.Messsage == "must be positive" {
|
||||
// return Of[int](0) // recover with default
|
||||
// }
|
||||
// }
|
||||
// return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
// return func(ctx validation.Context) validation.Validation[int] {
|
||||
// return either.Left[int](errs)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// validator := withDefault(validatePositive)
|
||||
// result := validator(-5)(nil)
|
||||
// // Result: Success(0) - recovered from failure
|
||||
//
|
||||
// # Example: Error Context Addition
|
||||
//
|
||||
// // Add contextual information to errors
|
||||
// addContext := ChainLeft(func(errs Errors) Validate[string, int] {
|
||||
// return func(input string) Reader[validation.Context, validation.Validation[int]] {
|
||||
// return func(ctx validation.Context) validation.Validation[int] {
|
||||
// return either.Left[int](validation.Errors{
|
||||
// {
|
||||
// Context: validation.Context{{Key: "user", Type: "User"}, {Key: "age", Type: "int"}},
|
||||
// Messsage: "failed to validate user age",
|
||||
// },
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// validator := addContext(someValidator)
|
||||
// // Errors will include both original error and context
|
||||
//
|
||||
// # Example: Input-Dependent Recovery
|
||||
//
|
||||
// // Recover with different defaults based on input
|
||||
// smartDefault := ChainLeft(func(errs Errors) Validate[string, int] {
|
||||
// return func(input string) Reader[validation.Context, validation.Validation[int]] {
|
||||
// return func(ctx validation.Context) validation.Validation[int] {
|
||||
// // Use input to determine appropriate default
|
||||
// if strings.Contains(input, "http") {
|
||||
// return validation.Of(80)
|
||||
// }
|
||||
// if strings.Contains(input, "https") {
|
||||
// return validation.Of(443)
|
||||
// }
|
||||
// return validation.Of(8080)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - Errors are accumulated, not replaced - this ensures no validation failures are lost
|
||||
// - The handler has access to both the errors and the original input
|
||||
// - Success values bypass the handler completely
|
||||
// - This enables sophisticated error handling strategies including recovery, enrichment, and transformation
|
||||
// - Use OrElse as a semantic alias when emphasizing fallback/alternative logic
|
||||
func ChainLeft[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return readert.Chain[Validate[I, A]](
|
||||
decode.ChainLeft,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// OrElse provides an alternative validation when the primary validation fails.
|
||||
//
|
||||
// This is a semantic alias for ChainLeft with identical behavior. The name "OrElse"
|
||||
// emphasizes the intent of providing fallback or alternative validation logic, making
|
||||
// code more readable when that's the primary use case.
|
||||
//
|
||||
// # Relationship to ChainLeft
|
||||
//
|
||||
// **OrElse and ChainLeft are functionally identical** - they produce exactly the same
|
||||
// results for all inputs. The choice between them is purely about code readability:
|
||||
//
|
||||
// - Use **OrElse** when emphasizing fallback/alternative validation logic
|
||||
// - Use **ChainLeft** when emphasizing technical error channel transformation
|
||||
//
|
||||
// Both maintain the critical property of **error aggregation**, ensuring all validation
|
||||
// failures are preserved and reported together.
|
||||
//
|
||||
// # Type Parameters
|
||||
//
|
||||
// - I: The input type
|
||||
// - A: The type of the validation result
|
||||
//
|
||||
// # Parameters
|
||||
//
|
||||
// - f: A Kleisli arrow that takes Errors and returns a Validate[I, A]. This function
|
||||
// is called only when validation fails, receiving the accumulated errors.
|
||||
//
|
||||
// # Returns
|
||||
//
|
||||
// An Operator[I, A, A] that transforms validators by providing alternative validation.
|
||||
//
|
||||
// # Example: Fallback Validation
|
||||
//
|
||||
// // Primary validator that may fail
|
||||
// validateFromConfig := func(key string) Reader[validation.Context, validation.Validation[string]] {
|
||||
// return func(ctx validation.Context) validation.Validation[string] {
|
||||
// // Try to get value from config
|
||||
// if value, ok := config[key]; ok {
|
||||
// return validation.Success(value)
|
||||
// }
|
||||
// return validation.FailureWithMessage[string](key, "not found in config")(ctx)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Use OrElse for semantic clarity - "try config, or else use environment"
|
||||
// withEnvFallback := OrElse(func(errs Errors) Validate[string, string] {
|
||||
// return func(key string) Reader[validation.Context, validation.Validation[string]] {
|
||||
// return func(ctx validation.Context) validation.Validation[string] {
|
||||
// if value := os.Getenv(key); value != "" {
|
||||
// return validation.Success(value)
|
||||
// }
|
||||
// return either.Left[string](errs) // propagate original errors
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
//
|
||||
// validator := withEnvFallback(validateFromConfig)
|
||||
// result := validator("DATABASE_URL")(nil)
|
||||
// // Tries config first, falls back to environment variable
|
||||
//
|
||||
// # Example: Default Value on Failure
|
||||
//
|
||||
// // Provide a default value when validation fails
|
||||
// withDefault := OrElse(func(errs Errors) Validate[int, int] {
|
||||
// return Of[int](0) // default to 0 on any failure
|
||||
// })
|
||||
//
|
||||
// validator := withDefault(someValidator)
|
||||
// result := validator(input)(nil)
|
||||
// // Always succeeds, using default value if validation fails
|
||||
//
|
||||
// # Example: Pipeline with Multiple Fallbacks
|
||||
//
|
||||
// // Build a validation pipeline with multiple fallback strategies
|
||||
// validator := F.Pipe2(
|
||||
// validateFromDatabase,
|
||||
// OrElse(func(errs Errors) Validate[string, Config] {
|
||||
// // Try cache as first fallback
|
||||
// return validateFromCache
|
||||
// }),
|
||||
// OrElse(func(errs Errors) Validate[string, Config] {
|
||||
// // Use default config as final fallback
|
||||
// return Of[string](defaultConfig)
|
||||
// }),
|
||||
// )
|
||||
// // Tries database, then cache, then default
|
||||
//
|
||||
// # Notes
|
||||
//
|
||||
// - Identical behavior to ChainLeft - they are aliases
|
||||
// - Errors are accumulated when transformations fail
|
||||
// - Success values pass through unchanged
|
||||
// - The handler has access to both errors and original input
|
||||
// - Choose OrElse for better readability when providing alternatives
|
||||
// - See ChainLeft documentation for detailed behavior and additional examples
|
||||
func OrElse[I, A any](f Kleisli[I, Errors, A]) Operator[I, A, A] {
|
||||
return ChainLeft(f)
|
||||
}
|
||||
|
||||
// MonadAp applies a validator containing a function to a validator containing a value.
|
||||
//
|
||||
// This is the applicative apply operation for Validate. It allows you to apply
|
||||
|
||||
@@ -849,3 +849,428 @@ func TestFunctorLaws(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainLeft tests the ChainLeft function
|
||||
func TestChainLeft(t *testing.T) {
|
||||
t.Run("transforms failures while preserving successes", func(t *testing.T) {
|
||||
// Create a failing validator
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "validation failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler that recovers from specific errors
|
||||
handler := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "validation failed" {
|
||||
return Of[int](0) // recover with default
|
||||
}
|
||||
}
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return E.Left[int](errs)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler(failingValidator)
|
||||
result := validator(-5)(nil)
|
||||
|
||||
assert.Equal(t, validation.Of(0), result, "Should recover from failure")
|
||||
})
|
||||
|
||||
t.Run("preserves success values unchanged", func(t *testing.T) {
|
||||
successValidator := Of[int](42)
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](input, "should not be called")(ctx)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler(successValidator)
|
||||
result := validator(100)(nil)
|
||||
|
||||
assert.Equal(t, validation.Of(42), result, "Success should pass through unchanged")
|
||||
})
|
||||
|
||||
t.Run("aggregates errors when transformation also fails", func(t *testing.T) {
|
||||
failingValidator := func(s string) Reader[validation.Context, validation.Validation[string]] {
|
||||
return func(ctx validation.Context) validation.Validation[string] {
|
||||
return validation.FailureWithMessage[string](s, "original error")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
handler := ChainLeft(func(errs Errors) Validate[string, string] {
|
||||
return func(input string) Reader[validation.Context, validation.Validation[string]] {
|
||||
return func(ctx validation.Context) validation.Validation[string] {
|
||||
return validation.FailureWithMessage[string](input, "additional error")(ctx)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler(failingValidator)
|
||||
result := validator("test")(nil)
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
_, errors := E.Unwrap(result)
|
||||
assert.Len(t, errors, 2, "Should aggregate both errors")
|
||||
|
||||
messages := make([]string, len(errors))
|
||||
for i, err := range errors {
|
||||
messages[i] = err.Messsage
|
||||
}
|
||||
assert.Contains(t, messages, "original error")
|
||||
assert.Contains(t, messages, "additional error")
|
||||
})
|
||||
|
||||
t.Run("adds context to errors", func(t *testing.T) {
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "invalid value")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
addContext := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return E.Left[int](validation.Errors{
|
||||
{
|
||||
Context: validation.Context{{Key: "user", Type: "User"}, {Key: "age", Type: "int"}},
|
||||
Messsage: "failed to validate user age",
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := addContext(failingValidator)
|
||||
result := validator(150)(nil)
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
_, errors := E.Unwrap(result)
|
||||
assert.Len(t, errors, 2, "Should have both original and context errors")
|
||||
})
|
||||
|
||||
t.Run("can be composed in pipeline", func(t *testing.T) {
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "error1")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
handler1 := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](input, "error2")(ctx)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
handler2 := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](input, "error3")(ctx)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler2(handler1(failingValidator))
|
||||
result := validator(42)(nil)
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
_, errors := E.Unwrap(result)
|
||||
assert.GreaterOrEqual(t, len(errors), 2, "Should accumulate errors through pipeline")
|
||||
})
|
||||
|
||||
t.Run("provides access to original input", func(t *testing.T) {
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler uses input to determine recovery strategy
|
||||
handler := ChainLeft(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
// Use input value to decide on recovery
|
||||
if input < 0 {
|
||||
return validation.Of(0)
|
||||
}
|
||||
if input > 100 {
|
||||
return validation.Of(100)
|
||||
}
|
||||
return E.Left[int](errs)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler(failingValidator)
|
||||
|
||||
result1 := validator(-10)(nil)
|
||||
assert.Equal(t, validation.Of(0), result1, "Should recover negative to 0")
|
||||
|
||||
result2 := validator(150)(nil)
|
||||
assert.Equal(t, validation.Of(100), result2, "Should recover large to 100")
|
||||
})
|
||||
|
||||
t.Run("works with different input and output types", func(t *testing.T) {
|
||||
// Validator that converts string to int
|
||||
parseValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](s, "parse failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Handler that provides default based on input string
|
||||
handler := ChainLeft(func(errs Errors) Validate[string, int] {
|
||||
return func(input string) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
if input == "default" {
|
||||
return validation.Of(42)
|
||||
}
|
||||
return E.Left[int](errs)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := handler(parseValidator)
|
||||
result := validator("default")(nil)
|
||||
|
||||
assert.Equal(t, validation.Of(42), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestOrElse tests the OrElse function
|
||||
func TestOrElse(t *testing.T) {
|
||||
t.Run("provides fallback for failing validation", func(t *testing.T) {
|
||||
// Primary validator that fails
|
||||
primaryValidator := func(s string) Reader[validation.Context, validation.Validation[string]] {
|
||||
return func(ctx validation.Context) validation.Validation[string] {
|
||||
return validation.FailureWithMessage[string](s, "not found")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Use OrElse to provide fallback
|
||||
withFallback := OrElse(func(errs Errors) Validate[string, string] {
|
||||
return Of[string]("default value")
|
||||
})
|
||||
|
||||
validator := withFallback(primaryValidator)
|
||||
result := validator("missing")(nil)
|
||||
|
||||
assert.Equal(t, validation.Of("default value"), result)
|
||||
})
|
||||
|
||||
t.Run("preserves success values unchanged", func(t *testing.T) {
|
||||
successValidator := Of[string]("success")
|
||||
|
||||
withFallback := OrElse(func(errs Errors) Validate[string, string] {
|
||||
return Of[string]("fallback")
|
||||
})
|
||||
|
||||
validator := withFallback(successValidator)
|
||||
result := validator("input")(nil)
|
||||
|
||||
assert.Equal(t, validation.Of("success"), result, "Should not use fallback for success")
|
||||
})
|
||||
|
||||
t.Run("aggregates errors when fallback also fails", func(t *testing.T) {
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "primary failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
withFallback := OrElse(func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](input, "fallback failed")(ctx)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := withFallback(failingValidator)
|
||||
result := validator(42)(nil)
|
||||
|
||||
assert.True(t, E.IsLeft(result))
|
||||
_, errors := E.Unwrap(result)
|
||||
assert.Len(t, errors, 2, "Should aggregate both errors")
|
||||
|
||||
messages := make([]string, len(errors))
|
||||
for i, err := range errors {
|
||||
messages[i] = err.Messsage
|
||||
}
|
||||
assert.Contains(t, messages, "primary failed")
|
||||
assert.Contains(t, messages, "fallback failed")
|
||||
})
|
||||
|
||||
t.Run("supports multiple fallback strategies", func(t *testing.T) {
|
||||
failingValidator := func(s string) Reader[validation.Context, validation.Validation[string]] {
|
||||
return func(ctx validation.Context) validation.Validation[string] {
|
||||
return validation.FailureWithMessage[string](s, "not in database")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// First fallback: try cache
|
||||
tryCache := OrElse(func(errs Errors) Validate[string, string] {
|
||||
return func(input string) Reader[validation.Context, validation.Validation[string]] {
|
||||
return func(ctx validation.Context) validation.Validation[string] {
|
||||
if input == "cached" {
|
||||
return validation.Of("from cache")
|
||||
}
|
||||
return E.Left[string](errs)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Second fallback: use default
|
||||
useDefault := OrElse(func(errs Errors) Validate[string, string] {
|
||||
return Of[string]("default")
|
||||
})
|
||||
|
||||
// Compose fallbacks
|
||||
validator := useDefault(tryCache(failingValidator))
|
||||
|
||||
// Test with cached value
|
||||
result1 := validator("cached")(nil)
|
||||
assert.Equal(t, validation.Of("from cache"), result1)
|
||||
|
||||
// Test with non-cached value (should use default)
|
||||
result2 := validator("other")(nil)
|
||||
assert.Equal(t, validation.Of("default"), result2)
|
||||
})
|
||||
|
||||
t.Run("provides input-dependent fallback", func(t *testing.T) {
|
||||
failingValidator := func(s string) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](s, "parse failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback with different defaults based on input
|
||||
smartFallback := OrElse(func(errs Errors) Validate[string, int] {
|
||||
return func(input string) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
// Provide context-aware defaults
|
||||
if input == "http" {
|
||||
return validation.Of(80)
|
||||
}
|
||||
if input == "https" {
|
||||
return validation.Of(443)
|
||||
}
|
||||
return validation.Of(8080)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
validator := smartFallback(failingValidator)
|
||||
|
||||
result1 := validator("http")(nil)
|
||||
assert.Equal(t, validation.Of(80), result1)
|
||||
|
||||
result2 := validator("https")(nil)
|
||||
assert.Equal(t, validation.Of(443), result2)
|
||||
|
||||
result3 := validator("other")(nil)
|
||||
assert.Equal(t, validation.Of(8080), result3)
|
||||
})
|
||||
|
||||
t.Run("is equivalent to ChainLeft", func(t *testing.T) {
|
||||
// Create identical handlers
|
||||
handler := func(errs Errors) Validate[int, int] {
|
||||
return func(input int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
if input < 0 {
|
||||
return validation.Of(0)
|
||||
}
|
||||
return E.Left[int](errs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
failingValidator := func(n int) Reader[validation.Context, validation.Validation[int]] {
|
||||
return func(ctx validation.Context) validation.Validation[int] {
|
||||
return validation.FailureWithMessage[int](n, "failed")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Apply with ChainLeft
|
||||
withChainLeft := ChainLeft(handler)(failingValidator)
|
||||
|
||||
// Apply with OrElse
|
||||
withOrElse := OrElse(handler)(failingValidator)
|
||||
|
||||
// Test with same inputs
|
||||
inputs := []int{-10, 0, 10, -5, 100}
|
||||
for _, input := range inputs {
|
||||
result1 := withChainLeft(input)(nil)
|
||||
result2 := withOrElse(input)(nil)
|
||||
|
||||
// Results should be identical
|
||||
assert.Equal(t, E.IsLeft(result1), E.IsLeft(result2))
|
||||
if E.IsRight(result1) {
|
||||
val1, _ := E.Unwrap(result1)
|
||||
val2, _ := E.Unwrap(result2)
|
||||
assert.Equal(t, val1, val2, "OrElse and ChainLeft should produce identical results")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("works in complex validation pipeline", func(t *testing.T) {
|
||||
type Config struct {
|
||||
Port int
|
||||
Host string
|
||||
}
|
||||
|
||||
// Validator that tries to parse config
|
||||
parseConfig := func(s string) Reader[validation.Context, validation.Validation[Config]] {
|
||||
return func(ctx validation.Context) validation.Validation[Config] {
|
||||
return validation.FailureWithMessage[Config](s, "invalid config")(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to environment variables
|
||||
tryEnv := OrElse(func(errs Errors) Validate[string, Config] {
|
||||
return func(input string) Reader[validation.Context, validation.Validation[Config]] {
|
||||
return func(ctx validation.Context) validation.Validation[Config] {
|
||||
// Simulate env var lookup
|
||||
if input == "from_env" {
|
||||
return validation.Of(Config{Port: 8080, Host: "localhost"})
|
||||
}
|
||||
return E.Left[Config](errs)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Final fallback to defaults
|
||||
useDefaults := OrElse(func(errs Errors) Validate[string, Config] {
|
||||
return Of[string](Config{Port: 3000, Host: "0.0.0.0"})
|
||||
})
|
||||
|
||||
// Build pipeline
|
||||
validator := useDefaults(tryEnv(parseConfig))
|
||||
|
||||
// Test with env fallback
|
||||
result1 := validator("from_env")(nil)
|
||||
assert.True(t, E.IsRight(result1))
|
||||
if E.IsRight(result1) {
|
||||
cfg, _ := E.Unwrap(result1)
|
||||
assert.Equal(t, 8080, cfg.Port)
|
||||
assert.Equal(t, "localhost", cfg.Host)
|
||||
}
|
||||
|
||||
// Test with default fallback
|
||||
result2 := validator("other")(nil)
|
||||
assert.True(t, E.IsRight(result2))
|
||||
if E.IsRight(result2) {
|
||||
cfg, _ := E.Unwrap(result2)
|
||||
assert.Equal(t, 3000, cfg.Port)
|
||||
assert.Equal(t, "0.0.0.0", cfg.Host)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
162
v2/optics/codec/validation/OrElse_explanation.md
Normal file
162
v2/optics/codec/validation/OrElse_explanation.md
Normal file
@@ -0,0 +1,162 @@
|
||||
# OrElse is Equivalent to ChainLeft
|
||||
|
||||
## Overview
|
||||
|
||||
In [`optics/codec/validation/monad.go`](monad.go:474-476), the [`OrElse`](monad.go:474) function is defined as a simple alias for [`ChainLeft`](monad.go:304):
|
||||
|
||||
```go
|
||||
//go:inline
|
||||
func OrElse[A any](f Kleisli[Errors, A]) Operator[A, A] {
|
||||
return ChainLeft(f)
|
||||
}
|
||||
```
|
||||
|
||||
This means **`OrElse` and `ChainLeft` are functionally identical** - they produce exactly the same results for all inputs.
|
||||
|
||||
## Why Have Both?
|
||||
|
||||
While they are technically the same, they serve different **semantic purposes**:
|
||||
|
||||
### ChainLeft - Technical Perspective
|
||||
[`ChainLeft`](monad.go:304-309) emphasizes the **technical operation**: it chains a computation on the Left (failure) channel of the Either/Validation monad. This name comes from category theory and functional programming terminology.
|
||||
|
||||
### OrElse - Semantic Perspective
|
||||
[`OrElse`](monad.go:474-476) emphasizes the **intent**: it provides an alternative or fallback when validation fails. The name reads naturally in code: "try this validation, **or else** try this alternative."
|
||||
|
||||
## Key Behavior
|
||||
|
||||
Both functions share the same critical behavior that distinguishes them from standard Either operations:
|
||||
|
||||
### Error Aggregation
|
||||
When the transformation function returns a failure, **both the original errors AND the new errors are combined** using the Errors monoid. This ensures no validation errors are lost.
|
||||
|
||||
```go
|
||||
// Example: Error aggregation
|
||||
result := OrElse(func(errs Errors) Validation[string] {
|
||||
return Failures[string](Errors{
|
||||
&ValidationError{Messsage: "additional error"},
|
||||
})
|
||||
})(Failures[string](Errors{
|
||||
&ValidationError{Messsage: "original error"},
|
||||
}))
|
||||
|
||||
// Result contains BOTH errors: ["original error", "additional error"]
|
||||
```
|
||||
|
||||
### Success Pass-Through
|
||||
Success values pass through unchanged - the function is never called:
|
||||
|
||||
```go
|
||||
result := OrElse(func(errs Errors) Validation[int] {
|
||||
return Failures[int](Errors{
|
||||
&ValidationError{Messsage: "never called"},
|
||||
})
|
||||
})(Success(42))
|
||||
|
||||
// Result: Success(42) - unchanged
|
||||
```
|
||||
|
||||
### Error Recovery
|
||||
The function can recover from failures by returning a Success:
|
||||
|
||||
```go
|
||||
recoverFromNotFound := OrElse(func(errs Errors) Validation[int] {
|
||||
for _, err := range errs {
|
||||
if err.Messsage == "not found" {
|
||||
return Success(0) // recover with default
|
||||
}
|
||||
}
|
||||
return Failures[int](errs)
|
||||
})
|
||||
|
||||
result := recoverFromNotFound(Failures[int](Errors{
|
||||
&ValidationError{Messsage: "not found"},
|
||||
}))
|
||||
|
||||
// Result: Success(0) - recovered from failure
|
||||
```
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Fallback Validation (OrElse reads better)
|
||||
```go
|
||||
validatePositive := func(x int) Validation[int] {
|
||||
if x > 0 {
|
||||
return Success(x)
|
||||
}
|
||||
return Failures[int](Errors{
|
||||
&ValidationError{Messsage: "must be positive"},
|
||||
})
|
||||
}
|
||||
|
||||
// Use OrElse for semantic clarity
|
||||
withDefault := OrElse(func(errs Errors) Validation[int] {
|
||||
return Success(1) // default to 1 if validation fails
|
||||
})
|
||||
|
||||
result := F.Pipe1(validatePositive(-5), withDefault)
|
||||
// Result: Success(1)
|
||||
```
|
||||
|
||||
### 2. Error Context Addition (ChainLeft reads better)
|
||||
```go
|
||||
addContext := ChainLeft(func(errs Errors) Validation[string] {
|
||||
return Failures[string](Errors{
|
||||
&ValidationError{
|
||||
Messsage: "validation failed in user.email field",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
result := F.Pipe1(
|
||||
Failures[string](Errors{
|
||||
&ValidationError{Messsage: "invalid format"},
|
||||
}),
|
||||
addContext,
|
||||
)
|
||||
// Result contains: ["invalid format", "validation failed in user.email field"]
|
||||
```
|
||||
|
||||
### 3. Pipeline Composition
|
||||
Both can be used in pipelines, with errors accumulating at each step:
|
||||
|
||||
```go
|
||||
result := F.Pipe2(
|
||||
Failures[int](Errors{
|
||||
&ValidationError{Messsage: "database error"},
|
||||
}),
|
||||
OrElse(func(errs Errors) Validation[int] {
|
||||
return Failures[int](Errors{
|
||||
&ValidationError{Messsage: "context added"},
|
||||
})
|
||||
}),
|
||||
OrElse(func(errs Errors) Validation[int] {
|
||||
return Failures[int](errs) // propagate
|
||||
}),
|
||||
)
|
||||
// Errors accumulate at each step in the pipeline
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
The test suite in [`monad_test.go`](monad_test.go:1698) includes comprehensive tests proving that `OrElse` and `ChainLeft` are equivalent:
|
||||
|
||||
- ✅ Identical behavior for Success values
|
||||
- ✅ Identical behavior for error recovery
|
||||
- ✅ Identical behavior for error aggregation
|
||||
- ✅ Identical behavior in pipeline composition
|
||||
- ✅ Identical behavior for multiple error scenarios
|
||||
|
||||
Run the tests:
|
||||
```bash
|
||||
go test -v -run TestOrElse ./optics/codec/validation
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
|
||||
**`OrElse` is exactly the same as `ChainLeft`** - they are aliases with identical implementations and behavior. The choice between them is purely about **code readability and semantic intent**:
|
||||
|
||||
- Use **`OrElse`** when emphasizing fallback/alternative validation logic
|
||||
- Use **`ChainLeft`** when emphasizing technical error channel transformation
|
||||
|
||||
Both maintain the critical validation property of **error aggregation**, ensuring all validation failures are preserved and reported together.
|
||||
318
v2/optics/codec/validation/bind.go
Normal file
318
v2/optics/codec/validation/bind.go
Normal file
@@ -0,0 +1,318 @@
|
||||
// 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 validation
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
A "github.com/IBM/fp-go/v2/internal/apply"
|
||||
C "github.com/IBM/fp-go/v2/internal/chain"
|
||||
F "github.com/IBM/fp-go/v2/internal/functor"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
)
|
||||
|
||||
// Do creates an empty context of type S to be used with the Bind operation.
|
||||
// This is the starting point for building up a context using do-notation style.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Result struct {
|
||||
// x int
|
||||
// y string
|
||||
// }
|
||||
// result := Do(Result{})
|
||||
func Do[S any](
|
||||
empty S,
|
||||
) Validation[S] {
|
||||
return Of(empty)
|
||||
}
|
||||
|
||||
// Bind attaches the result of a computation to a context S1 to produce a context S2.
|
||||
// This is used in do-notation style to sequentially build up a context.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{}),
|
||||
// Bind(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, func(s State) Validation[int] { return Success(42) }),
|
||||
// )
|
||||
func Bind[S1, S2, A any](
|
||||
setter func(A) func(S1) S2,
|
||||
f Kleisli[S1, A],
|
||||
) Operator[S1, S2] {
|
||||
return C.Bind(
|
||||
Chain[S1, S2],
|
||||
Map[A, S2],
|
||||
setter,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// 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, not an Option.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; computed int }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{x: 5}),
|
||||
// Let(func(c int) func(State) State {
|
||||
// return func(s State) State { s.computed = c; return s }
|
||||
// }, func(s State) int { return s.x * 2 }),
|
||||
// )
|
||||
func Let[S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
f func(S1) B,
|
||||
) Operator[S1, S2] {
|
||||
return F.Let(
|
||||
Map[S1, S2],
|
||||
key,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// LetTo attaches a constant value to a context S1 to produce a context S2.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; name string }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{x: 5}),
|
||||
// LetTo(func(n string) func(State) State {
|
||||
// return func(s State) State { s.name = n; return s }
|
||||
// }, "example"),
|
||||
// )
|
||||
func LetTo[S1, S2, B any](
|
||||
key func(B) func(S1) S2,
|
||||
b B,
|
||||
) Operator[S1, S2] {
|
||||
return F.LetTo(
|
||||
Map[S1, S2],
|
||||
key,
|
||||
b,
|
||||
)
|
||||
}
|
||||
|
||||
// BindTo initializes a new state S1 from a value T.
|
||||
// This is typically used as the first operation after creating a Validation value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { value int }
|
||||
// result := F.Pipe1(
|
||||
// Success(42),
|
||||
// BindTo(func(x int) State { return State{value: x} }),
|
||||
// )
|
||||
func BindTo[S1, T any](
|
||||
setter func(T) S1,
|
||||
) Operator[T, S1] {
|
||||
return C.BindTo(
|
||||
Map[T, S1],
|
||||
setter,
|
||||
)
|
||||
}
|
||||
|
||||
// ApS attaches a value to a context S1 to produce a context S2 by considering the context and the value concurrently.
|
||||
// This uses the applicative functor pattern, allowing parallel composition.
|
||||
//
|
||||
// IMPORTANT: Unlike Bind which fails fast, ApS aggregates ALL validation errors from both the context
|
||||
// and the value. If both validations fail, all errors are collected and returned together.
|
||||
// This is useful for validating multiple independent fields and reporting all errors at once.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type State struct { x int; y int }
|
||||
// result := F.Pipe2(
|
||||
// Do(State{}),
|
||||
// ApS(func(x int) func(State) State {
|
||||
// return func(s State) State { s.x = x; return s }
|
||||
// }, Success(42)),
|
||||
// )
|
||||
//
|
||||
// Error aggregation example:
|
||||
//
|
||||
// stateFailure := Failures[State](Errors{&ValidationError{Messsage: "state error"}})
|
||||
// valueFailure := Failures[int](Errors{&ValidationError{Messsage: "value error"}})
|
||||
// result := ApS(setter, valueFailure)(stateFailure)
|
||||
// // Result contains BOTH errors: ["state error", "value error"]
|
||||
func ApS[S1, S2, T any](
|
||||
setter func(T) func(S1) S2,
|
||||
fa Validation[T],
|
||||
) Operator[S1, S2] {
|
||||
return A.ApS(
|
||||
Ap[S2, T],
|
||||
Map[S1, func(T) S2],
|
||||
setter,
|
||||
fa,
|
||||
)
|
||||
}
|
||||
|
||||
// ApSL attaches a value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines ApS with a lens, allowing you to use
|
||||
// optics to update nested structures in a more composable way.
|
||||
//
|
||||
// IMPORTANT: Like ApS, this function aggregates ALL validation errors. If both the context
|
||||
// and the value fail validation, all errors are collected and returned together.
|
||||
// This enables comprehensive error reporting for complex nested structures.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// This eliminates the need to manually write setter functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Address struct {
|
||||
// Street string
|
||||
// City string
|
||||
// }
|
||||
//
|
||||
// type Person struct {
|
||||
// Name string
|
||||
// Address Address
|
||||
// }
|
||||
//
|
||||
// // Create a lens for the Address field
|
||||
// addressLens := lens.MakeLens(
|
||||
// func(p Person) Address { return p.Address },
|
||||
// func(p Person, a Address) Person { p.Address = a; return p },
|
||||
// )
|
||||
//
|
||||
// // Use ApSL to update the address
|
||||
// result := F.Pipe2(
|
||||
// Success(Person{Name: "Alice"}),
|
||||
// ApSL(
|
||||
// addressLens,
|
||||
// Success(Address{Street: "Main St", City: "NYC"}),
|
||||
// ),
|
||||
// )
|
||||
func ApSL[S, T any](
|
||||
lens L.Lens[S, T],
|
||||
fa Validation[T],
|
||||
) Operator[S, S] {
|
||||
return ApS(lens.Set, fa)
|
||||
}
|
||||
|
||||
// BindL attaches the result of a computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Bind with a lens, allowing you to use
|
||||
// optics to update nested structures based on their current values.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The computation function f receives the current value of the focused field and returns
|
||||
// a Validation that produces the new value.
|
||||
//
|
||||
// Unlike ApSL, BindL uses monadic sequencing, meaning the computation f can depend on
|
||||
// the current value of the focused field.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Increment the counter, but fail if it would exceed 100
|
||||
// increment := func(v int) Validation[int] {
|
||||
// if v >= 100 {
|
||||
// return Failures[int](Errors{&ValidationError{Messsage: "exceeds limit"}})
|
||||
// }
|
||||
// return Success(v + 1)
|
||||
// }
|
||||
//
|
||||
// result := F.Pipe1(
|
||||
// Success(Counter{Value: 42}),
|
||||
// BindL(valueLens, increment),
|
||||
// ) // Success(Counter{Value: 43})
|
||||
func BindL[S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Kleisli[T, T],
|
||||
) Operator[S, S] {
|
||||
return Bind(lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetL attaches the result of a pure computation to a context using a lens-based setter.
|
||||
// This is a convenience function that combines Let with a lens, allowing you to use
|
||||
// optics to update nested structures with pure transformations.
|
||||
//
|
||||
// The lens parameter provides both the getter and setter for a field within the structure S.
|
||||
// The transformation function f receives the current value of the focused field and returns
|
||||
// the new value directly (not wrapped in Validation).
|
||||
//
|
||||
// This is useful for pure transformations that cannot fail, such as mathematical operations,
|
||||
// string manipulations, or other deterministic updates.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Counter struct {
|
||||
// Value int
|
||||
// }
|
||||
//
|
||||
// valueLens := lens.MakeLens(
|
||||
// func(c Counter) int { return c.Value },
|
||||
// func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
// )
|
||||
//
|
||||
// // Double the counter value
|
||||
// double := func(v int) int { return v * 2 }
|
||||
//
|
||||
// result := F.Pipe1(
|
||||
// Success(Counter{Value: 21}),
|
||||
// LetL(valueLens, double),
|
||||
// ) // Success(Counter{Value: 42})
|
||||
func LetL[S, T any](
|
||||
lens L.Lens[S, T],
|
||||
f Endomorphism[T],
|
||||
) Operator[S, S] {
|
||||
return Let(lens.Set, function.Flow2(lens.Get, f))
|
||||
}
|
||||
|
||||
// LetToL attaches a constant value to a context using a lens-based setter.
|
||||
// This is a convenience function that combines LetTo with a lens, allowing you to use
|
||||
// optics to set nested fields to specific values.
|
||||
//
|
||||
// The lens parameter provides the setter for a field within the structure S.
|
||||
// Unlike LetL which transforms the current value, LetToL simply replaces it with
|
||||
// the provided constant value b.
|
||||
//
|
||||
// This is useful for resetting fields, initializing values, or setting fields to
|
||||
// predetermined constants.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// type Config struct {
|
||||
// Debug bool
|
||||
// Timeout int
|
||||
// }
|
||||
//
|
||||
// debugLens := lens.MakeLens(
|
||||
// func(c Config) bool { return c.Debug },
|
||||
// func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
// )
|
||||
//
|
||||
// result := F.Pipe1(
|
||||
// Success(Config{Debug: true, Timeout: 30}),
|
||||
// LetToL(debugLens, false),
|
||||
// ) // Success(Config{Debug: false, Timeout: 30})
|
||||
func LetToL[S, T any](
|
||||
lens L.Lens[S, T],
|
||||
b T,
|
||||
) Operator[S, S] {
|
||||
return LetTo(lens.Set, b)
|
||||
}
|
||||
540
v2/optics/codec/validation/bind_test.go
Normal file
540
v2/optics/codec/validation/bind_test.go
Normal file
@@ -0,0 +1,540 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
F "github.com/IBM/fp-go/v2/function"
|
||||
L "github.com/IBM/fp-go/v2/optics/lens"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDo(t *testing.T) {
|
||||
t.Run("creates successful validation with empty state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
result := Do(State{})
|
||||
|
||||
assert.Equal(t, Of(State{}), result)
|
||||
})
|
||||
|
||||
t.Run("creates successful validation with initialized state", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y string
|
||||
}
|
||||
initial := State{x: 42, y: "hello"}
|
||||
result := Do(initial)
|
||||
|
||||
assert.Equal(t, Of(initial), result)
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
intResult := Do(0)
|
||||
assert.Equal(t, Of(0), intResult)
|
||||
|
||||
strResult := Do("")
|
||||
assert.Equal(t, Of(""), strResult)
|
||||
|
||||
type Custom struct{ Value int }
|
||||
customResult := Do(Custom{Value: 100})
|
||||
assert.Equal(t, Of(Custom{Value: 100}), customResult)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBind(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("binds successful validation to state", func(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validation[int] { return Success(42) }),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validation[int] { return Success(10) }),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 42, y: 10}), result)
|
||||
})
|
||||
|
||||
t.Run("propagates failure", func(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validation[int] { return Success(42) }),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validation[int] {
|
||||
return Failures[int](Errors{&ValidationError{Messsage: "y failed"}})
|
||||
}),
|
||||
)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "y failed", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("can access previous state values", func(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(State{}),
|
||||
Bind(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) Validation[int] { return Success(10) }),
|
||||
Bind(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) Validation[int] {
|
||||
// y depends on x
|
||||
return Success(s.x * 2)
|
||||
}),
|
||||
)
|
||||
|
||||
assert.Equal(t, Success(State{x: 10, y: 20}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLet(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
computed int
|
||||
}
|
||||
|
||||
t.Run("attaches pure computation result to state", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
Do(State{x: 5}),
|
||||
Let(func(c int) func(State) State {
|
||||
return func(s State) State { s.computed = c; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 5, computed: 10}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := Failures[State](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := Let(func(c int) func(State) State {
|
||||
return func(s State) State { s.computed = c; return s }
|
||||
}, func(s State) int { return s.x * 2 })(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "error", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("chains multiple Let operations", func(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
z int
|
||||
}
|
||||
result := F.Pipe3(
|
||||
Do(State{x: 5}),
|
||||
Let(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, func(s State) int { return s.x * 2 }),
|
||||
Let(func(z int) func(State) State {
|
||||
return func(s State) State { s.z = z; return s }
|
||||
}, func(s State) int { return s.y + 10 }),
|
||||
Let(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, func(s State) int { return s.z * 3 }),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 60, y: 10, z: 20}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetTo(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
name string
|
||||
}
|
||||
|
||||
t.Run("attaches constant value to state", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
Do(State{x: 5}),
|
||||
LetTo(func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "example"),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 5, name: "example"}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := Failures[State](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := LetTo(func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "example")(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("sets multiple constant values", func(t *testing.T) {
|
||||
type State struct {
|
||||
name string
|
||||
version int
|
||||
active bool
|
||||
}
|
||||
result := F.Pipe3(
|
||||
Do(State{}),
|
||||
LetTo(func(n string) func(State) State {
|
||||
return func(s State) State { s.name = n; return s }
|
||||
}, "app"),
|
||||
LetTo(func(v int) func(State) State {
|
||||
return func(s State) State { s.version = v; return s }
|
||||
}, 2),
|
||||
LetTo(func(a bool) func(State) State {
|
||||
return func(s State) State { s.active = a; return s }
|
||||
}, true),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{name: "app", version: 2, active: true}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindTo(t *testing.T) {
|
||||
type State struct {
|
||||
value int
|
||||
}
|
||||
|
||||
t.Run("initializes state from value", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
Success(42),
|
||||
BindTo(func(x int) State { return State{value: x} }),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{value: 42}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := Failures[int](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := BindTo(func(x int) State { return State{value: x} })(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "error", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
type StringState struct {
|
||||
text string
|
||||
}
|
||||
result := F.Pipe1(
|
||||
Success("hello"),
|
||||
BindTo(func(s string) StringState { return StringState{text: s} }),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(StringState{text: "hello"}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApS(t *testing.T) {
|
||||
type State struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
t.Run("attaches value using applicative pattern", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
Do(State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Success(42)),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 42}), result)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors from both validations", func(t *testing.T) {
|
||||
stateFailure := Failures[State](Errors{&ValidationError{Messsage: "state error"}})
|
||||
valueFailure := Failures[int](Errors{&ValidationError{Messsage: "value error"}})
|
||||
|
||||
result := ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, valueFailure)(stateFailure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(State) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
messages := []string{errors[0].Messsage, errors[1].Messsage}
|
||||
assert.Contains(t, messages, "state error")
|
||||
assert.Contains(t, messages, "value error")
|
||||
})
|
||||
|
||||
t.Run("combines multiple ApS operations", func(t *testing.T) {
|
||||
result := F.Pipe2(
|
||||
Do(State{}),
|
||||
ApS(func(x int) func(State) State {
|
||||
return func(s State) State { s.x = x; return s }
|
||||
}, Success(10)),
|
||||
ApS(func(y int) func(State) State {
|
||||
return func(s State) State { s.y = y; return s }
|
||||
}, Success(20)),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(State{x: 10, y: 20}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestApSL(t *testing.T) {
|
||||
type Address struct {
|
||||
Street string
|
||||
City string
|
||||
}
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Address Address
|
||||
}
|
||||
|
||||
t.Run("updates nested structure using lens", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
result := F.Pipe1(
|
||||
Success(Person{Name: "Alice"}),
|
||||
ApSL(
|
||||
addressLens,
|
||||
Success(Address{Street: "Main St", City: "NYC"}),
|
||||
),
|
||||
)
|
||||
|
||||
expected := Person{
|
||||
Name: "Alice",
|
||||
Address: Address{Street: "Main St", City: "NYC"},
|
||||
}
|
||||
assert.Equal(t, Of(expected), result)
|
||||
})
|
||||
|
||||
t.Run("accumulates errors", func(t *testing.T) {
|
||||
addressLens := L.MakeLens(
|
||||
func(p Person) Address { return p.Address },
|
||||
func(p Person, a Address) Person { p.Address = a; return p },
|
||||
)
|
||||
|
||||
personFailure := Failures[Person](Errors{&ValidationError{Messsage: "person error"}})
|
||||
addressFailure := Failures[Address](Errors{&ValidationError{Messsage: "address error"}})
|
||||
|
||||
result := ApSL(addressLens, addressFailure)(personFailure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(Person) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("updates field based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Validation[int] {
|
||||
return Success(v + 1)
|
||||
}
|
||||
|
||||
result := F.Pipe1(
|
||||
Success(Counter{Value: 42}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(Counter{Value: 43}), result)
|
||||
})
|
||||
|
||||
t.Run("fails validation based on current value", func(t *testing.T) {
|
||||
increment := func(v int) Validation[int] {
|
||||
if v >= 100 {
|
||||
return Failures[int](Errors{&ValidationError{Messsage: "exceeds limit"}})
|
||||
}
|
||||
return Success(v + 1)
|
||||
}
|
||||
|
||||
result := F.Pipe1(
|
||||
Success(Counter{Value: 100}),
|
||||
BindL(valueLens, increment),
|
||||
)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
errors := either.MonadFold(result,
|
||||
F.Identity[Errors],
|
||||
func(Counter) Errors { return nil },
|
||||
)
|
||||
assert.Len(t, errors, 1)
|
||||
assert.Equal(t, "exceeds limit", errors[0].Messsage)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
increment := func(v int) Validation[int] {
|
||||
return Success(v + 1)
|
||||
}
|
||||
|
||||
failure := Failures[Counter](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := BindL(valueLens, increment)(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetL(t *testing.T) {
|
||||
type Counter struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
valueLens := L.MakeLens(
|
||||
func(c Counter) int { return c.Value },
|
||||
func(c Counter, v int) Counter { c.Value = v; return c },
|
||||
)
|
||||
|
||||
t.Run("transforms field with pure function", func(t *testing.T) {
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
result := F.Pipe1(
|
||||
Success(Counter{Value: 21}),
|
||||
LetL(valueLens, double),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(Counter{Value: 42}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
failure := Failures[Counter](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := LetL(valueLens, double)(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("chains multiple transformations", func(t *testing.T) {
|
||||
add10 := func(v int) int { return v + 10 }
|
||||
double := func(v int) int { return v * 2 }
|
||||
|
||||
result := F.Pipe2(
|
||||
Success(Counter{Value: 5}),
|
||||
LetL(valueLens, add10),
|
||||
LetL(valueLens, double),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(Counter{Value: 30}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLetToL(t *testing.T) {
|
||||
type Config struct {
|
||||
Debug bool
|
||||
Timeout int
|
||||
}
|
||||
|
||||
debugLens := L.MakeLens(
|
||||
func(c Config) bool { return c.Debug },
|
||||
func(c Config, d bool) Config { c.Debug = d; return c },
|
||||
)
|
||||
|
||||
t.Run("sets field to constant value", func(t *testing.T) {
|
||||
result := F.Pipe1(
|
||||
Success(Config{Debug: true, Timeout: 30}),
|
||||
LetToL(debugLens, false),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(Config{Debug: false, Timeout: 30}), result)
|
||||
})
|
||||
|
||||
t.Run("preserves failure", func(t *testing.T) {
|
||||
failure := Failures[Config](Errors{&ValidationError{Messsage: "error"}})
|
||||
result := LetToL(debugLens, false)(failure)
|
||||
|
||||
assert.True(t, either.IsLeft(result))
|
||||
})
|
||||
|
||||
t.Run("sets multiple fields", func(t *testing.T) {
|
||||
timeoutLens := L.MakeLens(
|
||||
func(c Config) int { return c.Timeout },
|
||||
func(c Config, t int) Config { c.Timeout = t; return c },
|
||||
)
|
||||
|
||||
result := F.Pipe2(
|
||||
Success(Config{Debug: true, Timeout: 30}),
|
||||
LetToL(debugLens, false),
|
||||
LetToL(timeoutLens, 60),
|
||||
)
|
||||
|
||||
assert.Equal(t, Of(Config{Debug: false, Timeout: 60}), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBindOperationsComposition(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
Age int
|
||||
Email string
|
||||
}
|
||||
|
||||
t.Run("combines Do, Bind, Let, and LetTo", func(t *testing.T) {
|
||||
result := F.Pipe4(
|
||||
Do(User{}),
|
||||
LetTo(func(n string) func(User) User {
|
||||
return func(u User) User { u.Name = n; return u }
|
||||
}, "Alice"),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Validation[int] {
|
||||
// Age validation
|
||||
if len(u.Name) > 0 {
|
||||
return Success(25)
|
||||
}
|
||||
return Failures[int](Errors{&ValidationError{Messsage: "name required"}})
|
||||
}),
|
||||
Let(func(e string) func(User) User {
|
||||
return func(u User) User { u.Email = e; return u }
|
||||
}, func(u User) string {
|
||||
// Derive email from name
|
||||
return u.Name + "@example.com"
|
||||
}),
|
||||
Bind(func(a int) func(User) User {
|
||||
return func(u User) User { u.Age = a; return u }
|
||||
}, func(u User) Validation[int] {
|
||||
// Validate age is positive
|
||||
if u.Age > 0 {
|
||||
return Success(u.Age)
|
||||
}
|
||||
return Failures[int](Errors{&ValidationError{Messsage: "age must be positive"}})
|
||||
}),
|
||||
)
|
||||
|
||||
expected := User{Name: "Alice", Age: 25, Email: "Alice@example.com"}
|
||||
assert.Equal(t, Of(expected), result)
|
||||
})
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
package validation
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/array"
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/function"
|
||||
"github.com/IBM/fp-go/v2/internal/applicative"
|
||||
)
|
||||
|
||||
var errorsMonoid = ErrorsMonoid()
|
||||
|
||||
// Of creates a successful validation result containing the given value.
|
||||
// This is the pure/return operation for the Validation monad.
|
||||
//
|
||||
@@ -28,37 +32,376 @@ func Of[A any](a A) Validation[A] {
|
||||
// return func(age int) User { return User{name, age} }
|
||||
// }))(validateName))(validateAge)
|
||||
func Ap[B, A any](fa Validation[A]) Operator[func(A) B, B] {
|
||||
return either.ApV[B, A](ErrorsMonoid())(fa)
|
||||
return either.ApV[B, A](errorsMonoid)(fa)
|
||||
}
|
||||
|
||||
// MonadAp applies a validation containing a function to a validation containing a value.
|
||||
// This is the applicative apply operation that **accumulates errors** from both validations.
|
||||
//
|
||||
// **Key behavior**: Unlike Either's MonadAp which fails fast (returns first error),
|
||||
// this validation-specific implementation **accumulates all errors** using the Errors monoid.
|
||||
// When both the function validation and value validation fail, all errors from both are combined.
|
||||
//
|
||||
// This error accumulation is the defining characteristic of the Validation applicative,
|
||||
// making it ideal for scenarios where you want to collect all validation failures at once
|
||||
// rather than stopping at the first error.
|
||||
//
|
||||
// Behavior:
|
||||
// - Both succeed: applies the function to the value → Success(result)
|
||||
// - Function fails, value succeeds: returns function's errors → Failure(func errors)
|
||||
// - Function succeeds, value fails: returns value's errors → Failure(value errors)
|
||||
// - Both fail: **combines all errors** → Failure(func errors + value errors)
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Form validation: collect all field errors at once
|
||||
// - Configuration validation: report all invalid settings together
|
||||
// - Data validation: accumulate all constraint violations
|
||||
// - Multi-field validation: validate independent fields in parallel
|
||||
//
|
||||
// Example - Both succeed:
|
||||
//
|
||||
// double := func(x int) int { return x * 2 }
|
||||
// result := MonadAp(Of(double), Of(21))
|
||||
// // Result: Success(42)
|
||||
//
|
||||
// Example - Error accumulation (key feature):
|
||||
//
|
||||
// funcValidation := Failures[func(int) int](Errors{
|
||||
// &ValidationError{Messsage: "function error"},
|
||||
// })
|
||||
// valueValidation := Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "value error"},
|
||||
// })
|
||||
// result := MonadAp(funcValidation, valueValidation)
|
||||
// // Result: Failure with BOTH errors: ["function error", "value error"]
|
||||
//
|
||||
// Example - Validating multiple fields:
|
||||
//
|
||||
// type User struct {
|
||||
// Name string
|
||||
// Age int
|
||||
// }
|
||||
//
|
||||
// makeUser := func(name string) func(int) User {
|
||||
// return func(age int) User { return User{name, age} }
|
||||
// }
|
||||
//
|
||||
// nameValidation := validateName("ab") // Fails: too short
|
||||
// ageValidation := validateAge(16) // Fails: too young
|
||||
//
|
||||
// // First apply name
|
||||
// step1 := MonadAp(Of(makeUser), nameValidation)
|
||||
// // Then apply age
|
||||
// result := MonadAp(step1, ageValidation)
|
||||
// // Result contains ALL validation errors from both fields
|
||||
func MonadAp[B, A any](fab Validation[func(A) B], fa Validation[A]) Validation[B] {
|
||||
return either.MonadApV[B, A](ErrorsMonoid())(fab, fa)
|
||||
return either.MonadApV[B, A](errorsMonoid)(fab, fa)
|
||||
}
|
||||
|
||||
// Map transforms the value inside a successful validation using the provided function.
|
||||
// If the validation is a failure, the errors are preserved unchanged.
|
||||
// This is the functor map operation for Validation.
|
||||
//
|
||||
// Example:
|
||||
// Map is used for transforming successful values without changing the validation context.
|
||||
// It's the most basic operation for working with validated values and forms the foundation
|
||||
// for more complex validation pipelines.
|
||||
//
|
||||
// Behavior:
|
||||
// - Success: applies function to value → Success(f(value))
|
||||
// - Failure: preserves errors unchanged → Failure(same errors)
|
||||
//
|
||||
// This is useful for:
|
||||
// - Type transformations: converting validated values to different types
|
||||
// - Value transformations: normalizing, formatting, or computing derived values
|
||||
// - Pipeline composition: chaining multiple transformations
|
||||
// - Preserving validation context: errors pass through unchanged
|
||||
//
|
||||
// Example - Transform successful value:
|
||||
//
|
||||
// doubled := Map(func(x int) int { return x * 2 })(Of(21))
|
||||
// // Result: Success(42)
|
||||
//
|
||||
// Example - Failure preserved:
|
||||
//
|
||||
// result := Map(func(x int) int { return x * 2 })(
|
||||
// Failures[int](Errors{&ValidationError{Messsage: "invalid"}}),
|
||||
// )
|
||||
// // Result: Failure with same error: ["invalid"]
|
||||
//
|
||||
// Example - Type transformation:
|
||||
//
|
||||
// toString := Map(func(x int) string { return fmt.Sprintf("%d", x) })
|
||||
// result := toString(Of(42))
|
||||
// // Result: Success("42")
|
||||
//
|
||||
// Example - Chaining transformations:
|
||||
//
|
||||
// result := F.Pipe3(
|
||||
// Of(5),
|
||||
// Map(func(x int) int { return x + 10 }), // 15
|
||||
// Map(func(x int) int { return x * 2 }), // 30
|
||||
// Map(func(x int) string { return fmt.Sprintf("%d", x) }), // "30"
|
||||
// )
|
||||
// // Result: Success("30")
|
||||
func Map[A, B any](f func(A) B) Operator[A, B] {
|
||||
return either.Map[Errors](f)
|
||||
}
|
||||
|
||||
// MonadMap transforms the value inside a successful validation using the provided function.
|
||||
// If the validation is a failure, the errors are preserved unchanged.
|
||||
// This is the non-curried version of [Map].
|
||||
//
|
||||
// MonadMap is useful when you have both the validation and the transformation function
|
||||
// available at the same time, rather than needing to create a reusable operator.
|
||||
//
|
||||
// Behavior:
|
||||
// - Success: applies function to value → Success(f(value))
|
||||
// - Failure: preserves errors unchanged → Failure(same errors)
|
||||
//
|
||||
// Example - Transform successful value:
|
||||
//
|
||||
// result := MonadMap(Of(21), func(x int) int { return x * 2 })
|
||||
// // Result: Success(42)
|
||||
//
|
||||
// Example - Failure preserved:
|
||||
//
|
||||
// result := MonadMap(
|
||||
// Failures[int](Errors{&ValidationError{Messsage: "invalid"}}),
|
||||
// func(x int) int { return x * 2 },
|
||||
// )
|
||||
// // Result: Failure with same error: ["invalid"]
|
||||
//
|
||||
// Example - Type transformation:
|
||||
//
|
||||
// result := MonadMap(Of(42), func(x int) string {
|
||||
// return fmt.Sprintf("Value: %d", x)
|
||||
// })
|
||||
// // Result: Success("Value: 42")
|
||||
//
|
||||
// Example - Computing derived values:
|
||||
//
|
||||
// type User struct { FirstName, LastName string }
|
||||
// result := MonadMap(
|
||||
// Of(User{"John", "Doe"}),
|
||||
// func(u User) string { return u.FirstName + " " + u.LastName },
|
||||
// )
|
||||
// // Result: Success("John Doe")
|
||||
func MonadMap[A, B any](fa Validation[A], f func(A) B) Validation[B] {
|
||||
return either.MonadMap(fa, f)
|
||||
}
|
||||
|
||||
// Chain is the curried version of [MonadChain].
|
||||
// Sequences two validation computations where the second depends on the first.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// validatePositive := func(x int) Validation[int] {
|
||||
// if x > 0 { return Success(x) }
|
||||
// return Failure("must be positive")
|
||||
// }
|
||||
// result := Chain(validatePositive)(Success(42)) // Success(42)
|
||||
func Chain[A, B any](f Kleisli[A, B]) Operator[A, B] {
|
||||
return either.Chain(f)
|
||||
}
|
||||
|
||||
// MonadChain sequences two validation computations where the second depends on the first.
|
||||
// If the first validation fails, returns the failure without executing the second.
|
||||
// This is the monadic bind operation for Validation.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// result := MonadChain(
|
||||
// Success(42),
|
||||
// func(x int) Validation[string] {
|
||||
// return Success(fmt.Sprintf("Value: %d", x))
|
||||
// },
|
||||
// ) // Success("Value: 42")
|
||||
func MonadChain[A, B any](fa Validation[A], f Kleisli[A, B]) Validation[B] {
|
||||
return either.MonadChain(fa, f)
|
||||
}
|
||||
|
||||
// chainErrors is an internal helper that chains error transformations while accumulating errors.
|
||||
// When the transformation function f returns a failure, it concatenates the original errors (e1)
|
||||
// with the new errors (e2) using the Errors monoid, ensuring all validation errors are preserved.
|
||||
func chainErrors[A any](f Kleisli[Errors, A]) func(Errors) Validation[A] {
|
||||
return func(e1 Errors) Validation[A] {
|
||||
return either.MonadFold(
|
||||
f(e1),
|
||||
function.Flow2(array.Concat(e1), either.Left[A]),
|
||||
Of[A],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ChainLeft is the curried version of [MonadChainLeft].
|
||||
// Returns a function that transforms validation failures while preserving successes.
|
||||
//
|
||||
// Unlike the standard Either ChainLeft which replaces errors, this validation-specific
|
||||
// implementation **aggregates errors** using the Errors monoid. When the transformation
|
||||
// function returns a failure, both the original errors and the new errors are combined,
|
||||
// ensuring no validation errors are lost.
|
||||
//
|
||||
// This is particularly useful for:
|
||||
// - Error recovery with fallback validation
|
||||
// - Adding contextual information to existing errors
|
||||
// - Transforming error types while preserving all error details
|
||||
// - Building error handling pipelines that accumulate failures
|
||||
//
|
||||
// Key behavior:
|
||||
// - Success values pass through unchanged
|
||||
// - When transforming failures, if the transformation also fails, **all errors are aggregated**
|
||||
// - If the transformation succeeds, it recovers from the original failure
|
||||
//
|
||||
// Example - Error recovery with aggregation:
|
||||
//
|
||||
// recoverFromNotFound := ChainLeft(func(errs Errors) Validation[int] {
|
||||
// // Check if this is a "not found" error
|
||||
// for _, err := range errs {
|
||||
// if err.Messsage == "not found" {
|
||||
// return Success(0) // recover with default
|
||||
// }
|
||||
// }
|
||||
// // Add context to existing errors
|
||||
// return Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "recovery failed"},
|
||||
// })
|
||||
// // Result will contain BOTH original errors AND "recovery failed"
|
||||
// })
|
||||
//
|
||||
// result := recoverFromNotFound(Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "database error"},
|
||||
// }))
|
||||
// // Result contains: ["database error", "recovery failed"]
|
||||
//
|
||||
// Example - Adding context to errors:
|
||||
//
|
||||
// addContext := ChainLeft(func(errs Errors) Validation[string] {
|
||||
// // Add contextual information
|
||||
// return Failures[string](Errors{
|
||||
// &ValidationError{
|
||||
// Messsage: "validation failed in user.email field",
|
||||
// },
|
||||
// })
|
||||
// // Original errors are preserved and new context is added
|
||||
// })
|
||||
//
|
||||
// result := F.Pipe1(
|
||||
// Failures[string](Errors{
|
||||
// &ValidationError{Messsage: "invalid format"},
|
||||
// }),
|
||||
// addContext,
|
||||
// )
|
||||
// // Result contains: ["invalid format", "validation failed in user.email field"]
|
||||
//
|
||||
// Example - Success values pass through:
|
||||
//
|
||||
// handler := ChainLeft(func(errs Errors) Validation[int] {
|
||||
// return Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "never called"},
|
||||
// })
|
||||
// })
|
||||
// result := handler(Success(42)) // Success(42) - unchanged
|
||||
func ChainLeft[A any](f Kleisli[Errors, A]) Operator[A, A] {
|
||||
return either.Fold(
|
||||
chainErrors(f),
|
||||
Of[A],
|
||||
)
|
||||
}
|
||||
|
||||
// MonadChainLeft sequences a computation on the failure (Left) channel of a Validation.
|
||||
// If the Validation is a failure, applies the function to transform or recover from the errors.
|
||||
// If the Validation is a success, returns the success value unchanged.
|
||||
//
|
||||
// **Critical difference from Either.MonadChainLeft**: This validation-specific implementation
|
||||
// **aggregates errors** using the Errors monoid. When the transformation function returns a
|
||||
// failure, both the original errors and the new errors are combined, ensuring comprehensive
|
||||
// error reporting.
|
||||
//
|
||||
// This is the dual of [MonadChain] - while Chain operates on success values, ChainLeft
|
||||
// operates on failure values. It's particularly useful for:
|
||||
// - Error recovery: converting specific errors into successful values
|
||||
// - Error enrichment: adding context or transforming error messages
|
||||
// - Fallback logic: providing alternative validations when the first fails
|
||||
// - Error aggregation: combining multiple validation failures
|
||||
//
|
||||
// The function parameter receives the collection of validation errors and must return
|
||||
// a new Validation[A]. This allows you to:
|
||||
// - Recover by returning Success(value)
|
||||
// - Transform errors by returning Failures(newErrors) - **original errors are preserved**
|
||||
// - Implement conditional error handling based on error content
|
||||
//
|
||||
// Example - Error recovery:
|
||||
//
|
||||
// result := MonadChainLeft(
|
||||
// Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "not found"},
|
||||
// }),
|
||||
// func(errs Errors) Validation[int] {
|
||||
// // Check if we can recover
|
||||
// for _, err := range errs {
|
||||
// if err.Messsage == "not found" {
|
||||
// return Success(0) // recover with default value
|
||||
// }
|
||||
// }
|
||||
// return Failures[int](errs) // propagate errors
|
||||
// },
|
||||
// ) // Success(0)
|
||||
//
|
||||
// Example - Error aggregation (key feature):
|
||||
//
|
||||
// result := MonadChainLeft(
|
||||
// Failures[string](Errors{
|
||||
// &ValidationError{Messsage: "error 1"},
|
||||
// &ValidationError{Messsage: "error 2"},
|
||||
// }),
|
||||
// func(errs Errors) Validation[string] {
|
||||
// // Transformation also fails
|
||||
// return Failures[string](Errors{
|
||||
// &ValidationError{Messsage: "error 3"},
|
||||
// })
|
||||
// },
|
||||
// )
|
||||
// // Result contains ALL errors: ["error 1", "error 2", "error 3"]
|
||||
// // This is different from Either.MonadChainLeft which would only keep "error 3"
|
||||
//
|
||||
// Example - Adding context to errors:
|
||||
//
|
||||
// result := MonadChainLeft(
|
||||
// Failures[int](Errors{
|
||||
// &ValidationError{Value: "abc", Messsage: "invalid number"},
|
||||
// }),
|
||||
// func(errs Errors) Validation[int] {
|
||||
// // Add contextual information
|
||||
// contextErrors := Errors{
|
||||
// &ValidationError{
|
||||
// Context: []ContextEntry{{Key: "user", Type: "User"}, {Key: "age", Type: "int"}},
|
||||
// Messsage: "failed to parse user age",
|
||||
// },
|
||||
// }
|
||||
// return Failures[int](contextErrors)
|
||||
// },
|
||||
// )
|
||||
// // Result contains both original error and context:
|
||||
// // ["invalid number", "failed to parse user age"]
|
||||
//
|
||||
// Example - Success values pass through:
|
||||
//
|
||||
// result := MonadChainLeft(
|
||||
// Success(42),
|
||||
// func(errs Errors) Validation[int] {
|
||||
// return Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "never called"},
|
||||
// })
|
||||
// },
|
||||
// ) // Success(42) - unchanged
|
||||
func MonadChainLeft[A any](fa Validation[A], f Kleisli[Errors, A]) Validation[A] {
|
||||
return either.MonadFold(
|
||||
fa,
|
||||
chainErrors(f),
|
||||
Of[A],
|
||||
)
|
||||
}
|
||||
|
||||
// Applicative creates an Applicative instance for Validation with error accumulation.
|
||||
//
|
||||
// This returns a lawful Applicative that accumulates validation errors using the Errors monoid.
|
||||
@@ -123,6 +466,11 @@ func MonadChain[A, B any](fa Validation[A], f Kleisli[A, B]) Validation[B] {
|
||||
// An Applicative instance with Of, Map, and Ap operations that accumulate errors
|
||||
func Applicative[A, B any]() applicative.Applicative[A, B, Validation[A], Validation[B], Validation[func(A) B]] {
|
||||
return either.ApplicativeV[Errors, A, B](
|
||||
ErrorsMonoid(),
|
||||
errorsMonoid,
|
||||
)
|
||||
}
|
||||
|
||||
//go:inline
|
||||
func OrElse[A any](f Kleisli[Errors, A]) Operator[A, A] {
|
||||
return ChainLeft(f)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -74,12 +74,7 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
t.Run("empty returns successful validation with empty string", func(t *testing.T) {
|
||||
empty := m.Empty()
|
||||
|
||||
assert.True(t, either.IsRight(empty))
|
||||
value := either.MonadFold(empty,
|
||||
func(Errors) string { return "ERROR" },
|
||||
F.Identity[string],
|
||||
)
|
||||
assert.Equal(t, "", value)
|
||||
assert.Equal(t, Success(""), empty)
|
||||
})
|
||||
|
||||
t.Run("concat combines successful validations", func(t *testing.T) {
|
||||
@@ -88,12 +83,7 @@ func TestApplicativeMonoid(t *testing.T) {
|
||||
|
||||
result := m.Concat(v1, v2)
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) string { return "" },
|
||||
F.Identity[string],
|
||||
)
|
||||
assert.Equal(t, "Hello World", value)
|
||||
assert.Equal(t, Success("Hello World"), result)
|
||||
})
|
||||
|
||||
t.Run("concat with failure returns failure", func(t *testing.T) {
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
// 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 validation
|
||||
|
||||
import (
|
||||
"github.com/IBM/fp-go/v2/either"
|
||||
"github.com/IBM/fp-go/v2/endomorphism"
|
||||
"github.com/IBM/fp-go/v2/monoid"
|
||||
"github.com/IBM/fp-go/v2/reader"
|
||||
"github.com/IBM/fp-go/v2/result"
|
||||
@@ -9,13 +25,35 @@ import (
|
||||
|
||||
type (
|
||||
// Result represents a computation that may succeed with a value of type A or fail with an error.
|
||||
// This is an alias for result.Result[A], which is Either[error, A].
|
||||
//
|
||||
// Used for converting validation results to standard Go error handling patterns.
|
||||
Result[A any] = result.Result[A]
|
||||
|
||||
// Either represents a value that can be one of two types: Left (error) or Right (success).
|
||||
// This is an alias for either.Either[E, A], a disjoint union type.
|
||||
//
|
||||
// In the validation context:
|
||||
// - Left[E]: Contains error information of type E
|
||||
// - Right[A]: Contains a successfully validated value of type A
|
||||
Either[E, A any] = either.Either[E, A]
|
||||
|
||||
// ContextEntry represents a single entry in the validation context path.
|
||||
// It tracks the location and type information during nested validation.
|
||||
// It tracks the location and type information during nested validation,
|
||||
// enabling precise error reporting with full path information.
|
||||
//
|
||||
// Fields:
|
||||
// - Key: The key or field name (e.g., "email", "address", "items[0]")
|
||||
// - Type: The expected type name (e.g., "string", "int", "User")
|
||||
// - Actual: The actual value being validated (for error reporting)
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// entry := ContextEntry{
|
||||
// Key: "user.email",
|
||||
// Type: "string",
|
||||
// Actual: 12345,
|
||||
// }
|
||||
ContextEntry struct {
|
||||
Key string // The key or field name (for objects/maps)
|
||||
Type string // The expected type name
|
||||
@@ -23,42 +61,200 @@ type (
|
||||
}
|
||||
|
||||
// Context is a stack of ContextEntry values representing the path through
|
||||
// nested structures during validation. Used to provide detailed error messages.
|
||||
// nested structures during validation. Used to provide detailed error messages
|
||||
// that show exactly where in a nested structure a validation failure occurred.
|
||||
//
|
||||
// The context builds up as validation descends into nested structures:
|
||||
// - [] - root level
|
||||
// - [{Key: "user"}] - inside user object
|
||||
// - [{Key: "user"}, {Key: "address"}] - inside user.address
|
||||
// - [{Key: "user"}, {Key: "address"}, {Key: "zipCode"}] - at user.address.zipCode
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// ctx := Context{
|
||||
// {Key: "user", Type: "User"},
|
||||
// {Key: "address", Type: "Address"},
|
||||
// {Key: "zipCode", Type: "string"},
|
||||
// }
|
||||
// // Represents path: user.address.zipCode
|
||||
Context = []ContextEntry
|
||||
|
||||
// ValidationError represents a single validation failure with context.
|
||||
// ValidationError represents a single validation failure with full context information.
|
||||
// It implements the error interface and provides detailed information about what failed,
|
||||
// where it failed, and why it failed.
|
||||
//
|
||||
// Fields:
|
||||
// - Value: The actual value that failed validation
|
||||
// - Context: The path to the value in nested structures (e.g., user.address.zipCode)
|
||||
// - Messsage: Human-readable error description
|
||||
// - Cause: Optional underlying error that caused the validation failure
|
||||
//
|
||||
// The ValidationError type implements:
|
||||
// - error interface: For standard Go error handling
|
||||
// - fmt.Formatter: For custom formatting with %v, %+v
|
||||
// - slog.LogValuer: For structured logging with slog
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// err := &ValidationError{
|
||||
// Value: "not-an-email",
|
||||
// Context: []ContextEntry{{Key: "user"}, {Key: "email"}},
|
||||
// Messsage: "invalid email format",
|
||||
// Cause: nil,
|
||||
// }
|
||||
// fmt.Printf("%v", err) // at user.email: invalid email format
|
||||
// fmt.Printf("%+v", err) // at user.email: invalid email format
|
||||
// // value: "not-an-email"
|
||||
ValidationError struct {
|
||||
Value any // The value that failed validation
|
||||
Context Context // The path to the value in nested structures
|
||||
Messsage string // Human-readable error message
|
||||
Cause error
|
||||
Cause error // Optional underlying error cause
|
||||
}
|
||||
|
||||
// Errors is a collection of validation errors.
|
||||
// This type is used to accumulate multiple validation failures,
|
||||
// allowing all errors to be reported at once rather than failing fast.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// errors := Errors{
|
||||
// &ValidationError{Value: "", Messsage: "name is required"},
|
||||
// &ValidationError{Value: "invalid", Messsage: "invalid email"},
|
||||
// &ValidationError{Value: -1, Messsage: "age must be positive"},
|
||||
// }
|
||||
Errors = []*ValidationError
|
||||
|
||||
// validationErrors wraps a collection of validation errors with an optional root cause.
|
||||
// It provides structured error information for validation failures.
|
||||
// It provides structured error information for validation failures and implements
|
||||
// the error interface for integration with standard Go error handling.
|
||||
//
|
||||
// This type is internal and created via MakeValidationErrors.
|
||||
// It implements:
|
||||
// - error interface: For standard Go error handling
|
||||
// - fmt.Formatter: For custom formatting with %v, %+v
|
||||
// - slog.LogValuer: For structured logging with slog
|
||||
//
|
||||
// Fields:
|
||||
// - errors: The collection of individual validation errors
|
||||
// - cause: Optional root cause error
|
||||
validationErrors struct {
|
||||
errors Errors
|
||||
cause error
|
||||
}
|
||||
|
||||
// Validation represents the result of a validation operation.
|
||||
// Left contains validation errors, Right contains the successfully validated value.
|
||||
// It's an Either type where:
|
||||
// - Left(Errors): Validation failed with one or more errors
|
||||
// - Right(A): Successfully validated value of type A
|
||||
//
|
||||
// This type supports applicative operations, allowing multiple validations
|
||||
// to be combined while accumulating all errors rather than failing fast.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Success case
|
||||
// valid := Success(42) // Right(42)
|
||||
//
|
||||
// // Failure case
|
||||
// invalid := Failures[int](Errors{
|
||||
// &ValidationError{Messsage: "must be positive"},
|
||||
// }) // Left([...])
|
||||
//
|
||||
// // Combining validations (accumulates all errors)
|
||||
// result := Ap(Ap(Of(func(x int) func(y int) int {
|
||||
// return func(y int) int { return x + y }
|
||||
// }))(validateX))(validateY)
|
||||
Validation[A any] = Either[Errors, A]
|
||||
|
||||
// Reader represents a computation that depends on an environment R and produces a value A.
|
||||
// This is an alias for reader.Reader[R, A], which is func(R) A.
|
||||
//
|
||||
// In the validation context, Reader is used for context-dependent validation operations
|
||||
// where the validation logic needs access to the current validation context path.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// validateWithContext := func(ctx Context) Validation[int] {
|
||||
// // Use ctx to provide detailed error messages
|
||||
// return Success(42)
|
||||
// }
|
||||
Reader[R, A any] = reader.Reader[R, A]
|
||||
|
||||
// Kleisli represents a function from A to a validated B.
|
||||
// It's a Reader that takes an input A and produces a Validation[B].
|
||||
// This is the fundamental building block for composable validation operations.
|
||||
//
|
||||
// Type: func(A) Validation[B]
|
||||
//
|
||||
// Kleisli arrows can be composed using Chain/Bind operations to build
|
||||
// complex validation pipelines from simple validation functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// validatePositive := func(x int) Validation[int] {
|
||||
// if x > 0 {
|
||||
// return Success(x)
|
||||
// }
|
||||
// return Failures[int](/* error */)
|
||||
// }
|
||||
//
|
||||
// validateEven := func(x int) Validation[int] {
|
||||
// if x%2 == 0 {
|
||||
// return Success(x)
|
||||
// }
|
||||
// return Failures[int](/* error */)
|
||||
// }
|
||||
//
|
||||
// // Compose validations
|
||||
// validatePositiveEven := Chain(validateEven)(Success(42))
|
||||
Kleisli[A, B any] = Reader[A, Validation[B]]
|
||||
|
||||
// Operator represents a validation transformation that takes a validated A and produces a validated B.
|
||||
// It's a specialized Kleisli arrow for composing validation operations.
|
||||
// It's a specialized Kleisli arrow for composing validation operations where the input
|
||||
// is already a Validation[A].
|
||||
//
|
||||
// Type: func(Validation[A]) Validation[B]
|
||||
//
|
||||
// Operators are used to transform and compose validation results, enabling
|
||||
// functional composition of validation pipelines.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// // Transform a validated int to a validated string
|
||||
// intToString := Map(func(x int) string {
|
||||
// return strconv.Itoa(x)
|
||||
// }) // Operator[int, string]
|
||||
//
|
||||
// result := intToString(Success(42)) // Success("42")
|
||||
Operator[A, B any] = Kleisli[Validation[A], B]
|
||||
|
||||
// Monoid represents an algebraic structure with an associative binary operation and an identity element.
|
||||
// This is an alias for monoid.Monoid[A].
|
||||
//
|
||||
// In the validation context, monoids are used to combine validation results:
|
||||
// - ApplicativeMonoid: Combines successful validations using the monoid operation
|
||||
// - AlternativeMonoid: Provides fallback behavior for failed validations
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// import N "github.com/IBM/fp-go/v2/number"
|
||||
//
|
||||
// intAdd := N.MonoidSum[int]()
|
||||
// m := ApplicativeMonoid(intAdd)
|
||||
// result := m.Concat(Success(5), Success(3)) // Success(8)
|
||||
Monoid[A any] = monoid.Monoid[A]
|
||||
|
||||
// Endomorphism represents a function from a type to itself: func(A) A.
|
||||
// This is an alias for endomorphism.Endomorphism[A].
|
||||
//
|
||||
// In the validation context, endomorphisms are used with LetL to transform
|
||||
// values within a validation context using pure functions.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// double := func(x int) int { return x * 2 } // Endomorphism[int]
|
||||
// result := LetL(lens, double)(Success(21)) // Success(42)
|
||||
Endomorphism[A any] = endomorphism.Endomorphism[A]
|
||||
)
|
||||
|
||||
@@ -186,12 +186,7 @@ func TestSuccess(t *testing.T) {
|
||||
t.Run("creates right either with value", func(t *testing.T) {
|
||||
result := Success(42)
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(Errors) int { return 0 },
|
||||
F.Identity[int],
|
||||
)
|
||||
assert.Equal(t, 42, value)
|
||||
assert.Equal(t, Success(42), result)
|
||||
})
|
||||
|
||||
t.Run("works with different types", func(t *testing.T) {
|
||||
@@ -327,7 +322,7 @@ func TestValidationIntegration(t *testing.T) {
|
||||
&ValidationError{Value: "bad", Messsage: "error"},
|
||||
})
|
||||
|
||||
assert.True(t, either.IsRight(success))
|
||||
assert.Equal(t, Success(42), success)
|
||||
assert.True(t, either.IsLeft(failure))
|
||||
})
|
||||
|
||||
@@ -512,12 +507,7 @@ func TestToResult(t *testing.T) {
|
||||
|
||||
result := ToResult(validation)
|
||||
|
||||
assert.True(t, either.IsRight(result))
|
||||
value := either.MonadFold(result,
|
||||
func(error) int { return 0 },
|
||||
F.Identity[int],
|
||||
)
|
||||
assert.Equal(t, 42, value)
|
||||
assert.Equal(t, either.Of[error](42), result)
|
||||
})
|
||||
|
||||
t.Run("converts failed validation to result with error", func(t *testing.T) {
|
||||
@@ -569,23 +559,18 @@ func TestToResult(t *testing.T) {
|
||||
// String type
|
||||
strValidation := Success("hello")
|
||||
strResult := ToResult(strValidation)
|
||||
assert.True(t, either.IsRight(strResult))
|
||||
assert.Equal(t, either.Of[error]("hello"), strResult)
|
||||
|
||||
// Bool type
|
||||
boolValidation := Success(true)
|
||||
boolResult := ToResult(boolValidation)
|
||||
assert.True(t, either.IsRight(boolResult))
|
||||
assert.Equal(t, either.Of[error](true), boolResult)
|
||||
|
||||
// Struct type
|
||||
type User struct{ Name string }
|
||||
userValidation := Success(User{Name: "Alice"})
|
||||
userResult := ToResult(userValidation)
|
||||
assert.True(t, either.IsRight(userResult))
|
||||
user := either.MonadFold(userResult,
|
||||
func(error) User { return User{} },
|
||||
F.Identity[User],
|
||||
)
|
||||
assert.Equal(t, "Alice", user.Name)
|
||||
assert.Equal(t, either.Of[error](User{Name: "Alice"}), userResult)
|
||||
})
|
||||
|
||||
t.Run("preserves error context in result", func(t *testing.T) {
|
||||
|
||||
@@ -1104,6 +1104,8 @@ func After[R, E, A any](timestamp time.Time) Operator[R, E, A, A] {
|
||||
// If the ReaderIOEither is Left, it applies the provided function to the error value,
|
||||
// which returns a new ReaderIOEither 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 IO computations
|
||||
// that need access to configuration or dependencies. The error type can be widened from E1 to E2.
|
||||
//
|
||||
|
||||
@@ -531,3 +531,205 @@ func TestReadIO(t *testing.T) {
|
||||
assert.Equal(t, E.Right[error](25), result)
|
||||
})
|
||||
}
|
||||
|
||||
// TestChainLeftIdenticalToOrElse proves that ChainLeft and OrElse are identical functions.
|
||||
// This test verifies that both functions produce the same results for all scenarios with reader context:
|
||||
// - Left values with error recovery using reader context
|
||||
// - Left values with error transformation
|
||||
// - Right values passing through unchanged
|
||||
// - Error type widening
|
||||
func TestChainLeftIdenticalToOrElse(t *testing.T) {
|
||||
type Config struct {
|
||||
fallbackValue int
|
||||
retryEnabled bool
|
||||
}
|
||||
|
||||
// Test 1: Left value with error recovery using reader context
|
||||
t.Run("Left value recovery with reader - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
recoveryFn := func(e string) ReaderIOEither[Config, string, int] {
|
||||
if e == "recoverable" {
|
||||
return func(cfg Config) IOE.IOEither[string, int] {
|
||||
return IOE.Right[string](cfg.fallbackValue)
|
||||
}
|
||||
}
|
||||
return Left[Config, int](e)
|
||||
}
|
||||
|
||||
input := Left[Config, int]("recoverable")
|
||||
cfg := Config{fallbackValue: 42, retryEnabled: true}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(recoveryFn)(input)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(recoveryFn)(input)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](42), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 2: Left value with error transformation
|
||||
t.Run("Left value transformation - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
transformFn := func(e string) ReaderIOEither[Config, string, int] {
|
||||
return Left[Config, int]("transformed: " + e)
|
||||
}
|
||||
|
||||
input := Left[Config, int]("original error")
|
||||
cfg := Config{fallbackValue: 0, retryEnabled: false}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(transformFn)(input)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(transformFn)(input)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Left[int]("transformed: original error"), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 3: Right value - both should pass through unchanged
|
||||
t.Run("Right value passthrough - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
handlerFn := func(e string) ReaderIOEither[Config, string, int] {
|
||||
return Left[Config, int]("should not be called")
|
||||
}
|
||||
|
||||
input := Right[Config, string](100)
|
||||
cfg := Config{fallbackValue: 0, retryEnabled: false}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(handlerFn)(input)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(handlerFn)(input)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](100), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 4: Error type widening
|
||||
t.Run("Error type widening - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
widenFn := func(e string) ReaderIOEither[Config, int, int] {
|
||||
return Left[Config, int](404)
|
||||
}
|
||||
|
||||
input := Left[Config, int]("not found")
|
||||
cfg := Config{fallbackValue: 0, retryEnabled: false}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(widenFn)(input)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(widenFn)(input)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Left[int](404), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 5: Composition in pipeline with reader context
|
||||
t.Run("Pipeline composition with reader - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
recoveryFn := func(e string) ReaderIOEither[Config, string, int] {
|
||||
if e == "network error" {
|
||||
return func(cfg Config) IOE.IOEither[string, int] {
|
||||
if cfg.retryEnabled {
|
||||
return IOE.Right[string](cfg.fallbackValue)
|
||||
}
|
||||
return IOE.Left[int]("retry disabled")
|
||||
}
|
||||
}
|
||||
return Left[Config, int](e)
|
||||
}
|
||||
|
||||
input := Left[Config, int]("network error")
|
||||
cfg := Config{fallbackValue: 99, retryEnabled: true}
|
||||
|
||||
// Using ChainLeft in pipeline
|
||||
resultChainLeft := F.Pipe1(input, ChainLeft(recoveryFn))(cfg)()
|
||||
|
||||
// Using OrElse in pipeline
|
||||
resultOrElse := F.Pipe1(input, OrElse(recoveryFn))(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](99), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 6: Multiple chained operations with reader context
|
||||
t.Run("Multiple operations with reader - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
handler1 := func(e string) ReaderIOEither[Config, string, int] {
|
||||
if e == "error1" {
|
||||
return Right[Config, string](1)
|
||||
}
|
||||
return Left[Config, int](e)
|
||||
}
|
||||
|
||||
handler2 := func(e string) ReaderIOEither[Config, string, int] {
|
||||
if e == "error2" {
|
||||
return func(cfg Config) IOE.IOEither[string, int] {
|
||||
return IOE.Right[string](cfg.fallbackValue)
|
||||
}
|
||||
}
|
||||
return Left[Config, int](e)
|
||||
}
|
||||
|
||||
input := Left[Config, int]("error2")
|
||||
cfg := Config{fallbackValue: 2, retryEnabled: false}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := F.Pipe2(
|
||||
input,
|
||||
ChainLeft(handler1),
|
||||
ChainLeft(handler2),
|
||||
)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := F.Pipe2(
|
||||
input,
|
||||
OrElse(handler1),
|
||||
OrElse(handler2),
|
||||
)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](2), resultChainLeft)
|
||||
})
|
||||
|
||||
// Test 7: Reader context is properly threaded through both functions
|
||||
t.Run("Reader context threading - ChainLeft equals OrElse", func(t *testing.T) {
|
||||
var chainLeftCfg, orElseCfg *Config
|
||||
|
||||
recoveryFn := func(e string) ReaderIOEither[Config, string, int] {
|
||||
return func(cfg Config) IOE.IOEither[string, int] {
|
||||
// Capture the config to verify it's passed correctly
|
||||
if chainLeftCfg == nil {
|
||||
chainLeftCfg = &cfg
|
||||
} else {
|
||||
orElseCfg = &cfg
|
||||
}
|
||||
return IOE.Right[string](cfg.fallbackValue)
|
||||
}
|
||||
}
|
||||
|
||||
input := Left[Config, int]("error")
|
||||
cfg := Config{fallbackValue: 123, retryEnabled: true}
|
||||
|
||||
// Using ChainLeft
|
||||
resultChainLeft := ChainLeft(recoveryFn)(input)(cfg)()
|
||||
|
||||
// Using OrElse
|
||||
resultOrElse := OrElse(recoveryFn)(input)(cfg)()
|
||||
|
||||
// Both should produce identical results
|
||||
assert.Equal(t, resultOrElse, resultChainLeft)
|
||||
assert.Equal(t, E.Right[string](123), resultChainLeft)
|
||||
|
||||
// Verify both received the same config
|
||||
assert.NotNil(t, chainLeftCfg)
|
||||
assert.NotNil(t, orElseCfg)
|
||||
assert.Equal(t, *chainLeftCfg, *orElseCfg)
|
||||
assert.Equal(t, cfg, *chainLeftCfg)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -475,6 +475,8 @@ func Alt[A any](that Lazy[Result[A]]) Operator[A, A] {
|
||||
// If the Result is Left, it applies the provided function to the error value,
|
||||
// which returns a new Result 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.
|
||||
//
|
||||
// Example:
|
||||
@@ -656,3 +658,118 @@ func InstanceOf[A any](a any) Result[A] {
|
||||
}
|
||||
return Left[A](fmt.Errorf("expected %T, got %T", res, a))
|
||||
}
|
||||
|
||||
// MonadChainLeft sequences a computation on the Left (error) channel.
|
||||
// If the Result is Left, applies the function to transform or recover from the error.
|
||||
// If the Result is Right, returns the Right value unchanged.
|
||||
//
|
||||
// This is the dual of [MonadChain] - while Chain operates on Right values,
|
||||
// ChainLeft operates on Left (error) values. It's particularly useful for:
|
||||
// - Error recovery: converting specific errors into successful values
|
||||
// - Error transformation: changing error types or adding context
|
||||
// - Fallback logic: providing alternative computations when errors occur
|
||||
//
|
||||
// Note: MonadChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// The function parameter receives the error value and must return a new Result[A].
|
||||
// This allows you to:
|
||||
// - Recover by returning Right[error](value)
|
||||
// - Transform the error by returning Left[A](newError)
|
||||
// - Implement conditional error handling based on error content
|
||||
//
|
||||
// Example - Error recovery:
|
||||
//
|
||||
// result := result.MonadChainLeft(
|
||||
// result.Left[int](errors.New("not found")),
|
||||
// func(err error) result.Result[int] {
|
||||
// if err.Error() == "not found" {
|
||||
// return result.Right(0) // recover with default value
|
||||
// }
|
||||
// return result.Left[int](err) // propagate other errors
|
||||
// },
|
||||
// ) // Right(0)
|
||||
//
|
||||
// Example - Error type transformation:
|
||||
//
|
||||
// result := result.MonadChainLeft(
|
||||
// result.Left[string](errors.New("database error")),
|
||||
// func(err error) result.Result[string] {
|
||||
// return result.Left[string](fmt.Errorf("wrapped: %w", err))
|
||||
// },
|
||||
// ) // Left(wrapped error)
|
||||
//
|
||||
// Example - Right values pass through:
|
||||
//
|
||||
// result := result.MonadChainLeft(
|
||||
// result.Right(42),
|
||||
// func(err error) result.Result[int] {
|
||||
// return result.Right(0) // never called
|
||||
// },
|
||||
// ) // Right(42) - unchanged
|
||||
//
|
||||
//go:inline
|
||||
func MonadChainLeft[A any](fa Result[A], f Kleisli[error, A]) Result[A] {
|
||||
return either.MonadChainLeft(fa, f)
|
||||
}
|
||||
|
||||
// ChainLeft is the curried version of [MonadChainLeft].
|
||||
// Returns a function that transforms Left (error) values while preserving Right values.
|
||||
//
|
||||
// Note: ChainLeft is identical to [OrElse] - both provide the same functionality for error recovery.
|
||||
//
|
||||
// This curried form is particularly useful in functional pipelines and for creating
|
||||
// reusable error handlers that can be composed with other operations.
|
||||
//
|
||||
// The returned function can be used with [F.Pipe1], [F.Pipe2], etc., to build
|
||||
// complex error handling pipelines in a point-free style.
|
||||
//
|
||||
// Example - Creating reusable error handlers:
|
||||
//
|
||||
// // Handler that recovers from "not found" errors
|
||||
// recoverNotFound := result.ChainLeft(func(err error) result.Result[int] {
|
||||
// if err.Error() == "not found" {
|
||||
// return result.Right(0)
|
||||
// }
|
||||
// return result.Left[int](err)
|
||||
// })
|
||||
//
|
||||
// result1 := recoverNotFound(result.Left[int](errors.New("not found"))) // Right(0)
|
||||
// result2 := recoverNotFound(result.Right(42)) // Right(42)
|
||||
//
|
||||
// Example - Using in pipelines:
|
||||
//
|
||||
// result := F.Pipe2(
|
||||
// result.Left[int](errors.New("timeout")),
|
||||
// result.ChainLeft(func(err error) result.Result[int] {
|
||||
// if err.Error() == "timeout" {
|
||||
// return result.Right(999) // fallback value
|
||||
// }
|
||||
// return result.Left[int](err)
|
||||
// }),
|
||||
// result.Map(func(n int) string {
|
||||
// return fmt.Sprintf("Value: %d", n)
|
||||
// }),
|
||||
// ) // Right("Value: 999")
|
||||
//
|
||||
// Example - Composing multiple error handlers:
|
||||
//
|
||||
// // First handler: convert error to string
|
||||
// toStringError := result.ChainLeft(func(err error) result.Result[int] {
|
||||
// return result.Left[int](errors.New(err.Error()))
|
||||
// })
|
||||
//
|
||||
// // Second handler: add prefix
|
||||
// addPrefix := result.ChainLeft(func(err error) result.Result[int] {
|
||||
// return result.Left[int](fmt.Errorf("Error: %w", err))
|
||||
// })
|
||||
//
|
||||
// result := F.Pipe2(
|
||||
// result.Left[int](errors.New("failed")),
|
||||
// toStringError,
|
||||
// addPrefix,
|
||||
// ) // Left(Error: failed)
|
||||
//
|
||||
//go:inline
|
||||
func ChainLeft[A any](f Kleisli[error, A]) Operator[A, A] {
|
||||
return either.ChainLeft(f)
|
||||
}
|
||||
|
||||
@@ -356,3 +356,296 @@ func TestInstanceOf(t *testing.T) {
|
||||
assert.Equal(t, 2, v["b"])
|
||||
})
|
||||
}
|
||||
|
||||
// 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) Result[int] {
|
||||
if err.Error() == "not found" {
|
||||
return Right(0) // default value
|
||||
}
|
||||
return Left[int](err)
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of(0), result)
|
||||
})
|
||||
|
||||
t.Run("Left value error is transformed", func(t *testing.T) {
|
||||
// Transform error with additional context
|
||||
result := MonadChainLeft(
|
||||
Left[int](errors.New("database error")),
|
||||
func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("wrapped: %w", err))
|
||||
},
|
||||
)
|
||||
assert.True(t, IsLeft(result))
|
||||
_, err := UnwrapError(result)
|
||||
assert.Contains(t, err.Error(), "wrapped:")
|
||||
assert.Contains(t, err.Error(), "database error")
|
||||
})
|
||||
|
||||
t.Run("Right value passes through unchanged", func(t *testing.T) {
|
||||
// Right value should not be affected
|
||||
result := MonadChainLeft(
|
||||
Right(42),
|
||||
func(err error) Result[int] {
|
||||
return Left[int](errors.New("should not be called"))
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of(42), result)
|
||||
})
|
||||
|
||||
t.Run("Chain multiple error transformations", func(t *testing.T) {
|
||||
// First transformation
|
||||
step1 := MonadChainLeft(
|
||||
Left[int](errors.New("error1")),
|
||||
func(err error) Result[int] {
|
||||
return Left[int](errors.New("error2"))
|
||||
},
|
||||
)
|
||||
// Second transformation
|
||||
step2 := MonadChainLeft(
|
||||
step1,
|
||||
func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("final: %s", err.Error()))
|
||||
},
|
||||
)
|
||||
assert.True(t, IsLeft(step2))
|
||||
_, err := UnwrapError(step2)
|
||||
assert.Equal(t, "final: error2", err.Error())
|
||||
})
|
||||
|
||||
t.Run("Error recovery with fallback", func(t *testing.T) {
|
||||
// Recover from specific errors
|
||||
result := MonadChainLeft(
|
||||
Left[int](errors.New("timeout")),
|
||||
func(err error) Result[int] {
|
||||
if err.Error() == "timeout" {
|
||||
return Right(999) // fallback value
|
||||
}
|
||||
return Left[int](err)
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of(999), result)
|
||||
})
|
||||
|
||||
t.Run("Conditional error handling", func(t *testing.T) {
|
||||
// Handle different error types differently
|
||||
handleError := func(err error) Result[string] {
|
||||
switch err.Error() {
|
||||
case "not found":
|
||||
return Right("default")
|
||||
case "timeout":
|
||||
return Right("retry")
|
||||
default:
|
||||
return Left[string](err)
|
||||
}
|
||||
}
|
||||
|
||||
result1 := MonadChainLeft(Left[string](errors.New("not found")), handleError)
|
||||
assert.Equal(t, Of("default"), result1)
|
||||
|
||||
result2 := MonadChainLeft(Left[string](errors.New("timeout")), handleError)
|
||||
assert.Equal(t, Of("retry"), result2)
|
||||
|
||||
result3 := MonadChainLeft(Left[string](errors.New("other")), handleError)
|
||||
assert.True(t, IsLeft(result3))
|
||||
})
|
||||
|
||||
t.Run("Type preservation", func(t *testing.T) {
|
||||
// Ensure type is preserved through transformation
|
||||
result := MonadChainLeft(
|
||||
Left[string](errors.New("error")),
|
||||
func(err error) Result[string] {
|
||||
return Right("recovered")
|
||||
},
|
||||
)
|
||||
assert.Equal(t, Of("recovered"), result)
|
||||
})
|
||||
}
|
||||
|
||||
// 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) Result[int] {
|
||||
if err.Error() == "not found" {
|
||||
return Right(0)
|
||||
}
|
||||
return Left[int](err)
|
||||
})
|
||||
|
||||
result := handleNotFound(Left[int](errors.New("not found")))
|
||||
assert.Equal(t, Of(0), result)
|
||||
})
|
||||
|
||||
t.Run("Curried function with Right value", func(t *testing.T) {
|
||||
handler := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](errors.New("should not be called"))
|
||||
})
|
||||
|
||||
result := handler(Right(42))
|
||||
assert.Equal(t, Of(42), result)
|
||||
})
|
||||
|
||||
t.Run("Use in pipeline with Pipe", func(t *testing.T) {
|
||||
// Create error transformer
|
||||
wrapError := ChainLeft(func(err error) Result[string] {
|
||||
return Left[string](fmt.Errorf("Error: %w", err))
|
||||
})
|
||||
|
||||
result := F.Pipe1(
|
||||
Left[string](errors.New("failed")),
|
||||
wrapError,
|
||||
)
|
||||
assert.True(t, IsLeft(result))
|
||||
_, err := UnwrapError(result)
|
||||
assert.Contains(t, err.Error(), "Error:")
|
||||
assert.Contains(t, err.Error(), "failed")
|
||||
})
|
||||
|
||||
t.Run("Compose multiple ChainLeft operations", func(t *testing.T) {
|
||||
// First handler: convert error to string representation
|
||||
handler1 := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](errors.New(err.Error()))
|
||||
})
|
||||
|
||||
// Second handler: add prefix to error
|
||||
handler2 := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("Handled: %w", err))
|
||||
})
|
||||
|
||||
result := F.Pipe2(
|
||||
Left[int](errors.New("original")),
|
||||
handler1,
|
||||
handler2,
|
||||
)
|
||||
assert.True(t, IsLeft(result))
|
||||
_, err := UnwrapError(result)
|
||||
assert.Contains(t, err.Error(), "Handled:")
|
||||
assert.Contains(t, err.Error(), "original")
|
||||
})
|
||||
|
||||
t.Run("Error recovery in pipeline", func(t *testing.T) {
|
||||
// Handler that recovers from specific errors
|
||||
recoverFromTimeout := ChainLeft(func(err error) Result[int] {
|
||||
if err.Error() == "timeout" {
|
||||
return Right(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(0), result1)
|
||||
|
||||
// Test with other error
|
||||
result2 := F.Pipe1(
|
||||
Left[int](errors.New("other error")),
|
||||
recoverFromTimeout,
|
||||
)
|
||||
assert.True(t, IsLeft(result2))
|
||||
})
|
||||
|
||||
t.Run("ChainLeft with Map combination", func(t *testing.T) {
|
||||
// Combine ChainLeft with Map to handle both channels
|
||||
errorHandler := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("Error: %w", err))
|
||||
})
|
||||
|
||||
valueMapper := Map(func(n int) string {
|
||||
return fmt.Sprintf("Value: %d", n)
|
||||
})
|
||||
|
||||
// Test with Left
|
||||
result1 := F.Pipe2(
|
||||
Left[int](errors.New("fail")),
|
||||
errorHandler,
|
||||
valueMapper,
|
||||
)
|
||||
assert.True(t, IsLeft(result1))
|
||||
|
||||
// Test with Right
|
||||
result2 := F.Pipe2(
|
||||
Right(42),
|
||||
errorHandler,
|
||||
valueMapper,
|
||||
)
|
||||
assert.Equal(t, Of("Value: 42"), result2)
|
||||
})
|
||||
|
||||
t.Run("Reusable error handlers", func(t *testing.T) {
|
||||
// Create a library of reusable error handlers
|
||||
recoverNotFound := ChainLeft(func(err error) Result[string] {
|
||||
if err.Error() == "not found" {
|
||||
return Right("default")
|
||||
}
|
||||
return Left[string](err)
|
||||
})
|
||||
|
||||
recoverTimeout := ChainLeft(func(err error) Result[string] {
|
||||
if err.Error() == "timeout" {
|
||||
return Right("retry")
|
||||
}
|
||||
return Left[string](err)
|
||||
})
|
||||
|
||||
// Apply handlers in sequence
|
||||
result := F.Pipe2(
|
||||
Left[string](errors.New("not found")),
|
||||
recoverNotFound,
|
||||
recoverTimeout,
|
||||
)
|
||||
assert.Equal(t, Of("default"), result)
|
||||
})
|
||||
|
||||
t.Run("Error transformation pipeline", func(t *testing.T) {
|
||||
// Build a pipeline that transforms errors step by step
|
||||
addContext := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("context: %w", err))
|
||||
})
|
||||
|
||||
addTimestamp := ChainLeft(func(err error) Result[int] {
|
||||
return Left[int](fmt.Errorf("[2024-01-01] %w", err))
|
||||
})
|
||||
|
||||
result := F.Pipe2(
|
||||
Left[int](errors.New("base error")),
|
||||
addContext,
|
||||
addTimestamp,
|
||||
)
|
||||
assert.True(t, IsLeft(result))
|
||||
_, err := UnwrapError(result)
|
||||
assert.Contains(t, err.Error(), "[2024-01-01]")
|
||||
assert.Contains(t, err.Error(), "context:")
|
||||
assert.Contains(t, err.Error(), "base error")
|
||||
})
|
||||
|
||||
t.Run("Conditional recovery based on error content", func(t *testing.T) {
|
||||
// Recover from errors matching specific patterns
|
||||
smartRecover := ChainLeft(func(err error) Result[int] {
|
||||
msg := err.Error()
|
||||
if msg == "not found" {
|
||||
return Right(0)
|
||||
}
|
||||
if msg == "timeout" {
|
||||
return Right(-1)
|
||||
}
|
||||
if msg == "unauthorized" {
|
||||
return Right(-2)
|
||||
}
|
||||
return Left[int](err)
|
||||
})
|
||||
|
||||
assert.Equal(t, Of(0), smartRecover(Left[int](errors.New("not found"))))
|
||||
assert.Equal(t, Of(-1), smartRecover(Left[int](errors.New("timeout"))))
|
||||
assert.Equal(t, Of(-2), smartRecover(Left[int](errors.New("unauthorized"))))
|
||||
assert.True(t, IsLeft(smartRecover(Left[int](errors.New("unknown")))))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user