Skip to content

Commit

Permalink
Update apply scalafmt (#211)
Browse files Browse the repository at this point in the history
* Update scalafmt

* Apply scalafmt
  • Loading branch information
mdedetrich authored Mar 11, 2022
1 parent 376b2b0 commit 22869d9
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: Check project is formatted
uses: jrouly/scalafmt-native-action@v1
with:
version: '2.7.5'
version: '3.4.3'
15 changes: 9 additions & 6 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
version = 2.7.5
version = 3.4.3
runner.dialect = scala213
preset = default
align.preset = most
maxColumn = 120
project.git = true
align.tokens.add = [
docstrings.style = AsteriskSpace
align.tokens."+" = [
{code = ":=", owner = "Term.ApplyInfix"}
]
rewrite.rules = [RedundantBraces, RedundantParens]

# TODO update scalafmt for Scala 3 support
project.excludeFilters = [
"src/main/scala-3/net/ceedubs/ficus/util/EnumerationUtil.scala"
]
fileOverride {
"glob:**/src/main/scala-3/**" {
runner.dialect = scala3
}
}
19 changes: 10 additions & 9 deletions src/main/scala/net/ceedubs/ficus/readers/DurationReaders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ import scala.util.Try
trait DurationReaders {

/** A reader for for a scala.concurrent.duration.FiniteDuration. This reader should be able to read any valid duration
* format as defined by the <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">HOCON spec</a>.
* For example, it can read "15 minutes" or "1 day".
*/
* format as defined by the <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">HOCON spec</a>. For
* example, it can read "15 minutes" or "1 day".
*/
implicit def finiteDurationReader: ValueReader[FiniteDuration] = new ValueReader[FiniteDuration] {
def read(config: Config, path: String): FiniteDuration = {
val nanos = config.getDuration(path, NANOSECONDS)
Duration.fromNanos(nanos)
}
}

/** A reader for for a scala.concurrent.duration.Duration. This reader should be able to read any valid duration format
* as defined by the <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">HOCON spec</a> and positive
* and negative infinite values supported by Duration's <a href="http://www.scala-lang.org/api/current/scala/
* concurrent/duration/Duration$.html#apply(s:String):scala.concurrent.duration.Duration">apply</a> method.
* For example, it can read "15 minutes", "1 day", "-Inf", or "PlusInf".
*/
/** A reader for for a scala.concurrent.duration.Duration. This reader should be able to read any valid duration
* format as defined by the <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">HOCON spec</a> and
* positive and negative infinite values supported by Duration's <a
* href="http://www.scala-lang.org/api/current/scala/
* concurrent/duration/Duration$.html#apply(s:String):scala.concurrent.duration.Duration">apply</a> method. For
* example, it can read "15 minutes", "1 day", "-Inf", or "PlusInf".
*/
implicit def durationReader: ValueReader[Duration] = new ValueReader[Duration] {
def read(config: Config, path: String): Duration =
(Try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ trait InetSocketAddressReaders {
implicit val inetSocketAddressListReader: ValueReader[List[InetSocketAddress]] =
new ValueReader[List[InetSocketAddress]] {
def read(config: Config, path: String): List[InetSocketAddress] =
try config
.getString(path)
.split(", *")
.toList
.map(parseHostAndPort)
.partition(_.isEmpty) match {
case (errors, ok) if errors.isEmpty =>
ok.flatten
case _ =>
throw new IllegalArgumentException("Cannot parse string into hosts and ports")
} catch {
try
config
.getString(path)
.split(", *")
.toList
.map(parseHostAndPort)
.partition(_.isEmpty) match {
case (errors, ok) if errors.isEmpty =>
ok.flatten
case _ =>
throw new IllegalArgumentException("Cannot parse string into hosts and ports")
}
catch {
case e: Exception =>
throw new ConfigException.WrongType(config.origin(), path, "java.net.InetSocketAddress", "String", e)
}
}

implicit val inetSocketAddressReader: ValueReader[InetSocketAddress] = new ValueReader[InetSocketAddress] {
def read(config: Config, path: String): InetSocketAddress =
try parseHostAndPort(config.getString(path))
.getOrElse(throw new IllegalArgumentException("Cannot parse string into host and port"))
try
parseHostAndPort(config.getString(path))
.getOrElse(throw new IllegalArgumentException("Cannot parse string into host and port"))
catch {
case e: Exception =>
throw new ConfigException.WrongType(config.origin(), path, "java.net.InetSocketAddress", "String", e)
Expand Down
23 changes: 13 additions & 10 deletions src/main/scala/net/ceedubs/ficus/readers/NameMapper.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package net.ceedubs.ficus.readers

/** Defines an object that knows to map between names as they found in the code
* to those who should be defined in the configuration
*/
/** Defines an object that knows to map between names as they found in the code to those who should be defined in the
* configuration
*/
trait NameMapper {

/** Maps between the name in the code to name in configuration
* @param name The name as found in the code
*/
* @param name
* The name as found in the code
*/
def map(name: String): String

}

/** Helper object to get the current name mapper
*/
*/
object NameMapper {

/** Gets the name mapper from the implicit scope
* @param nameMapper The name mapper from the implicit scope, or the default name mapper if not found
* @return The name mapper to be used in current implicit scope
*/
* @param nameMapper
* The name mapper from the implicit scope, or the default name mapper if not found
* @return
* The name mapper to be used in current implicit scope
*/
def apply()(implicit nameMapper: NameMapper = DefaultNameMapper): NameMapper = nameMapper

}

/** Default implementation for name mapper, names in code equivalent to names in configuration
*/
*/
case object DefaultNameMapper extends NameMapper {

override def map(name: String): String = name
Expand Down
57 changes: 28 additions & 29 deletions src/main/scala/net/ceedubs/ficus/readers/ValueReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ trait ValueReader[A] { self =>
def read(config: Config, path: String): A

/** Turns a ValueReader[A] into a ValueReader[B] by applying the provided transformation `f` on the item of type A
* that is read from config
*/
* that is read from config
*/
def map[B](f: A => B): ValueReader[B] = new ValueReader[B] {
def read(config: Config, path: String): B = f(self.read(config, path))
}
Expand All @@ -19,36 +19,35 @@ trait ValueReader[A] { self =>
object ValueReader {
implicit def generatedReader[A](implicit generated: Generated[ValueReader[A]]): ValueReader[A] = generated.value

/** Returns the implicit ValueReader[A] in scope.
* `ValueReader[A]` is equivalent to `implicitly[ValueReader[A]]`
*/
/** Returns the implicit ValueReader[A] in scope. `ValueReader[A]` is equivalent to `implicitly[ValueReader[A]]`
*/
def apply[A](implicit reader: ValueReader[A]): ValueReader[A] = reader

/** ValueReader that receives a Config whose root is the path being read.
*
* This is generally the most concise way to implement a ValueReader that doesn't depend on the path of the value
* being read.
*
* For example to read a `case class FooBar(foo: Foo, bar: Bar)`, instead of
* {{{
* new ValueReader[FooBar] {
* def read(config: Config, path: String): FooBar = {
* val localizedConfig = config.getConfig(path)
* FooBar(
* foo = localizedConfig.as[Foo]("foo"),
* bar = localizedConfig.as[Bar]("bar"))
* }
* }
* }}}
* you could do
* {{{
* ValueReader.relative[FooBar] { config =>
* FooBar(
* foo = config.as[Foo]("foo"),
* bar = config.as[Bar]("bar))
* }
* }}}
*/
*
* This is generally the most concise way to implement a ValueReader that doesn't depend on the path of the value
* being read.
*
* For example to read a `case class FooBar(foo: Foo, bar: Bar)`, instead of
* {{{
* new ValueReader[FooBar] {
* def read(config: Config, path: String): FooBar = {
* val localizedConfig = config.getConfig(path)
* FooBar(
* foo = localizedConfig.as[Foo]("foo"),
* bar = localizedConfig.as[Bar]("bar"))
* }
* }
* }}}
* you could do
* {{{
* ValueReader.relative[FooBar] { config =>
* FooBar(
* foo = config.as[Foo]("foo"),
* bar = config.as[Bar]("bar))
* }
* }}}
*/
def relative[A](f: Config => A): ValueReader[A] = new ValueReader[A] {
def read(config: Config, path: String): A = f(config.getConfig(path))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ object HyphenNameMapper extends NameMapper {
private lazy val r = "((?<=[a-z0-9])[A-Z]|(?<=[a-zA-Z])[0-9]|(?!^)[A-Z](?=[a-z]))".r

/** Maps from a camelCasedName to a hyphenated-name
*/
*/
override def map(name: String): String = r.replaceAllIn(name, m => s"-${m.group(1)}").toLowerCase
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ object HyphenNameMapperNoDigits extends NameMapper {
private lazy val r = "((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))".r

/** Maps from a camelCasedName to a hyphenated-name
*/
*/
override def map(name: String): String = r.replaceAllIn(name, m => s"-${m.group(1)}").toLowerCase
}

0 comments on commit 22869d9

Please sign in to comment.