Jackson serialization for GREL controls

This commit is contained in:
Antonin Delpeuch 2018-09-23 14:46:48 +01:00
parent 7648ca91ca
commit c140f90db1
16 changed files with 124 additions and 88 deletions

View File

@ -35,6 +35,13 @@ package com.google.refine.grel;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.Jsonizable;
import com.google.refine.expr.Evaluable;
@ -47,4 +54,29 @@ public interface Control extends Jsonizable {
public Object call(Properties bindings, Evaluable[] args);
public String checkArguments(Evaluable[] args);
@JsonProperty("description")
public String getDescription();
@JsonProperty("params")
@JsonInclude(Include.NON_EMPTY)
default public String getParams() {
return "";
}
@JsonProperty("returns")
public String getReturns();
@Override
default public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value(getDescription());
if (!getParams().isEmpty()) {
writer.key("params"); writer.value(getParams());
}
writer.key("returns"); writer.value(getReturns());
writer.endObject();
}
}

View File

@ -148,15 +148,17 @@ public class Filter implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression test which should return a boolean. If the boolean is true, pushes v onto the result array.";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression test which should return a boolean. If the boolean is true, pushes v onto the result array."
);
writer.key("params"); writer.value("expression a, variable v, expression test");
writer.key("returns"); writer.value("array");
writer.endObject();
@Override
public String getParams() {
return "expression a, variable v, expression test";
}
@Override
public String getReturns() {
return "array";
}
}

View File

@ -40,7 +40,6 @@ import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.Evaluable;
@ -145,15 +144,17 @@ public class ForEach implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression e, and pushes the result onto the result array.";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression a to an array. Then for each array element, binds its value to variable name v, evaluates expression e, and pushes the result onto the result array."
);
writer.key("params"); writer.value("expression a, variable v, expression e");
writer.key("returns"); writer.value("array");
writer.endObject();
@Override
public String getParams() {
return "expression a, variable v, expression e";
}
@Override
public String getReturns() {
return "array";
}
}

View File

@ -150,15 +150,17 @@ public class ForEachIndex implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression a to an array. Then for each array element, binds its index to variable i and its value to variable name v, evaluates expression e, and pushes the result onto the result array.";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression a to an array. Then for each array element, binds its index to variable i and its value to variable name v, evaluates expression e, and pushes the result onto the result array."
);
writer.key("params"); writer.value("expression a, variable i, variable v, expression e");
writer.key("returns"); writer.value("array");
writer.endObject();
@Override
public String getParams() {
return "expression a, variable i, variable v, expression e";
}
@Override
public String getReturns() {
return "array";
}
}

View File

@ -85,16 +85,18 @@ public class ForNonBlank implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression o. If it is non-blank, binds its value to variable name v, evaluates expression eNonBlank and returns the result. " +
"Otherwise (if o evaluates to blank), evaluates expression eBlank and returns that result instead.";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression o. If it is non-blank, binds its value to variable name v, evaluates expression eNonBlank and returns the result. " +
"Otherwise (if o evaluates to blank), evaluates expression eBlank and returns that result instead."
);
writer.key("params"); writer.value("expression o, variable v, expression eNonBlank, expression eBlank");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
@Override
public String getParams() {
return "expression o, variable v, expression eNonBlank, expression eBlank";
}
@Override
public String getReturns() {
return "Depends on actual arguments";
}
}

View File

@ -132,15 +132,17 @@ public class ForRange implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Iterates over the variable v starting at \"from\", incrementing by \"step\" each time while less than \"to\". At each iteration, evaluates expression e, and pushes the result onto the result array.";
}
writer.object();
writer.key("description"); writer.value(
"Iterates over the variable v starting at \"from\", incrementing by \"step\" each time while less than \"to\". At each iteration, evaluates expression e, and pushes the result onto the result array."
);
writer.key("params"); writer.value("number from, number to, number step, variable v, expression e");
writer.key("returns"); writer.value("array");
writer.endObject();
@Override
public String getParams() {
return "number from, number to, number step, variable v, expression e";
}
@Override
public String getReturns() {
return "array";
}
}

View File

@ -65,16 +65,18 @@ public class If implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " +
"Otherwise, evaluates expression eFalse and returns that result instead.";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression o. If it is true, evaluates expression eTrue and returns the result. " +
"Otherwise, evaluates expression eFalse and returns that result instead."
);
writer.key("params"); writer.value("expression o, expression eTrue, expression eFalse");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
@Override
public String getParams() {
return "expression o, expression eTrue, expression eFalse";
}
@Override
public String getReturns() {
return "Depends on actual arguments";
}
}

View File

@ -37,7 +37,7 @@ import com.google.refine.expr.ExpressionUtils;
public class IsBlank extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is null or an empty string";
}

View File

@ -2,7 +2,7 @@ package com.google.refine.grel.controls;
public class IsEmptyString extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is an empty string";
}

View File

@ -37,7 +37,7 @@ import com.google.refine.expr.ExpressionUtils;
public class IsError extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is an error";
}

View File

@ -37,7 +37,7 @@ import com.google.refine.expr.ExpressionUtils;
public class IsNonBlank extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is not null and not an empty string";
}

View File

@ -35,7 +35,7 @@ package com.google.refine.grel.controls;
public class IsNotNull extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is not null";
}

View File

@ -35,7 +35,7 @@ package com.google.refine.grel.controls;
public class IsNull extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o is null";
}

View File

@ -37,7 +37,7 @@ import org.apache.commons.lang3.StringUtils;
public class IsNumeric extends IsTest {
@Override
protected String getDescription() {
public String getDescription() {
return "Returns whether o can represent a number";
}

View File

@ -35,9 +35,6 @@ package com.google.refine.grel.controls;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.expr.EvalError;
import com.google.refine.expr.Evaluable;
import com.google.refine.grel.Control;
@ -64,17 +61,14 @@ abstract class IsTest implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getParams() {
return "expression o";
}
writer.object();
writer.key("description"); writer.value(getDescription());
writer.key("params"); writer.value("expression o");
writer.key("returns"); writer.value("boolean");
writer.endObject();
@Override
public String getReturns() {
return "boolean";
}
abstract protected boolean test(Object v);
abstract protected String getDescription();
}

View File

@ -35,9 +35,6 @@ package com.google.refine.grel.controls;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.expr.Evaluable;
import com.google.refine.grel.Control;
import com.google.refine.grel.ControlFunctionRegistry;
@ -82,15 +79,17 @@ public class With implements Control {
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
public String getDescription() {
return "Evaluates expression o and binds its value to variable name v. Then evaluates expression e and returns that result";
}
writer.object();
writer.key("description"); writer.value(
"Evaluates expression o and binds its value to variable name v. Then evaluates expression e and returns that result"
);
writer.key("params"); writer.value("expression o, variable v, expression e");
writer.key("returns"); writer.value("Depends on actual arguments");
writer.endObject();
@Override
public String getParams() {
return "expression o, variable v, expression e";
}
@Override
public String getReturns() {
return "Depends on actual arguments";
}
}