From e166806d1b088f2057a1ba67b6e110c6311ea9a1 Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Mon, 5 Feb 2024 13:46:12 +0100 Subject: [PATCH] fix: adjust to some linter findings Signed-off-by: Dr. Carsten Leue --- .../http/builder/builder_test.go | 4 +-- context/readerioeither/http/request_test.go | 2 +- http/builder/builder.go | 4 +-- internal/foldable/types.go | 26 +++++++++++++++++++ internal/fromeither/types.go | 24 +++++++++++++++++ internal/fromio/types.go | 20 ++++++++++++++ ioeither/http/builder/builder_test.go | 4 +-- ioeither/http/retry_test.go | 2 +- iterator/stateless/iterator_test.go | 4 +-- samples/http/http_test.go | 14 +++++----- .../chapter06_exampleapplication_test.go | 16 ++++++------ .../chapter10_applicativefunctor_test.go | 8 +++--- .../chapter11_transformagain_test.go | 14 +++++----- .../chapter12_traversing_test.go | 2 +- samples/presentation/benchmarks/http_test.go | 10 +++---- 15 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 internal/foldable/types.go create mode 100644 internal/fromeither/types.go create mode 100644 internal/fromio/types.go diff --git a/context/readerioeither/http/builder/builder_test.go b/context/readerioeither/http/builder/builder_test.go index 0385983..256ff47 100644 --- a/context/readerioeither/http/builder/builder_test.go +++ b/context/readerioeither/http/builder/builder_test.go @@ -32,12 +32,12 @@ import ( func TestBuilderWithQuery(t *testing.T) { // add some query withLimit := R.WithQueryArg("limit")("10") - withUrl := R.WithUrl("http://www.example.org?a=b") + withURL := R.WithUrl("http://www.example.org?a=b") b := F.Pipe2( R.Default, withLimit, - withUrl, + withURL, ) req := F.Pipe3( diff --git a/context/readerioeither/http/request_test.go b/context/readerioeither/http/request_test.go index 515a374..e51e115 100644 --- a/context/readerioeither/http/request_test.go +++ b/context/readerioeither/http/request_test.go @@ -31,7 +31,7 @@ import ( ) type PostItem struct { - UserId uint `json:"userId"` + UserID uint `json:"userId"` Id uint `json:"id"` Title string `json:"title"` Body string `json:"body"` diff --git a/http/builder/builder.go b/http/builder/builder.go index 54b6bd3..65e4559 100644 --- a/http/builder/builder.go +++ b/http/builder/builder.go @@ -76,7 +76,7 @@ var ( noBody = O.None[E.Either[error, []byte]]() noQueryArg = O.None[string]() - parseUrl = E.Eitherize1(url.Parse) + parseURL = E.Eitherize1(url.Parse) parseQuery = E.Eitherize1(url.ParseQuery) // WithQuery creates a [Endomorphism] for a complete set of query parameters @@ -153,7 +153,7 @@ func (builder *Builder) GetTargetUrl() E.Either[error, string] { return F.Pipe3( builder, Url.Get, - parseUrl, + parseURL, E.Chain(F.Flow4( T.Replicate2[*url.URL], T.Map2( diff --git a/internal/foldable/types.go b/internal/foldable/types.go new file mode 100644 index 0000000..b75a6f9 --- /dev/null +++ b/internal/foldable/types.go @@ -0,0 +1,26 @@ +// Copyright (c) 2024 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 foldable + +import ( + M "github.com/IBM/fp-go/monoid" +) + +type Foldable[A, B, HKTA any] interface { + Reduce(func(B, A) B, B) func(HKTA) B + ReduceRight(func(B, A) B, B) func(HKTA) B + FoldMap(m M.Monoid[B]) func(func(A) B) func(HKTA) B +} diff --git a/internal/fromeither/types.go b/internal/fromeither/types.go new file mode 100644 index 0000000..a51e95e --- /dev/null +++ b/internal/fromeither/types.go @@ -0,0 +1,24 @@ +// Copyright (c) 2024 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 fromeither + +import ( + ET "github.com/IBM/fp-go/either" +) + +type FromEither[E, A, HKTA any] interface { + FromEither(ET.Either[E, A]) HKTA +} diff --git a/internal/fromio/types.go b/internal/fromio/types.go new file mode 100644 index 0000000..d2fc404 --- /dev/null +++ b/internal/fromio/types.go @@ -0,0 +1,20 @@ +// Copyright (c) 2024 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 fromio + +type FromIO[A, GA ~func() A, HKTA any] interface { + FromIO(GA) HKTA +} diff --git a/ioeither/http/builder/builder_test.go b/ioeither/http/builder/builder_test.go index 8ac27ec..e926962 100644 --- a/ioeither/http/builder/builder_test.go +++ b/ioeither/http/builder/builder_test.go @@ -31,12 +31,12 @@ import ( func TestBuilderWithQuery(t *testing.T) { // add some query withLimit := R.WithQueryArg("limit")("10") - withUrl := R.WithUrl("http://www.example.org?a=b") + withURL := R.WithUrl("http://www.example.org?a=b") b := F.Pipe2( R.Default, withLimit, - withUrl, + withURL, ) req := F.Pipe3( diff --git a/ioeither/http/retry_test.go b/ioeither/http/retry_test.go index b199b40..4eb1a2d 100644 --- a/ioeither/http/retry_test.go +++ b/ioeither/http/retry_test.go @@ -40,7 +40,7 @@ var testLogPolicy = R.CapDelay( ) type PostItem struct { - UserId uint `json:"userId"` + UserID uint `json:"userId"` Id uint `json:"id"` Title string `json:"title"` Body string `json:"body"` diff --git a/iterator/stateless/iterator_test.go b/iterator/stateless/iterator_test.go index d6ebd47..e40ffe6 100644 --- a/iterator/stateless/iterator_test.go +++ b/iterator/stateless/iterator_test.go @@ -67,8 +67,8 @@ func isPrimeNumber(num int) bool { if num <= 2 { return true } - sq_root := int(math.Sqrt(float64(num))) - for i := 2; i <= sq_root; i++ { + sqRoot := int(math.Sqrt(float64(num))) + for i := 2; i <= sqRoot; i++ { if num%i == 0 { return false } diff --git a/samples/http/http_test.go b/samples/http/http_test.go index f2b39c6..183fefa 100644 --- a/samples/http/http_test.go +++ b/samples/http/http_test.go @@ -33,8 +33,8 @@ import ( ) type PostItem struct { - UserId uint `json:"userId"` - Id uint `json:"id"` + UserID uint `json:"userId"` + ID uint `json:"id"` Title string `json:"title"` Body string `json:"body"` } @@ -43,7 +43,7 @@ type CatFact struct { Fact string `json:"fact"` } -func idxToUrl(idx int) string { +func idxToURL(idx int) string { return fmt.Sprintf("https://jsonplaceholder.typicode.com/posts/%d", idx+1) } @@ -59,7 +59,7 @@ func TestMultipleHttpRequests(t *testing.T) { count := 10 data := F.Pipe3( - A.MakeBy(count, idxToUrl), + A.MakeBy(count, idxToURL), R.TraverseArray(F.Flow3( H.MakeGetRequest, readSinglePost, @@ -74,7 +74,7 @@ func TestMultipleHttpRequests(t *testing.T) { assert.Equal(t, E.Of[error](count), result()) } -func heterogeneousHttpRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] { +func heterogeneousHTTPRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] { // prepare the http client client := H.MakeClient(HTTP.DefaultClient) // readSinglePost sends a GET request and parses the response as [PostItem] @@ -97,7 +97,7 @@ func heterogeneousHttpRequests() R.ReaderIOEither[T.Tuple2[PostItem, CatFact]] { // TestHeterogeneousHttpRequests shows how to execute multiple HTTP requests in parallel when // the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types func TestHeterogeneousHttpRequests(t *testing.T) { - data := heterogeneousHttpRequests() + data := heterogeneousHTTPRequests() result := data(context.Background()) @@ -108,6 +108,6 @@ func TestHeterogeneousHttpRequests(t *testing.T) { // the response structure of these requests is different. We use [R.TraverseTuple2] to account for the different types func BenchmarkHeterogeneousHttpRequests(b *testing.B) { for n := 0; n < b.N; n++ { - heterogeneousHttpRequests()(context.Background())() + heterogeneousHTTPRequests()(context.Background())() } } diff --git a/samples/mostly-adequate/chapter06_exampleapplication_test.go b/samples/mostly-adequate/chapter06_exampleapplication_test.go index 2f9ad02..5cd49d3 100644 --- a/samples/mostly-adequate/chapter06_exampleapplication_test.go +++ b/samples/mostly-adequate/chapter06_exampleapplication_test.go @@ -67,26 +67,26 @@ func Example_application() { S.Format[string](fmt.Sprintf("https://%s%s%%s", host, path)), ) // flick returns jsonP, we extract the JSON body, this is handled by jquery in the original code - sanitizeJsonP := Replace(regexp.MustCompile(`(?s)^\s*\((.*)\)\s*$`))("$1") + sanitizeJSONP := Replace(regexp.MustCompile(`(?s)^\s*\((.*)\)\s*$`))("$1") // parse jsonP - parseJsonP := F.Flow3( - sanitizeJsonP, + parseJSONP := F.Flow3( + sanitizeJSONP, S.ToBytes, J.Unmarshal[FlickrFeed], ) // markup img := S.Format[string]("") // lenses - mediaUrl := F.Flow2( + mediaURL := F.Flow2( FlickrItem.getMedia, FlickrMedia.getLink, ) - mediaUrls := F.Flow2( + mediaURLs := F.Flow2( FlickrFeed.getItems, - A.Map(mediaUrl), + A.Map(mediaURL), ) images := F.Flow2( - mediaUrls, + mediaURLs, A.Map(img), ) @@ -97,7 +97,7 @@ func Example_application() { url, H.MakeGetRequest, H.ReadText(client), - R.ChainEitherK(parseJsonP), + R.ChainEitherK(parseJSONP), R.Map(images), ) diff --git a/samples/mostly-adequate/chapter10_applicativefunctor_test.go b/samples/mostly-adequate/chapter10_applicativefunctor_test.go index a2f52ef..cea65ce 100644 --- a/samples/mostly-adequate/chapter10_applicativefunctor_test.go +++ b/samples/mostly-adequate/chapter10_applicativefunctor_test.go @@ -32,7 +32,7 @@ import ( type ( PostItem struct { - UserId uint `json:"userId"` + UserID uint `json:"userId"` Id uint `json:"id"` Title string `json:"title"` Body string `json:"body"` @@ -77,7 +77,7 @@ func (player Player) getName() string { return player.Name } -func (player Player) getId() int { +func (player Player) getID() int { return player.Id } @@ -85,7 +85,7 @@ func (item PostItem) getTitle() string { return item.Title } -func idxToUrl(idx int) string { +func idxToURL(idx int) string { return fmt.Sprintf("https://jsonplaceholder.typicode.com/posts/%d", idx+1) } @@ -101,7 +101,7 @@ func Example_renderPage() { // get returns the title of the nth item from the REST service get := F.Flow4( - idxToUrl, + idxToURL, H.MakeGetRequest, H.ReadJson[PostItem](client), R.Map(PostItem.getTitle), diff --git a/samples/mostly-adequate/chapter11_transformagain_test.go b/samples/mostly-adequate/chapter11_transformagain_test.go index 1ef7dfe..3ac312d 100644 --- a/samples/mostly-adequate/chapter11_transformagain_test.go +++ b/samples/mostly-adequate/chapter11_transformagain_test.go @@ -26,7 +26,7 @@ import ( S "github.com/IBM/fp-go/string" ) -func findUserById(id int) IOE.IOEither[error, Chapter08User] { +func findUserByID(id int) IOE.IOEither[error, Chapter08User] { switch id { case 1: return IOE.Of[error](albert08) @@ -52,15 +52,15 @@ func Example_solution11A() { } func Example_solution11B() { - findByNameId := F.Flow2( - findUserById, + findByNameID := F.Flow2( + findUserByID, IOE.Map[error](Chapter08User.getName), ) - fmt.Println(findByNameId(1)()) - fmt.Println(findByNameId(2)()) - fmt.Println(findByNameId(3)()) - fmt.Println(findByNameId(4)()) + fmt.Println(findByNameID(1)()) + fmt.Println(findByNameID(2)()) + fmt.Println(findByNameID(3)()) + fmt.Println(findByNameID(4)()) // Output: // Right[, string](Albert) diff --git a/samples/mostly-adequate/chapter12_traversing_test.go b/samples/mostly-adequate/chapter12_traversing_test.go index afdbe38..e5db267 100644 --- a/samples/mostly-adequate/chapter12_traversing_test.go +++ b/samples/mostly-adequate/chapter12_traversing_test.go @@ -42,7 +42,7 @@ var ( } // validate :: Player -> Either error Player - validatePlayer = E.FromPredicate(P.ContraMap(Player.getName)(S.IsNonEmpty), F.Flow2(Player.getId, errors.OnSome[int]("player %d must have a name"))) + validatePlayer = E.FromPredicate(P.ContraMap(Player.getName)(S.IsNonEmpty), F.Flow2(Player.getID, errors.OnSome[int]("player %d must have a name"))) // readfile :: String -> String -> Task Error String readfile = F.Curry2(func(encoding, file string) IOE.IOEither[error, string] { diff --git a/samples/presentation/benchmarks/http_test.go b/samples/presentation/benchmarks/http_test.go index 53d55de..9b4d3db 100644 --- a/samples/presentation/benchmarks/http_test.go +++ b/samples/presentation/benchmarks/http_test.go @@ -31,7 +31,7 @@ import ( ) type PostItem struct { - UserId uint `json:"userId"` + UserID uint `json:"userId"` Id uint `json:"id"` Title string `json:"title"` Body string `json:"body"` @@ -41,7 +41,7 @@ type CatFact struct { Fact string `json:"fact"` } -func heterogeneousHttpRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem, CatFact]] { +func heterogeneousHTTPRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem, CatFact]] { // prepare the http client client := H.MakeClient(HTTP.DefaultClient) // readSinglePost sends a GET request and parses the response as [PostItem] @@ -64,7 +64,7 @@ func heterogeneousHttpRequests(count int) R.ReaderIOEither[[]T.Tuple2[PostItem, ) } -func heterogeneousHttpRequestsIdiomatic(count int) ([]T.Tuple2[PostItem, CatFact], error) { +func heterogeneousHTTPRequestsIdiomatic(count int) ([]T.Tuple2[PostItem, CatFact], error) { // prepare the http client var result []T.Tuple2[PostItem, CatFact] @@ -109,13 +109,13 @@ func BenchmarkHeterogeneousHttpRequests(b *testing.B) { b.Run("functional", func(b *testing.B) { for n := 0; n < b.N; n++ { - benchResults = heterogeneousHttpRequests(count)(context.Background())() + benchResults = heterogeneousHTTPRequests(count)(context.Background())() } }) b.Run("idiomatic", func(b *testing.B) { for n := 0; n < b.N; n++ { - benchResults, _ = heterogeneousHttpRequestsIdiomatic(count) + benchResults, _ = heterogeneousHTTPRequestsIdiomatic(count) } })