Skip to content

Commit

Permalink
[Java] Validate the type is correct when using a valueRef for a enum …
Browse files Browse the repository at this point in the history
…constant. Issue #528.
  • Loading branch information
mjpt777 committed Nov 10, 2017
1 parent fcf27d7 commit e4641f1
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,34 @@ public void validate(final Node node)

checkForValidName(node, name);

if (presence == Presence.CONSTANT && null != valueRef)
{
final String typeName = type == null ? null : type.name();

if (!(type instanceof EnumType))
{
handleError(node, "valueRef only valid for enum constants, type is " + typeName);
}

final int periodIndex = valueRef.indexOf('.');
if (periodIndex < 1 || periodIndex == (valueRef.length() - 1))
{
handleError(
node, "valueRef format not valid for constant (enum-name.valid-value-name): " + valueRef);
}

final String valueRefType = valueRef.substring(0, periodIndex);
if (!valueRefType.equals(typeName))
{
handleError(node, "valueRef type " + valueRefType + " does not match " + typeName);
}
}

if (type instanceof EnumType && presence == Presence.CONSTANT)
{
if (null == valueRef)
{
handleError(node, "valueRef not set for constant Enum");
handleError(node, "valueRef not set for constant enum");
}
else
{
Expand All @@ -114,7 +137,7 @@ public void validate(final Node node)
final String valueRefType = valueRef.substring(0, periodIndex);
if (!valueRefType.equals(type.name()))
{
handleError(node, "valueRef for Enum name not found: " + valueRefType);
handleError(node, "valueRef for enum name not found: " + valueRefType);
}

final String validValueName = valueRef.substring(periodIndex + 1);
Expand Down

0 comments on commit e4641f1

Please sign in to comment.