mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-28 19:49:07 +02:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a14feff1d6 | ||
|
f3642bad60 | ||
|
997627b318 | ||
|
1e1411c003 | ||
|
9ed9f8a171 | ||
|
e7428549e4 | ||
|
aef0048119 | ||
|
709d74b135 | ||
|
38c6541254 |
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@@ -17,8 +17,8 @@ on:
|
||||
env:
|
||||
# Currently no way to detect automatically
|
||||
DEFAULT_BRANCH: main
|
||||
GO_VERSION: 1.20.6 # renovate: datasource=golang-version depName=golang
|
||||
NODE_VERSION: 18
|
||||
GO_VERSION: 1.21.6 # renovate: datasource=golang-version depName=golang
|
||||
NODE_VERSION: 20
|
||||
DRY_RUN: true
|
||||
|
||||
jobs:
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go-version: [ '1.20.x', '1.21.x' ]
|
||||
go-version: [ '1.20.x', '1.21.x']
|
||||
steps:
|
||||
# full checkout for semantic-release
|
||||
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
|
||||
|
@@ -24,10 +24,10 @@ import (
|
||||
func concat[T any](left, right []T) []T {
|
||||
// some performance checks
|
||||
ll := len(left)
|
||||
lr := len(right)
|
||||
if ll == 0 {
|
||||
return right
|
||||
}
|
||||
lr := len(right)
|
||||
if lr == 0 {
|
||||
return left
|
||||
}
|
||||
|
72
context/readerioeither/http/builder/builder.go
Normal file
72
context/readerioeither/http/builder/builder.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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 builder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
RIOE "github.com/IBM/fp-go/context/readerioeither"
|
||||
RIOEH "github.com/IBM/fp-go/context/readerioeither/http"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
R "github.com/IBM/fp-go/http/builder"
|
||||
H "github.com/IBM/fp-go/http/headers"
|
||||
LZ "github.com/IBM/fp-go/lazy"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
)
|
||||
|
||||
func Requester(builder *R.Builder) RIOEH.Requester {
|
||||
|
||||
withBody := F.Curry3(func(data []byte, url string, method string) RIOE.ReaderIOEither[*http.Request] {
|
||||
return RIOE.TryCatch(func(ctx context.Context) func() (*http.Request, error) {
|
||||
return func() (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(data))
|
||||
if err == nil {
|
||||
req.Header.Set(H.ContentLength, strconv.Itoa(len(data)))
|
||||
H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
}
|
||||
return req, err
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
withoutBody := F.Curry2(func(url string, method string) RIOE.ReaderIOEither[*http.Request] {
|
||||
return RIOE.TryCatch(func(ctx context.Context) func() (*http.Request, error) {
|
||||
return func() (*http.Request, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, url, nil)
|
||||
if err == nil {
|
||||
H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
}
|
||||
return req, err
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
return F.Pipe5(
|
||||
builder.GetBody(),
|
||||
O.Fold(LZ.Of(E.Of[error](withoutBody)), E.Map[error](withBody)),
|
||||
E.Ap[func(string) RIOE.ReaderIOEither[*http.Request]](builder.GetTargetUrl()),
|
||||
E.Flap[error, RIOE.ReaderIOEither[*http.Request]](builder.GetMethod()),
|
||||
E.GetOrElse(RIOE.Left[*http.Request]),
|
||||
RIOE.Map(func(req *http.Request) *http.Request {
|
||||
req.Header = H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
return req
|
||||
}),
|
||||
)
|
||||
}
|
59
context/readerioeither/http/builder/builder_test.go
Normal file
59
context/readerioeither/http/builder/builder_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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 builder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
RIOE "github.com/IBM/fp-go/context/readerioeither"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
R "github.com/IBM/fp-go/http/builder"
|
||||
IO "github.com/IBM/fp-go/io"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuilderWithQuery(t *testing.T) {
|
||||
// add some query
|
||||
withLimit := R.WithQueryArg("limit")("10")
|
||||
withUrl := R.WithUrl("http://www.example.org?a=b")
|
||||
|
||||
b := F.Pipe2(
|
||||
R.Default,
|
||||
withLimit,
|
||||
withUrl,
|
||||
)
|
||||
|
||||
req := F.Pipe3(
|
||||
b,
|
||||
Requester,
|
||||
RIOE.Map(func(r *http.Request) *url.URL {
|
||||
return r.URL
|
||||
}),
|
||||
RIOE.ChainFirstIOK(func(u *url.URL) IO.IO[any] {
|
||||
return IO.FromImpure(func() {
|
||||
q := u.Query()
|
||||
assert.Equal(t, "10", q.Get("limit"))
|
||||
assert.Equal(t, "b", q.Get("a"))
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
assert.True(t, E.IsRight(req(context.Background())()))
|
||||
}
|
@@ -67,16 +67,17 @@ func MapTo[E, A, B any](b B) func(Either[E, A]) Either[E, B] {
|
||||
return F.Bind2nd(MonadMapTo[E, A, B], b)
|
||||
}
|
||||
|
||||
func MonadMapLeft[E, A, B any](fa Either[E, A], f func(E) B) Either[B, A] {
|
||||
return MonadFold(fa, F.Flow2(f, Left[A, B]), Right[B, A])
|
||||
func MonadMapLeft[E1, A, E2 any](fa Either[E1, A], f func(E1) E2) Either[E2, A] {
|
||||
return MonadFold(fa, F.Flow2(f, Left[A, E2]), Right[E2, A])
|
||||
}
|
||||
|
||||
func Map[E, A, B any](f func(a A) B) func(fa Either[E, A]) Either[E, B] {
|
||||
return Chain(F.Flow2(f, Right[E, B]))
|
||||
}
|
||||
|
||||
func MapLeft[E, A, B any](f func(E) B) func(fa Either[E, A]) Either[B, A] {
|
||||
return F.Bind2nd(MonadMapLeft[E, A, B], f)
|
||||
// MapLeft applies a mapping function to the error channel
|
||||
func MapLeft[A, E1, E2 any](f func(E1) E2) func(fa Either[E1, A]) Either[E2, A] {
|
||||
return F.Bind2nd(MonadMapLeft[E1, A, E2], f)
|
||||
}
|
||||
|
||||
func MonadChain[E, A, B any](fa Either[E, A], f func(a A) Either[E, B]) Either[E, B] {
|
||||
|
@@ -28,6 +28,16 @@ func Of[ENDO ~func(A) A, F ~func(A) A, A any](f F) ENDO {
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap converts any function to an [Endomorphism]
|
||||
func Wrap[ENDO ~func(A) A, F ~func(A) A, A any](f F) ENDO {
|
||||
return Of[ENDO](f)
|
||||
}
|
||||
|
||||
// Unwrap converts any [Endomorphism] to a normal function
|
||||
func Unwrap[F ~func(A) A, ENDO ~func(A) A, A any](f ENDO) F {
|
||||
return Of[F](f)
|
||||
}
|
||||
|
||||
func Identity[ENDO ~func(A) A, A any]() ENDO {
|
||||
return Of[ENDO](F.Identity[A])
|
||||
}
|
||||
|
@@ -29,6 +29,16 @@ func Of[F ~func(A) A, A any](f F) Endomorphism[A] {
|
||||
return G.Of[Endomorphism[A]](f)
|
||||
}
|
||||
|
||||
// Wrap converts any function to an [Endomorphism]
|
||||
func Wrap[F ~func(A) A, A any](f F) Endomorphism[A] {
|
||||
return G.Wrap[Endomorphism[A]](f)
|
||||
}
|
||||
|
||||
// Unwrap converts any [Endomorphism] to a function
|
||||
func Unwrap[F ~func(A) A, A any](f Endomorphism[A]) F {
|
||||
return G.Unwrap[F](f)
|
||||
}
|
||||
|
||||
// Identity returns the identity [Endomorphism]
|
||||
func Identity[A any]() Endomorphism[A] {
|
||||
return G.Identity[Endomorphism[A]]()
|
||||
|
311
http/builder/builder.go
Normal file
311
http/builder/builder.go
Normal file
@@ -0,0 +1,311 @@
|
||||
// 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 builder
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
ENDO "github.com/IBM/fp-go/endomorphism"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
C "github.com/IBM/fp-go/http/content"
|
||||
FM "github.com/IBM/fp-go/http/form"
|
||||
H "github.com/IBM/fp-go/http/headers"
|
||||
J "github.com/IBM/fp-go/json"
|
||||
LZ "github.com/IBM/fp-go/lazy"
|
||||
L "github.com/IBM/fp-go/optics/lens"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
S "github.com/IBM/fp-go/string"
|
||||
T "github.com/IBM/fp-go/tuple"
|
||||
)
|
||||
|
||||
type (
|
||||
Builder struct {
|
||||
method O.Option[string]
|
||||
url string
|
||||
headers http.Header
|
||||
body O.Option[E.Either[error, []byte]]
|
||||
query url.Values
|
||||
}
|
||||
|
||||
// Endomorphism returns an [ENDO.Endomorphism] that transforms a builder
|
||||
Endomorphism = ENDO.Endomorphism[*Builder]
|
||||
)
|
||||
|
||||
var (
|
||||
// Default is the default builder
|
||||
Default = &Builder{method: O.Some(defaultMethod()), headers: make(http.Header), body: noBody}
|
||||
|
||||
defaultMethod = F.Constant(http.MethodGet)
|
||||
|
||||
// Monoid is the [M.Monoid] for the [Endomorphism]
|
||||
Monoid = ENDO.Monoid[*Builder]()
|
||||
|
||||
// Url is a [L.Lens] for the URL
|
||||
Url = L.MakeLensRef((*Builder).GetUrl, (*Builder).SetUrl)
|
||||
// Method is a [L.Lens] for the HTTP method
|
||||
Method = L.MakeLensRef((*Builder).GetMethod, (*Builder).SetMethod)
|
||||
// Body is a [L.Lens] for the request body
|
||||
Body = L.MakeLensRef((*Builder).GetBody, (*Builder).SetBody)
|
||||
// Headers is a [L.Lens] for the complete set of request headers
|
||||
Headers = L.MakeLensRef((*Builder).GetHeaders, (*Builder).SetHeaders)
|
||||
// Query is a [L.Lens] for the set of query parameters
|
||||
Query = L.MakeLensRef((*Builder).GetQuery, (*Builder).SetQuery)
|
||||
|
||||
rawQuery = L.MakeLensRef(getRawQuery, setRawQuery)
|
||||
|
||||
getHeader = F.Bind2of2((*Builder).GetHeader)
|
||||
delHeader = F.Bind2of2((*Builder).DelHeader)
|
||||
setHeader = F.Bind2of3((*Builder).SetHeader)
|
||||
|
||||
noHeader = O.None[string]()
|
||||
noBody = O.None[E.Either[error, []byte]]()
|
||||
noQueryArg = O.None[string]()
|
||||
|
||||
parseUrl = E.Eitherize1(url.Parse)
|
||||
parseQuery = E.Eitherize1(url.ParseQuery)
|
||||
|
||||
// WithQuery creates a [Endomorphism] for a complete set of query parameters
|
||||
WithQuery = Query.Set
|
||||
// WithMethod creates a [Endomorphism] for a certain method
|
||||
WithMethod = Method.Set
|
||||
// WithUrl creates a [Endomorphism] for a certain method
|
||||
WithUrl = Url.Set
|
||||
// WithHeaders creates a [Endomorphism] for a set of headers
|
||||
WithHeaders = Headers.Set
|
||||
// WithBody creates a [Endomorphism] for a request body
|
||||
WithBody = F.Flow2(
|
||||
O.Of[E.Either[error, []byte]],
|
||||
Body.Set,
|
||||
)
|
||||
// WithBytes creates a [Endomorphism] for a request body using bytes
|
||||
WithBytes = F.Flow2(
|
||||
E.Of[error, []byte],
|
||||
WithBody,
|
||||
)
|
||||
// WithContentType adds the [H.ContentType] header
|
||||
WithContentType = WithHeader(H.ContentType)
|
||||
// WithAuthorization adds the [H.Authorization] header
|
||||
WithAuthorization = WithHeader(H.Authorization)
|
||||
|
||||
// WithGet adds the [http.MethodGet] method
|
||||
WithGet = WithMethod(http.MethodGet)
|
||||
// WithPost adds the [http.MethodPost] method
|
||||
WithPost = WithMethod(http.MethodPost)
|
||||
// WithPut adds the [http.MethodPut] method
|
||||
WithPut = WithMethod(http.MethodPut)
|
||||
// WithDelete adds the [http.MethodDelete] method
|
||||
WithDelete = WithMethod(http.MethodDelete)
|
||||
|
||||
// WithBearer creates a [Endomorphism] to add a Bearer [H.Authorization] header
|
||||
WithBearer = F.Flow2(
|
||||
S.Format[string]("Bearer %s"),
|
||||
WithAuthorization,
|
||||
)
|
||||
|
||||
// WithoutBody creates a [Endomorphism] to remove the body
|
||||
WithoutBody = F.Pipe1(
|
||||
noBody,
|
||||
Body.Set,
|
||||
)
|
||||
|
||||
// WithFormData creates a [Endomorphism] to send form data payload
|
||||
WithFormData = F.Flow4(
|
||||
url.Values.Encode,
|
||||
S.ToBytes,
|
||||
WithBytes,
|
||||
ENDO.Chain(WithContentType(C.FormEncoded)),
|
||||
)
|
||||
)
|
||||
|
||||
func setRawQuery(u *url.URL, raw string) *url.URL {
|
||||
u.RawQuery = raw
|
||||
return u
|
||||
}
|
||||
|
||||
func getRawQuery(u *url.URL) string {
|
||||
return u.RawQuery
|
||||
}
|
||||
|
||||
func (builder *Builder) clone() *Builder {
|
||||
cpy := *builder
|
||||
cpy.headers = cpy.headers.Clone()
|
||||
return &cpy
|
||||
}
|
||||
|
||||
// GetTargetUrl constructs a full URL with query parameters on top of the provided URL string
|
||||
func (builder *Builder) GetTargetUrl() E.Either[error, string] {
|
||||
// construct the final URL
|
||||
return F.Pipe3(
|
||||
builder,
|
||||
Url.Get,
|
||||
parseUrl,
|
||||
E.Chain(F.Flow4(
|
||||
T.Replicate2[*url.URL],
|
||||
T.Map2(
|
||||
F.Flow2(
|
||||
F.Curry2(setRawQuery),
|
||||
E.Of[error, func(string) *url.URL],
|
||||
),
|
||||
F.Flow3(
|
||||
rawQuery.Get,
|
||||
parseQuery,
|
||||
E.Map[error](F.Flow2(
|
||||
F.Curry2(FM.ValuesMonoid.Concat)(builder.GetQuery()),
|
||||
(url.Values).Encode,
|
||||
)),
|
||||
),
|
||||
),
|
||||
T.Tupled2(E.MonadAp[*url.URL, error, string]),
|
||||
E.Map[error]((*url.URL).String),
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
func (builder *Builder) GetUrl() string {
|
||||
return builder.url
|
||||
}
|
||||
|
||||
func (builder *Builder) GetMethod() string {
|
||||
return F.Pipe1(
|
||||
builder.method,
|
||||
O.GetOrElse(defaultMethod),
|
||||
)
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeaders() http.Header {
|
||||
return builder.headers
|
||||
}
|
||||
|
||||
func (builder *Builder) GetQuery() url.Values {
|
||||
return builder.query
|
||||
}
|
||||
|
||||
func (builder *Builder) SetQuery(query url.Values) *Builder {
|
||||
builder.query = query
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) GetBody() O.Option[E.Either[error, []byte]] {
|
||||
return builder.body
|
||||
}
|
||||
|
||||
func (builder *Builder) SetMethod(method string) *Builder {
|
||||
builder.method = O.Some(method)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetUrl(url string) *Builder {
|
||||
builder.url = url
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetHeaders(headers http.Header) *Builder {
|
||||
builder.headers = headers
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetBody(body O.Option[E.Either[error, []byte]]) *Builder {
|
||||
builder.body = body
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetHeader(name, value string) *Builder {
|
||||
builder.headers.Set(name, value)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) DelHeader(name string) *Builder {
|
||||
builder.headers.Del(name)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeader(name string) O.Option[string] {
|
||||
return F.Pipe2(
|
||||
name,
|
||||
builder.headers.Get,
|
||||
O.FromPredicate(S.IsNonEmpty),
|
||||
)
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeaderValues(name string) []string {
|
||||
return builder.headers.Values(name)
|
||||
}
|
||||
|
||||
// Header returns a [L.Lens] for a single header
|
||||
func Header(name string) L.Lens[*Builder, O.Option[string]] {
|
||||
get := getHeader(name)
|
||||
set := F.Bind1of2(setHeader(name))
|
||||
del := F.Flow2(
|
||||
LZ.Of[*Builder],
|
||||
LZ.Map(delHeader(name)),
|
||||
)
|
||||
|
||||
return L.MakeLens(get, func(b *Builder, value O.Option[string]) *Builder {
|
||||
cpy := b.clone()
|
||||
return F.Pipe1(
|
||||
value,
|
||||
O.Fold(del(cpy), set(cpy)),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// WithHeader creates a [Endomorphism] for a certain header
|
||||
func WithHeader(name string) func(value string) Endomorphism {
|
||||
return F.Flow2(
|
||||
O.Of[string],
|
||||
Header(name).Set,
|
||||
)
|
||||
}
|
||||
|
||||
// WithoutHeader creates a [Endomorphism] to remove a certain header
|
||||
func WithoutHeader(name string) Endomorphism {
|
||||
return Header(name).Set(noHeader)
|
||||
}
|
||||
|
||||
// WithJson creates a [Endomorphism] to send JSON payload
|
||||
func WithJson[T any](data T) Endomorphism {
|
||||
return Monoid.Concat(
|
||||
F.Pipe2(
|
||||
data,
|
||||
J.Marshal[T],
|
||||
WithBody,
|
||||
),
|
||||
WithContentType(C.Json),
|
||||
)
|
||||
}
|
||||
|
||||
// QueryArg is a [L.Lens] for the first value of a query argument
|
||||
func QueryArg(name string) L.Lens[*Builder, O.Option[string]] {
|
||||
return F.Pipe1(
|
||||
Query,
|
||||
L.Compose[*Builder](FM.AtValue(name)),
|
||||
)
|
||||
}
|
||||
|
||||
// WithQueryArg creates a [Endomorphism] for a certain query argument
|
||||
func WithQueryArg(name string) func(value string) Endomorphism {
|
||||
return F.Flow2(
|
||||
O.Of[string],
|
||||
QueryArg(name).Set,
|
||||
)
|
||||
}
|
||||
|
||||
// WithoutQueryArg creates a [Endomorphism] that removes a query argument
|
||||
func WithoutQueryArg(name string) Endomorphism {
|
||||
return QueryArg(name).Set(noQueryArg)
|
||||
}
|
68
http/builder/builder_test.go
Normal file
68
http/builder/builder_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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 builder
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
C "github.com/IBM/fp-go/http/content"
|
||||
FD "github.com/IBM/fp-go/http/form"
|
||||
H "github.com/IBM/fp-go/http/headers"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuilder(t *testing.T) {
|
||||
|
||||
name := H.ContentType
|
||||
withContentType := WithHeader(name)
|
||||
withoutContentType := WithoutHeader(name)
|
||||
|
||||
b1 := F.Pipe1(
|
||||
Default,
|
||||
withContentType(C.Json),
|
||||
)
|
||||
|
||||
b2 := F.Pipe1(
|
||||
b1,
|
||||
withContentType(C.TextPlain),
|
||||
)
|
||||
|
||||
b3 := F.Pipe1(
|
||||
b2,
|
||||
withoutContentType,
|
||||
)
|
||||
|
||||
assert.Equal(t, O.None[string](), Default.GetHeader(name))
|
||||
assert.Equal(t, O.Of(C.Json), b1.GetHeader(name))
|
||||
assert.Equal(t, O.Of(C.TextPlain), b2.GetHeader(name))
|
||||
assert.Equal(t, O.None[string](), b3.GetHeader(name))
|
||||
}
|
||||
|
||||
func TestWithFormData(t *testing.T) {
|
||||
data := F.Pipe1(
|
||||
FD.Default,
|
||||
FD.WithValue("a")("b"),
|
||||
)
|
||||
|
||||
res := F.Pipe1(
|
||||
Default,
|
||||
WithFormData(data),
|
||||
)
|
||||
|
||||
assert.Equal(t, C.FormEncoded, Headers.Get(res).Get(H.ContentType))
|
||||
}
|
@@ -133,6 +133,29 @@ func Delay[GA ~func() A, A any](delay time.Duration) func(GA) GA {
|
||||
}
|
||||
}
|
||||
|
||||
func after(timestamp time.Time) func() {
|
||||
return func() {
|
||||
// check if we need to wait
|
||||
current := time.Now()
|
||||
if current.Before(timestamp) {
|
||||
time.Sleep(timestamp.Sub(current))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given timestamp
|
||||
func After[GA ~func() A, A any](timestamp time.Time) func(GA) GA {
|
||||
aft := after(timestamp)
|
||||
return func(ga GA) GA {
|
||||
return MakeIO[GA](func() A {
|
||||
// wait as long as necessary
|
||||
aft()
|
||||
// execute after wait
|
||||
return ga()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Now returns the current timestamp
|
||||
func Now[GA ~func() time.Time]() GA {
|
||||
return MakeIO[GA](time.Now)
|
||||
|
10
io/io.go
10
io/io.go
@@ -146,3 +146,13 @@ func MonadFlap[B, A any](fab IO[func(A) B], a A) IO[B] {
|
||||
func Flap[FAB ~func(A) B, GFAB ~func() FAB, GB ~func() B, A, B any](a A) func(IO[func(A) B]) IO[B] {
|
||||
return G.Flap[func(A) B, IO[func(A) B], IO[B], A, B](a)
|
||||
}
|
||||
|
||||
// Delay creates an operation that passes in the value after some delay
|
||||
func Delay[A any](delay time.Duration) func(IO[A]) IO[A] {
|
||||
return G.Delay[IO[A]](delay)
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given timestamp
|
||||
func After[A any](timestamp time.Time) func(IO[A]) IO[A] {
|
||||
return G.After[IO[A]](timestamp)
|
||||
}
|
||||
|
@@ -207,11 +207,16 @@ func MapLeft[GA1 ~func() ET.Either[E1, A], GA2 ~func() ET.Either[E2, A], E1, E2,
|
||||
return F.Bind2nd(MonadMapLeft[GA1, GA2, E1, E2, A], f)
|
||||
}
|
||||
|
||||
// Delay creates an operation that passes in the value after some delay
|
||||
// Delay creates an operation that passes in the value after some [time.Duration]
|
||||
func Delay[GA ~func() ET.Either[E, A], E, A any](delay time.Duration) func(GA) GA {
|
||||
return IO.Delay[GA](delay)
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given [time.Time]
|
||||
func After[GA ~func() ET.Either[E, A], E, A any](timestamp time.Time) func(GA) GA {
|
||||
return IO.After[GA](timestamp)
|
||||
}
|
||||
|
||||
func MonadBiMap[GA ~func() ET.Either[E1, A], GB ~func() ET.Either[E2, B], E1, E2, A, B any](fa GA, f func(E1) E2, g func(A) B) GB {
|
||||
return eithert.MonadBiMap(IO.MonadMap[GA, GB, ET.Either[E1, A], ET.Either[E2, B]], fa, f, g)
|
||||
}
|
||||
|
43
ioeither/generic/logging.go
Normal file
43
ioeither/generic/logging.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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 generic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
B "github.com/IBM/fp-go/bytes"
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
)
|
||||
|
||||
// LogJson converts the argument to JSON and then logs it via the format string
|
||||
// Can be used with [ChainFirst]
|
||||
func LogJson[GA ~func() ET.Either[error, any], A any](prefix string) func(A) GA {
|
||||
return func(a A) GA {
|
||||
// log this
|
||||
return F.Pipe3(
|
||||
ET.TryCatchError(json.MarshalIndent(a, "", " ")),
|
||||
ET.Map[error](B.ToString),
|
||||
FromEither[func() ET.Either[error, string]],
|
||||
Chain[func() ET.Either[error, string], GA](func(data string) GA {
|
||||
return FromImpure[GA](func() {
|
||||
log.Printf(prefix, data)
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
@@ -18,213 +18,26 @@ package builder
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
ENDO "github.com/IBM/fp-go/endomorphism"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
C "github.com/IBM/fp-go/http/content"
|
||||
FM "github.com/IBM/fp-go/http/form"
|
||||
R "github.com/IBM/fp-go/http/builder"
|
||||
H "github.com/IBM/fp-go/http/headers"
|
||||
IOG "github.com/IBM/fp-go/io/generic"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
IOEH "github.com/IBM/fp-go/ioeither/http"
|
||||
J "github.com/IBM/fp-go/json"
|
||||
LZ "github.com/IBM/fp-go/lazy"
|
||||
L "github.com/IBM/fp-go/optics/lens"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
S "github.com/IBM/fp-go/string"
|
||||
T "github.com/IBM/fp-go/tuple"
|
||||
)
|
||||
|
||||
type (
|
||||
Builder struct {
|
||||
method O.Option[string]
|
||||
url string
|
||||
headers http.Header
|
||||
body O.Option[IOE.IOEither[error, []byte]]
|
||||
query url.Values
|
||||
}
|
||||
|
||||
// Endomorphism returns an [ENDO.Endomorphism] that transforms a builder
|
||||
Endomorphism = ENDO.Endomorphism[*Builder]
|
||||
)
|
||||
|
||||
var (
|
||||
// Default is the default builder
|
||||
Default = &Builder{method: O.Some(defaultMethod()), headers: make(http.Header), body: noBody}
|
||||
|
||||
defaultMethod = F.Constant(http.MethodGet)
|
||||
|
||||
// Monoid is the [M.Monoid] for the [Endomorphism]
|
||||
Monoid = ENDO.Monoid[*Builder]()
|
||||
|
||||
// Url is a [L.Lens] for the URL
|
||||
Url = L.MakeLensRef((*Builder).GetUrl, (*Builder).SetUrl)
|
||||
// Method is a [L.Lens] for the HTTP method
|
||||
Method = L.MakeLensRef((*Builder).GetMethod, (*Builder).SetMethod)
|
||||
// Body is a [L.Lens] for the request body
|
||||
Body = L.MakeLensRef((*Builder).GetBody, (*Builder).SetBody)
|
||||
// Headers is a [L.Lens] for the complete set of request headers
|
||||
Headers = L.MakeLensRef((*Builder).GetHeaders, (*Builder).SetHeaders)
|
||||
// Query is a [L.Lens] for the set of query parameters
|
||||
Query = L.MakeLensRef((*Builder).GetQuery, (*Builder).SetQuery)
|
||||
|
||||
rawQuery = L.MakeLensRef(getRawQuery, setRawQuery)
|
||||
|
||||
getHeader = F.Bind2of2((*Builder).GetHeader)
|
||||
delHeader = F.Bind2of2((*Builder).DelHeader)
|
||||
setHeader = F.Bind2of3((*Builder).SetHeader)
|
||||
|
||||
noHeader = O.None[string]()
|
||||
noBody = O.None[IOE.IOEither[error, []byte]]()
|
||||
noQueryArg = O.None[string]()
|
||||
|
||||
parseUrl = E.Eitherize1(url.Parse)
|
||||
parseQuery = E.Eitherize1(url.ParseQuery)
|
||||
|
||||
// WithQuery creates a [Endomorphism] for a complete set of query parameters
|
||||
WithQuery = Query.Set
|
||||
// WithMethod creates a [Endomorphism] for a certain method
|
||||
WithMethod = Method.Set
|
||||
// WithUrl creates a [Endomorphism] for a certain method
|
||||
WithUrl = Url.Set
|
||||
// WithHeaders creates a [Endomorphism] for a set of headers
|
||||
WithHeaders = Headers.Set
|
||||
// WithBody creates a [Endomorphism] for a request body
|
||||
WithBody = F.Flow2(
|
||||
O.Of[IOE.IOEither[error, []byte]],
|
||||
Body.Set,
|
||||
)
|
||||
// WithBytes creates a [Endomorphism] for a request body using bytes
|
||||
WithBytes = F.Flow2(
|
||||
IOE.Of[error, []byte],
|
||||
WithBody,
|
||||
)
|
||||
// WithContentType adds the [H.ContentType] header
|
||||
WithContentType = WithHeader(H.ContentType)
|
||||
// WithAuthorization adds the [H.Authorization] header
|
||||
WithAuthorization = WithHeader(H.Authorization)
|
||||
|
||||
// WithGet adds the [http.MethodGet] method
|
||||
WithGet = WithMethod(http.MethodGet)
|
||||
// WithPost adds the [http.MethodPost] method
|
||||
WithPost = WithMethod(http.MethodPost)
|
||||
// WithPut adds the [http.MethodPut] method
|
||||
WithPut = WithMethod(http.MethodPut)
|
||||
// WithDelete adds the [http.MethodDelete] method
|
||||
WithDelete = WithMethod(http.MethodDelete)
|
||||
|
||||
// WithBearer creates a [Endomorphism] to add a Bearer [H.Authorization] header
|
||||
WithBearer = F.Flow2(
|
||||
S.Format[string]("Bearer %s"),
|
||||
WithAuthorization,
|
||||
)
|
||||
|
||||
// Requester creates a requester from a builder
|
||||
Requester = (*Builder).Requester
|
||||
|
||||
// WithoutBody creates a [Endomorphism] to remove the body
|
||||
WithoutBody = F.Pipe1(
|
||||
noBody,
|
||||
Body.Set,
|
||||
)
|
||||
)
|
||||
|
||||
func setRawQuery(u *url.URL, raw string) *url.URL {
|
||||
u.RawQuery = raw
|
||||
return u
|
||||
}
|
||||
|
||||
func getRawQuery(u *url.URL) string {
|
||||
return u.RawQuery
|
||||
}
|
||||
|
||||
func (builder *Builder) clone() *Builder {
|
||||
cpy := *builder
|
||||
cpy.headers = cpy.headers.Clone()
|
||||
return &cpy
|
||||
}
|
||||
|
||||
func (builder *Builder) GetUrl() string {
|
||||
return builder.url
|
||||
}
|
||||
|
||||
func (builder *Builder) GetMethod() string {
|
||||
return F.Pipe1(
|
||||
builder.method,
|
||||
O.GetOrElse(defaultMethod),
|
||||
)
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeaders() http.Header {
|
||||
return builder.headers
|
||||
}
|
||||
|
||||
func (builder *Builder) GetQuery() url.Values {
|
||||
return builder.query
|
||||
}
|
||||
|
||||
func (builder *Builder) SetQuery(query url.Values) *Builder {
|
||||
builder.query = query
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) GetBody() O.Option[IOE.IOEither[error, []byte]] {
|
||||
return builder.body
|
||||
}
|
||||
|
||||
func (builder *Builder) SetMethod(method string) *Builder {
|
||||
builder.method = O.Some(method)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetUrl(url string) *Builder {
|
||||
builder.url = url
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetHeaders(headers http.Header) *Builder {
|
||||
builder.headers = headers
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetBody(body O.Option[IOE.IOEither[error, []byte]]) *Builder {
|
||||
builder.body = body
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) SetHeader(name, value string) *Builder {
|
||||
builder.headers.Set(name, value)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) DelHeader(name string) *Builder {
|
||||
builder.headers.Del(name)
|
||||
return builder
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeader(name string) O.Option[string] {
|
||||
return F.Pipe2(
|
||||
name,
|
||||
builder.headers.Get,
|
||||
O.FromPredicate(S.IsNonEmpty),
|
||||
)
|
||||
}
|
||||
|
||||
func (builder *Builder) GetHeaderValues(name string) []string {
|
||||
return builder.headers.Values(name)
|
||||
}
|
||||
|
||||
func (builder *Builder) Requester() IOEH.Requester {
|
||||
func Requester(builder *R.Builder) IOEH.Requester {
|
||||
|
||||
withBody := F.Curry3(func(data []byte, url string, method string) IOE.IOEither[error, *http.Request] {
|
||||
return IOE.TryCatchError(func() (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, bytes.NewReader(data))
|
||||
if err == nil {
|
||||
req.Header.Set(H.ContentLength, strconv.Itoa(len(data)))
|
||||
H.Monoid.Concat(req.Header, builder.headers)
|
||||
H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
}
|
||||
return req, err
|
||||
})
|
||||
@@ -234,123 +47,21 @@ func (builder *Builder) Requester() IOEH.Requester {
|
||||
return IOE.TryCatchError(func() (*http.Request, error) {
|
||||
req, err := http.NewRequest(method, url, nil)
|
||||
if err == nil {
|
||||
H.Monoid.Concat(req.Header, builder.headers)
|
||||
H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
}
|
||||
return req, err
|
||||
})
|
||||
})
|
||||
|
||||
// construct the final URL
|
||||
targetUrl := F.Pipe3(
|
||||
builder,
|
||||
Url.Get,
|
||||
parseUrl,
|
||||
E.Chain(F.Flow4(
|
||||
T.Replicate2[*url.URL],
|
||||
T.Map2(
|
||||
F.Flow2(
|
||||
F.Curry2(setRawQuery),
|
||||
E.Of[error, func(string) *url.URL],
|
||||
),
|
||||
F.Flow3(
|
||||
rawQuery.Get,
|
||||
parseQuery,
|
||||
E.Map[error](F.Flow2(
|
||||
F.Curry2(FM.ValuesMonoid.Concat)(builder.GetQuery()),
|
||||
(url.Values).Encode,
|
||||
)),
|
||||
),
|
||||
),
|
||||
T.Tupled2(E.MonadAp[*url.URL, error, string]),
|
||||
E.Map[error]((*url.URL).String),
|
||||
)),
|
||||
)
|
||||
|
||||
return F.Pipe5(
|
||||
builder,
|
||||
Body.Get,
|
||||
O.Fold(LZ.Of(IOE.Of[error](withoutBody)), IOE.Map[error](withBody)),
|
||||
IOG.Map[IOE.IOEither[error, func(string) func(string) IOE.IOEither[error, *http.Request]], IOE.IOEither[error, func(string) IOE.IOEither[error, *http.Request]]](E.Ap[func(string) IOE.IOEither[error, *http.Request]](targetUrl)),
|
||||
IOE.Flap[error, IOE.IOEither[error, *http.Request]](builder.GetMethod()),
|
||||
IOE.Flatten[error, *http.Request],
|
||||
builder.GetBody(),
|
||||
O.Fold(LZ.Of(E.Of[error](withoutBody)), E.Map[error](withBody)),
|
||||
E.Ap[func(string) IOE.IOEither[error, *http.Request]](builder.GetTargetUrl()),
|
||||
E.Flap[error, IOE.IOEither[error, *http.Request]](builder.GetMethod()),
|
||||
E.GetOrElse(IOE.Left[*http.Request, error]),
|
||||
IOE.Map[error](func(req *http.Request) *http.Request {
|
||||
req.Header = H.Monoid.Concat(req.Header, builder.GetHeaders())
|
||||
return req
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
// Header returns a [L.Lens] for a single header
|
||||
func Header(name string) L.Lens[*Builder, O.Option[string]] {
|
||||
get := getHeader(name)
|
||||
set := F.Bind1of2(setHeader(name))
|
||||
del := F.Flow2(
|
||||
LZ.Of[*Builder],
|
||||
LZ.Map(delHeader(name)),
|
||||
)
|
||||
|
||||
return L.MakeLens(get, func(b *Builder, value O.Option[string]) *Builder {
|
||||
cpy := b.clone()
|
||||
return F.Pipe1(
|
||||
value,
|
||||
O.Fold(del(cpy), set(cpy)),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// WithHeader creates a [Endomorphism] for a certain header
|
||||
func WithHeader(name string) func(value string) Endomorphism {
|
||||
return F.Flow2(
|
||||
O.Of[string],
|
||||
Header(name).Set,
|
||||
)
|
||||
}
|
||||
|
||||
// WithoutHeader creates a [Endomorphism] to remove a certain header
|
||||
func WithoutHeader(name string) Endomorphism {
|
||||
return Header(name).Set(noHeader)
|
||||
}
|
||||
|
||||
// WithFormData creates a [Endomorphism] to send form data payload
|
||||
func WithFormData(value url.Values) Endomorphism {
|
||||
return F.Flow2(
|
||||
F.Pipe4(
|
||||
value,
|
||||
url.Values.Encode,
|
||||
S.ToBytes,
|
||||
IOE.Of[error, []byte],
|
||||
WithBody,
|
||||
),
|
||||
WithContentType(C.FormEncoded),
|
||||
)
|
||||
}
|
||||
|
||||
// WithJson creates a [Endomorphism] to send JSON payload
|
||||
func WithJson[T any](data T) Endomorphism {
|
||||
return F.Flow2(
|
||||
F.Pipe3(
|
||||
data,
|
||||
J.Marshal[T],
|
||||
IOE.FromEither[error, []byte],
|
||||
WithBody,
|
||||
),
|
||||
WithContentType(C.Json),
|
||||
)
|
||||
}
|
||||
|
||||
// QueryArg is a [L.Lens] for the first value of a query argument
|
||||
func QueryArg(name string) L.Lens[*Builder, O.Option[string]] {
|
||||
return F.Pipe1(
|
||||
Query,
|
||||
L.Compose[*Builder](FM.AtValue(name)),
|
||||
)
|
||||
}
|
||||
|
||||
// WithQueryArg creates a [Endomorphism] for a certain query argument
|
||||
func WithQueryArg(name string) func(value string) Endomorphism {
|
||||
return F.Flow2(
|
||||
O.Of[string],
|
||||
QueryArg(name).Set,
|
||||
)
|
||||
}
|
||||
|
||||
// WithoutQueryArg creates a [Endomorphism] that removes a query argument
|
||||
func WithoutQueryArg(name string) Endomorphism {
|
||||
return QueryArg(name).Set(noQueryArg)
|
||||
}
|
||||
|
@@ -22,54 +22,26 @@ import (
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
C "github.com/IBM/fp-go/http/content"
|
||||
H "github.com/IBM/fp-go/http/headers"
|
||||
R "github.com/IBM/fp-go/http/builder"
|
||||
IO "github.com/IBM/fp-go/io"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBuilder(t *testing.T) {
|
||||
|
||||
name := H.ContentType
|
||||
withContentType := WithHeader(name)
|
||||
withoutContentType := WithoutHeader(name)
|
||||
|
||||
b1 := F.Pipe1(
|
||||
Default,
|
||||
withContentType(C.Json),
|
||||
)
|
||||
|
||||
b2 := F.Pipe1(
|
||||
b1,
|
||||
withContentType(C.TextPlain),
|
||||
)
|
||||
|
||||
b3 := F.Pipe1(
|
||||
b2,
|
||||
withoutContentType,
|
||||
)
|
||||
|
||||
assert.Equal(t, O.None[string](), Default.GetHeader(name))
|
||||
assert.Equal(t, O.Of(C.Json), b1.GetHeader(name))
|
||||
assert.Equal(t, O.Of(C.TextPlain), b2.GetHeader(name))
|
||||
assert.Equal(t, O.None[string](), b3.GetHeader(name))
|
||||
}
|
||||
|
||||
func TestBuilderWithQuery(t *testing.T) {
|
||||
// add some query
|
||||
withLimit := WithQueryArg("limit")("10")
|
||||
withUrl := WithUrl("http://www.example.org?a=b")
|
||||
withLimit := R.WithQueryArg("limit")("10")
|
||||
withUrl := R.WithUrl("http://www.example.org?a=b")
|
||||
|
||||
b := F.Pipe2(
|
||||
Default,
|
||||
R.Default,
|
||||
withLimit,
|
||||
withUrl,
|
||||
)
|
||||
|
||||
req := F.Pipe2(
|
||||
b.Requester(),
|
||||
req := F.Pipe3(
|
||||
b,
|
||||
Requester,
|
||||
IOE.Map[error](func(r *http.Request) *url.URL {
|
||||
return r.URL
|
||||
}),
|
||||
|
@@ -16,6 +16,8 @@
|
||||
package ioeither
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
G "github.com/IBM/fp-go/ioeither/generic"
|
||||
@@ -173,7 +175,7 @@ func MonadMapLeft[E1, E2, A any](fa IOEither[E1, A], f func(E1) E2) IOEither[E2,
|
||||
return G.MonadMapLeft[IOEither[E1, A], IOEither[E2, A]](fa, f)
|
||||
}
|
||||
|
||||
func MapLeft[E1, E2, A any](f func(E1) E2) func(IOEither[E1, A]) IOEither[E2, A] {
|
||||
func MapLeft[A, E1, E2 any](f func(E1) E2) func(IOEither[E1, A]) IOEither[E2, A] {
|
||||
return G.MapLeft[IOEither[E1, A], IOEither[E2, A]](f)
|
||||
}
|
||||
|
||||
@@ -276,3 +278,13 @@ func Flap[E, B, A any](a A) func(IOEither[E, func(A) B]) IOEither[E, B] {
|
||||
func ToIOOption[E, A any](ioe IOEither[E, A]) IOO.IOOption[A] {
|
||||
return G.ToIOOption[IOO.IOOption[A]](ioe)
|
||||
}
|
||||
|
||||
// Delay creates an operation that passes in the value after some delay
|
||||
func Delay[E, A any](delay time.Duration) func(IOEither[E, A]) IOEither[E, A] {
|
||||
return G.Delay[IOEither[E, A]](delay)
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given [time.Time]
|
||||
func After[E, A any](timestamp time.Time) func(IOEither[E, A]) IOEither[E, A] {
|
||||
return G.After[IOEither[E, A]](timestamp)
|
||||
}
|
||||
|
26
ioeither/logging.go
Normal file
26
ioeither/logging.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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 ioeither
|
||||
|
||||
import (
|
||||
G "github.com/IBM/fp-go/ioeither/generic"
|
||||
)
|
||||
|
||||
// LogJson converts the argument to pretty printed JSON and then logs it via the format string
|
||||
// Can be used with [ChainFirst]
|
||||
func LogJson[A any](prefix string) func(A) IOEither[error, any] {
|
||||
return G.LogJson[IOEither[error, any], A](prefix)
|
||||
}
|
42
ioeither/logging_test.go
Normal file
42
ioeither/logging_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// 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 ioeither
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
E "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogging(t *testing.T) {
|
||||
|
||||
type SomeData struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
src := &SomeData{Key: "key", Value: "value"}
|
||||
|
||||
res := F.Pipe1(
|
||||
Of[error](src),
|
||||
ChainFirst(LogJson[*SomeData]("Data: \n%s")),
|
||||
)
|
||||
|
||||
dst := res()
|
||||
assert.Equal(t, E.Of[error](src), dst)
|
||||
}
|
@@ -219,6 +219,11 @@ func Delay[GA ~func() O.Option[A], A any](delay time.Duration) func(GA) GA {
|
||||
return IO.Delay[GA](delay)
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given [time.Time]
|
||||
func After[GA ~func() O.Option[A], A any](timestamp time.Time) func(GA) GA {
|
||||
return IO.After[GA](timestamp)
|
||||
}
|
||||
|
||||
// Fold convers an IOOption into an IO
|
||||
func Fold[GA ~func() O.Option[A], GB ~func() B, A, B any](onNone func() GB, onSome func(A) GB) func(GA) GB {
|
||||
return optiont.MatchE(IO.MonadChain[GA, GB, O.Option[A], B], onNone, onSome)
|
||||
|
@@ -16,6 +16,8 @@
|
||||
package iooption
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
IO "github.com/IBM/fp-go/io"
|
||||
@@ -164,3 +166,13 @@ func MonadChainFirstIOK[A, B any](first IOOption[A], f func(A) IO.IO[B]) IOOptio
|
||||
func ChainFirstIOK[A, B any](f func(A) IO.IO[B]) func(IOOption[A]) IOOption[A] {
|
||||
return G.ChainFirstIOK[IOOption[A], IO.IO[B]](f)
|
||||
}
|
||||
|
||||
// Delay creates an operation that passes in the value after some delay
|
||||
func Delay[A any](delay time.Duration) func(IOOption[A]) IOOption[A] {
|
||||
return G.Delay[IOOption[A]](delay)
|
||||
}
|
||||
|
||||
// After creates an operation that passes after the given [time.Time]
|
||||
func After[A any](timestamp time.Time) func(IOOption[A]) IOOption[A] {
|
||||
return G.After[IOOption[A]](timestamp)
|
||||
}
|
||||
|
@@ -164,3 +164,12 @@ func MonadFlap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L,
|
||||
func Flap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, B], L, E, A, B any](a A) func(GEFAB) GEB {
|
||||
return FC.Flap(MonadMap[GEFAB, GEB], a)
|
||||
}
|
||||
|
||||
func MonadMapLeft[GA1 ~func(C) ET.Either[E1, A], GA2 ~func(C) ET.Either[E2, A], C, E1, E2, A any](fa GA1, f func(E1) E2) GA2 {
|
||||
return eithert.MonadMapLeft(R.MonadMap[GA1, GA2, C, ET.Either[E1, A], ET.Either[E2, A]], fa, f)
|
||||
}
|
||||
|
||||
// MapLeft applies a mapping function to the error channel
|
||||
func MapLeft[GA1 ~func(C) ET.Either[E1, A], GA2 ~func(C) ET.Either[E2, A], C, E1, E2, A any](f func(E1) E2) func(GA1) GA2 {
|
||||
return F.Bind2nd(MonadMapLeft[GA1, GA2, C, E1, E2, A], f)
|
||||
}
|
||||
|
@@ -151,3 +151,12 @@ func MonadFlap[L, E, A, B any](fab ReaderEither[L, E, func(A) B], a A) ReaderEit
|
||||
func Flap[L, E, B, A any](a A) func(ReaderEither[L, E, func(A) B]) ReaderEither[L, E, B] {
|
||||
return G.Flap[ReaderEither[L, E, func(A) B], ReaderEither[L, E, B]](a)
|
||||
}
|
||||
|
||||
func MonadMapLeft[C, E1, E2, A any](fa ReaderEither[C, E1, A], f func(E1) E2) ReaderEither[C, E2, A] {
|
||||
return G.MonadMapLeft[ReaderEither[C, E1, A], ReaderEither[C, E2, A]](fa, f)
|
||||
}
|
||||
|
||||
// MapLeft applies a mapping function to the error channel
|
||||
func MapLeft[C, E1, E2, A any](f func(E1) E2) func(ReaderEither[C, E1, A]) ReaderEither[C, E2, A] {
|
||||
return G.MapLeft[ReaderEither[C, E1, A], ReaderEither[C, E2, A]](f)
|
||||
}
|
||||
|
@@ -440,3 +440,12 @@ func MonadFlap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E,
|
||||
func Flap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], R, E, B, A any](a A) func(GREAB) GREB {
|
||||
return FC.Flap(MonadMap[GREAB, GREB], a)
|
||||
}
|
||||
|
||||
func MonadMapLeft[GREA1 ~func(R) GEA1, GREA2 ~func(R) GEA2, GEA1 ~func() ET.Either[E1, A], GEA2 ~func() ET.Either[E2, A], R, E1, E2, A any](fa GREA1, f func(E1) E2) GREA2 {
|
||||
return eithert.MonadMapLeft(G.MonadMap[GREA1, GREA2], fa, f)
|
||||
}
|
||||
|
||||
// MapLeft applies a mapping function to the error channel
|
||||
func MapLeft[GREA1 ~func(R) GEA1, GREA2 ~func(R) GEA2, GEA1 ~func() ET.Either[E1, A], GEA2 ~func() ET.Either[E2, A], R, E1, E2, A any](f func(E1) E2) func(GREA1) GREA2 {
|
||||
return F.Bind2nd(MonadMapLeft[GREA1, GREA2], f)
|
||||
}
|
||||
|
@@ -280,3 +280,12 @@ func MonadFlap[R, E, B, A any](fab ReaderIOEither[R, E, func(A) B], a A) ReaderI
|
||||
func Flap[R, E, B, A any](a A) func(ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B] {
|
||||
return G.Flap[ReaderIOEither[R, E, func(A) B], ReaderIOEither[R, E, B]](a)
|
||||
}
|
||||
|
||||
func MonadMapLeft[R, E1, E2, A any](fa ReaderIOEither[R, E1, A], f func(E1) E2) ReaderIOEither[R, E2, A] {
|
||||
return G.MonadMapLeft[ReaderIOEither[R, E1, A], ReaderIOEither[R, E2, A]](fa, f)
|
||||
}
|
||||
|
||||
// MapLeft applies a mapping function to the error channel
|
||||
func MapLeft[R, A, E1, E2 any](f func(E1) E2) func(ReaderIOEither[R, E1, A]) ReaderIOEither[R, E2, A] {
|
||||
return G.MapLeft[ReaderIOEither[R, E1, A], ReaderIOEither[R, E2, A]](f)
|
||||
}
|
||||
|
@@ -19,6 +19,7 @@ import (
|
||||
"sort"
|
||||
|
||||
F "github.com/IBM/fp-go/function"
|
||||
RAG "github.com/IBM/fp-go/internal/array"
|
||||
FC "github.com/IBM/fp-go/internal/functor"
|
||||
G "github.com/IBM/fp-go/internal/record"
|
||||
Mg "github.com/IBM/fp-go/magma"
|
||||
@@ -334,6 +335,62 @@ func ToEntries[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](r M) GT {
|
||||
return ToArray[M, GT](r)
|
||||
}
|
||||
|
||||
// FromFoldableMap uses the reduce method for a higher kinded type to transform
|
||||
// its values into a tuple. The key and value are then used to populate the map. Duplicate
|
||||
// values are resolved via the provided [Mg.Magma]
|
||||
func FromFoldableMap[
|
||||
FCT ~func(A) T.Tuple2[K, V],
|
||||
HKTA any,
|
||||
FOLDABLE ~func(func(M, A) M, M) func(HKTA) M,
|
||||
M ~map[K]V,
|
||||
A any,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V], fld FOLDABLE) func(f FCT) func(fa HKTA) M {
|
||||
return func(f FCT) func(fa HKTA) M {
|
||||
return fld(func(dst M, a A) M {
|
||||
if IsEmpty(dst) {
|
||||
dst = make(M)
|
||||
}
|
||||
e := f(a)
|
||||
k := T.First(e)
|
||||
old, ok := dst[k]
|
||||
if ok {
|
||||
dst[k] = m.Concat(old, T.Second(e))
|
||||
} else {
|
||||
dst[k] = T.Second(e)
|
||||
}
|
||||
return dst
|
||||
}, Empty[M]())
|
||||
}
|
||||
}
|
||||
|
||||
func FromFoldable[
|
||||
HKTA any,
|
||||
FOLDABLE ~func(func(M, T.Tuple2[K, V]) M, M) func(HKTA) M,
|
||||
M ~map[K]V,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V], red FOLDABLE) func(fa HKTA) M {
|
||||
return FromFoldableMap[func(T.Tuple2[K, V]) T.Tuple2[K, V], HKTA, FOLDABLE](m, red)(F.Identity[T.Tuple2[K, V]])
|
||||
}
|
||||
|
||||
func FromArrayMap[
|
||||
FCT ~func(A) T.Tuple2[K, V],
|
||||
GA ~[]A,
|
||||
M ~map[K]V,
|
||||
A any,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V]) func(f FCT) func(fa GA) M {
|
||||
return FromFoldableMap[FCT](m, F.Bind23of3(RAG.Reduce[GA, A, M]))
|
||||
}
|
||||
|
||||
func FromArray[
|
||||
GA ~[]T.Tuple2[K, V],
|
||||
M ~map[K]V,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V]) func(fa GA) M {
|
||||
return FromFoldable[GA](m, F.Bind23of3(RAG.Reduce[GA, T.Tuple2[K, V], M]))
|
||||
}
|
||||
|
||||
func FromEntries[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](fa GT) M {
|
||||
m := make(M)
|
||||
for _, t := range fa {
|
||||
|
@@ -16,6 +16,7 @@
|
||||
package record
|
||||
|
||||
import (
|
||||
EM "github.com/IBM/fp-go/endomorphism"
|
||||
Mg "github.com/IBM/fp-go/magma"
|
||||
Mo "github.com/IBM/fp-go/monoid"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
@@ -291,6 +292,44 @@ func Copy[K comparable, V any](m map[K]V) map[K]V {
|
||||
}
|
||||
|
||||
// Clone creates a deep copy of the map using the provided endomorphism to clone the values
|
||||
func Clone[K comparable, V any](f func(V) V) func(m map[K]V) map[K]V {
|
||||
func Clone[K comparable, V any](f EM.Endomorphism[V]) EM.Endomorphism[map[K]V] {
|
||||
return G.Clone[map[K]V](f)
|
||||
}
|
||||
|
||||
// FromFoldableMap converts from a reducer to a map
|
||||
// Duplicate keys are resolved by the provided [Mg.Magma]
|
||||
func FromFoldableMap[
|
||||
FOLDABLE ~func(func(map[K]V, A) map[K]V, map[K]V) func(HKTA) map[K]V, // the reduce function
|
||||
A any,
|
||||
HKTA any,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V], red FOLDABLE) func(f func(A) T.Tuple2[K, V]) func(fa HKTA) map[K]V {
|
||||
return G.FromFoldableMap[func(A) T.Tuple2[K, V]](m, red)
|
||||
}
|
||||
|
||||
// FromArrayMap converts from an array to a map
|
||||
// Duplicate keys are resolved by the provided [Mg.Magma]
|
||||
func FromArrayMap[
|
||||
A any,
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V]) func(f func(A) T.Tuple2[K, V]) func(fa []A) map[K]V {
|
||||
return G.FromArrayMap[func(A) T.Tuple2[K, V], []A, map[K]V](m)
|
||||
}
|
||||
|
||||
// FromFoldable converts from a reducer to a map
|
||||
// Duplicate keys are resolved by the provided [Mg.Magma]
|
||||
func FromFoldable[
|
||||
HKTA any,
|
||||
FOLDABLE ~func(func(map[K]V, T.Tuple2[K, V]) map[K]V, map[K]V) func(HKTA) map[K]V, // the reduce function
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V], red FOLDABLE) func(fa HKTA) map[K]V {
|
||||
return G.FromFoldable[HKTA, FOLDABLE](m, red)
|
||||
}
|
||||
|
||||
// FromArray converts from an array to a map
|
||||
// Duplicate keys are resolved by the provided [Mg.Magma]
|
||||
func FromArray[
|
||||
K comparable,
|
||||
V any](m Mg.Magma[V]) func(fa []T.Tuple2[K, V]) map[K]V {
|
||||
return G.FromArray[[]T.Tuple2[K, V], map[K]V](m)
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
// Copyright (c) 2023 IBM Corp.
|
||||
// All rights reserved.
|
||||
// Copyright (c) 2023 IBM Corp.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -23,8 +23,10 @@ import (
|
||||
|
||||
A "github.com/IBM/fp-go/array"
|
||||
"github.com/IBM/fp-go/internal/utils"
|
||||
Mg "github.com/IBM/fp-go/magma"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
S "github.com/IBM/fp-go/string"
|
||||
T "github.com/IBM/fp-go/tuple"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -149,3 +151,28 @@ func TestCopyVsClone(t *testing.T) {
|
||||
assert.NotEqual(t, cpy, cln)
|
||||
assert.Equal(t, src, cpy)
|
||||
}
|
||||
|
||||
func TestFromArrayMap(t *testing.T) {
|
||||
src1 := A.From("a", "b", "c", "a")
|
||||
frm := FromArrayMap[string, string](Mg.Second[string]())
|
||||
|
||||
f := frm(T.Replicate2[string])
|
||||
|
||||
res1 := f(src1)
|
||||
|
||||
assert.Equal(t, map[string]string{
|
||||
"a": "a",
|
||||
"b": "b",
|
||||
"c": "c",
|
||||
}, res1)
|
||||
|
||||
src2 := A.From("A", "B", "C", "A")
|
||||
|
||||
res2 := f(src2)
|
||||
|
||||
assert.Equal(t, map[string]string{
|
||||
"A": "A",
|
||||
"B": "B",
|
||||
"C": "C",
|
||||
}, res2)
|
||||
}
|
||||
|
Reference in New Issue
Block a user