Skip to content

Commit

Permalink
Add support for java.time.Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Aug 12, 2024
1 parent 1d8058f commit 791ec04
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/args.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ Args.integer(name)

### Date/Time Args
The following methods produce `Args` whose type parameter is a Date/Time type of the `java.time` library.
- Produces `Args[java.time.Duration]`. The input must be a time-based amount of time in the ISO-8601 format, such as 'P1DT2H3M'.
```scala mdoc:silent
Args.duration(name)
```
- Produces `Args[java.time.Instant]`. The input must be an instant in time in UTC format, such as 2007-12-03T10:15:30.00Z.
```scala mdoc:silent
Args.instant(name)
Expand Down
4 changes: 4 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ Options.integer(name)

### Date/Time Options
The following methods produce `Options` whose type parameter is a Date/Time type of the `java.time` library.
- Produces `Options[java.time.Duration]`. The input must be a time-based amount of time in the ISO-8601 format, such as 'P1DT2H3M'.
```scala mdoc:silent
Options.duration(name)
```
- Produces `Options[java.time.Instant]`. The input must be an instant in time in UTC format, such as 2007-12-03T10:15:30.00Z.
```scala mdoc:silent
Options.instant(name)
Expand Down
2 changes: 2 additions & 0 deletions zio-cli/jvm/src/test/scala/zio/cli/PrimTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ object PrimTypeSpec extends ZIOSpecDefault {
Gen.bigDecimal(BigDecimal("1.41421356237309504880168"), BigDecimal("50.4")),
"BigDecimal"
),
simplePrimTypeSuite(PrimType.Duration, anyDuration, "Duration"),
simplePrimTypeSuite(PrimType.Integer, anyBigInt, "Integer"),
simplePrimTypeSuite(PrimType.Instant, Gen.instant, "Instant"),
simplePrimTypeSuite(PrimType.LocalDateTime, anyLocalDateTime, "LocalDateTime"),
Expand Down Expand Up @@ -180,6 +181,7 @@ object PrimTypeSpec extends ZIOSpecDefault {
val anyBigInt: Gen[Any, BigInt] = Gen.long(0, Long.MaxValue).map(BigInt(_))
val anyBoolean: Gen[Any, Boolean] =
Gen.fromIterable(List(true, false))
val anyDuration = Gen.finiteDuration
val anyInstant = Gen.instant.map(_.atZone(ZoneOffset.UTC))
val anyPeriod = for {
first <- Gen.localDateTime.map(_.toLocalDate)
Expand Down
18 changes: 18 additions & 0 deletions zio-cli/shared/src/main/scala/zio/cli/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import zio._
import zio.cli.HelpDoc.{Span, p}

import java.time.{
Duration => JDuration,
Instant => JInstant,
LocalDate => JLocalDate,
LocalDateTime => JLocalDateTime,
Expand Down Expand Up @@ -325,6 +326,23 @@ object Args extends ArgsPlatformSpecific {
val decimal: Args[BigDecimal] =
Single(None, PrimType.Decimal)

/**
* Creates a duration argument with a custom argument name
*
* @param name
* Argument name
* @return
* Duration argument
*/
def duration(name: String): Args[JDuration] =
Single(Some(name), PrimType.Duration)

/**
* Creates a duration argument with 'duration' as argument name
*/
val duration: Args[JDuration] =
Single(None, PrimType.Duration)

/**
* Creates an integer argument with a custom argument name
*
Expand Down
7 changes: 7 additions & 0 deletions zio-cli/shared/src/main/scala/zio/cli/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import zio.{IO, UIO, ZIO, Zippable}
import zio.cli.oauth2._

import java.time.{
Duration => JDuration,
Instant => JInstant,
LocalDate => JLocalDate,
LocalDateTime => JLocalDateTime,
Expand Down Expand Up @@ -811,6 +812,12 @@ object Options extends OptionsPlatformSpecific {
def decimal(name: String): Options[BigDecimal] =
Single(name, Vector.empty, PrimType.Decimal)

/**
* Creates a parameter excepting a date-based amount of time in the ISO-8601 format, such as 'P1DT2H3M'.
*/
def duration(name: String): Options[JDuration] =
Single(name, Vector.empty, PrimType.Duration)

/**
* Creates a parameter expecting an integer.
*/
Expand Down
18 changes: 18 additions & 0 deletions zio-cli/shared/src/main/scala/zio/cli/PrimType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import zio.cli.HelpDoc.Span.text
import zio.cli.files.FileSystem

import java.time.{
Duration => JDuration,
Instant => JInstant,
LocalDate => JLocalDate,
LocalDateTime => JLocalDateTime,
Expand Down Expand Up @@ -205,6 +206,23 @@ object PrimType extends PathPlatformSpecific {
lazy val FalseValues: Set[String] = Set("false", "0", "n", "no", "off")
}

/**
* Type representing a time-based amount of time in the ISO-8601 format, such as 'P1DT2H3M'.
*/
case object Duration extends PrimType[JDuration] { self =>
lazy val isBool: Boolean = false

lazy val typeName: String = "duration"

lazy val choices: Option[String] = None

def validate(value: Option[String], conf: CliConfig): IO[String, JDuration] =
attempt(value, JDuration.parse, self.typeName)

lazy val helpDoc: HelpDoc.Span =
text("A time-based amount of time in the ISO-8601 format, such as 'P1DT2H3M'.")
}

/**
* Type representing parameter for instant in time in UTC format, such as 2007-12-03T10:15:30.00Z.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ object PrimTypeCompletion {
ZIO.succeed(Set("true", "false").filter(_.startsWith(prefix))).map(appendSpaces)
case PrimType.Decimal =>
ZIO.succeed(Set.empty)
case PrimType.Duration =>
ZIO.succeed(Set.empty)
case PrimType.Enumeration(cases @ _*) =>
ZIO
.succeed(cases.collect {
Expand Down

0 comments on commit 791ec04

Please sign in to comment.