This is an automated email from the ASF dual-hosted git repository. arosien pushed a commit to branch daffodil-vscode-tdml in repository https://gitbox.apache.org/repos/asf/daffodil-vscode.git
commit 182b2624b122c06064721d979fa1c7d4d5db2635 Author: Adam Rosien <[email protected]> AuthorDate: Tue Jul 26 10:48:36 2022 -0700 Use Option for TDMLConfig. --- .../org.apache.daffodil.debugger.dap/Parse.scala | 103 ++++++++++----------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/server/core/src/main/scala/org.apache.daffodil.debugger.dap/Parse.scala b/server/core/src/main/scala/org.apache.daffodil.debugger.dap/Parse.scala index 56ce09d..d1f8baf 100644 --- a/server/core/src/main/scala/org.apache.daffodil.debugger.dap/Parse.scala +++ b/server/core/src/main/scala/org.apache.daffodil.debugger.dap/Parse.scala @@ -157,12 +157,30 @@ object Parse { object Debugee { case class LaunchArgs( - schemaPath: Path, - dataPath: Path, + defaultSchemaPath: Path, + defaultDataPath: Path, stopOnEntry: Boolean, infosetOutput: LaunchArgs.InfosetOutput, - tdmlConfig: LaunchArgs.TDMLConfig + tdmlConfig: Option[LaunchArgs.TDMLConfig] ) extends Arguments { + def dataPath: Path = + tdmlConfig + .map { + case LaunchArgs.TDMLConfig(_, name, description, path) => + val (_, dataPath) = TDMLWrapper.execute(defaultSchemaPath, defaultDataPath, name, description, path) + dataPath + } + .getOrElse(defaultDataPath) + + def schemaPath: Path = + tdmlConfig + .map { + case LaunchArgs.TDMLConfig(_, name, description, path) => + val (schemaPath, _) = TDMLWrapper.execute(defaultSchemaPath, defaultDataPath, name, description, path) + schemaPath + } + .getOrElse(defaultSchemaPath) + def data: IO[InputStream] = IO.blocking(FileUtils.readFileToByteArray(dataPath.toFile)) .map(new ByteArrayInputStream(_)) @@ -170,7 +188,6 @@ object Parse { object LaunchArgs { sealed trait InfosetOutput - sealed trait TDMLConfig object InfosetOutput { case object None extends InfosetOutput @@ -178,10 +195,7 @@ object Parse { case class File(path: Path) extends InfosetOutput } - object TDMLConfig { - case object None extends TDMLConfig - case class Config(action: String, name: String, description: String, path: String) extends TDMLConfig - } + case class TDMLConfig(action: String, name: String, description: String, path: String) def parse(arguments: JsonObject): EitherNel[String, LaunchArgs] = ( @@ -236,28 +250,31 @@ object Parse { } }, Option(arguments.getAsJsonObject("tdmlConfig")) match { - case None => Right(LaunchArgs.TDMLConfig.None).toEitherNel + case None => Right(Option.empty[LaunchArgs.TDMLConfig]).toEitherNel case Some(tdmlConfig) => Option(tdmlConfig.getAsJsonPrimitive("action")) match { - case None => Right(LaunchArgs.TDMLConfig.None).toEitherNel + case None => Right(Option.empty[LaunchArgs.TDMLConfig]).toEitherNel case Some(action) => action.getAsString() match { - case "none" => Right(LaunchArgs.TDMLConfig.None).toEitherNel + case "none" => Right(Option.empty[LaunchArgs.TDMLConfig]).toEitherNel case "generate" | "append" | "execute" => Right( - LaunchArgs.TDMLConfig.Config( - Option(tdmlConfig.getAsJsonPrimitive("action")) - .map(_.getAsString()) - .getOrElse("None"), - Option(tdmlConfig.getAsJsonPrimitive("name")) - .map(_.getAsString()) - .getOrElse("Default Test Case"), - Option(tdmlConfig.getAsJsonPrimitive("description")) - .map(_.getAsString()) - .getOrElse("Generated by DFDL VSCode Extension"), - Option(tdmlConfig.getAsJsonPrimitive("path")) - .map(_.getAsString()) - .getOrElse("") + // TODO: use mapN instead of providing default empty values + Some( + LaunchArgs.TDMLConfig( + Option(tdmlConfig.getAsJsonPrimitive("action")) + .map(_.getAsString()) + .getOrElse("None"), + Option(tdmlConfig.getAsJsonPrimitive("name")) + .map(_.getAsString()) + .getOrElse("Default Test Case"), + Option(tdmlConfig.getAsJsonPrimitive("description")) + .map(_.getAsString()) + .getOrElse("Generated by DFDL VSCode Extension"), + Option(tdmlConfig.getAsJsonPrimitive("path")) + .map(_.getAsString()) + .getOrElse("") + ) ) ).toEitherNel case invalidType => @@ -277,15 +294,6 @@ object Parse { def debugee(request: Request): EitherNel[String, Resource[IO, DAPodil.Debugee]] = Debugee.LaunchArgs .parse(request.arguments) - .map { args => - // TODO: move this into Debugee.LaunchArgs - args.tdmlConfig match { - case Debugee.LaunchArgs.TDMLConfig.Config("execute", name, description, tdmlPath) => - val (schemaPath, dataPath) = TDMLWrapper.execute(args.schemaPath, args.dataPath, name, description, tdmlPath) - args.copy(schemaPath = schemaPath, dataPath = dataPath) - case _ => args - } - } .map(debugee) def debugee(args: Debugee.LaunchArgs): Resource[IO, DAPodil.Debugee] = @@ -371,7 +379,7 @@ object Parse { dapEvents.offer(None) // ensure dapEvents is terminated when the parse is terminated ) ++ Stream.eval( args.tdmlConfig match { - case Debugee.LaunchArgs.TDMLConfig.Config(action, name, description, tdmlPath) => + case Some(Debugee.LaunchArgs.TDMLConfig(action, name, description, tdmlPath)) => if (action == "generate") args.infosetOutput match { case Debugee.LaunchArgs.InfosetOutput.File(path) => @@ -662,7 +670,7 @@ object Parse { dataPath: String, stopOnEntry: Boolean, infosetOutput: InfosetOutput, - tdmlConfig: TDMLConfig + tdmlConfig: Option[TDMLConfig] ) sealed trait InfosetOutput { @@ -673,38 +681,25 @@ object Parse { case InfosetOutput.File(_) => "file" } } - sealed trait TDMLConfig { - val action: String - val name: String - val description: String - val path: String - } object InfosetOutput { case object None extends InfosetOutput case object Console extends InfosetOutput case class File(path: String) extends InfosetOutput - + def apply(that: Debugee.LaunchArgs.InfosetOutput): InfosetOutput = that match { case Debugee.LaunchArgs.InfosetOutput.None => None case Debugee.LaunchArgs.InfosetOutput.Console => Console case Debugee.LaunchArgs.InfosetOutput.File(path) => File(path.toString) } - } - object TDMLConfig { - case class Config(action: String, name: String, description: String, path: String) extends TDMLConfig - case object None extends TDMLConfig { - val action = "none" - val name = "Default Test Case Name" - val description = "Generated by DFDL VSCode Extension" - val path = "" } + case class TDMLConfig(action: String, name: String, description: String, path: String) + object TDMLConfig { def apply(that: Debugee.LaunchArgs.TDMLConfig): TDMLConfig = that match { - case Debugee.LaunchArgs.TDMLConfig.None => None - case Debugee.LaunchArgs.TDMLConfig.Config(action, name, description, path) => - Config(action, name, description, path) + case Debugee.LaunchArgs.TDMLConfig(action, name, description, path) => + TDMLConfig(action, name, description, path) } } @@ -717,7 +712,7 @@ object Parse { launchArgs.dataPath.toString(), launchArgs.stopOnEntry, InfosetOutput(launchArgs.infosetOutput), - TDMLConfig(launchArgs.tdmlConfig) + launchArgs.tdmlConfig.map(ConfigEvent.TDMLConfig.apply) ), BuildInfo( DAPBuildInfo.version,
