Skip to content

Commit

Permalink
fix: linter bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 7, 2024
1 parent 9b3d9c6 commit 909f7c3
Show file tree
Hide file tree
Showing 42 changed files with 184 additions and 103 deletions.
4 changes: 2 additions & 2 deletions array/nonempty/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func From[A any](first A, data ...A) NonEmptyArray[A] {
return buffer
}

func IsEmpty[A any](as NonEmptyArray[A]) bool {
func IsEmpty[A any](_ NonEmptyArray[A]) bool {
return false
}

func IsNonEmpty[A any](as NonEmptyArray[A]) bool {
func IsNonEmpty[A any](_ NonEmptyArray[A]) bool {
return true
}

Expand Down
16 changes: 8 additions & 8 deletions bounded/bounded.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ type bounded[T any] struct {
b T
}

func (self bounded[T]) Equals(x, y T) bool {
return self.e(x, y)
func (b bounded[T]) Equals(x, y T) bool {
return b.e(x, y)
}

func (self bounded[T]) Compare(x, y T) int {
return self.c(x, y)
func (b bounded[T]) Compare(x, y T) int {
return b.c(x, y)
}

func (self bounded[T]) Top() T {
return self.t
func (b bounded[T]) Top() T {
return b.t
}

func (self bounded[T]) Bottom() T {
return self.b
func (b bounded[T]) Bottom() T {
return b.b
}

// MakeBounded creates an instance of a bounded type
Expand Down
2 changes: 1 addition & 1 deletion constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Of[E, A any](m M.Monoid[E]) func(A) Const[E, A] {
return F.Constant1[A](Make[E, A](m.Empty()))
}

func MonadMap[E, A, B any](fa Const[E, A], f func(A) B) Const[E, B] {
func MonadMap[E, A, B any](fa Const[E, A], _ func(A) B) Const[E, B] {
return Make[E, B](fa.value)
}

Expand Down
2 changes: 1 addition & 1 deletion context/readerioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Requester(builder *R.Builder) RIOEH.Requester {
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.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 {
Expand Down
9 changes: 8 additions & 1 deletion context/readerioeither/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,19 @@ func ReadText(client Client) func(Requester) RIOE.ReaderIOEither[string] {
}

// ReadJson sends a request, reads the response and parses the response as JSON
//
// Deprecated: use [ReadJSON] instead
func ReadJson[A any](client Client) func(Requester) RIOE.ReaderIOEither[A] {
return ReadJSON[A](client)
}

// ReadJSON sends a request, reads the response and parses the response as JSON
func ReadJSON[A any](client Client) func(Requester) RIOE.ReaderIOEither[A] {
return F.Flow3(
ReadFullResponse(client),
RIOE.ChainFirstEitherK(F.Flow2(
H.Response,
H.ValidateJsonResponse,
H.ValidateJSONResponse,
)),
RIOE.ChainEitherK(F.Flow2(
H.Body,
Expand Down
6 changes: 3 additions & 3 deletions context/readerioeither/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestSendSingleRequest(t *testing.T) {

req1 := MakeGetRequest("https://jsonplaceholder.typicode.com/posts/1")

readItem := ReadJson[PostItem](client)
readItem := ReadJSON[PostItem](client)

resp1 := readItem(req1)

Expand Down Expand Up @@ -112,7 +112,7 @@ func TestSendSingleRequestWithHeaderUnsafe(t *testing.T) {
R.Map(setHeaderUnsafe("Content-Type", "text/html")),
)

readItem := ReadJson[PostItem](client)
readItem := ReadJSON[PostItem](client)

resp1 := F.Pipe2(
req1,
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestSendSingleRequestWithHeaderSafe(t *testing.T) {
WithHeader("Content-Type", "text/html").
Build()

readItem := ReadJson[PostItem](client)
readItem := ReadJSON[PostItem](client)

response := F.Pipe2(
request,
Expand Down
4 changes: 2 additions & 2 deletions di/erasure/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func MakeInjector(providers []Provider) InjectableFactory {
var resolved sync.Map

// provide a mapping for all providers
factoryById := assembleProviders(providers)
factoryByID := assembleProviders(providers)

// the actual factory, we need lazy initialization
var injFct InjectableFactory
Expand All @@ -149,7 +149,7 @@ func MakeInjector(providers []Provider) InjectableFactory {
T.Map2(F.Flow3(
Dependency.Id,
R.Lookup[ProviderFactory, string],
I.Ap[O.Option[ProviderFactory]](factoryById),
I.Ap[O.Option[ProviderFactory]](factoryByID),
), handleMissingProvider),
T.Tupled2(O.MonadGetOrElse[ProviderFactory]),
IG.Ap[ProviderFactory](injFct),
Expand Down
10 changes: 5 additions & 5 deletions di/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ type MultiInjectionToken[T any] interface {
}

// makeID creates a generator of unique string IDs
func makeId() IO.IO[string] {
func makeID() IO.IO[string] {
var count atomic.Int64
return IO.MakeIO(func() string {
return strconv.FormatInt(count.Add(1), 16)
})
}

// genId is the common generator of unique string IDs
var genId = makeId()
// genID is the common generator of unique string IDs
var genID = makeID()

type tokenBase struct {
name string
Expand Down Expand Up @@ -156,7 +156,7 @@ func (m *multiInjectionToken[T]) Item() InjectionToken[T] {

// makeToken create a unique [InjectionToken] for a specific type
func makeInjectionToken[T any](name string, providerFactory O.Option[DIE.ProviderFactory]) InjectionToken[T] {
id := genId()
id := genID()
toIdentity := toType[T]()
return &injectionToken[T]{
token[T]{makeTokenBase(name, id, DIE.Identity, providerFactory), toIdentity},
Expand All @@ -178,7 +178,7 @@ func MakeTokenWithDefault[T any](name string, providerFactory DIE.ProviderFactor

// MakeMultiToken creates a [MultiInjectionToken]
func MakeMultiToken[T any](name string) MultiInjectionToken[T] {
id := genId()
id := genID()
toItem := toType[T]()
toContainer := toArrayType(toItem)
containerName := fmt.Sprintf("Container[%s]", name)
Expand Down
2 changes: 1 addition & 1 deletion either/either.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func MonadChainFirst[E, A, B any](ma Either[E, A], f func(a A) Either[E, B]) Eit
)
}

func MonadChainTo[A, E, B any](ma Either[E, A], mb Either[E, B]) Either[E, B] {
func MonadChainTo[A, E, B any](_ Either[E, A], mb Either[E, B]) Either[E, B] {
return mb
}

Expand Down
4 changes: 2 additions & 2 deletions eq/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type eq[T any] struct {
c func(x, y T) bool
}

func (self eq[T]) Equals(x, y T) bool {
return self.c(x, y)
func (e eq[T]) Equals(x, y T) bool {
return e.c(x, y)
}

func strictEq[A comparable](a, b A) bool {
Expand Down
41 changes: 37 additions & 4 deletions http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ var (
Monoid = ENDO.Monoid[*Builder]()

// Url is a [L.Lens] for the URL
Url = L.MakeLensRef((*Builder).GetUrl, (*Builder).SetUrl)
//
// Deprecated: use [URL] instead
Url = L.MakeLensRef((*Builder).GetURL, (*Builder).SetURL)
// 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
Expand All @@ -83,8 +87,12 @@ var (
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
// WithUrl creates a [Endomorphism] for the URL
//
// Deprecated: use [WithURL] instead
WithUrl = URL.Set
// WithURL creates a [Endomorphism] for the URL
WithURL = URL.Set
// WithHeaders creates a [Endomorphism] for a set of headers
WithHeaders = Headers.Set
// WithBody creates a [Endomorphism] for a request body
Expand Down Expand Up @@ -148,7 +156,14 @@ func (builder *Builder) clone() *Builder {
}

// GetTargetUrl constructs a full URL with query parameters on top of the provided URL string
//
// Deprecated: use [GetTargetURL] instead
func (builder *Builder) GetTargetUrl() E.Either[error, string] {
return builder.GetTargetURL()
}

// 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,
Expand Down Expand Up @@ -176,10 +191,15 @@ func (builder *Builder) GetTargetUrl() E.Either[error, string] {
)
}

// Deprecated: use [GetURL] instead
func (builder *Builder) GetUrl() string {
return builder.url
}

func (builder *Builder) GetURL() string {
return builder.url
}

func (builder *Builder) GetMethod() string {
return F.Pipe1(
builder.method,
Expand Down Expand Up @@ -209,11 +229,17 @@ func (builder *Builder) SetMethod(method string) *Builder {
return builder
}

// Deprecated: use [SetURL] instead
func (builder *Builder) SetUrl(url string) *Builder {
builder.url = url
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
Expand Down Expand Up @@ -278,14 +304,21 @@ func WithoutHeader(name string) Endomorphism {
}

// WithJson creates a [Endomorphism] to send JSON payload
//
// Deprecated: use [WithJSON] instead
func WithJson[T any](data T) Endomorphism {
return WithJSON[T](data)
}

// 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),
WithContentType(C.JSON),
)
}

Expand Down
4 changes: 2 additions & 2 deletions http/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBuilder(t *testing.T) {

b1 := F.Pipe1(
Default,
withContentType(C.Json),
withContentType(C.JSON),
)

b2 := F.Pipe1(
Expand All @@ -48,7 +48,7 @@ func TestBuilder(t *testing.T) {
)

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.JSON), b1.GetHeader(name))
assert.Equal(t, O.Of(C.TextPlain), b2.GetHeader(name))
assert.Equal(t, O.None[string](), b3.GetHeader(name))
}
Expand Down
3 changes: 2 additions & 1 deletion http/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package content

const (
TextPlain = "text/plain"
Json = "application/json"
JSON = "application/json"
Json = JSON // Deprecated: use [JSON] instead
FormEncoded = "application/x-www-form-urlencoded"
)
16 changes: 10 additions & 6 deletions http/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,33 @@ type (

var (
// mime type to check if a media type matches
reJsonMimeType = regexp.MustCompile(`application/(?:\w+\+)?json`)
reJSONMimeType = regexp.MustCompile(`application/(?:\w+\+)?json`)
// ValidateResponse validates an HTTP response and returns an [E.Either] if the response is not a success
ValidateResponse = E.FromPredicate(isValidStatus, StatusCodeError)
// alidateJsonContentTypeString parses a content type a validates that it is valid JSON
validateJsonContentTypeString = F.Flow2(
validateJSONContentTypeString = F.Flow2(
ParseMediaType,
E.ChainFirst(F.Flow2(
T.First[string, map[string]string],
E.FromPredicate(reJsonMimeType.MatchString, func(mimeType string) error {
E.FromPredicate(reJSONMimeType.MatchString, func(mimeType string) error {
return fmt.Errorf("mimetype [%s] is not a valid JSON content type", mimeType)
}),
)),
)
// ValidateJsonResponse checks if an HTTP response is a valid JSON response
ValidateJsonResponse = F.Flow2(
// ValidateJSONResponse checks if an HTTP response is a valid JSON response
ValidateJSONResponse = F.Flow2(
E.Of[error, *H.Response],
E.ChainFirst(F.Flow5(
GetHeader,
R.Lookup[H.Header](HeaderContentType),
O.Chain(A.First[string]),
E.FromOption[string](errors.OnNone("unable to access the [%s] header", HeaderContentType)),
E.ChainFirst(validateJsonContentTypeString),
E.ChainFirst(validateJSONContentTypeString),
)))
// ValidateJsonResponse checks if an HTTP response is a valid JSON response
//
// Deprecated: use [ValidateJSONResponse] instead
ValidateJsonResponse = ValidateJSONResponse
)

const (
Expand Down
4 changes: 2 additions & 2 deletions http/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Error[A any](t *testing.T) func(E.Either[error, A]) bool {
func TestValidateJsonContentTypeString(t *testing.T) {

res := F.Pipe1(
validateJsonContentTypeString(C.Json),
validateJSONContentTypeString(C.JSON),
NoError[ParsedMediaType](t),
)

Expand All @@ -49,7 +49,7 @@ func TestValidateJsonContentTypeString(t *testing.T) {
func TestValidateInvalidJsonContentTypeString(t *testing.T) {

res := F.Pipe1(
validateJsonContentTypeString("application/xml"),
validateJSONContentTypeString("application/xml"),
Error[ParsedMediaType](t),
)

Expand Down

0 comments on commit 909f7c3

Please sign in to comment.