Skip to content

Commit

Permalink
[Java] Better handling of versions and constants in JsonPrinter. Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Jul 2, 2020
1 parent 6e604be commit ad76638
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onEncoding(
final Token typeToken,
final int actingVersion)
{
final CharSequence value = readEncodingAsString(buffer, index, typeToken, actingVersion);
final CharSequence value = readEncodingAsString(buffer, index, typeToken, fieldToken.version(), actingVersion);

printScope();
out.append(compositeLevel > 0 ? typeToken.name() : fieldToken.name())
Expand All @@ -80,7 +80,8 @@ public void onEnum(
final int actingVersion)
{
final Token typeToken = tokens.get(beginIndex + 1);
final long encodedValue = readEncodingAsLong(buffer, bufferIndex, typeToken, actingVersion);
final long encodedValue = readEncodingAsLong(
buffer, bufferIndex, typeToken, fieldToken.version(), actingVersion);

String value = null;
if (fieldToken.isConstantEncoding())
Expand Down Expand Up @@ -118,7 +119,8 @@ public void onBitSet(
final int actingVersion)
{
final Token typeToken = tokens.get(beginIndex + 1);
final long encodedValue = readEncodingAsLong(buffer, bufferIndex, typeToken, actingVersion);
final long encodedValue = readEncodingAsLong(
buffer, bufferIndex, typeToken, fieldToken.version(), actingVersion);

printScope();
out.append(determineName(0, fieldToken, tokens, beginIndex)).append(':');
Expand All @@ -140,14 +142,12 @@ public void onBeginComposite(
final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
{
++compositeLevel;

namedScope.push(determineName(1, fieldToken, tokens, fromIndex) + ".");
}

public void onEndComposite(final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
{
--compositeLevel;

namedScope.pop();
}

Expand Down Expand Up @@ -218,17 +218,22 @@ private String determineName(
}

private static CharSequence readEncodingAsString(
final DirectBuffer buffer, final int index, final Token typeToken, final int actingVersion)
final DirectBuffer buffer,
final int index,
final Token typeToken,
final int fieldVersion,
final int actingVersion)
{
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, actingVersion);
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, fieldVersion, actingVersion);
if (null != constOrNotPresentValue)
{
if (constOrNotPresentValue.size() == 1)
final String characterEncoding = constOrNotPresentValue.characterEncoding();
if (constOrNotPresentValue.size() == 1 && null != characterEncoding)
{
try
{
final byte[] bytes = { (byte)constOrNotPresentValue.longValue() };
return new String(bytes, constOrNotPresentValue.characterEncoding());
return new String(bytes, characterEncoding);
}
catch (final UnsupportedEncodingException ex)
{
Expand All @@ -237,7 +242,24 @@ private static CharSequence readEncodingAsString(
}
else
{
return constOrNotPresentValue.toString();
final String value = constOrNotPresentValue.toString();
final int size = typeToken.arrayLength();

if (size < 2)
{
return value;
}

final StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++)
{
sb.append(value).append(", ");
}

sb.setLength(sb.length() - 2);

return sb;
}
}

Expand All @@ -257,9 +279,13 @@ private static CharSequence readEncodingAsString(
}

private static long readEncodingAsLong(
final DirectBuffer buffer, final int bufferIndex, final Token typeToken, final int actingVersion)
final DirectBuffer buffer,
final int bufferIndex,
final Token typeToken,
final int fieldVersion,
final int actingVersion)
{
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, actingVersion);
final PrimitiveValue constOrNotPresentValue = constOrNotPresentValue(typeToken, fieldVersion, actingVersion);
if (null != constOrNotPresentValue)
{
return constOrNotPresentValue.longValue();
Expand All @@ -268,15 +294,16 @@ private static long readEncodingAsLong(
return Types.getLong(buffer, bufferIndex, typeToken.encoding());
}

private static PrimitiveValue constOrNotPresentValue(final Token token, final int actingVersion)
private static PrimitiveValue constOrNotPresentValue(
final Token typeToken, final int fieldVersion, final int actingVersion)
{
if (token.isConstantEncoding())
if (typeToken.isConstantEncoding())
{
return token.encoding().constValue();
return typeToken.encoding().constValue();
}
else if (token.isOptionalEncoding() && actingVersion < token.version())
else if (typeToken.isOptionalEncoding() && actingVersion < fieldVersion)
{
return token.encoding().applicableNullValue();
return typeToken.encoding().applicableNullValue();
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public static void main(final String[] args) throws Exception
System.out.println("\n*** OTF Example ***\n");

// Encode up message and schema as if we just got them off the wire.
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY);
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
encodeSchema(encodedSchemaBuffer);

final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY);
final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
encodeTestMessage(encodedMsgBuffer);

// Now lets decode the schema IR so we have IR objects.
Expand Down Expand Up @@ -96,7 +96,7 @@ public static void main(final String[] args) throws Exception

private static void encodeSchema(final ByteBuffer byteBuffer) throws Exception
{
final Path path = Paths.get("example-schema.xml");
final Path path = Paths.get("example-extension-schema.xml");
try (InputStream in = new BufferedInputStream(Files.newInputStream(path)))
{
final MessageSchema schema = XmlSchemaParser.parse(in, ParserOptions.DEFAULT);
Expand All @@ -111,7 +111,6 @@ private static void encodeSchema(final ByteBuffer byteBuffer) throws Exception
private static void encodeTestMessage(final ByteBuffer byteBuffer)
{
final UnsafeBuffer buffer = new UnsafeBuffer(byteBuffer);

final int encodedLength = ExampleUsingGeneratedStub.encode(CAR_ENCODER, buffer, HEADER_ENCODER);

byteBuffer.position(encodedLength);
Expand Down
46 changes: 24 additions & 22 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/PrimitiveValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,33 +379,35 @@ public String characterEncoding()
}

/**
* Return String representation of this object
* Return String value representation of this object.
*
* @return String representing object value
* @return String representing object value.
*/
public String toString()
{
switch (representation)
if (Representation.LONG == representation)
{
case LONG:
return Long.toString(longValue);

case DOUBLE:
return Double.toString(doubleValue);

case BYTE_ARRAY:
try
{
return null == characterEncoding ?
new String(byteArrayValue) : new String(byteArrayValue, characterEncoding);
}
catch (final UnsupportedEncodingException ex)
{
throw new IllegalStateException(ex);
}

default:
throw new IllegalStateException("Unsupported Representation: " + representation);
return Long.toString(longValue);
}
else if (Representation.DOUBLE == representation)
{
return Double.toString(doubleValue);
}
else if (Representation.BYTE_ARRAY == representation)
{
try
{
return null == characterEncoding ?
new String(byteArrayValue) : new String(byteArrayValue, characterEncoding);
}
catch (final UnsupportedEncodingException ex)
{
throw new IllegalStateException(ex);
}
}
else
{
throw new IllegalStateException("Unsupported Representation: " + representation);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public void print(final ByteBuffer encodedMessage, final StringBuilder output)

public void print(final StringBuilder output, final UnsafeBuffer buffer, final int bufferOffset)
{
final int blockLength = headerDecoder.getBlockLength(buffer, bufferOffset);
final int templateId = headerDecoder.getTemplateId(buffer, bufferOffset);
final int schemaId = headerDecoder.getSchemaId(buffer, bufferOffset);
final int actingVersion = headerDecoder.getSchemaVersion(buffer, bufferOffset);
final int blockLength = headerDecoder.getBlockLength(buffer, bufferOffset);

validateId(schemaId);
validateVersion(schemaId, actingVersion);

final int messageOffset = bufferOffset + headerDecoder.encodedLength();
final List<Token> msgTokens = ir.getMessage(templateId);
Expand All @@ -74,15 +73,6 @@ private void validateId(final int schemaId)
}
}

private void validateVersion(final int schemaId, final int actingVersion)
{
if (actingVersion > ir.version())
{
throw new IllegalArgumentException(
"Required schema version " + actingVersion + " but was " + ir.version() + " for schema id " + schemaId);
}
}

public String print(final ByteBuffer encodedMessage)
{
final StringBuilder sb = new StringBuilder();
Expand Down
Loading

0 comments on commit ad76638

Please sign in to comment.