Skip to content

Commit

Permalink
Merge pull request #3526 from scala-steward-org/topic/tweak-refined-code
Browse files Browse the repository at this point in the history
Tweak code that uses refined so that it compiles with Scala 3
  • Loading branch information
fthomas authored Jan 7, 2025
2 parents dd4b207 + a84aba9 commit a35f3dc
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.scalasteward.core.application
import cats.effect._
import cats.effect.implicits._
import cats.syntax.all._
import eu.timepit.refined.auto._
import eu.timepit.refined.types.numeric.PosInt
import org.http4s.Uri
import org.http4s.client.Client
import org.http4s.headers.`User-Agent`
Expand Down Expand Up @@ -94,7 +94,7 @@ object Context {
userAgent <- Resource.eval(F.fromEither(`User-Agent`.parse(1)(userAgentString)))
middleware = ClientConfiguration
.setUserAgent[F](userAgent)
.andThen(ClientConfiguration.retryAfter[F](maxAttempts = 5))
.andThen(ClientConfiguration.retryAfter[F](maxAttempts = PosInt.unsafeFrom(5)))
defaultClient <- ClientConfiguration.build(
ClientConfiguration.BuilderMiddleware.default,
middleware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package org.scalasteward.core.client

import cats.effect._
import cats.syntax.all._
import eu.timepit.refined.auto._
import eu.timepit.refined.types.numeric.PosInt
import java.net.http.HttpClient
import java.net.http.HttpClient.Builder
Expand Down Expand Up @@ -69,7 +68,7 @@ object ClientConfiguration {
* max number times the HTTP request should be sent useful to avoid unexpected cloud provider
* costs
*/
def retryAfter[F[_]: Temporal](maxAttempts: PosInt = 5): Middleware[F] = { client =>
def retryAfter[F[_]: Temporal](maxAttempts: PosInt): Middleware[F] = { client =>
Client[F] { req =>
def run(attempt: Int = 1): Resource[F, Response[F]] = client
.run(req.putHeaders("X-Attempt" -> attempt.toString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import io.circe.{Decoder, Encoder, HCursor, Json}
import org.scalasteward.core.repoconfig.PullRequestGroup
import org.scalasteward.core.util
import org.scalasteward.core.util.Nel
import org.scalasteward.core.util.string.MinLengthString

sealed trait Update {

Expand Down Expand Up @@ -137,7 +136,7 @@ object Update {
val possibleMainArtifactIds = for {
prefix <- artifactIdsPrefix.toList
suffix <- commonSuffixes
} yield prefix.value + suffix
} yield prefix + suffix

artifactIds
.map(_.name)
Expand All @@ -151,8 +150,8 @@ object Update {
override def newerVersions: Nel[Version] =
forArtifactIds.head.newerVersions

def artifactIdsPrefix: Option[MinLengthString[3]] =
util.string.longestCommonPrefixGreater[3](artifactIds.map(_.name))
def artifactIdsPrefix: Option[String] =
util.string.longestCommonPrefixGteq(artifactIds.map(_.name), 3)
}

val commonSuffixes: List[String] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ object Selector {
private def heuristic1SearchTerms(update: Update.Single): List[String] = {
val terms = update match {
case s: Update.ForArtifactId => List(s.artifactId.name)
case g: Update.ForGroupId =>
g.artifactIds.map(_.name).toList ++ g.artifactIdsPrefix.map(_.value).toList
case g: Update.ForGroupId => g.artifactIds.map(_.name).toList ++ g.artifactIdsPrefix.toList
}
terms.map(Update.nameOf(update.groupId, _))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ package org.scalasteward.core.util

import cats.Foldable
import cats.syntax.all._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.MinSize
import eu.timepit.refined.refineV
import scala.util.matching.Regex
import shapeless.Witness

object string {
type MinLengthString[N] = String Refined MinSize[N]

/** Extracts words from a string.
*
Expand Down Expand Up @@ -53,10 +48,10 @@ object string {
s1.substring(0, i)
}

def longestCommonPrefixGreater[N <: Int: Witness.Aux](
xs: Nel[String]
): Option[MinLengthString[N]] =
refineV[MinSize[N]](xs.reduceLeft(longestCommonPrefix)).toOption
def longestCommonPrefixGteq(xs: Nel[String], n: Int): Option[String] = {
val prefix = xs.reduceLeft(longestCommonPrefix)
Option.when(prefix.length >= n)(prefix)
}

def removeSuffix(target: String, suffixes: List[String]): String =
suffixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.scalasteward.core.client

import cats.effect._
import cats.implicits._
import eu.timepit.refined.auto._
import eu.timepit.refined.types.numeric.PosInt
import munit.CatsEffectSuite
import org.http4s.HttpRoutes
Expand Down Expand Up @@ -116,12 +115,12 @@ class ClientConfigurationTest extends CatsEffectSuite {
retryAfter(initialClient)
}

val notEnoughRetries = clientWithMaxAttempts(1)
val notEnoughRetries = clientWithMaxAttempts(PosInt.unsafeFrom(1))
.run(GET(uri"/retry-after"))
.use(r => r.status.code.pure[IO])
.assertEquals(403)

val exactlyEnoughRetries = clientWithMaxAttempts(2)
val exactlyEnoughRetries = clientWithMaxAttempts(PosInt.unsafeFrom(2))
.run(GET(uri"/retry-after"))
.use(r => r.status.code.pure[IO])
.assertEquals(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class stringTest extends ScalaCheckSuite {
assertEquals(string.longestCommonPrefix("abc", "def"), "")
}

test("longestCommonPrefixGreater") {
assertEquals(string.longestCommonPrefixGreater[1](Nel.of("abcde", "abchk")).get.value, "abc")
assertEquals(string.longestCommonPrefixGreater[3](Nel.of("abcde", "abchk")).get.value, "abc")
assertEquals(string.longestCommonPrefixGreater[3](Nel.of("abcde", "abhk")), None)
test("longestCommonPrefixGteq") {
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abchk"), 1), Some("abc"))
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abchk"), 3), Some("abc"))
assertEquals(string.longestCommonPrefixGteq(Nel.of("abcde", "abhk"), 3), None)
}

test("rightmostLabel") {
Expand Down

0 comments on commit a35f3dc

Please sign in to comment.