Skip to content

Commit

Permalink
ScriptEngine added methods: getSerializationFormats() and
Browse files Browse the repository at this point in the history
getDeserializationFormats()
  • Loading branch information
mattirn committed Apr 4, 2020
1 parent 664eef8 commit 5438565
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
14 changes: 4 additions & 10 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ private Object slurpcmd(Builtins.CommandInput input) {
try {
if (!opt.args().isEmpty()){
Charset encoding = opt.isSet("encoding") ? Charset.forName(opt.get("encoding")): StandardCharsets.UTF_8;
String format = opt.isSet("format") ? opt.get("format") : "";
String format = opt.isSet("format") ? opt.get("format") : engine.getSerializationFormats().get(0);
out = slurp(Paths.get(opt.args().get(0)), encoding, format);
}
} catch (Exception e) {
Expand All @@ -1637,18 +1637,12 @@ public void persist(Path file, Object object) {

@Override
public Object slurp(Path file) throws IOException {
return slurp(file, StandardCharsets.UTF_8, "JSON");
return slurp(file, StandardCharsets.UTF_8, engine.getSerializationFormats().get(0));
}

private Object slurp(Path file, Charset encoding, String format) throws IOException {
Object out = null;
byte[] encoded = Files.readAllBytes(file);
if (format.equalsIgnoreCase("TXT")) {
out = new String(encoded, encoding);
} else {
out = engine.deserialize(new String(encoded, encoding), format);
}
return out;
return engine.deserialize(new String(encoded, encoding), format);
}

private Object aliascmd(Builtins.CommandInput input) {
Expand Down Expand Up @@ -1766,7 +1760,7 @@ private List<Completer> slurpCompleter(String command) {
List<OptDesc> optDescs = commandOptions("slurp");
for (OptDesc o : optDescs) {
if (o.shortOption() != null && o.shortOption().equals("-f")) {
o.setValueCompleter(new StringsCompleter("TXT", "JSON", "GROOVY"));
o.setValueCompleter(new StringsCompleter(engine.getDeserializationFormats()));
break;
}
}
Expand Down
14 changes: 10 additions & 4 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
package org.jline.groovy;

import java.nio.charset.StandardCharsets
import java.nio.file.Path;

import java.nio.file.Path
import org.jline.script.GroovyEngine.Format
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.json.JsonParserType
Expand Down Expand Up @@ -44,8 +44,14 @@ public class Utils {
|| (json.startsWith("[") && json.endsWith("]"))) && json.length() > 5) ? JsonOutput.prettyPrint(json) : json
}

static void persist(Path file, Object object) {
file.toFile().write(JsonOutput.toJson(object))
static void persist(Path file, Object object, Format format) {
if (format == Format.JSON) {
file.toFile().write(JsonOutput.toJson(object))
} else if (format == Format.NONE) {
file.toFile().write(toString(object))
} else {
throw new IllegalArgumentException();
}
}

}
54 changes: 34 additions & 20 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class GroovyEngine implements ScriptEngine {
public enum Format {JSON, GROOVY, NONE};
private static final String REGEX_SYSTEM_VAR = "[A-Z]+[A-Z_]*";
private static final String REGEX_VAR = "[a-zA-Z_]+[a-zA-Z0-9_]*";
private static final Pattern PATTERN_FUNCTION_DEF=Pattern.compile("^def\\s+(" + REGEX_VAR + ")\\s*\\(([a-zA-Z0-9_ ,]*)\\)\\s*\\{(.*)?\\}(|\n)$"
Expand Down Expand Up @@ -76,55 +77,68 @@ public Map<String,Object> find(String name) {
}

@Override
public Object deserialize(String variable, String format) {
Object out = variable;
if (format.equalsIgnoreCase("TXT")) {
public List<String> getSerializationFormats() {
return Arrays.asList(Format.JSON.toString(), Format.NONE.toString());
}

@Override
public List<String> getDeserializationFormats() {
return Arrays.asList(Format.JSON.toString(), Format.GROOVY.toString(), Format.NONE.toString());
}

@Override
public Object deserialize(String value, String formatStr) {
Object out = value;
Format format = formatStr != null && !formatStr.isEmpty() ? Format.valueOf(formatStr.toUpperCase()) : null;
if (format == Format.NONE) {
// do nothing
} else if (format.equalsIgnoreCase("JSON")) {
out = Utils.toObject(variable);
} else if (format.equalsIgnoreCase("GROOVY")) {
} else if (format == Format.JSON) {
out = Utils.toObject(value);
} else if (format == Format.GROOVY) {
try {
out = execute(variable);
out = execute(value);
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
}
} else {
variable = variable.trim();
boolean hasCurly = variable.contains("{") && variable.contains("}");
value = value.trim();
boolean hasCurly = value.contains("{") && value.contains("}");
try {
if (variable.startsWith("[") && variable.endsWith("]")) {
if (value.startsWith("[") && value.endsWith("]")) {
try {
if (hasCurly) {
out = Utils.toObject(variable); // try json
out = Utils.toObject(value); // try json
} else {
out = execute(variable);
out = execute(value);
}
} catch (Exception e) {
if (hasCurly) {
try {
out = execute(variable);
out = execute(value);
} catch (Exception e2) {

}
} else {
out = Utils.toObject(variable); // try json
out = Utils.toObject(value); // try json
}
}
} else if (variable.startsWith("{") && variable.endsWith("}")) {
out = Utils.toObject(variable);
} else if (value.startsWith("{") && value.endsWith("}")) {
out = Utils.toObject(value);
}
} catch (Exception e) {
}
}
return out;
}

@Override
public void persist(Path file, Object object) {
persist(file, object, getSerializationFormats().get(0));
}

@Override
public void persist(Path file, Object object, String format) {
if (!format.equalsIgnoreCase("JSON")) {
throw new IllegalArgumentException();
}
Utils.persist(file, object);
Utils.persist(file, object, Format.valueOf(format.toUpperCase()));
}

@Override
Expand Down
18 changes: 14 additions & 4 deletions reader/src/main/java/org/jline/reader/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ default Map<String,Object> find() {
* @return deserialized value
*/
default Object deserialize(String value) {
return deserialize(value, "");
return deserialize(value, null);
}

/**
Expand All @@ -111,14 +111,24 @@ default Object deserialize(String value) {
*/
Object deserialize(String value, String format);

/**
*
* @return Supported serialization formats
*/
List<String> getSerializationFormats();

/**
*
* @return Supported deserialization formats
*/
List<String> getDeserializationFormats();

/**
* Persists object value to file.
* @param file file
* @param object object
*/
default void persist(Path file, Object object) {
persist(file, object, "JSON");
}
void persist(Path file, Object object);

/**
* Persists object value to file.
Expand Down

0 comments on commit 5438565

Please sign in to comment.