Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit de6a1cd

Browse files
authored
Fixing playground and adding a way to specify literal inputs (#77)
1 parent 00b5751 commit de6a1cd

File tree

9 files changed

+63
-20
lines changed

9 files changed

+63
-20
lines changed

native-cli/src/main/java/org/mule/weave/cli/pico/AbstractPicoExecCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public abstract class AbstractPicoExecCommand implements Callable<Integer> {
2121
protected java.util.Map<String, String> params = null;
2222
@CommandLine.Option(names = {"--input", "-i"}, description = {"Declares a new input."}, paramLabel = "<Name=File>")
2323
protected java.util.Map<String, File> inputs = null;
24+
25+
@CommandLine.Option(names = {"--literal-input", "-li"}, description = {"Declares a new literal input."}, paramLabel = "<Name=InputContent>")
26+
protected java.util.Map<String, String> literalInput = null;
2427
@CommandLine.Option(names = {"--verbose", "-v"}, description = {"Run the script as untrusted, which means that the script has no privileges."}, defaultValue = "false")
2528
protected boolean verbose = false;
2629
@CommandLine.Option(names = {"--untrusted"}, description = {"Run the script as untrusted, which means that the script has no privileges."}, defaultValue = "false")

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRepl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected Integer doCall() {
2929
Option.empty(),
3030
Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
3131
Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, File>empty()),
32+
Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
3233
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
3334
calculateRuntimeVersion()
3435
);

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunScript.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ protected Integer doCall() {
7171
None$.empty(),
7272
Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
7373
Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, File>empty()),
74+
Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
7475
Option.apply(output).map((s) -> s.getAbsolutePath()),
7576
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
7677
dataWeaveVersionOption

native-cli/src/main/java/org/mule/weave/cli/pico/PicoRunSpell.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ protected Integer doCall() {
138138
Option.apply(resolver),
139139
Optional.ofNullable(params).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, String>empty()),
140140
Optional.ofNullable(inputs).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, File>empty()),
141+
Optional.ofNullable(literalInput).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, String>empty()),
141142
Option.apply(output).map(File::getAbsolutePath),
142143
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
143144
dataWeaveVersionOption

