1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-12-09 23:11:40 +02:00

Compare commits

..

9 Commits

Author SHA1 Message Date
Dr. Carsten Leue
5675893896 fix: add DI
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-11-25 16:55:37 +01:00
Carsten Leue
2f0ea57da5 Merge branch 'main' into dependency-injection 2023-11-11 22:22:01 +01:00
Carsten Leue
796f627d20 fix: add Switch to function package
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-11 14:00:41 +01:00
Carsten Leue
77e1efe6be fix: simplify provider
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-10 22:13:14 +01:00
Carsten Leue
677392dfce fix: simplify DI implementation
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-10 21:44:24 +01:00
Carsten Leue
6a8c3b3a21 Merge branch 'main' into dependency-injection 2023-11-10 21:40:41 +01:00
Carsten Leue
cc2f8e54e5 fix: add multi provider
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-09 21:53:55 +01:00
Carsten Leue
16f708d69f fix: add initial DI implementation
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-09 19:44:11 +01:00
Carsten Leue
37430c0698 fix: checkin
Signed-off-by: Carsten Leue <carsten.leue@de.ibm.com>
2023-11-08 20:21:38 +01:00
6 changed files with 33 additions and 161 deletions

View File

@@ -1,25 +0,0 @@
// Copyright (c) 2023 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 function
// Flip reverses the order of parameters of a curried function
func Flip[T1, T2, R any](f func(T1) func(T2) R) func(T2) func(T1) R {
return func(t2 T2) func(T1) R {
return func(t1 T1) R {
return f(t1)(t2)
}
}
}

View File

@@ -1,36 +0,0 @@
// Copyright (c) 2023 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 function
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlip(t *testing.T) {
x := Curry2(func(a, b string) string {
return fmt.Sprintf("%s:%s", a, b)
})
assert.Equal(t, "a:b", x("a")("b"))
y := Flip(x)
assert.Equal(t, "b:a", y("a")("b"))
}

View File

