• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pureconfig / pureconfig / 16844894943

09 Aug 2025 03:28AM UTC coverage: 94.686% (-0.04%) from 94.726%
16844894943

Pull #1838

web-flow
Merge 12f0062cf into 38f73c635
Pull Request #1838: Rid of deprecated URL constructor

1 of 1 new or added line in 1 file covered. (100.0%)

95 existing lines in 26 files now uncovered.

2744 of 2898 relevant lines covered (94.69%)

2.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/core/src/main/scala/pureconfig/PeriodUtils.scala
1
package pureconfig
2

3
import java.time.Period
4

5
import scala.util.{Success, Try}
6

7
import com.typesafe.config.impl.ConfigImplUtil
8

9
import pureconfig.error.{CannotConvert, FailureReason}
10

11
/** Utility functions for converting a `String` to a `Period`. The parser accepts the HOCON unit syntax.
12
  */
13
private[pureconfig] object PeriodUtils {
14

15
  /** Convert a string to a Period while trying to maintain compatibility with Typesafe's abbreviations.
16
    */
17
  val fromString: String => Either[FailureReason, Period] = { str =>
18
    Try(Right(Period.parse(str))).getOrElse(typesafeConfigParsePeriod(str))
2✔
19
  }
20

21
  // This is a copy of the period parser in `com.typesafe.config.impl.SimpleConfig` adapted to be safer (dealing with
22
  // `Either` values instead of throwing exceptions). Try not to refactor this too much so as to be easy for anyone
23
  // to compare this code with the original.
24
  def typesafeConfigParsePeriod(input: String): Either[FailureReason, Period] = {
1✔
25
    val (rawValueStr, rawUnitStr) = splitUnits(ConfigImplUtil.unicodeTrim(input))
2✔
26
    val valueStr = ConfigImplUtil.unicodeTrim(rawValueStr)
2✔
27
    val unitStr = pluralize(rawUnitStr)
2✔
28

29
    // we use days as the default value in order to be compatible with Typesafe Config
30
    // (https://github.com/lightbend/config/blob/master/HOCON.md#period-format)
31
    Try(valueStr.toInt) match {
2✔
32
      case Success(n) =>
1✔
33
        unitStr match {
1✔
34
          case "" | "d" | "days" => Right(Period.ofDays(n))
2✔
35
          case "w" | "weeks" => Right(Period.ofWeeks(n))
2✔
36
          case "m" | "mo" | "months" => Right(Period.ofMonths(n))
2✔
37
          case "y" | "years" => Right(Period.ofYears(n))
2✔
38
          case _ => Left(CannotConvert(input, "Period", s"Could not parse time unit '$unitStr' (try d, w, mo, y)"))
2✔
39
        }
40
      case _ =>
1✔
41
        Left(CannotConvert(input, "Period", s"Could not parse duration number '$valueStr'"))
2✔
42
    }
43
  }
44

45
  private def splitUnits(s: String): (String, String) =
1✔
46
    s.splitAt(s.lastIndexWhere(!_.isLetter) + 1)
2✔
47

48
  private def pluralize(s: String): String =
1✔
UNCOV
49
    if (s.length > 2 && !s.endsWith("s")) s + "s" else s
1✔
50
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc