Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing playground and adding a way to specify literal inputs #77

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public abstract class AbstractPicoExecCommand implements Callable<Integer> {
protected java.util.Map<String, String> params = null;
@CommandLine.Option(names = {"--input", "-i"}, description = {"Declares a new input."}, paramLabel = "<Name=File>")
protected java.util.Map<String, File> inputs = null;

@CommandLine.Option(names = {"--literal-input", "-li"}, description = {"Declares a new literal input."}, paramLabel = "<Name=InputContent>")
protected java.util.Map<String, String> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected Integer doCall() {
Option.empty(),
Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, File>empty()),
Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
calculateRuntimeVersion()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected Integer doCall() {
None$.empty(),
Optional.ofNullable(params).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
Optional.ofNullable(inputs).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, File>empty()),
Optional.ofNullable(literalInput).map((s) -> toScalaMap(s)).orElse(Map$.MODULE$.<String, String>empty()),
Option.apply(output).map((s) -> s.getAbsolutePath()),
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
dataWeaveVersionOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected Integer doCall() {
Option.apply(resolver),
Optional.ofNullable(params).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, String>empty()),
Optional.ofNullable(inputs).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, File>empty()),
Optional.ofNullable(literalInput).map(AbstractPicoRunCommand::toScalaMap).orElse(Map$.MODULE$.<String, String>empty()),
Option.apply(output).map(File::getAbsolutePath),
Option.apply(privileges).map((s) -> JavaConverters.asScalaBuffer(s).toSeq()),
dataWeaveVersionOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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])
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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")
Expand Down