From de6a1cde27d41b1364737eb4c145b078f8e0a7c2 Mon Sep 17 00:00:00 2001 From: Mariano de Achaval Date: Thu, 2 Mar 2023 09:55:24 -0300 Subject: [PATCH] Fixing playground and adding a way to specify literal inputs (#77) --- .../cli/pico/AbstractPicoExecCommand.java | 3 ++ .../org/mule/weave/cli/pico/PicoRepl.java | 1 + .../mule/weave/cli/pico/PicoRunScript.java | 1 + .../org/mule/weave/cli/pico/PicoRunSpell.java | 1 + .../cli/commands/AddWizardCommand.scala | 2 +- .../dwnative/cli/commands/ReplCommand.scala | 42 ++++++++++++------- .../cli/commands/RunWeaveCommand.scala | 7 +++- .../dwnative/cli/utils/SpellsUtils.scala | 8 +++- .../weave/dwnative/cli/DataWeaveCLITest.scala | 18 +++++++- 9 files changed, 63 insertions(+), 20 deletions(-) diff --git a/native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java b/native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java index b0f14e5..f973bf4 100644 --- a/native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java +++ b/native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java @@ -21,6 +21,9 @@ public abstract class AbstractPicoExecCommand implements Callable { protected java.util.Map params = null; @CommandLine.Option(names = {"--input", "-i"}, description = {"Declares a new input."}, paramLabel = "") protected java.util.Map inputs = null; + + @CommandLine.Option(names = {"--literal-input", "-li"}, description = {"Declares a new literal input."}, paramLabel = "") + protected java.util.Map literalInput = null; @CommandLine.Option(names = {"--verbose", "-v"}, description = {"Run the script as untrusted, which means that the script has no privileges."}, defaultValue = "false") protected boolean verbose = false; @CommandLine.Option(names = {"--untrusted"}, description = {"Run the script as untrusted, which means that the script has no privileges."}, defaultValue = "false") diff --git a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java index 85ac9c1..1aa358f 100644 --- a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java +++ b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java @@ -29,6 +29,7 @@ protected Integer doCall() { Option.empty(), Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), + Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()), calculateRuntimeVersion() ); diff --git a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java index a1edef4..4bf5fe1 100644 --- a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java +++ b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java @@ -71,6 +71,7 @@ protected Integer doCall() { None$.empty(), Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), + Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.empty()), Option.apply(output).map((s) -> s.getAbsolutePath()), Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()), dataWeaveVersionOption diff --git a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java index d85d2f1..eae5200 100644 --- a/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java +++ b/native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java @@ -138,6 +138,7 @@ protected Integer doCall() { Option.apply(resolver), Optional.ofNullable(params).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.empty()), Optional.ofNullable(inputs).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.empty()), + Optional.ofNullable(literalInput).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.empty()), Option.apply(output).map(File::getAbsolutePath), Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()), dataWeaveVersionOption diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/AddWizardCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/AddWizardCommand.scala index 0081c61..9eff18b 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/AddWizardCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/AddWizardCommand.scala @@ -12,7 +12,7 @@ class AddWizardCommand(config: CloneWizardConfig, console: Console) extends Weav override def exec(): Int = { val wizard = config.wizardName console.info(s"Downloading Grimoire From The Wise: `$wizard`.") - val wizardName = if (wizard == null) "DW" else wizard + val wizardName = if (utils.isDataWeaveWizard(wizard)) "DW" else wizard val wizardFolder = utils.grimoireFolder(wizard) if (wizardFolder.exists()) { console.error(s"Wizard `$wizard` was already added.") diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ReplCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ReplCommand.scala index 8a56040..3902508 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ReplCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ReplCommand.scala @@ -72,6 +72,11 @@ class ReplCommand(config: ReplConfiguration, console: Console) extends WeaveComm config.inputs.foreach((input) => { scriptingBindings.addBinding(input._1, input._2, getMimeTypeByFileExtension(input._2)) }) + + config.literalInputs.foreach((input) => { + scriptingBindings.addBinding(input._1, input._2, None) + }) + val value: Array[KeyValuePair] = config.params.toSeq.map(prop => KeyValuePair(KeyValue(prop._1), StringValue(prop._2)) ).toArray @@ -85,22 +90,30 @@ class ReplCommand(config: ReplConfiguration, console: Console) extends WeaveComm var continue = true while (continue) { System.out.print(">>> ") - var str = input.nextLine() - while (str.endsWith("\\")) { - System.out.print("... ") - str = str.substring(0, str.length - 1) + "\n" + input.nextLine() - } - if (str.equals("quit()")) { - continue = false - } else { - val defaultOutputType: String = console.envVar(DW_DEFAULT_OUTPUT_MIMETYPE_VAR).getOrElse("application/dw") - val result: WeaveExecutionResult = nativeRuntime.run(str, NameIdentifier.ANONYMOUS_NAME.name, scriptingBindings, console.out, defaultOutputType, config.maybePrivileges) - //load inputs from - if (!result.success()) { - System.out.println(result.result()) + try { + var str = input.nextLine() + while (str.endsWith("\\")) { + System.out.print("... ") + str = str.substring(0, str.length - 1) + "\n" + input.nextLine() + } + if (str.equals("quit()")) { + continue = false } else { - System.out.println("") + val defaultOutputType: String = console.envVar(DW_DEFAULT_OUTPUT_MIMETYPE_VAR).getOrElse("application/dw") + val result: WeaveExecutionResult = nativeRuntime.run(str, NameIdentifier.ANONYMOUS_NAME.name, scriptingBindings, console.out, defaultOutputType, config.maybePrivileges) + //load inputs from + if (!result.success()) { + System.out.println(result.result()) + } else { + System.out.println("") + } } + } catch { + case e: Exception => { + console.fatal("Fatal error while reading the input: " + e.getMessage) + return ExitCodes.FAILURE + } + } } ExitCodes.SUCCESS @@ -111,5 +124,6 @@ case class ReplConfiguration(path: Array[String], dependencyResolver: Option[(NativeRuntime) => Array[DependencyResolutionResult]], params: Map[String, String], inputs: Map[String, File], + literalInputs: Map[String, String], maybePrivileges: Option[Seq[String]], maybeLanguageLevel: Option[DataWeaveVersion]) diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala index 92fdfaf..f242eaf 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala @@ -84,12 +84,16 @@ class RunWeaveCommand(val config: WeaveRunnerConfig, console: Console) extends W val defaultInputType: String = console.envVar(DW_DEFAULT_INPUT_MIMETYPE_VAR).getOrElse(DEFAULT_MIME_TYPE) val scriptingBindings: ScriptingBindings = new ScriptingBindings - if (config.inputs.isEmpty) { + if (config.inputs.isEmpty && config.literalInput.isEmpty) { scriptingBindings.addBinding("payload", console.in, defaultInputType) } else { config.inputs.foreach((input) => { scriptingBindings.addBinding(input._1, input._2, getMimeTypeByFileExtension(input._2)) }) + + config.literalInput.foreach((input) => { + scriptingBindings.addBinding(input._1, input._2, None) + }) } val value: Array[KeyValuePair] = config.params.toSeq.map(prop => @@ -169,6 +173,7 @@ case class WeaveRunnerConfig(path: Array[String], dependencyResolver: Option[(NativeRuntime) => Array[DependencyResolutionResult]], params: Map[String, String], inputs: Map[String, File], + literalInput: Map[String, String], outputPath: Option[String], maybePrivileges: Option[Seq[String]], maybeLanguageLevel: Option[DataWeaveVersion]) diff --git a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala index d6de9e7..14891c2 100644 --- a/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala +++ b/native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala @@ -14,7 +14,7 @@ object SpellsUtils { class SpellsUtils(console: Console) { def grimoireName(user: String): String = { - if (user == null) { + if (isDataWeaveWizard(user)) { DATA_WEAVE_GRIMOIRE_FOLDER } else { s"${user}-$DATA_WEAVE_GRIMOIRE_FOLDER" @@ -55,12 +55,16 @@ class SpellsUtils(console: Console) { } def buildRepoUrl(user: String): String = { - val domain = if (user == null) "mulesoft-labs" else user + val domain = if (isDataWeaveWizard(user)) "mulesoft-labs" else user val repo = grimoireName(user) val url = s"https://github.com/${domain}/${repo}.git" url } + def isDataWeaveWizard(user: String): Boolean = { + user == null || user.isBlank + } + def updateLastUpdateTimeStamp(): Boolean = { val lastUpdate: File = lastUpdatedMarkFile() lastUpdate.setLastModified(System.currentTimeMillis()) diff --git a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala index f80b389..ea6400b 100644 --- a/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala +++ b/native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala @@ -25,6 +25,20 @@ class DataWeaveCLITest extends FreeSpec with Matchers { result.trim shouldBe "1" } + "should support literal inputs" in { + val stream = new ByteArrayOutputStream() + val console = new TestConsole(System.in, stream) + val dwcli = createCommandLine(console) + dwcli.execute("run", "--literal-input", "test=[1,2,3]", + "input test json\n" + + " output json \n" + + "---\n" + + "test[1]") + val source = Source.fromBytes(stream.toByteArray, "UTF-8") + val result: String = source.mkString + result.trim shouldBe "2" + } + private def createCommandLine(console: TestConsole) = { new CommandLine(new DataWeaveCLIRunner(), new DWFactory(console)) } @@ -210,8 +224,8 @@ class DataWeaveCLITest extends FreeSpec with Matchers { val dwcli = createCommandLine(testConsole) val exitCode = dwcli.execute( "run", - "-p","name=Mariano", - "-p","lastname=Lischetti", + "-p", "name=Mariano", + "-p", "lastname=Lischetti", "{fullName: params.name ++ \" \" ++ params.lastname}") exitCode shouldBe 0 val source = Source.fromBytes(stream.toByteArray, "UTF-8")