Skip to content

Commit

Permalink
Close #354 - Replace opaque types (newtypes) with refined4s
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-lee committed Dec 31, 2023
1 parent 997014c commit 1e1c121
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 181 deletions.
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ lazy val props =
val Scala2Version = "2.13.10"
val Scala3Version = "3.2.2"
val CrossScalaVersions = List("2.12.17", Scala2Version, Scala3Version).distinct
val ProjectScalaVersion = Scala2Version
// val ProjectScalaVersion = Scala3Version
// val ProjectScalaVersion = Scala2Version
val ProjectScalaVersion = Scala3Version

val CatsVersion = "2.9.0"
val CatsEffectVersion = "3.5.2"
Expand All @@ -145,7 +145,7 @@ lazy val props =

val ScalaXml2Version = "2.1.0"

val Refined4sVersion = "0.7.0"
val Refined4sVersion = "0.8.0"

val ExtrasVersion = "0.44.0"

Expand Down Expand Up @@ -211,7 +211,7 @@ def libraryDependenciesPostProcess(

def scalacOptionsPostProcess(scalaVersion: String, options: Seq[String]): Seq[String] =
if (scalaVersion.startsWith("3.")) {
options
options ++ List("-explain")
// scala3cLanguageOptions ++
// options.filterNot(o =>
// o == "-language:dynamics,existentials,higherKinds,reflectiveCalls,experimental.macros,implicitConversions" || o == "UTF-8",
Expand Down
24 changes: 6 additions & 18 deletions modules/maven2sbt-core/src/main/scala-3/maven2sbt/core/Libs.scala
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package maven2sbt.core

import refined4s.*

/** @author Kevin Lee
* @since 2021-03-04
*/
final case class Libs(dependencies: List[(Libs.LibValName, Dependency)])

object Libs extends LibsPlus {

type LibsName = LibsName.LibsName
object LibsName {
opaque type LibsName = String
def apply(libsName: String): LibsName = libsName

given libsNameCanEqual: CanEqual[LibsName, LibsName] = CanEqual.derived

extension (libsName: LibsName) def value: String = libsName
}

type LibValName = LibValName.LibValName
object LibValName {
opaque type LibValName = String
def apply(libValName: String): LibValName = libValName

given libValNameCanEqual: CanEqual[LibValName, LibValName] = CanEqual.derived
type LibsName = LibsName.Type
object LibsName extends Newtype[String]

extension (libValName: LibValName) def value: String = libValName
}
type LibValName = LibValName.Type
object LibValName extends Newtype[String]

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package maven2sbt.core

import refined4s.*

import scala.xml.Elem
import StringUtils._
import StringUtils.*

/** @author Kevin Lee
* @since 2019-04-21
Expand All @@ -10,25 +12,10 @@ final case class MavenProperty(key: MavenProperty.Name, value: MavenProperty.Val

object MavenProperty extends MavenPropertyPlus {

type Name = Name.Name
object Name {
opaque type Name = String
def apply(name: String): Name = name

given nameCanEqual: CanEqual[Name, Name] = CanEqual.derived

extension (name: Name) def value: String = name
}

type Value = Value.Value
object Value {
opaque type Value = String
def apply(value: String): Value = value

given valueCanEqual: CanEqual[Value, Value] = CanEqual.derived

extension (value: Value) def value: String = value
type Name = Name.Type
object Name extends Newtype[String]

}
type Value = Value.Type
object Value extends Newtype[String]

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package maven2sbt.core

import refined4s.*

/** @author Kevin Lee
* @since 2020-12-13
*/
object Props extends PropsPlus {

type PropsName = PropsName.PropsName
object PropsName {
opaque type PropsName = String
def apply(propsName: String): PropsName = propsName

given propsNameCanEqual: CanEqual[PropsName, PropsName] = CanEqual.derived

extension (propsName: PropsName) def value: String = propsName
}
type PropsName = PropsName.Type
object PropsName extends Newtype[String]

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package maven2sbt.core

import Repository._
import Repository.*
import cats.Show
import cats.syntax.all._
import cats.syntax.all.*

import refined4s.*
import refined4s.modules.cats.derivation.*

import scala.xml.Elem

Expand All @@ -13,44 +16,13 @@ final case class Repository(id: Option[RepoId], name: Option[RepoName], url: Rep

object Repository extends RepositoryPlus {

type RepoId = RepoId.RepoId
object RepoId {
opaque type RepoId = String
def apply(repoId: String): RepoId = repoId

def unapply(repoId: RepoId): Option[String] =
repoId.value.some

given repoIdCanEqual: CanEqual[RepoId, RepoId] = CanEqual.derived

extension (repoId: RepoId) def value: String = repoId

given show: Show[RepoId] = _.value
}

type RepoName = RepoName.RepoName
object RepoName {
opaque type RepoName = String
def apply(repoName: String): RepoName = repoName

given repoNameCanEqual: CanEqual[RepoName, RepoName] = CanEqual.derived

def unapply(repoName: RepoName): Option[String] =
repoName.value.some

extension (repoName: RepoName) def value: String = repoName

given show: Show[RepoName] = _.value
}

type RepoUrl = RepoUrl.RepoUrl
object RepoUrl {
opaque type RepoUrl = String
def apply(repoUrl: String): RepoUrl = repoUrl
type RepoId = RepoId.Type
object RepoId extends Newtype[String], CatsEqShow[String]

given repoUrlCanEqual: CanEqual[RepoUrl, RepoUrl] = CanEqual.derived
type RepoName = RepoName.Type
object RepoName extends Newtype[String], CatsEqShow[String]

extension (repoUrl: RepoUrl) def value: String = repoUrl
}
type RepoUrl = RepoUrl.Type
object RepoUrl extends Newtype[String], CatsEqShow[String]

}
106 changes: 15 additions & 91 deletions modules/maven2sbt-core/src/main/scala-3/maven2sbt/core/data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,33 @@ import just.fp.Named

import cats.*

import refined4s.*
import refined4s.modules.cats.derivation.*

/** @author Kevin Lee
* @since 2019-04-22
*/
object data {

type GroupId = GroupId.GroupId
object GroupId {
opaque type GroupId = String

def apply(groupId: String): GroupId = groupId

given canEqualGroupId: CanEqual[GroupId, GroupId] = CanEqual.derived

given eq: Eq[GroupId] = Eq.fromUniversalEquals[GroupId]

extension (groupId: GroupId) {
def value: String = groupId

// def render(propsName: Props.PropsName): RenderedString =
// StringUtils.renderWithProps(propsName, groupId.value)
}

given show: Show[GroupId] = _.value
type GroupId = GroupId.Type
object GroupId extends Newtype[String], CatsEqShow[String] {

given named: Named[GroupId] = Named.named("organization")

given groupIdRender: Render[GroupId] =
Render.namedRender("groupId", (propsName, groupId) => StringUtils.renderWithProps(propsName, groupId.value))

// The reason to have `Some[String]` as a return type here is
// https://github.com/scala/bug/issues/12232
// So sad :(
def unapply(groupId: GroupId): Some[String] =
Some(groupId.value)

}

type ArtifactId = ArtifactId.ArtifactId
object ArtifactId {
opaque type ArtifactId = String
def apply(artifactId: String): ArtifactId = artifactId

given artifactIdCanEqual: CanEqual[ArtifactId, ArtifactId] = CanEqual.derived
type ArtifactId = ArtifactId.Type
object ArtifactId extends Newtype[String], CatsEqShow[String] {

extension (artifactId: ArtifactId) {
def value: String = artifactId

def render(propsName: Props.PropsName): RenderedString =
StringUtils.renderWithProps(propsName, artifactId.value)

}

given eq: Eq[ArtifactId] = Eq.fromUniversalEquals[ArtifactId]

given show: Show[ArtifactId] = _.value

given named: Named[ArtifactId] = Named.named("name")

given artifactIdRender: Render[ArtifactId] =
Expand All @@ -68,63 +39,33 @@ object data {
(propsName, artifactId) => artifactId.render(propsName),
)

// The reason to have `Some[String]` as a return type here is
// https://github.com/scala/bug/issues/12232
// So sad :(
def unapply(artifactId: ArtifactId): Some[String] =
Some(artifactId.value)

}

type Version = Version.Version
object Version {
opaque type Version = String
def apply(version: String): Version = version

given versionCanEqual: CanEqual[Version, Version] = CanEqual.derived
type Version = Version.Type
object Version extends Newtype[String], CatsEqShow[String] {

extension (version: Version) {
def value: String = version

def render(propsName: Props.PropsName): RenderedString =
StringUtils.renderWithProps(propsName, version.value)
}

given eq: Eq[Version] = Eq.fromUniversalEquals[Version]

given show: Show[Version] = _.value

given named: Named[Version] = Named.named("version")

given versionRender: Render[Version] =
Render.namedRender("version", (propsName, version) => version.render(propsName))

// The reason to have `Some[String]` as a return type here is
// https://github.com/scala/bug/issues/12232
// So sad :(
def unapply(version: Version): Some[String] =
Some(version.value)

}

type ScalaVersion = ScalaVersion.ScalaVersion
object ScalaVersion {
opaque type ScalaVersion = String
def apply(scalaVersion: String): ScalaVersion = scalaVersion

given scalaVersionCanEqual: CanEqual[ScalaVersion, ScalaVersion] = CanEqual.derived
type ScalaVersion = ScalaVersion.Type
object ScalaVersion extends Newtype[String], CatsEqShow[String] {

extension (scalaVersion: ScalaVersion) {
def value: String = scalaVersion

def render(propsName: Props.PropsName): RenderedString =
StringUtils.renderWithProps(propsName, scalaVersion.value)
}

given eq: Eq[ScalaVersion] = Eq.fromUniversalEquals[ScalaVersion]

given show: Show[ScalaVersion] = _.value

given named: Named[ScalaVersion] = Named.named("scalaVersion")

given scalaVersionRender: Render[ScalaVersion] =
Expand All @@ -133,30 +74,13 @@ object data {
(propsName, scalaVersion) => scalaVersion.render(propsName),
)

def unapply(scalaVersion: ScalaVersion): Option[String] =
Option(scalaVersion.value)

}

type ScalaBinaryVersion = ScalaBinaryVersion.ScalaBinaryVersion
object ScalaBinaryVersion {
opaque type ScalaBinaryVersion = String
def apply(scalaBinaryVersion: String): ScalaBinaryVersion = scalaBinaryVersion

given scalaBinaryVersionCanEqual: CanEqual[ScalaBinaryVersion, ScalaBinaryVersion] = CanEqual.derived

extension (scalaBinaryVersion: ScalaBinaryVersion) def value: String = scalaBinaryVersion

type Name = Name.Name
object Name {
opaque type Name = String
def apply(name: String): Name = name
type ScalaBinaryVersion = ScalaBinaryVersion.Type
object ScalaBinaryVersion extends Newtype[String], CatsEqShow[String] {

given nameCanEqual: CanEqual[Name, Name] = CanEqual.derived

extension (name: Name) def value: String = name

}
type Name = Name.Type
object Name extends Newtype[String], CatsEqShow[String]
}

extension (s: RenderedString) {
Expand Down

0 comments on commit 1e1c121

Please sign in to comment.