Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #509 #629

Closed
wants to merge 14 commits into from
21 changes: 13 additions & 8 deletions src/main/java/net/openhft/chronicle/wire/Marshallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package net.openhft.chronicle.wire;

import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.annotation.DontChain;
import net.openhft.chronicle.core.io.IORuntimeException;
import net.openhft.chronicle.core.io.Resettable;
Expand All @@ -34,12 +35,16 @@

import static net.openhft.chronicle.wire.WireMarshaller.WIRE_MARSHALLER_CL;
import static net.openhft.chronicle.wire.WireType.TEXT;
import static net.openhft.chronicle.wire.WireType.YAML;

/**
* The implementation of this interface is both readable and write-able as marshallable data.
*/
@DontChain
public interface Marshallable extends WriteMarshallable, ReadMarshallable, Resettable {
WireType FROM_STRING_WIRE = Jvm.getBoolean("wire.testAsYaml")
? YAML : TEXT;

static boolean $equals(@NotNull WriteMarshallable $this, Object o) {
return o instanceof WriteMarshallable &&
($this == o || Wires.isEquals($this, o));
Expand All @@ -50,17 +55,17 @@ public interface Marshallable extends WriteMarshallable, ReadMarshallable, Reset
}

static String $toString(WriteMarshallable $this) {
return TEXT.asString($this);
return FROM_STRING_WIRE.asString($this);
}

@Nullable
static <T> T fromString(@NotNull CharSequence cs) {
return TEXT.fromString(cs);
return FROM_STRING_WIRE.fromString(cs);
}

@Nullable
static <T> T fromString(@NotNull Class<T> tClass, @NotNull CharSequence cs) {
return TEXT.fromString(tClass, cs);
return FROM_STRING_WIRE.fromString(tClass, cs);
}

/**
Expand All @@ -71,12 +76,12 @@ static <T> T fromString(@NotNull Class<T> tClass, @NotNull CharSequence cs) {
*/
@NotNull
static <T> T fromFile(String filename) throws IOException {
return TEXT.fromFile(filename);
return FROM_STRING_WIRE.fromFile(filename);
}

static <T> T fromString(@NotNull InputStream is) {
Scanner s = new Scanner(is).useDelimiter("\\A");
return TEXT.fromString(s.hasNext() ? s.next() : "");
return FROM_STRING_WIRE.fromString(s.hasNext() ? s.next() : "");
}

/**
Expand All @@ -88,17 +93,17 @@ static <T> T fromString(@NotNull InputStream is) {
*/
@Nullable
static <T> T fromFile(@NotNull Class<T> expectedType, String filename) throws IOException {
return TEXT.fromFile(expectedType, filename);
return FROM_STRING_WIRE.fromFile(expectedType, filename);
}

@NotNull
static <T> Stream<T> streamFromFile(String filename) throws IOException {
return TEXT.streamFromFile(filename);
return FROM_STRING_WIRE.streamFromFile(filename);
}

@Nullable
static <T> Stream<T> streamFromFile(@NotNull Class<T> expectedType, String filename) throws IOException {
return TEXT.streamFromFile(expectedType, filename);
return FROM_STRING_WIRE.streamFromFile(expectedType, filename);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public void readMarshallableInputOrder(T t, @NotNull WireIn in, T defaults, bool
}

public boolean matchesFieldName(StringBuilder sb, FieldAccess field) {
return sb.length() == 0 || StringUtils.isEqual(field.field.getName(), sb);
return sb.length() == 0 || StringUtils.equalsCaseIgnore(field.field.getName(), sb);
}

public void writeKey(T t, Bytes<?> bytes) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/openhft/chronicle/wire/YamlTokeniser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void reset() {
blockQuote = 0;
hasSequenceEntry = false;
lastKeyPosition = -1;
pushed.clear();
last = YamlToken.STREAM_START;
pushContext0(YamlToken.STREAM_START, NO_INDENT);
}
Expand Down Expand Up @@ -657,6 +658,10 @@ public long lineStart() {
return lineStart;
}

public void lineStart(long lineStart) {
this.lineStart = lineStart;
}

public long blockStart() {
return blockStart;
}
Expand Down
56 changes: 44 additions & 12 deletions src/main/java/net/openhft/chronicle/wire/YamlWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public <T> T methodWriter(@NotNull Class<T> tClass, Class... additional) {
() -> newTextMethodWriterInvocationHandler(tClass));
for (Class aClass : additional)
builder.addInterface(aClass);
useTextDocuments();
builder.marshallableOut(this);
return builder.build();
}
Expand Down Expand Up @@ -249,6 +250,7 @@ protected void initReadContext() {
if (readContext == null)
useBinaryDocuments();
readContext.start();
yt.lineStart(bytes.readPosition());
}

@NotNull
Expand Down Expand Up @@ -450,6 +452,10 @@ protected StringBuilder readField(@NotNull StringBuilder sb) {
public <K> K readEvent(@NotNull Class<K> expectedClass) {
startEventIfTop();
switch (yt.current()) {
case MAPPING_START:
yt.next();
assert yt.current() == YamlToken.MAPPING_KEY;
// Deliberate fall-through
case MAPPING_KEY:
YamlToken next = yt.next();
if (next == YamlToken.MAPPING_KEY) {
Expand Down Expand Up @@ -532,6 +538,7 @@ public ValueIn read(String keyName) {
return valueIn;
}
}
// Next lines not covered by any tests
yt.revertToContext(contextSize);
bytes.readPosition(pos);
yt.next();
Expand All @@ -545,7 +552,8 @@ public ValueIn read(String keyName) {
return valueIn;

keys.push(lastKeyPosition);
valueIn.consumeAny(minIndent);
// Avoid consuming '}' but consume to next mapping key
valueIn.consumeAny(minIndent >= 0 ? minIndent : Integer.MAX_VALUE);
}

return defaultValueIn;
Expand Down Expand Up @@ -589,11 +597,10 @@ public ValueIn getValueIn() {
@NotNull
@Override
public Wire readComment(@NotNull StringBuilder s) {
sb.setLength(0);
s.setLength(0);
if (yt.current() == YamlToken.COMMENT) {
// Skip the initial '#'
YamlToken next = yt.next();
sb.append(yt.text());
s.append(yt.text());
yt.next();
}
return this;
}
Expand Down Expand Up @@ -729,11 +736,12 @@ public void endEvent() {
yt.next(Integer.MIN_VALUE);
} else {
while (yt.current() == YamlToken.MAPPING_KEY) {
yt.next();
valueIn.consumeAny(minIndent);
}
}
if (yt.current() == YamlToken.MAPPING_END || yt.current() == YamlToken.DOCUMENT_END || yt.current() == YamlToken.NONE) {
if (yt.current() == YamlToken.MAPPING_END ||
yt.current() == YamlToken.DOCUMENT_END ||
yt.current() == YamlToken.NONE) {
yt.next(Integer.MIN_VALUE);
return;
}
Expand Down Expand Up @@ -1315,7 +1323,6 @@ public <T, K> WireIn sequence(@NotNull T t, K kls, @NotNull TriConsumer<T, K, Va
while (true) {
switch (yt.current()) {
case SEQUENCE_ENTRY:
yt.next(Integer.MIN_VALUE);
tReader.accept(t, kls, YamlWire.this.valueIn);
continue;

Expand Down Expand Up @@ -1352,9 +1359,11 @@ public boolean hasNext() {
public boolean hasNextSequenceItem() {
consumePadding();
switch (yt.current()) {
// Perhaps should be negative selection instead of positive
case SEQUENCE_START:
case TEXT:
case SEQUENCE_ENTRY:
// Allows scalar value to be converted into singleton array
case TEXT:
return true;
}
return false;
Expand Down Expand Up @@ -1445,7 +1454,20 @@ String stringForCode(int code) {
@Override
public <T> WireIn typeLiteralAsText(T t, @NotNull BiConsumer<T, CharSequence> classNameConsumer)
throws IORuntimeException, BufferUnderflowException {
throw new UnsupportedOperationException(yt.toString());
if (yt.current() != YamlToken.TAG)
throw new UnsupportedOperationException(yt.toString());

if (!yt.isText("type"))
throw new UnsupportedOperationException(yt.text());

if (yt.next() != YamlToken.TEXT)
throw new UnsupportedOperationException(yt.toString());

StringBuilder stringBuilder = acquireStringBuilder();
textTo(stringBuilder);
classNameConsumer.accept(t, stringBuilder);

return YamlWire.this;
}

@Override
Expand All @@ -1454,7 +1476,13 @@ public Type typeLiteral(BiFunction<CharSequence, ClassNotFoundException, Type> u
if (yt.current() == YamlToken.TAG) {
if (yt.text().equals("type")) {
if (yt.next() == YamlToken.TEXT) {
Class aClass = classLookup().forName(yt.text());
String text = yt.text();
Type aClass;
try {
aClass = classLookup().forName(text);
} catch (ClassNotFoundRuntimeException e) {
aClass = unresolvedHandler.apply(text, e.getCause());
}
yt.next();
return aClass;
}
Expand Down Expand Up @@ -1659,6 +1687,8 @@ public int uint16() {
@Override
public long int64() {
consumePadding();
if (yt.current() == YamlToken.SEQUENCE_ENTRY)
yt.next();
valueIn.skipType();
if (yt.current() != YamlToken.TEXT) {
Jvm.warn().on(getClass(), "Unable to read " + valueIn.object() + " as a long.");
Expand All @@ -1671,9 +1701,11 @@ public long int64() {
@Override
public double float64() {
consumePadding();
if (yt.current() == YamlToken.SEQUENCE_ENTRY)
yt.next();
valueIn.skipType();
if (yt.current() != YamlToken.TEXT) {
Jvm.warn().on(getClass(), "Unable to read " + valueIn.object() + " as a long.");
Jvm.warn().on(getClass(), "Unable to read " + valueIn.object() + " as a double.");
return 0;
}
return getADouble();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public static Collection<Object[]> data() {
"}\n" +
"...\n",
w.toString())},
{WireType.YAML,
(Consumer<WireIn>) w -> assertEquals("" +
"handle: !CAPTData {\n" +
" value: 0\n" +
"}\n" +
"...\n",
w.toString())},
{WireType.BINARY,
(Consumer<WireIn>) w -> assertEquals("" +
"1f 00 00 00 # msg-length\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,51 @@

import net.openhft.chronicle.bytes.BytesMarshallable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeFalse;

@RunWith(value = Parameterized.class)
public class DeserializeFromNakedFileTest extends WireTestCommon {
private final WireType wireType;

public DeserializeFromNakedFileTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> combinations() {
Object[][] list = {
{WireType.TEXT},
{WireType.YAML}
};
return Arrays.asList(list);
}

@Test
public void testPOJO() throws IOException {
PlainOldJavaClass res = Marshallable.fromFile(PlainOldJavaClass.class, "naked.yaml");
PlainOldJavaClass res = wireType.fromFile(PlainOldJavaClass.class, "naked.yaml");

assertEquals(20, res.heartBtInt);
}

@Test
public void testSelfDescribing() throws IOException {
SelfDescribingClass res = Marshallable.fromFile(SelfDescribingClass.class, "naked.yaml");
SelfDescribingClass res = wireType.fromFile(SelfDescribingClass.class, "naked.yaml");

assertEquals(20, res.heartBtInt);
}

@Test
public void testBytes() throws IOException {
BytesClass res = Marshallable.fromFile(BytesClass.class, "naked.yaml");
assumeFalse(wireType == WireType.YAML);
BytesClass res = wireType.fromFile(BytesClass.class, "naked.yaml");

// The result of parsing first 4 bytes as integer value
assertEquals(0x72616548, res.heartBtInt);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/openhft/chronicle/wire/FIX42Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void dump() {
@NotNull MarketDataSnapshot mds = new MarketDataSnapshot("EURUSD", 1.1187, 1.1179);
mds.writeMarshallable(wire);
System.out.println(wire.getClass().getSimpleName() + ", fixed=" + fixed + ", numericField=" + numericField + ", fieldLess=" + fieldLess);
if (wire instanceof TextWire)
if (!wire.isBinary())
assertEquals(dump, wire.bytes().toString());
else
assertEquals(dump, wire.bytes().toHexString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ public ForwardAndBackwardCompatibilityMarshallableTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{WireType.JSON},
{WireType.TEXT},
{WireType.YAML},
{WireType.BINARY}
});
}
Expand All @@ -58,9 +59,6 @@ public void marshableStringBuilderTest() throws Exception {
wire.writeDocument(false, w -> new MDTO2(1, 2, "3").writeMarshallable(w));
// System.out.println(Wires.fromSizePrefixedBlobs(wire));

if (wire instanceof TextWire)
((TextWire) wire).useBinaryDocuments();

try (DocumentContext dc = wire.readingDocument()) {
if (!dc.isPresent())
Assert.fail();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/net/openhft/chronicle/wire/NestedMapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public NestedMapsTest(WireType wireType) {
this.wireType = wireType;
}

@Parameterized.Parameters
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> wireTypes() {
return Arrays.asList(
new Object[]{WireType.JSON},
new Object[]{WireType.TEXT},
new Object[]{WireType.YAML},
new Object[]{WireType.BINARY},
new Object[]{WireType.FIELDLESS_BINARY}
);
Expand Down
Loading