diff --git a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/arbitraries/SbeArbitraries.java b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/arbitraries/SbeArbitraries.java index 2a3d2bd9b3..cc2f5ff9ab 100644 --- a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/arbitraries/SbeArbitraries.java +++ b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/arbitraries/SbeArbitraries.java @@ -150,10 +150,18 @@ private static Arbitrary enumTypeSchema() private static Arbitrary setTypeSchema() { return Arbitraries.oneOf( - Arbitraries.integers().between(0, 7).set().map(choices -> new SetSchema("uint8", choices)), - Arbitraries.integers().between(0, 15).set().map(choices -> new SetSchema("uint16", choices)), - Arbitraries.integers().between(0, 31).set().map(choices -> new SetSchema("uint32", choices)), - Arbitraries.integers().between(0, 63).set().map(choices -> new SetSchema("uint64", choices)) + Arbitraries.integers().between(0, 7).set() + .ofMaxSize(8) + .map(choices -> new SetSchema("uint8", choices)), + Arbitraries.integers().between(0, 15).set() + .ofMaxSize(16) + .map(choices -> new SetSchema("uint16", choices)), + Arbitraries.integers().between(0, 31).set() + .ofMaxSize(32) + .map(choices -> new SetSchema("uint32", choices)), + Arbitraries.integers().between(0, 63).set() + .ofMaxSize(64) + .map(choices -> new SetSchema("uint64", choices)) ); } @@ -213,14 +221,19 @@ private static Arbitrary presence() private static Arbitrary varDataSchema() { - return Arbitraries.of(VarDataSchema.Encoding.values()) - .map(VarDataSchema::new); + return Combinators.combine( + Arbitraries.of(VarDataSchema.Encoding.values()), + Arbitraries.of( + PrimitiveType.UINT8, + PrimitiveType.UINT16, + PrimitiveType.UINT32 + ) + ).as(VarDataSchema::new); } public static Arbitrary messageSchema() { return Combinators.combine( - withDuplicates( 3, Combinators.combine( diff --git a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/TestXmlSchemaWriter.java b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/TestXmlSchemaWriter.java index 1481126646..7ccd273017 100644 --- a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/TestXmlSchemaWriter.java +++ b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/TestXmlSchemaWriter.java @@ -20,6 +20,7 @@ import org.agrona.collections.MutableInteger; import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.w3c.dom.Node; import java.io.File; import java.io.StringWriter; @@ -74,7 +75,7 @@ private static void writeTo( final Element topLevelTypes = createTypesElement(document); root.appendChild(topLevelTypes); - final HashMap typeToName = new HashMap<>(); + final HashMap typeToName = new HashMap<>(); final TypeSchemaConverter typeSchemaConverter = new TypeSchemaConverter( document, @@ -88,7 +89,8 @@ private static void writeTo( topLevelTypes, typeSchemaConverter, schema.blockFields().stream().map(FieldSchema::type).collect(Collectors.toList()), - schema.groups()); + schema.groups(), + schema.varData()); final Element message = document.createElement("sbe:message"); message.setAttribute("name", "TestMessage"); @@ -129,7 +131,7 @@ private static void writeTo( private static void appendMembers( final Document document, - final HashMap typeToName, + final HashMap typeToName, final List blockFields, final List groups, final List varData, @@ -174,21 +176,10 @@ private static void appendMembers( for (final VarDataSchema data : varData) { final int id = nextMemberId.getAndIncrement(); - final Element element = document.createElement("data"); element.setAttribute("id", Integer.toString(id)); element.setAttribute("name", "member" + id); - switch (data.encoding()) - { - case ASCII: - element.setAttribute("type", "varStringEncoding"); - break; - case BYTES: - element.setAttribute("type", "varDataEncoding"); - break; - default: - throw new IllegalStateException("Unknown encoding: " + data.encoding()); - } + element.setAttribute("type", requireNonNull(typeToName.get(data))); parent.appendChild(element); } } @@ -213,27 +204,6 @@ private static Element createTypesElement(final Document document) createTypeElement(document, "numInGroup", "uint16") )); - final Element varString = createTypeElement(document, "varData", "uint8"); - varString.setAttribute("length", "0"); - varString.setAttribute("characterEncoding", "US-ASCII"); - types.appendChild(createCompositeElement( - document, - "varStringEncoding", - createTypeElement(document, "length", "uint16"), - varString - )); - - final Element varData = createTypeElement(document, "varData", "uint8"); - final Element varDataLength = createTypeElement(document, "length", "uint32"); - varDataLength.setAttribute("maxValue", "1000000"); - varData.setAttribute("length", "0"); - types.appendChild(createCompositeElement( - document, - "varDataEncoding", - varDataLength, - varData - )); - return types; } @@ -359,7 +329,8 @@ private static void appendTypes( final Element topLevelTypes, final TypeSchemaConverter typeSchemaConverter, final List blockFields, - final List groups) + final List groups, + final List varDataFields) { for (final TypeSchema field : blockFields) { @@ -376,23 +347,29 @@ private static void appendTypes( topLevelTypes, typeSchemaConverter, group.blockFields().stream().map(FieldSchema::type).collect(Collectors.toList()), - group.groups() - ); + group.groups(), + group.varData()); + } + + for (final VarDataSchema varData : varDataFields) + { + topLevelTypes.appendChild(typeSchemaConverter.convert(varData)); } } + @SuppressWarnings("EnhancedSwitchMigration") private static final class TypeSchemaConverter implements TypeSchemaVisitor { private final Document document; private final Element topLevelTypes; - private final Map typeToName; - private final Function nextName; + private final Map typeToName; + private final Function nextName; private Element result; private TypeSchemaConverter( final Document document, final Element topLevelTypes, - final Map typeToName) + final Map typeToName) { this.document = document; this.topLevelTypes = topLevelTypes; @@ -481,5 +458,31 @@ public Element convert(final TypeSchema type) type.accept(this); return requireNonNull(result); } + + public Node convert(final VarDataSchema varData) + { + final Element lengthElement = createTypeElement(document, "length", + varData.lengthEncoding().primitiveName()); + + if (varData.lengthEncoding().size() >= 4) + { + lengthElement.setAttribute("maxValue", Integer.toString(1_000_000)); + } + + final Element varDataElement = createTypeElement(document, "varData", "uint8"); + varDataElement.setAttribute("length", "0"); + + if (varData.dataEncoding().equals(VarDataSchema.Encoding.ASCII)) + { + varDataElement.setAttribute("characterEncoding", "US-ASCII"); + } + + return createCompositeElement( + document, + typeToName.computeIfAbsent(varData, nextName), + lengthElement, + varDataElement + ); + } } } diff --git a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/VarDataSchema.java b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/VarDataSchema.java index c6b39db56b..b5e98c02c7 100644 --- a/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/VarDataSchema.java +++ b/sbe-tool/src/propertyTest/java/uk/co/real_logic/sbe/properties/schema/VarDataSchema.java @@ -16,18 +16,29 @@ package uk.co.real_logic.sbe.properties.schema; +import uk.co.real_logic.sbe.PrimitiveType; + public final class VarDataSchema { - private final Encoding encoding; + private final Encoding dataEncoding; + private final PrimitiveType lengthEncoding; + + public VarDataSchema( + final Encoding dataEncoding, + final PrimitiveType lengthEncoding) + { + this.dataEncoding = dataEncoding; + this.lengthEncoding = lengthEncoding; + } - public VarDataSchema(final Encoding encoding) + public Encoding dataEncoding() { - this.encoding = encoding; + return dataEncoding; } - public Encoding encoding() + public PrimitiveType lengthEncoding() { - return encoding; + return lengthEncoding; } public enum Encoding