Merge pull request #1414 from OpenRefine/issue1377

Expose fields as attributes in Jython
This commit is contained in:
Jacky 2018-01-26 13:14:10 -05:00 committed by GitHub
commit 54e25408fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="main/src"/>
<classpathentry kind="src" path="extensions/jython/tests/src"/>
<classpathentry kind="src" path="server/src"/>
<classpathentry kind="src" path="extensions/gdata/src"/>
<classpathentry kind="src" path="extensions/database/src"/>

View File

@ -55,7 +55,12 @@ public class JythonHasFieldsWrapper extends PyObject {
@Override
public PyObject __finditem__(PyObject key) {
String k = (String) key.__tojava__(String.class);
Object v = _obj.getField(k, _bindings);
return __findattr_ex__(k);
}
@Override
public PyObject __findattr_ex__(String name) {
Object v = _obj.getField(name, _bindings);
if (v != null) {
if (v instanceof PyObject) {
return (PyObject) v;
@ -70,5 +75,4 @@ public class JythonHasFieldsWrapper extends PyObject {
return null;
}
}
}

View File

@ -0,0 +1,50 @@
package com.google.refine.jython;
import java.util.Properties;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.refine.expr.Evaluable;
import com.google.refine.expr.HasFields;
import com.google.refine.model.Cell;
import com.google.refine.model.Row;
public class JythonAttributeTest {
class MyFieldObject implements HasFields {
@Override
public Object getField(String name, Properties bindings) {
if ("sunshine".equals(name)) {
return "hammock";
}
return null;
}
@Override
public boolean fieldAlsoHasFields(String name) {
return true;
}
}
@Test
public void testWrappedObjectsHaveAttributes() {
Row row = new Row(2);
row.setCell(0, new Cell("sunshine", null));
row.setCell(1, new Cell("hammock", null));
Properties props = new Properties();
MyFieldObject obj = new MyFieldObject();
JythonHasFieldsWrapper wrapper = new JythonHasFieldsWrapper(obj, props);
Assert.assertEquals(wrapper.__findattr__("sunshine").toString(), "hammock");
props.put("cell", obj);
Evaluable eval = new JythonEvaluable("return cell.sunshine");
String result = (String)eval.evaluate(props).toString();
Assert.assertEquals(result, "hammock");
}
}