@@ -275,27 +275,6 @@ func ChainFirstIOK[GA ~func() ET.Either[E, A], GIOB ~func() B, E, A, B any](f fu
) )
} }
// MonadChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
func MonadChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](first GA, f func(A) ET.Either[E, B]) GA {
return FE.MonadChainFirstEitherK(
MonadChain[GA, GA, E, A, A],
MonadMap[func() ET.Either[E, B], GA, E, B, A],
FromEither[func() ET.Either[E, B], E, B],
first,
f,
)
}
// ChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
func ChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](f func(A) ET.Either[E, B]) func(GA) GA {
return FE.ChainFirstEitherK(
MonadChain[GA, GA, E, A, A],
MonadMap[func() ET.Either[E, B], GA, E, B, A],
FromEither[func() ET.Either[E, B], E, B],
f,
)
}
// Swap changes the order of type parameters // Swap changes the order of type parameters
func Swap[GEA ~func() ET.Either[E, A], GAE ~func() ET.Either[A, E], E, A any](val GEA) GAE { func Swap[GEA ~func() ET.Either[E, A], GAE ~func() ET.Either[A, E], E, A any](val GEA) GAE {
return MonadFold(val, Right[GAE], Left[GAE]) return MonadFold(val, Right[GAE], Left[GAE])

View File

@@ -20,94 +20,54 @@ import (
"net/http" "net/http"
B "github.com/IBM/fp-go/bytes" B "github.com/IBM/fp-go/bytes"
ER "github.com/IBM/fp-go/errors"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
H "github.com/IBM/fp-go/http"
IOE "github.com/IBM/fp-go/ioeither" IOE "github.com/IBM/fp-go/ioeither"
IOEF "github.com/IBM/fp-go/ioeither/file" IOEF "github.com/IBM/fp-go/ioeither/file"
J "github.com/IBM/fp-go/json" J "github.com/IBM/fp-go/json"
T "github.com/IBM/fp-go/tuple"
) )
type ( type Client interface {
// Requester is a reader that constructs a request Do(req *http.Request) IOE.IOEither[error, *http.Response]
Requester = IOE.IOEither[error, *http.Request] }
Client interface { type client struct {
Do(Requester) IOE.IOEither[error, *http.Response] delegate *http.Client
} }
client struct { func (client client) Do(req *http.Request) IOE.IOEither[error, *http.Response] {
delegate *http.Client return IOE.TryCatch(func() (*http.Response, error) {
doIOE func(*http.Request) IOE.IOEither[error, *http.Response] return client.delegate.Do(req)
} }, ER.IdentityError)
)
var (
// MakeRequest is an eitherized version of [http.NewRequest]
MakeRequest = IOE.Eitherize3(http.NewRequest)
makeRequest = F.Bind13of3(MakeRequest)
// specialize
MakeGetRequest = makeRequest("GET", nil)
)
func (client client) Do(req Requester) IOE.IOEither[error, *http.Response] {
return F.Pipe1(
req,
IOE.Chain(client.doIOE),
)
} }
func MakeClient(httpClient *http.Client) Client { func MakeClient(httpClient *http.Client) Client {
return client{delegate: httpClient, doIOE: IOE.Eitherize1(httpClient.Do)} return client{delegate: httpClient}
} }
// ReadFullResponse sends a request, reads the response as a byte array and represents the result as a tuple func ReadAll(client Client) func(*http.Request) IOE.IOEither[error, []byte] {
func ReadFullResponse(client Client) func(Requester) IOE.IOEither[error, H.FullResponse] { return func(req *http.Request) IOE.IOEither[error, []byte] {
return F.Flow3( return IOEF.ReadAll(F.Pipe2(
client.Do, req,
IOE.ChainEitherK(H.ValidateResponse), client.Do,
IOE.Chain(func(resp *http.Response) IOE.IOEither[error, H.FullResponse] { IOE.Map[error](func(resp *http.Response) io.ReadCloser {
return F.Pipe1( return resp.Body
F.Pipe3( }),
resp, ),
H.GetBody, )
IOE.Of[error, io.ReadCloser], }
IOEF.ReadAll[io.ReadCloser],
),
IOE.Map[error](F.Bind1st(T.MakeTuple2[*http.Response, []byte], resp)),
)
}),
)
} }
// ReadAll sends a request and reads the response as bytes func ReadText(client Client) func(*http.Request) IOE.IOEither[error, string] {
func ReadAll(client Client) func(Requester) IOE.IOEither[error, []byte] {
return F.Flow2(
ReadFullResponse(client),
IOE.Map[error](H.Body),
)
}
// ReadText sends a request, reads the response and represents the response as a text string
func ReadText(client Client) func(Requester) IOE.IOEither[error, string] {
return F.Flow2( return F.Flow2(
ReadAll(client), ReadAll(client),
IOE.Map[error](B.ToString), IOE.Map[error](B.ToString),
) )
} }
// ReadJson sends a request, reads the response and parses the response as JSON func ReadJson[A any](client Client) func(*http.Request) IOE.IOEither[error, A] {
func ReadJson[A any](client Client) func(Requester) IOE.IOEither[error, A] { return F.Flow2(
return F.Flow3( ReadAll(client),
ReadFullResponse(client), IOE.ChainEitherK(J.Unmarshal[A]),
IOE.ChainFirstEitherK(F.Flow2(
H.Response,
H.ValidateJsonResponse,
)),
IOE.ChainEitherK(F.Flow2(
H.Body,
J.Unmarshal[A],
)),
) )
} }

View File

@@ -23,6 +23,7 @@ import (
AR "github.com/IBM/fp-go/array" AR "github.com/IBM/fp-go/array"
E "github.com/IBM/fp-go/either" E "github.com/IBM/fp-go/either"
HE "github.com/IBM/fp-go/either/http"
"github.com/IBM/fp-go/errors" "github.com/IBM/fp-go/errors"
F "github.com/IBM/fp-go/function" F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither" IOE "github.com/IBM/fp-go/ioeither"
@@ -52,9 +53,10 @@ func TestRetryHttp(t *testing.T) {
client := MakeClient(&http.Client{}) client := MakeClient(&http.Client{})
action := func(status R.RetryStatus) IOE.IOEither[error, *PostItem] { action := func(status R.RetryStatus) IOE.IOEither[error, *PostItem] {
return F.Pipe1( return F.Pipe2(
MakeGetRequest(urls[status.IterNumber]), HE.GetRequest(urls[status.IterNumber]),
ReadJson[*PostItem](client), IOE.FromEither[error, *http.Request],
IOE.Chain(ReadJson[*PostItem](client)),
) )
} }

View File

@@ -211,14 +211,6 @@ func ChainFirst[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEi
return G.ChainFirst[IOEither[E, A]](f) return G.ChainFirst[IOEither[E, A]](f)
} }
func MonadChainFirstEitherK[A, E, B any](ma IOEither[E, A], f func(A) ET.Either[E, B]) IOEither[E, A] {
return G.MonadChainFirstEitherK[IOEither[E, A]](ma, f)
}
func ChainFirstEitherK[A, E, B any](f func(A) ET.Either[E, B]) func(ma IOEither[E, A]) IOEither[E, A] {
return G.ChainFirstEitherK[IOEither[E, A]](f)
}
// MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad // MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] { func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
return G.MonadChainFirstIOK(ma, f) return G.MonadChainFirstIOK(ma, f)