Merge pull request #1414 from OpenRefine/issue1377
Expose fields as attributes in Jython
This commit is contained in:
commit
54e25408fb
@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" path="main/src"/>
|
<classpathentry kind="src" path="main/src"/>
|
||||||
|
<classpathentry kind="src" path="extensions/jython/tests/src"/>
|
||||||
<classpathentry kind="src" path="server/src"/>
|
<classpathentry kind="src" path="server/src"/>
|
||||||
<classpathentry kind="src" path="extensions/gdata/src"/>
|
<classpathentry kind="src" path="extensions/gdata/src"/>
|
||||||
<classpathentry kind="src" path="extensions/database/src"/>
|
<classpathentry kind="src" path="extensions/database/src"/>
|
||||||
|
@ -55,7 +55,12 @@ public class JythonHasFieldsWrapper extends PyObject {
|
|||||||
@Override
|
@Override
|
||||||
public PyObject __finditem__(PyObject key) {
|
public PyObject __finditem__(PyObject key) {
|
||||||
String k = (String) key.__tojava__(String.class);
|
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 != null) {
|
||||||
if (v instanceof PyObject) {
|
if (v instanceof PyObject) {
|
||||||
return (PyObject) v;
|
return (PyObject) v;
|
||||||
@ -70,5 +75,4 @@ public class JythonHasFieldsWrapper extends PyObject {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user