From e82575fe08f0c09451d1f60f90f071aec0075ff1 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Sun, 17 Dec 2023 13:24:05 +0100 Subject: [PATCH] fix: consistent endomorphism Signed-off-by: Dr. Carsten Leue --- endomorphism/generic/monoid.go | 8 ++------ http/form/form.go | 3 +++ ioeither/http/builder/builder.go | 27 ++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/endomorphism/generic/monoid.go b/endomorphism/generic/monoid.go index 98a94cd..9b8e0c6 100644 --- a/endomorphism/generic/monoid.go +++ b/endomorphism/generic/monoid.go @@ -29,15 +29,11 @@ func Of[ENDO ~func(A) A, F ~func(A) A, A any](f F) ENDO { } func Identity[ENDO ~func(A) A, A any]() ENDO { - return func(a A) A { - return a - } + return Of[ENDO](F.Identity[A]) } func Compose[ENDO ~func(A) A, A any](f1, f2 ENDO) ENDO { - return func(a A) A { - return F.Pipe2(a, f1, f2) - } + return Of[ENDO](F.Flow2(f1, f2)) } // Semigroup for the Endomorphism where the `concat` operation is the usual function composition. diff --git a/http/form/form.go b/http/form/form.go index 98318cb..702f8fe 100644 --- a/http/form/form.go +++ b/http/form/form.go @@ -38,6 +38,9 @@ var ( noField = O.None[string]() + // FormMonoid is the [M.Monoid] for the [FormEndomorphism] + FormMonoid = ENDO.Monoid[url.Values]() + // AtValues is a [L.Lens] that focusses on the values of a form field AtValues = LRG.AtRecord[url.Values, []string] diff --git a/ioeither/http/builder/builder.go b/ioeither/http/builder/builder.go index ddc58f1..40d6858 100644 --- a/ioeither/http/builder/builder.go +++ b/ioeither/http/builder/builder.go @@ -50,6 +50,9 @@ var ( defaultMethod = F.Constant(http.MethodGet) + // BuilderMonoid is the [M.Monoid] for the [BuilderEndomorphism] + BuilderMonoid = 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 @@ -67,15 +70,25 @@ var ( noBody = O.None[IOE.IOEither[error, []byte]]() // WithMethod creates a [BuilderBuilder] for a certain method - WithMethod = Method.Set + WithMethod = F.Flow2( + Method.Set, + ENDO.Of[func(*Builder) *Builder], + ) // WithUrl creates a [BuilderBuilder] for a certain method - WithUrl = Url.Set + WithUrl = F.Flow2( + Url.Set, + ENDO.Of[func(*Builder) *Builder], + ) // WithHeaders creates a [BuilderBuilder] for a set of headers - WithHeaders = Headers.Set + WithHeaders = F.Flow2( + Headers.Set, + ENDO.Of[func(*Builder) *Builder], + ) // WithBody creates a [BuilderBuilder] for a request body - WithBody = F.Flow2( + WithBody = F.Flow3( O.Of[IOE.IOEither[error, []byte]], Body.Set, + ENDO.Of[func(*Builder) *Builder], ) // WithContentType adds the content type header WithContentType = WithHeader("Content-Type") @@ -93,7 +106,11 @@ var ( Requester = (*Builder).Requester // WithoutBody creates a [BuilderBuilder] to remove the body - WithoutBody = Body.Set(noBody) + WithoutBody = F.Pipe2( + noBody, + Body.Set, + ENDO.Of[func(*Builder) *Builder], + ) ) func (builder *Builder) clone() *Builder {