final EnumType enumType = new EnumType(valueRefNode); if (enumType.encodingType() != primitiveType) final EnumType.ValidValue validValue = enumType.getValidValue(validValueName);
private void add(final EnumType type, final int offset, final Field field) final PrimitiveType encodingType = type.encodingType(); final Encoding.Builder encodingBuilder = new Encoding.Builder() .primitiveType(encodingType) .byteOrder(schema.byteOrder()); if (type.presence() == Presence.OPTIONAL) .name(type.name()) .referencedName(type.referencedName()) .size(encodingType.size()) .offset(offset) .version(type.sinceVersion()) .deprecated(type.deprecated()) .description(type.description()) .encoding(encodingBuilder.build()); builder.version(Math.max(field.sinceVersion(), type.sinceVersion())); for (final EnumType.ValidValue validValue : type.validValues())
@Test public void shouldHandleBinaryEnumType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"biOp\" encodingType=\"uint8\">" + " <validValue name=\"off\" description=\"switch is off\">0</validValue>" + " <validValue name=\"on\" description=\"switch is on\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("biOp"); assertThat(e.name(), is("biOp")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("on").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("off").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); }
final EnumType e = (EnumType)map.get("triOp"); assertThat(e.name(), is("triOp")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); for (final EnumType.ValidValue v : e.validValues())
private void add(final EnumType type, final int offset, final Field field) { final PrimitiveType encodingType = type.encodingType(); final Encoding.Builder encodingBuilder = new Encoding.Builder() .primitiveType(encodingType) .semanticType(semanticTypeOf(type, field)) .byteOrder(byteOrder); if (type.presence() == Presence.OPTIONAL) { encodingBuilder.nullValue(encodingType.nullValue()); } final Token.Builder builder = new Token.Builder() .signal(Signal.BEGIN_ENUM) .name(type.name()) .size(encodingType.size()) .offset(offset) .encoding(encodingBuilder.build()); if (field != null) { builder.version(field.sinceVersion()); } tokenList.add(builder.build()); for (final EnumType.ValidValue validValue : type.validValues()) { add(validValue, encodingType, field); } builder.signal(Signal.END_ENUM); tokenList.add(builder.build()); }
(node) -> addTypeWithNameCheck(typeByNameMap, new EnumType(node), node));
@Test public void shouldHandleCharEnumEncodingType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"mixed\" encodingType=\"char\">" + " <validValue name=\"Cee\">C</validValue>" + " <validValue name=\"One\">1</validValue>" + " <validValue name=\"Two\">2</validValue>" + " <validValue name=\"Eee\">E</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("mixed"); assertThat(e.encodingType(), is(PrimitiveType.CHAR)); assertThat(e.getValidValue("Cee").primitiveValue(), is(PrimitiveValue.parse("C", PrimitiveType.CHAR))); assertThat(e.getValidValue("One").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.CHAR))); assertThat(e.getValidValue("Two").primitiveValue(), is(PrimitiveValue.parse("2", PrimitiveType.CHAR))); assertThat(e.getValidValue("Eee").primitiveValue(), is(PrimitiveValue.parse("E", PrimitiveType.CHAR))); }
/** * The encodedLength (in octets) of the encodingType * * @return encodedLength of the encodingType */ public int encodedLength() { if (presence() == Presence.CONSTANT) { return 0; } return encodingType.size(); }
@Test public void shouldHandleEncodingTypesWithNamedTypes() throws Exception { final MessageSchema schema = parse(TestUtil.getLocalResource( "encoding-types-schema.xml"), ParserOptions.DEFAULT); final List<Field> fields = schema.getMessage(1).fields(); assertNotNull(fields); EnumType type = (EnumType)fields.get(1).type(); assertThat(type.encodingType(), is(PrimitiveType.CHAR)); type = (EnumType)fields.get(2).type(); assertThat(type.encodingType(), is(PrimitiveType.UINT8)); }
private PrimitiveValue lookupValueRef(final String valueRef) { final int periodIndex = valueRef.indexOf('.'); final String valueRefType = valueRef.substring(0, periodIndex); final String validValueName = valueRef.substring(periodIndex + 1); final EnumType enumType = (EnumType)schema.getType(valueRefType); final EnumType.ValidValue validValue = enumType.getValidValue(validValueName); return validValue.primitiveValue(); }
final CompositeType nestedCompositeType = (CompositeType)elements.get(3); assertThat(enumType.name(), is("enumOne")); assertThat(encodedDataType.name(), is("zeroth")); assertThat(setType.name(), is("setOne"));
@Test public void shouldHandleBooleanEnumType() throws Exception { final String testXmlString = "<types>" + "<enum name=\"Boolean\" encodingType=\"uint8\" semanticType=\"Boolean\">" + " <validValue name=\"False\">0</validValue>" + " <validValue name=\"True\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("Boolean"); assertThat(e.name(), is("Boolean")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("True").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("False").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); }
type = new EnumType(list.item(i));
if (((EncodedDataType)type).primitiveType() != enumType.encodingType())
private void validateValueRef(final Node node, final Map<String, Type> typeByNameMap) { final int periodIndex = valueRef.indexOf('.'); if (periodIndex < 1 || periodIndex == (valueRef.length() - 1)) { handleError(node, "valueRef format not valid (enum-name.valid-value-name): " + valueRef); } final String valueRefType = valueRef.substring(0, periodIndex); final Type valueType = typeByNameMap.get(valueRefType); if (null == valueType) { handleError(node, "valueRef for enum name not found: " + valueRefType); } if (valueType instanceof EnumType) { final EnumType enumType = (EnumType)valueType; final String validValueName = valueRef.substring(periodIndex + 1); if (null == enumType.getValidValue(validValueName)) { handleError(node, "valueRef for validValue name not found: " + validValueName); } } else { handleError(node, "valueRef for is not of type enum: " + valueRefType); } }
private void add(final EnumType type, final int offset, final Field field) final PrimitiveType encodingType = type.encodingType(); final Encoding.Builder encodingBuilder = new Encoding.Builder() .primitiveType(encodingType) .byteOrder(schema.byteOrder()); if (type.presence() == Presence.OPTIONAL) .name(type.name()) .referencedName(type.referencedName()) .size(encodingType.size()) .offset(offset) .version(type.sinceVersion()) .deprecated(type.deprecated()) .description(type.description()) .encoding(encodingBuilder.build()); builder.version(Math.max(field.sinceVersion(), type.sinceVersion())); for (final EnumType.ValidValue validValue : type.validValues())
@Test public void shouldHandleOptionalBooleanEnumType() throws Exception { final String nullValueStr = "255"; final String testXmlString = "<types>" + "<enum name=\"optionalBoolean\" encodingType=\"uint8\" presence=\"optional\"" + " nullValue=\"" + nullValueStr + "\" semanticType=\"Boolean\">" + " <validValue name=\"False\">0</validValue>" + " <validValue name=\"True\">1</validValue>" + "</enum>" + "</types>"; final Map<String, Type> map = parseTestXmlWithMap("/types/enum", testXmlString); final EnumType e = (EnumType)map.get("optionalBoolean"); assertThat(e.name(), is("optionalBoolean")); assertThat(e.encodingType(), is(PrimitiveType.UINT8)); assertThat(e.validValues().size(), is(2)); assertThat(e.getValidValue("True").primitiveValue(), is(PrimitiveValue.parse("1", PrimitiveType.UINT8))); assertThat(e.getValidValue("False").primitiveValue(), is(PrimitiveValue.parse("0", PrimitiveType.UINT8))); assertThat(e.nullValue(), is(PrimitiveValue.parse(nullValueStr, PrimitiveType.UINT8))); }
final EnumType enumType = new EnumType(valueRefNode); if (enumType.encodingType() != primitiveType) final EnumType.ValidValue validValue = enumType.getValidValue(validValueName);
type = addType(subTypeNode, subTypeName, new EnumType(subTypeNode, givenName, referencedName)); break;