Skip to content

Commit

Permalink
Add option TYPE=JSON to input() PROPS
Browse files Browse the repository at this point in the history
- Add option TYPE=JSON to input() PROPS, in which case the input will store the output as a json instead of a string property list
- Discussed in RPTools#1278
  • Loading branch information
Merudo committed Feb 16, 2020
1 parent 68015fc commit 4df6294
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public enum InputType {
CHECK(false, false, "SPAN=FALSE;"),
RADIO(true, false, "ORIENT=V;VALUE=NUMBER;SELECT=0;SPAN=FALSE;DELIMITER=,;"),
LABEL(false, false, "TEXT=TRUE;ICON=FALSE;ICONSIZE=50;SPAN=FALSE;"),
PROPS(false, true, "SETVARS=NONE;SPAN=FALSE;WIDTH=14;"),
PROPS(false, true, "SETVARS=NONE;SPAN=FALSE;WIDTH=14;TYPE=STRPROP;"),
TAB(false, true, "SELECT=FALSE;");
// @formatter: on

Expand Down Expand Up @@ -1120,6 +1120,7 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
VarSpec vs = panelVars.get(varCount);
JComponent comp = panelControls.get(varCount);
String newValue = null;
JsonObject jsonObject = null;
switch (vs.inputType) {
case TEXT:
{
Expand Down Expand Up @@ -1176,6 +1177,7 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
// all the new settings.
Component[] comps = ((JPanel) comp).getComponents();
StringBuilder sb = new StringBuilder();
jsonObject = new JsonObject();
int setVars = 0; // "NONE", no assignments made
if (vs.optionValues.optionEquals("SETVARS", "SUFFIXED")) setVars = 1;
if (vs.optionValues.optionEquals("SETVARS", "UNSUFFIXED")) setVars = 2;
Expand All @@ -1189,6 +1191,10 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
sb.append("=");
sb.append(value);
sb.append(" ; ");
if (vs.optionValues.optionEquals("TYPE", "JSON")) {
jsonObject.add(
key, JSONMacroFunctions.getInstance().convertPrimitiveFromString(value));
}
switch (setVars) {
case 0:
// Do nothing
Expand All @@ -1211,7 +1217,11 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}
// Set the variable to the value we got from the dialog box.
if (newValue != null) {
parser.setVariable(vs.name, newValue.trim());
if (vs.optionValues.optionEquals("TYPE", "JSON")) {
parser.setVariable(vs.name, jsonObject);
} else {
parser.setVariable(vs.name, newValue.trim());
}
allAssignments.append(vs.name + "=" + newValue.trim() + " ## ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
*/
package net.rptools.maptool.client.functions.json;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
Expand Down Expand Up @@ -692,10 +689,9 @@ private List<JsonElement> paramsAsJsonElements(String functionName, List<Object>
* @param functionName The name of the MT Script function that was called.
* @param params The parameters to extract as {@link JsonElement}s.
* @return The list of {@link JsonElement}s.
* @throws ParserException if the parameters can not be converted to {@link JsonElement}s.
*/
private List<JsonElement> paramsConvertedToJsonElements(String functionName, List<Object> params)
throws ParserException {
private List<JsonElement> paramsConvertedToJsonElements(
String functionName, List<Object> params) {
List<JsonElement> elements = new ArrayList<>();
for (int i = 0; i < params.size(); i++) {
elements.add(FunctionUtil.paramConvertedToJson(functionName, params, i));
Expand Down Expand Up @@ -880,10 +876,8 @@ private JsonElement jsonPathDelete(JsonElement json, String path) {
* @param key The new key to be added.
* @param info The new information to add.
* @return The resulting json data.
* @throws ParserException If there is an error adding the information.
*/
private JsonElement jsonPathPut(JsonElement json, String path, String key, Object info)
throws ParserException {
private JsonElement jsonPathPut(JsonElement json, String path, String key, Object info) {
Object value = asJsonElement(info);

return JsonPath.using(jaywayConfig).parse(shallowCopy(json)).put(path, key, value).json();
Expand All @@ -896,10 +890,8 @@ private JsonElement jsonPathPut(JsonElement json, String path, String key, Objec
* @param path The path to change the new information at.
* @param info The new information to change to.
* @return The resulting json data.
* @throws ParserException If there is an error changing the information.
*/
private JsonElement jsonPathSet(JsonElement json, String path, Object info)
throws ParserException {
private JsonElement jsonPathSet(JsonElement json, String path, Object info) {
Object value = asJsonElement(info);

return JsonPath.using(jaywayConfig).parse(shallowCopy(json)).set(path, value).json();
Expand Down Expand Up @@ -927,10 +919,8 @@ private JsonElement jsonPathAdd(JsonElement json, String path, Object info)
* @param json the json object to read the information from.
* @param path the path to read in the object.
* @return the return value as a MT Script type.
* @throws ParserException when there is an error converting the json objects.
*/
private Object jsonPathRead(JsonElement json, String path, Configuration config)
throws ParserException {
private Object jsonPathRead(JsonElement json, String path, Configuration config) {
JsonElement jsonElement = asJsonElement(json);
return typeConversion.asScriptType(JsonPath.using(config).parse(jsonElement).read(path));
}
Expand Down Expand Up @@ -962,6 +952,16 @@ public JsonElement asJsonElement(Object o) {
return typeConversion.asJsonElement(o);
}

/**
* Converts a <code>String</code> to a {@link JsonPrimitive}.
*
* @param string the String to convert.
* @return the converted value.
*/
public JsonPrimitive convertPrimitiveFromString(String string) {
return typeConversion.convertPrimitiveFromString(string);
}

/**
* Converts a JsonElement to a String that is safe to be returned to MTScript
*
Expand Down

0 comments on commit 4df6294

Please sign in to comment.