native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/AddWizardCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AddWizardCommand(config: CloneWizardConfig, console: Console) extends Weav
1212
override def exec(): Int = {
1313
val wizard = config.wizardName
1414
console.info(s"Downloading Grimoire From The Wise: `$wizard`.")
15-
val wizardName = if (wizard == null) "DW" else wizard
15+
val wizardName = if (utils.isDataWeaveWizard(wizard)) "DW" else wizard
1616
val wizardFolder = utils.grimoireFolder(wizard)
1717
if (wizardFolder.exists()) {
1818
console.error(s"Wizard `$wizard` was already added.")

native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/ReplCommand.scala

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class ReplCommand(config: ReplConfiguration, console: Console) extends WeaveComm
7272
config.inputs.foreach((input) => {
7373
scriptingBindings.addBinding(input._1, input._2, getMimeTypeByFileExtension(input._2))
7474
})
75+
76+
config.literalInputs.foreach((input) => {
77+
scriptingBindings.addBinding(input._1, input._2, None)
78+
})
79+
7580
val value: Array[KeyValuePair] = config.params.toSeq.map(prop =>
7681
KeyValuePair(KeyValue(prop._1), StringValue(prop._2))
7782
).toArray
@@ -85,22 +90,30 @@ class ReplCommand(config: ReplConfiguration, console: Console) extends WeaveComm
8590
var continue = true
8691
while (continue) {
8792
System.out.print(">>> ")
88-
var str = input.nextLine()
89-
while (str.endsWith("\\")) {
90-
System.out.print("... ")
91-
str = str.substring(0, str.length - 1) + "\n" + input.nextLine()
92-
}
93-
if (str.equals("quit()")) {
94-
continue = false
95-
} else {
96-
val defaultOutputType: String = console.envVar(DW_DEFAULT_OUTPUT_MIMETYPE_VAR).getOrElse("application/dw")
97-
val result: WeaveExecutionResult = nativeRuntime.run(str, NameIdentifier.ANONYMOUS_NAME.name, scriptingBindings, console.out, defaultOutputType, config.maybePrivileges)
98-
//load inputs from
99-
if (!result.success()) {
100-
System.out.println(result.result())
93+
try {
94+
var str = input.nextLine()
95+
while (str.endsWith("\\")) {
96+
System.out.print("... ")
97+
str = str.substring(0, str.length - 1) + "\n" + input.nextLine()
98+
}
99+
if (str.equals("quit()")) {
100+
continue = false
101101
} else {
102-
System.out.println("")
102+
val defaultOutputType: String = console.envVar(DW_DEFAULT_OUTPUT_MIMETYPE_VAR).getOrElse("application/dw")
103+
val result: WeaveExecutionResult = nativeRuntime.run(str, NameIdentifier.ANONYMOUS_NAME.name, scriptingBindings, console.out, defaultOutputType, config.maybePrivileges)
104+
//load inputs from
105+
if (!result.success()) {
106+
System.out.println(result.result())
107+
} else {
108+
System.out.println("")
109+
}
103110
}
111+
} catch {
112+
case e: Exception => {
113+
console.fatal("Fatal error while reading the input: " + e.getMessage)
114+
return ExitCodes.FAILURE
115+
}
116+
104117
}
105118
}
106119
ExitCodes.SUCCESS
@@ -111,5 +124,6 @@ case class ReplConfiguration(path: Array[String],
111124
dependencyResolver: Option[(NativeRuntime) => Array[DependencyResolutionResult]],
112125
params: Map[String, String],
113126
inputs: Map[String, File],
127+
literalInputs: Map[String, String],
114128
maybePrivileges: Option[Seq[String]],
115129
maybeLanguageLevel: Option[DataWeaveVersion])

native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/RunWeaveCommand.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,16 @@ class RunWeaveCommand(val config: WeaveRunnerConfig, console: Console) extends W
8484

8585
val defaultInputType: String = console.envVar(DW_DEFAULT_INPUT_MIMETYPE_VAR).getOrElse(DEFAULT_MIME_TYPE)
8686
val scriptingBindings: ScriptingBindings = new ScriptingBindings
87-
if (config.inputs.isEmpty) {
87+
if (config.inputs.isEmpty && config.literalInput.isEmpty) {
8888
scriptingBindings.addBinding("payload", console.in, defaultInputType)
8989
} else {
9090
config.inputs.foreach((input) => {
9191
scriptingBindings.addBinding(input._1, input._2, getMimeTypeByFileExtension(input._2))
9292
})
93+
94+
config.literalInput.foreach((input) => {
95+
scriptingBindings.addBinding(input._1, input._2, None)
96+
})
9397
}
9498

9599
val value: Array[KeyValuePair] = config.params.toSeq.map(prop =>
@@ -169,6 +173,7 @@ case class WeaveRunnerConfig(path: Array[String],
169173
dependencyResolver: Option[(NativeRuntime) => Array[DependencyResolutionResult]],
170174
params: Map[String, String],
171175
inputs: Map[String, File],
176+
literalInput: Map[String, String],
172177
outputPath: Option[String],
173178
maybePrivileges: Option[Seq[String]],
174179
maybeLanguageLevel: Option[DataWeaveVersion])

native-cli/src/main/scala/org/mule/weave/dwnative/cli/utils/SpellsUtils.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object SpellsUtils {
1414
class SpellsUtils(console: Console) {
1515

1616
def grimoireName(user: String): String = {
17-
if (user == null) {
17+
if (isDataWeaveWizard(user)) {
1818
DATA_WEAVE_GRIMOIRE_FOLDER
1919
} else {
2020
s"${user}-$DATA_WEAVE_GRIMOIRE_FOLDER"
@@ -55,12 +55,16 @@ class SpellsUtils(console: Console) {
5555
}
5656

5757
def buildRepoUrl(user: String): String = {
58-
val domain = if (user == null) "mulesoft-labs" else user
58+
val domain = if (isDataWeaveWizard(user)) "mulesoft-labs" else user
5959
val repo = grimoireName(user)
6060
val url = s"https://github.com/${domain}/${repo}.git"
6161
url
6262
}
6363

64+
def isDataWeaveWizard(user: String): Boolean = {
65+
user == null || user.isBlank
66+
}
67+
6468
def updateLastUpdateTimeStamp(): Boolean = {
6569
val lastUpdate: File = lastUpdatedMarkFile()
6670
lastUpdate.setLastModified(System.currentTimeMillis())

native-cli/src/test/scala/org/mule/weave/dwnative/cli/DataWeaveCLITest.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ class DataWeaveCLITest extends FreeSpec with Matchers {
2525
result.trim shouldBe "1"
2626
}
2727

28+
"should support literal inputs" in {
29+
val stream = new ByteArrayOutputStream()
30+
val console = new TestConsole(System.in, stream)
31+
val dwcli = createCommandLine(console)
32+
dwcli.execute("run", "--literal-input", "test=[1,2,3]",
33+
"input test json\n" +
34+
" output json \n" +
35+
"---\n" +
36+
"test[1]")
37+
val source = Source.fromBytes(stream.toByteArray, "UTF-8")
38+
val result: String = source.mkString
39+
result.trim shouldBe "2"
40+
}
41+
2842
private def createCommandLine(console: TestConsole) = {
2943
new CommandLine(new DataWeaveCLIRunner(), new DWFactory(console))
3044
}
@@ -210,8 +224,8 @@ class DataWeaveCLITest extends FreeSpec with Matchers {
210224
val dwcli = createCommandLine(testConsole)
211225
val exitCode = dwcli.execute(
212226
"run",
213-
"-p","name=Mariano",
214-
"-p","lastname=Lischetti",
227+
"-p", "name=Mariano",
228+
"-p", "lastname=Lischetti",
215229
"{fullName: params.name ++ \" \" ++ params.lastname}")
216230
exitCode shouldBe 0
217231
val source = Source.fromBytes(stream.toByteArray, "UTF-8")

0 commit comments

Comments
 (0)