Merge pull request #1515 from ostephens/conversions

Conversions
This commit is contained in:
Antonin Delpeuch 2018-02-22 09:49:49 +00:00 committed by GitHub
commit 3dcb3f1faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 32 deletions

View File

@ -50,29 +50,30 @@ import com.google.refine.expr.EvalError;
import com.google.refine.expr.util.CalendarParser; import com.google.refine.expr.util.CalendarParser;
import com.google.refine.expr.util.CalendarParserException; import com.google.refine.expr.util.CalendarParserException;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
public class ToDate implements Function { public class ToDate implements Function {
@Override @Override
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length == 0) {
// missing value, can this happen?
return null;
}
Object arg0 = args[0];
String o1; String o1;
if (args.length == 0) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects at least one argument");
} else {
Object arg0 = args[0];
if (arg0 instanceof Date) { if (arg0 instanceof Date) {
return arg0; return arg0;
} else if (arg0 instanceof Calendar) { } else if (arg0 instanceof Calendar) {
return ((Calendar) arg0).getTime(); return ((Calendar) arg0).getTime();
} else if (arg0 instanceof Long) { } else if (arg0 instanceof Long) {
o1 = ((Long) arg0).toString(); // treat integers as years o1 = ((Long) arg0).toString(); // treat integers as years
} else if (arg0 instanceof String) { } else if (arg0 instanceof String && arg0.toString().trim().length() > 0) {
o1 = (String) arg0; o1 = (String) arg0;
} else { } else {
// ignore cell values that aren't strings // ignore cell values that aren't Date, Calendar, Long or String
return new EvalError("Not a String - cannot parse to date"); return new EvalError("Unable to parse as date");
}
} }
// "o, boolean month_first (optional)" // "o, boolean month_first (optional)"
@ -98,13 +99,12 @@ public class ToDate implements Function {
// } catch (DatatypeConfigurationException e2) { // } catch (DatatypeConfigurationException e2) {
// } // }
} }
return new EvalError("Cannot parse to date"); return new EvalError("Unable to parse as date");
} }
} } else if (args.length>=2) {
// "o, format1, format2 (optional), ..." // "o, format1, format2 (optional), ..."
Locale locale = Locale.getDefault(); Locale locale = Locale.getDefault();
if (args.length>=2) {
for (int i=1;i<args.length;i++) { for (int i=1;i<args.length;i++) {
if (!(args[i] instanceof String)) { if (!(args[i] instanceof String)) {
// skip formats that aren't strings // skip formats that aren't strings
@ -156,9 +156,9 @@ public class ToDate implements Function {
} }
} }
return new EvalError("Unable to parse as date"); return new EvalError("Unable to parse as date");
} else {
return new EvalError("Unable to parse as date");
} }
return null;
} }

View File

@ -39,6 +39,7 @@ import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
public class ToNumber implements Function { public class ToNumber implements Function {
@ -58,13 +59,16 @@ public class ToNumber implements Function {
try { try {
return Double.parseDouble(s); return Double.parseDouble(s);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return new EvalError("Cannot parse to number"); return new EvalError("Unable to parse as number");
}
} else {
return new EvalError("Unable to parse as number");
} }
} }
} else {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects one non-null argument");
} }
} }
return null;
}
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)

View File

@ -43,6 +43,7 @@ import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.util.StringUtils; import com.google.refine.util.StringUtils;
@ -68,7 +69,7 @@ public class ToString implements Function {
} }
} }
} }
return new EvalError("ToString accepts an object and an optional second argument containing a date format string"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " accepts an object and an optional second argument containing a date format string");
} }

View File

@ -98,10 +98,9 @@ public class ToFromConversionTests extends RefineTest {
@Test @Test
public void testToNumber() { public void testToNumber() {
// Assert.assertTrue(invoke("toNumber") instanceof EvalError); Assert.assertTrue(invoke("toNumber") instanceof EvalError);
Assert.assertNull(invoke("toNumber")); Assert.assertTrue(invoke("toNumber", (Object) null) instanceof EvalError);
// Assert.assertTrue(invoke("toNumber", (Object) null) instanceof EvalError); Assert.assertTrue(invoke("toNumber", "") instanceof EvalError);
Assert.assertNull(invoke("toNumber", (Object) null));
Assert.assertTrue(invoke("toNumber", "string") instanceof EvalError); Assert.assertTrue(invoke("toNumber", "string") instanceof EvalError);
Assert.assertEquals(invoke("toNumber", "0.0"), 0.0); Assert.assertEquals(invoke("toNumber", "0.0"), 0.0);
Assert.assertEquals(invoke("toNumber", "123"), Long.valueOf(123)); Assert.assertEquals(invoke("toNumber", "123"), Long.valueOf(123));
@ -128,9 +127,9 @@ public class ToFromConversionTests extends RefineTest {
@Test @Test
public void testToDate() throws CalendarParserException { public void testToDate() throws CalendarParserException {
// Assert.assertTrue(invoke("toDate") instanceof EvalError); Assert.assertTrue(invoke("toDate") instanceof EvalError);
Assert.assertNull(invoke("toDate"));
Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError); Assert.assertTrue(invoke("toDate", (Object) null) instanceof EvalError);
Assert.assertTrue(invoke("toDate", "") instanceof EvalError);
Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError); Assert.assertTrue(invoke("toDate", 1.0) instanceof EvalError);
Assert.assertTrue(invoke("toDate", "2012-03-01","xxx") instanceof EvalError); // bad format string Assert.assertTrue(invoke("toDate", "2012-03-01","xxx") instanceof EvalError); // bad format string
Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar); Assert.assertTrue(invoke("toDate", "2012-03-01") instanceof GregorianCalendar);