Skip to content

Commit

Permalink
[Go] Refactor whitespace in GolangGenerator (real-logic#844)
Browse files Browse the repository at this point in the history
Adds function `generateWhitespace` which uses the same String.format
technique, but validates the input.  It also always generates at least
one space.

This change should only affect the whitespace of the output.  It does
not attempt to change the logic that might be the underlying cause of real-logic#844.
  • Loading branch information
neomantra committed May 11, 2021
1 parent 8f001ba commit fd19b3a
Showing 1 changed file with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ private void generateEnumDecls(
sb.append(String.format(
"\t%1$s%2$s%3$sEnum\n",
token.name(),
String.format(String.format("%%%ds", longest - token.name().length() + 1), " "),
generateWhitespace(longest - token.name().length() + 1),
enumName));
}

Expand All @@ -1568,7 +1568,7 @@ private void generateEnumDecls(
"\t%1$s%2$s%3$sEnum\n" +
"}\n",
nullValue,
String.format(String.format("%%%ds", longest - nullValue.length() + 1), " "),
generateWhitespace(longest - nullValue.length() + 1),
enumName));

// And now the Enum Values expressed as a variable
Expand Down Expand Up @@ -1616,7 +1616,7 @@ private void generateChoiceDecls(
sb.append(String.format(
"\t%1$s%2$s%3$sChoiceValue\n",
toUpperFirstChar(token.name()),
String.format(String.format("%%%ds", longest - token.name().length() + 1), " "),
generateWhitespace(longest - token.name().length() + 1),
toUpperFirstChar(encodingToken.applicableTypeName())));
}

Expand Down Expand Up @@ -1747,15 +1747,15 @@ private void generateTypeBody(
{
case BEGIN_ENUM:
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", length), " "))
.append(generateWhitespace(length))
.append(arrayspec)
.append(encodingToken.applicableTypeName())
.append("Enum\n");
break;

case BEGIN_SET:
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", length), " "))
.append(generateWhitespace(length))
.append(arrayspec)
.append(encodingToken.applicableTypeName())
.append("\n");
Expand All @@ -1778,7 +1778,7 @@ private void generateTypeBody(
arrayspec = "[" + encodingToken.encoding().constValue().size() + "]";
}
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", length), " "))
.append(generateWhitespace(length))
.append(arrayspec).append(golangType).append("\n");
break;
}
Expand All @@ -1790,7 +1790,7 @@ private void generateTypeBody(
sb.append(String.format(
"\t%1$s%2$s[]%3$s%1$s\n",
toUpperFirstChar(signalToken.name()),
String.format(String.format("%%%ds", length), " "),
generateWhitespace(length),
typeName));
generateTypeDeclaration(
nested,
Expand All @@ -1812,7 +1812,7 @@ private void generateTypeBody(
sb.append(String.format(
"\t%1$s%2$s[]%3$s\n",
toUpperFirstChar(signalToken.name()),
String.format(String.format("%%%ds", length), " "),
generateWhitespace(length),
golangTypeName(tokens.get(i + 3).encoding().primitiveType())));
break;

Expand Down Expand Up @@ -2009,29 +2009,29 @@ private void generateTypeBodyComposite(
{
arrayLength = token.encoding().constValue().size(); // can be 1
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", longest - propertyName.length() + 1), " "))
.append(generateWhitespace(longest - propertyName.length() + 1))
.append("[").append(arrayLength).append("]")
.append(golangTypeName(token.encoding().primitiveType())).append("\n");
}
else
{
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", longest - propertyName.length() + 1), " "))
.append(generateWhitespace(longest - propertyName.length() + 1))
.append((arrayLength > 1) ? ("[" + arrayLength + "]") : "")
.append(golangTypeName(token.encoding().primitiveType())).append("\n");
}
break;

case BEGIN_ENUM:
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", longest - propertyName.length() + 1), " "))
.append(generateWhitespace(longest - propertyName.length() + 1))
.append((arrayLength > 1) ? ("[" + arrayLength + "]") : "")
.append(propertyType).append("Enum\n");
break;

case BEGIN_SET:
sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", longest - propertyName.length() + 1), " "))
.append(generateWhitespace(longest - propertyName.length() + 1))
.append((arrayLength > 1) ? ("[" + arrayLength + "]") : "").append(propertyType).append("\n");
break;

Expand All @@ -2041,7 +2041,7 @@ private void generateTypeBodyComposite(
i += token.componentTokenCount() - 2;

sb.append("\t").append(propertyName)
.append(String.format(String.format("%%%ds", longest - propertyName.length() + 1), " "))
.append(generateWhitespace(longest - propertyName.length() + 1))
.append((arrayLength > 1) ? ("[" + arrayLength + "]") : "")
.append(typeName).append(propertyName).append("\n");
break;
Expand Down Expand Up @@ -2347,5 +2347,12 @@ private CharSequence generateLiteral(final PrimitiveType type, final String valu

return literal;
}

// Always generates at least one space
private String generateWhitespace(final int spaces)
{
final int limitedSpaces = Math.max(1, spaces);
return String.format(String.format("%%%ds", limitedSpaces), " ");
}
}

0 comments on commit fd19b3a

Please sign in to comment.