Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

tolerate mismatch between different kinds of Number. This allows pars…

…eInt calls, among other things.
  • Loading branch information...
commit 96e6b1970ac9bddff69f4f108380822b184a49b0 1 parent 79285e8
Robert Newson authored
53 src/main/java/com/github/rnewson/couchdb/lucene/couchdb/FieldType.java
View
@@ -16,6 +16,8 @@
* limitations under the License.
*/
+import java.util.Date;
+
import org.apache.commons.lang.time.DateUtils;
import org.apache.lucene.document.AbstractField;
import org.apache.lucene.document.Field;
@@ -32,7 +34,7 @@
DATE(8, SortField.LONG) {
@Override
- public NumericField toField(final String name, final String value, final ViewSettings settings) throws ParseException {
+ public NumericField toField(final String name, final Object value, final ViewSettings settings) throws ParseException {
return field(name, precisionStep, settings).setLongValue(toDate(value));
}
@@ -45,7 +47,7 @@ public Query toRangeQuery(final String name, final String lower, final String up
},
DOUBLE(8, SortField.DOUBLE) {
@Override
- public NumericField toField(final String name, final String value, final ViewSettings settings) {
+ public NumericField toField(final String name, final Object value, final ViewSettings settings) {
return field(name, precisionStep, settings).setDoubleValue(toDouble(value));
}
@@ -54,14 +56,17 @@ public Query toRangeQuery(final String name, final String lower, final String up
return NumericRangeQuery.newDoubleRange(name, precisionStep, toDouble(lower), toDouble(upper), inclusive, inclusive);
}
- private double toDouble(final String str) {
- return Double.parseDouble(str);
+ private double toDouble(final Object obj) {
+ if (obj instanceof Number) {
+ return ((Number)obj).doubleValue();
+ }
+ return Double.parseDouble(obj.toString());
}
},
FLOAT(4, SortField.FLOAT) {
@Override
- public NumericField toField(final String name, final String value, final ViewSettings settings) {
+ public NumericField toField(final String name, final Object value, final ViewSettings settings) {
return field(name, 4, settings).setFloatValue(toFloat(value));
}
@@ -70,13 +75,16 @@ public Query toRangeQuery(final String name, final String lower, final String up
return NumericRangeQuery.newFloatRange(name, precisionStep, toFloat(lower), toFloat(upper), inclusive, inclusive);
}
- private float toFloat(final String str) {
- return Float.parseFloat(str);
+ private float toFloat(final Object obj) {
+ if (obj instanceof Number) {
+ return ((Number)obj).floatValue();
+ }
+ return Float.parseFloat(obj.toString());
}
},
INT(4, SortField.INT) {
@Override
- public NumericField toField(final String name, final String value, final ViewSettings settings) {
+ public NumericField toField(final String name, final Object value, final ViewSettings settings) {
return field(name, 4, settings).setIntValue(toInt(value));
}
@@ -85,14 +93,17 @@ public Query toRangeQuery(final String name, final String lower, final String up
return NumericRangeQuery.newIntRange(name, precisionStep, toInt(lower), toInt(upper), inclusive, inclusive);
}
- private int toInt(final String str) {
- return Integer.parseInt(str);
+ private int toInt(final Object obj) {
+ if (obj instanceof Number) {
+ return ((Number)obj).intValue();
+ }
+ return Integer.parseInt(obj.toString());
}
},
LONG(8, SortField.LONG) {
@Override
- public NumericField toField(final String name, final String value, final ViewSettings settings) {
+ public NumericField toField(final String name, final Object value, final ViewSettings settings) {
return field(name, precisionStep, settings).setLongValue(toLong(value));
}
@@ -101,15 +112,18 @@ public Query toRangeQuery(final String name, final String lower, final String up
return NumericRangeQuery.newLongRange(name, precisionStep, toLong(lower), toLong(upper), inclusive, inclusive);
}
- private long toLong(final String str) {
- return Long.parseLong(str);
+ private long toLong(final Object obj) {
+ if (obj instanceof Number) {
+ return ((Number)obj).longValue();
+ }
+ return Long.parseLong(obj.toString());
}
},
STRING(0, SortField.STRING) {
@Override
- public Field toField(final String name, final String value, final ViewSettings settings) {
- return new Field(name, value, settings.getStore(), settings.getIndex());
+ public Field toField(final String name, final Object value, final ViewSettings settings) {
+ return new Field(name, value.toString(), settings.getStore(), settings.getIndex());
}
@Override
@@ -136,7 +150,7 @@ private FieldType(final int precisionStep, final int sortField) {
this.sortField = sortField;
}
- public abstract AbstractField toField(final String name, final String value, final ViewSettings settings) throws ParseException;
+ public abstract AbstractField toField(final String name, final Object value, final ViewSettings settings) throws ParseException;
public abstract Query toRangeQuery(final String name, final String lower, final String upper, final boolean inclusive)
throws ParseException;
@@ -145,9 +159,12 @@ public final int toSortField() {
return sortField;
}
- public static long toDate(final String str) throws ParseException {
+ public static long toDate(final Object obj) throws ParseException {
+ if (obj instanceof Date) {
+ return ((Date)obj).getTime();
+ }
try {
- return DateUtils.parseDate(str.toUpperCase(), DATE_PATTERNS).getTime();
+ return DateUtils.parseDate(obj.toString().toUpperCase(), DATE_PATTERNS).getTime();
} catch (final java.text.ParseException e) {
throw new ParseException(e.getMessage());
}
6 src/main/java/com/github/rnewson/couchdb/lucene/rhino/RhinoDocument.java
View
@@ -21,7 +21,6 @@
import java.util.Date;
import java.util.List;
-import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
@@ -88,8 +87,7 @@ public static void jsFunction_add(final Context cx, final Scriptable thisObj, fi
final String className = args[0].getClass().getName();
if (className.equals("org.mozilla.javascript.NativeDate")) {
- args[0] = DateFormatUtils.format((Date) Context.jsToJava(args[0], Date.class),
- FieldType.DATE_PATTERNS[0]);
+ args[0] = (Date) Context.jsToJava(args[0], Date.class);
}
if (!className.startsWith("java.lang.") &&
@@ -180,7 +178,7 @@ public Void handleResponse(final HttpResponse response) throws ClientProtocolExc
private void addField(final RhinoField field, final ViewSettings defaults, final Document out) throws ParseException {
final ViewSettings settings = new ViewSettings(field.settings, defaults);
final FieldType type = settings.getFieldType();
- out.add(type.toField(settings.getField(), field.value.toString(), settings));
+ out.add(type.toField(settings.getField(), field.value, settings));
}
}
13 src/test/java/com/github/rnewson/couchdb/lucene/DocumentConverterTest.java
View
@@ -227,7 +227,18 @@ public void testDateObject() throws Exception {
assertThat(result.length, is(1));
assertThat(result[0].getFieldable("num"), is(NumericField.class));
}
-
+
+ @Test
+ public void testParseInt() throws Exception {
+ final String fun = "function(doc) { var ret=new Document(); ret.add(parseInt(\"12.5\"), {type:\"int\", field:\"num\"}); return ret; }";
+ final DocumentConverter converter = new DocumentConverter(context, view(fun));
+ final Document[] result = converter.convert(
+ doc("{_id:\"hi\"}"),
+ settings(),
+ null);
+ assertThat(result.length, is(1));
+ assertThat(result[0].getFieldable("num"), is(NumericField.class));
+ }
private CouchDocument doc(final String json) {
return new CouchDocument(JSONObject.fromObject(json));
Please sign in to comment.
Something went wrong with that request. Please try again.