mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
parent
1066d61dae
commit
62cbb0613e
9 changed files with 48 additions and 127 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.logstash;
|
||||
|
||||
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFloat;
|
||||
import org.jruby.RubyString;
|
||||
import org.logstash.bivalues.BiValue;
|
||||
|
@ -21,8 +22,8 @@ public class Javafier {
|
|||
if (o instanceof RubyString) {
|
||||
return o.toString();
|
||||
}
|
||||
if (o instanceof String || o instanceof Float || o instanceof Double ||
|
||||
o instanceof Timestamp) {
|
||||
if (o instanceof String || o instanceof Float || o instanceof Double ||
|
||||
o instanceof Boolean || o instanceof Timestamp) {
|
||||
return o;
|
||||
}
|
||||
if (o instanceof RubyFloat) {
|
||||
|
@ -31,6 +32,9 @@ public class Javafier {
|
|||
if (o instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
|
||||
return ((JrubyTimestampExtLibrary.RubyTimestamp) o).getTimestamp();
|
||||
}
|
||||
if (o instanceof RubyBoolean) {
|
||||
return ((RubyBoolean) o).isTrue();
|
||||
}
|
||||
if (o instanceof BiValue) {
|
||||
return ((BiValue)o).javaValue();
|
||||
} else if(o instanceof ConvertedMap) {
|
||||
|
|
|
@ -10,15 +10,17 @@ import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
|||
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFloat;
|
||||
import org.jruby.RubyString;
|
||||
|
||||
public final class ObjectMappers {
|
||||
|
||||
private static final SimpleModule RUBY_SERIALIZERS =
|
||||
new SimpleModule("RubyStringSerializer")
|
||||
new SimpleModule("RubySerializers")
|
||||
.addSerializer(RubyString.class, new RubyStringSerializer())
|
||||
.addSerializer(RubyFloat.class, new RubyFloatSerializer());
|
||||
.addSerializer(RubyFloat.class, new RubyFloatSerializer())
|
||||
.addSerializer(RubyBoolean.class, new RubyBooleanSerializer());
|
||||
|
||||
public static final ObjectMapper JSON_MAPPER =
|
||||
new ObjectMapper().registerModule(RUBY_SERIALIZERS);
|
||||
|
@ -72,4 +74,22 @@ public final class ObjectMappers {
|
|||
generator.writeNumber(value.getDoubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializer for {@link RubyBoolean} since Jackson can't handle that type natively, so we
|
||||
* simply serialize it as if it were a {@code boolean}.
|
||||
*/
|
||||
private static final class RubyBooleanSerializer
|
||||
extends NonTypedScalarSerializerBase<RubyBoolean> {
|
||||
|
||||
RubyBooleanSerializer() {
|
||||
super(RubyBoolean.class, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final RubyBoolean value, final JsonGenerator generator,
|
||||
final SerializerProvider provider) throws IOException {
|
||||
generator.writeBoolean(value.isTrue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import org.jruby.Ruby;
|
||||
import org.jruby.RubyArray;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFloat;
|
||||
import org.jruby.RubyHash;
|
||||
import org.jruby.RubyString;
|
||||
|
@ -29,6 +30,7 @@ public final class Rubyfier {
|
|||
|
||||
public static IRubyObject deep(Ruby runtime, final Object input) {
|
||||
if (input instanceof RubyString || input instanceof RubyFloat
|
||||
|| input instanceof RubyBoolean
|
||||
|| input instanceof JrubyTimestampExtLibrary.RubyTimestamp) {
|
||||
return (IRubyObject) input;
|
||||
}
|
||||
|
@ -36,6 +38,7 @@ public final class Rubyfier {
|
|||
if (input instanceof Double || input instanceof Float) {
|
||||
return RubyUtil.RUBY.newFloat(((Number) input).doubleValue());
|
||||
}
|
||||
if (input instanceof Boolean) return RubyUtil.RUBY.newBoolean((Boolean) input);
|
||||
if (input instanceof Timestamp) {
|
||||
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
|
||||
RubyUtil.RUBY, (Timestamp) input
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import org.joda.time.DateTime;
|
||||
import org.jruby.RubyArray;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFloat;
|
||||
import org.jruby.RubyHash;
|
||||
import org.jruby.RubyString;
|
||||
|
@ -55,7 +56,7 @@ public final class Valuefier {
|
|||
if (o instanceof RubyString || o instanceof RubyFloat
|
||||
|| o instanceof JrubyTimestampExtLibrary.RubyTimestamp
|
||||
|| o instanceof ConvertedMap || o instanceof ConvertedList
|
||||
|| o instanceof BiValue) {
|
||||
|| o instanceof BiValue || o instanceof RubyBoolean) {
|
||||
return o;
|
||||
}
|
||||
if (o instanceof String) {
|
||||
|
@ -64,6 +65,9 @@ public final class Valuefier {
|
|||
if (o instanceof Float || o instanceof Double) {
|
||||
return RubyUtil.RUBY.newFloat(((Number) o).doubleValue());
|
||||
}
|
||||
if (o instanceof Boolean) {
|
||||
return RubyUtil.RUBY.newBoolean((Boolean) o);
|
||||
}
|
||||
if (o instanceof Timestamp) {
|
||||
return JrubyTimestampExtLibrary.RubyTimestamp.newRubyTimestamp(
|
||||
RubyUtil.RUBY, (Timestamp) o
|
||||
|
|
|
@ -4,7 +4,6 @@ import java.math.BigDecimal;
|
|||
import java.math.BigInteger;
|
||||
import java.util.HashMap;
|
||||
import org.jruby.RubyBignum;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyInteger;
|
||||
import org.jruby.RubySymbol;
|
||||
import org.jruby.ext.bigdecimal.RubyBigDecimal;
|
||||
|
@ -12,7 +11,6 @@ import org.jruby.java.proxies.JavaProxy;
|
|||
import org.jruby.runtime.builtin.IRubyObject;
|
||||
|
||||
public enum BiValues {
|
||||
JAVA_LANG_BOOLEAN(BiValueType.BOOLEAN),
|
||||
JAVA_LANG_INTEGER(BiValueType.INT),
|
||||
JAVA_LANG_LONG(BiValueType.LONG),
|
||||
JAVA_MATH_BIGDECIMAL(BiValueType.DECIMAL),
|
||||
|
@ -20,9 +18,6 @@ public enum BiValues {
|
|||
ORG_JRUBY_EXT_BIGDECIMAL_RUBYBIGDECIMAL(BiValueType.DECIMAL),
|
||||
ORG_JRUBY_JAVA_PROXIES_CONCRETEJAVAPROXY(BiValueType.JAVAPROXY),
|
||||
ORG_JRUBY_RUBYBIGNUM(BiValueType.BIGINT),
|
||||
ORG_JRUBY_RUBYBOOLEAN$FALSE(BiValueType.BOOLEAN),
|
||||
ORG_JRUBY_RUBYBOOLEAN$TRUE(BiValueType.BOOLEAN),
|
||||
ORG_JRUBY_RUBYBOOLEAN(BiValueType.BOOLEAN),
|
||||
ORG_JRUBY_RUBYFIXNUM(BiValueType.LONG),
|
||||
ORG_JRUBY_RUBYINTEGER(BiValueType.LONG),
|
||||
ORG_JRUBY_RUBYNIL(BiValueType.NULL),
|
||||
|
@ -31,15 +26,11 @@ public enum BiValues {
|
|||
|
||||
private static HashMap<String, String> initCache() {
|
||||
HashMap<String, String> hm = new HashMap<>();
|
||||
hm.put("java.lang.Boolean", "JAVA_LANG_BOOLEAN");
|
||||
hm.put("java.lang.Integer", "JAVA_LANG_INTEGER");
|
||||
hm.put("java.lang.Long", "JAVA_LANG_LONG");
|
||||
hm.put("java.math.BigDecimal", "JAVA_MATH_BIGDECIMAL");
|
||||
hm.put("java.math.BigInteger", "JAVA_MATH_BIGINTEGER");
|
||||
hm.put("org.jruby.RubyBignum", "ORG_JRUBY_RUBYBIGNUM");
|
||||
hm.put("org.jruby.RubyBoolean", "ORG_JRUBY_RUBYBOOLEAN");
|
||||
hm.put("org.jruby.RubyBoolean$False", "ORG_JRUBY_RUBYBOOLEAN$FALSE");
|
||||
hm.put("org.jruby.RubyBoolean$True", "ORG_JRUBY_RUBYBOOLEAN$TRUE");
|
||||
hm.put("org.jruby.RubyFixnum", "ORG_JRUBY_RUBYFIXNUM");
|
||||
hm.put("org.jruby.RubyInteger", "ORG_JRUBY_RUBYINTEGER");
|
||||
hm.put("org.jruby.RubyNil", "ORG_JRUBY_RUBYNIL");
|
||||
|
@ -119,14 +110,6 @@ public enum BiValues {
|
|||
return new BigDecimalBiValue((BigDecimal) value);
|
||||
}
|
||||
},
|
||||
BOOLEAN {
|
||||
BiValue build(Object value) {
|
||||
if (value instanceof IRubyObject) {
|
||||
return new BooleanBiValue((RubyBoolean) value);
|
||||
}
|
||||
return new BooleanBiValue((Boolean) value);
|
||||
}
|
||||
},
|
||||
NULL {
|
||||
NullBiValue build(Object value) {
|
||||
return NULL_BI_VALUE;
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package org.logstash.bivalues;
|
||||
|
||||
import org.jruby.Ruby;
|
||||
import org.jruby.RubyBoolean;
|
||||
|
||||
import java.io.ObjectStreamException;
|
||||
|
||||
|
||||
public class BooleanBiValue extends BiValue<RubyBoolean, Boolean> {
|
||||
|
||||
public BooleanBiValue(RubyBoolean rubyValue) {
|
||||
this.rubyValue = rubyValue;
|
||||
javaValue = null;
|
||||
}
|
||||
|
||||
public BooleanBiValue(Boolean javaValue) {
|
||||
this.javaValue = javaValue;
|
||||
rubyValue = null;
|
||||
}
|
||||
|
||||
private BooleanBiValue() {
|
||||
}
|
||||
|
||||
protected void addRuby(Ruby runtime) {
|
||||
rubyValue = RubyBoolean.newBoolean(runtime, javaValue);
|
||||
}
|
||||
|
||||
protected void addJava() {
|
||||
javaValue = rubyValue.isTrue();
|
||||
}
|
||||
|
||||
// Called when object is to be serialized on a stream to allow the object to substitute a proxy for itself.
|
||||
private Object writeReplace() throws ObjectStreamException {
|
||||
return newProxy(this);
|
||||
}
|
||||
}
|
|
@ -120,6 +120,18 @@ public final class EventTest {
|
|||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanFieldToJson() throws Exception {
|
||||
Event e = new Event();
|
||||
e.setField("[foo][bar][baz]", true);
|
||||
assertJsonEquals(
|
||||
String.format(
|
||||
"{\"@timestamp\":\"%s\",\"foo\":{\"bar\":{\"baz\":true}},\"@version\":\"1\"}",
|
||||
e.getTimestamp().toIso8601()
|
||||
), e.toJson()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFieldList() throws Exception {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
|
|
|
@ -7,7 +7,6 @@ import java.io.ObjectOutputStream;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import org.jruby.RubyBignum;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFixnum;
|
||||
import org.jruby.RubyInteger;
|
||||
import org.jruby.RubySymbol;
|
||||
|
@ -85,23 +84,6 @@ public class BiValueTest extends TestBase {
|
|||
assertEquals(v, subject.rubyValue(ruby));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanBiValueFromRuby() {
|
||||
BooleanBiValue subject = new BooleanBiValue(RubyBoolean.newBoolean(ruby, true));
|
||||
assertTrue(subject.hasRubyValue());
|
||||
assertFalse(subject.hasJavaValue());
|
||||
assertTrue(subject.javaValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBooleanBiValueFromJava() {
|
||||
RubyBoolean v = RubyBoolean.newBoolean(ruby, true);
|
||||
BooleanBiValue subject = new BooleanBiValue(true);
|
||||
assertFalse(subject.hasRubyValue());
|
||||
assertTrue(subject.hasJavaValue());
|
||||
assertEquals(v, subject.rubyValue(ruby));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBiValueFromJava() {
|
||||
NullBiValue subject = NullBiValue.newNullBiValue();
|
||||
|
|
|
@ -3,7 +3,6 @@ package org.logstash.bivalues;
|
|||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import org.jruby.RubyBignum;
|
||||
import org.jruby.RubyBoolean;
|
||||
import org.jruby.RubyFixnum;
|
||||
import org.jruby.RubyInteger;
|
||||
import org.jruby.RubyNil;
|
||||
|
@ -14,9 +13,7 @@ import org.junit.Test;
|
|||
import org.logstash.TestBase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BiValuesTest extends TestBase {
|
||||
|
||||
|
@ -80,54 +77,6 @@ public class BiValuesTest extends TestBase {
|
|||
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiValuesBooleanRubyTrue() {
|
||||
boolean jo = true;
|
||||
RubyBoolean ro = (RubyBoolean) JavaUtil.convertJavaToUsableRubyObject(ruby, jo);
|
||||
BiValue<RubyBoolean, Boolean> subject = BiValues.newBiValue(ro);
|
||||
|
||||
assertEquals(ro, subject.rubyValueUnconverted());
|
||||
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
|
||||
assertTrue(subject.javaValue());
|
||||
assertEquals(Boolean.class, subject.javaValue().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiValuesBooleanRubyFalse() {
|
||||
boolean jo = false;
|
||||
RubyBoolean ro = (RubyBoolean) JavaUtil.convertJavaToUsableRubyObject(ruby, jo);
|
||||
BiValue<RubyBoolean, Boolean> subject = BiValues.newBiValue(ro);
|
||||
|
||||
assertEquals(ro, subject.rubyValueUnconverted());
|
||||
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
|
||||
assertFalse(subject.javaValue());
|
||||
assertEquals(Boolean.class, subject.javaValue().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiValuesBooleanJavaTrue() {
|
||||
boolean jo = true;
|
||||
RubyBoolean ro = (RubyBoolean) JavaUtil.convertJavaToUsableRubyObject(ruby, jo);
|
||||
BiValue<RubyBoolean, Boolean> subject = BiValues.newBiValue(jo);
|
||||
|
||||
assertTrue(subject.javaValue());
|
||||
assertEquals(Boolean.class, subject.javaValue().getClass());
|
||||
assertEquals(ro, subject.rubyValue(ruby));
|
||||
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiValuesBooleanJavaFalse() {
|
||||
boolean jo = false;
|
||||
RubyBoolean ro = (RubyBoolean) JavaUtil.convertJavaToUsableRubyObject(ruby, jo);
|
||||
BiValue<RubyBoolean, Boolean> subject = BiValues.newBiValue(jo);
|
||||
|
||||
assertFalse(subject.javaValue());
|
||||
assertEquals(Boolean.class, subject.javaValue().getClass());
|
||||
assertEquals(ro, subject.rubyValue(ruby));
|
||||
assertEquals(ro.getClass(), subject.rubyValue(ruby).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBiValuesNilRuby() {
|
||||
RubyNil ro = (RubyNil) ruby.getNil();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue