Skip to content

Commit

Permalink
date range constraint: error when date is not correctly parsed
Browse files Browse the repository at this point in the history
Also changed the date in example to make it more plausible.
  • Loading branch information
hrj committed Sep 8, 2016
1 parent 9b3ff51 commit 40dacb3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
22 changes: 17 additions & 5 deletions base/src/main/scala/co/uproot/abandon/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object SettingsHelper {
val accountConfigs = config.optConfigList("accounts").getOrElse(Nil)
val accounts = accountConfigs.map(makeAccountSettings)
val eodConstraints = config.optConfigList("eodConstraints").getOrElse(Nil).map(makeEodConstraints(_))
val dateConstraints = config.optConfigList("dateConstraints").getOrElse(Nil).map(makeDateConstraints(_))
val dateConstraints = config.optConfigList("dateConstraints").getOrElse(Nil).map(makeDateConstraint(_))
Right(Settings(inputs, eodConstraints ++ dateConstraints, accounts, reports, ReportOptions(isRight), exports, Some(file)))
} catch {
case e: ConfigException => Left(e.getMessage)
Expand All @@ -101,11 +101,23 @@ object SettingsHelper {
}
}

def makeDateConstraints(config: Config): DateConstraint = {
val from = AbandonParser.dateExpr(ParserHelper.scanner(config.getString("from"))).map(Some(_)).getOrElse(None)
val to = AbandonParser.dateExpr(ParserHelper.scanner(config.getString("to"))).map(Some(_)).getOrElse(None)
private def getDate(name: String, config: Config) : Option[Date] = {
import AbandonParser.{Success, NoSuccess}

DateConstraint(from, to)
config.optional(name) { _.getString(_) } match {
case Some(valueStr) =>
val parseResult = AbandonParser.dateExpr(ParserHelper.scanner(valueStr))
parseResult match {
case Success(date, _) => Option(date)
case NoSuccess(_, _) =>
throw new ConfigException.BadValue(config.origin, name, "expected date expression")
}
case None => None
}
}

private def makeDateConstraint(config: Config): DateConstraint = {
DateConstraint(getDate("from", config), getDate("to", config))
}

def makeReportSettings(config: Config) = {
Expand Down
8 changes: 5 additions & 3 deletions examples/complete/accounts.conf
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ exports += {

}

// TODO: Change constraint "from" to "2012/april/1" when GH-93 is implemented

dateConstraints += {
from = "2012-01-01"
to = "2013-12-31"
}
from = "2012/march/31"
to = "2013/march/31"
}
2 changes: 1 addition & 1 deletion examples/complete/opening.ledger
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

; March 2012
2012/3/1 OpeningBalance
2012/3/31 OpeningBalance
Capital:Share:Hary 6,000
Capital:Share:Amy 4,000
Capital:Reserves_And_Surplus -1,500
Expand Down

0 comments on commit 40dacb3

Please sign in to comment.