Fix bug in join() GREL function

This commit is contained in:
Antonin Delpeuch 2018-11-19 16:42:41 +00:00
parent f3f6a2846b
commit c9786d0f9a
2 changed files with 8 additions and 1 deletions

View File

@ -41,6 +41,7 @@ import org.json.JSONException;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.ExpressionUtils; import com.google.refine.expr.ExpressionUtils;
import com.google.refine.expr.util.JsonValueConverter;
import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
@ -75,7 +76,7 @@ public class Join implements Function {
sb.append(separator); sb.append(separator);
} }
try { try {
sb.append(a.get(i).toString()); sb.append(JsonValueConverter.convert(a.get(i)).toString());
} catch (JSONException e) { } catch (JSONException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + return new EvalError(ControlFunctionRegistry.getFunctionName(this) +
" cannot retrieve element " + i + " of array"); " cannot retrieve element " + i + " of array");

View File

@ -179,6 +179,12 @@ public class GrelTests extends RefineTest {
Evaluable eval = MetaParser.parse("grel:" + test); Evaluable eval = MetaParser.parse("grel:" + test);
Assert.assertNull(eval.evaluate(bindings)); Assert.assertNull(eval.evaluate(bindings));
} }
@Test
public void testJoinJsonArray() throws ParsingException {
String test[] = { "\"{\\\"values\\\":[\\\"one\\\",\\\"two\\\",\\\"three\\\"]}\".parseJson().values.join(\",\")", "one,two,three" };
parseEval(bindings, test);
}
@Test @Test
public void testGetFieldFromNull() throws ParsingException { public void testGetFieldFromNull() throws ParsingException {