Skip to content

Commit

Permalink
Merge pull request #1024 from nbradac/cpp-and-csharp-package-overrides
Browse files Browse the repository at this point in the history
[C++,C#] respect the package override option for codecs and DTOs
  • Loading branch information
nbradac authored Nov 15, 2024
2 parents d38b4bf + ea52edd commit 0caf77e
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ csharp/sbe-generated/since-deprecated
csharp/sbe-generated/order_check
csharp/sbe-generated/mktdata/*.cs
csharp/sbe-generated/uk_co_real_logic_sbe_benchmarks_fix
csharp/sbe-generated/test_message_schema
csharp/sbe-tests/*.sbe
csharp/nuget/
csharp/csharp.sln.DotSettings.user
Expand Down
33 changes: 30 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,8 @@ tasks.register('generateCSharpTestCodecs', JavaExec) {
'sbe.target.language': 'uk.co.real_logic.sbe.generation.csharp.CSharp',
'sbe.xinclude.aware': 'true',
'sbe.validation.xsd': validationXsdPath,
'sbe.generate.precedence.checks': 'true')
'sbe.generate.precedence.checks': 'true',
'sbe.types.package.override': 'true')
args = ['sbe-tool/src/test/resources/FixBinary.xml',
'sbe-tool/src/test/resources/issue435.xml',
'sbe-tool/src/test/resources/issue483.xml',
Expand All @@ -912,15 +913,41 @@ tasks.register('generateCSharpTestDtos', JavaExec) {
'sbe.output.dir': 'csharp/sbe-generated',
'sbe.target.language': 'uk.co.real_logic.sbe.generation.csharp.CSharpDtos',
'sbe.xinclude.aware': 'true',
'sbe.validation.xsd': validationXsdPath)
'sbe.validation.xsd': validationXsdPath,
'sbe.types.package.override': 'true')
args = ['sbe-samples/src/main/resources/example-extension-schema.xml']
}

tasks.register('generateCSharpExplicitPackageOverrideCodecs', JavaExec) {
mainClass.set('uk.co.real_logic.sbe.SbeTool')
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
systemProperties(
'sbe.output.dir': 'csharp/sbe-generated',
'sbe.target.language': 'uk.co.real_logic.sbe.generation.csharp.CSharp',
'sbe.xinclude.aware': 'true',
'sbe.generate.precedence.checks': 'true',
'sbe.types.package.override': 'true')
args = ['sbe-tool/src/test/resources/explicit-package-test-schema.xml']
}

tasks.register('generateCSharpExplicitPackageOverrideDtos', JavaExec) {
mainClass.set('uk.co.real_logic.sbe.SbeTool')
classpath = project(':sbe-tool').sourceSets.main.runtimeClasspath
systemProperties(
'sbe.output.dir': 'csharp/sbe-generated',
'sbe.target.language': 'uk.co.real_logic.sbe.generation.csharp.CSharpDtos',
'sbe.xinclude.aware': 'true',
'sbe.types.package.override': 'true')
args = ['sbe-tool/src/test/resources/explicit-package-test-schema.xml']
}

tasks.register('generateCSharpCodecs') {
description = 'Generate csharp codecs'
dependsOn 'generateCSharpTestCodecs',
'generateCSharpTestDtos',
'generateCSharpCodecsWithXIncludes'
'generateCSharpCodecsWithXIncludes',
'generateCSharpExplicitPackageOverrideCodecs',
'generateCSharpExplicitPackageOverrideDtos'
}

tasks.register('generateJavaIrCodecs', JavaExec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ public CodeGenerator newInstance(final Ir ir, final String outputDir)
final NamespaceOutputManager outputManager = new NamespaceOutputManager(
outputDir, ir.applicableNamespace());
final boolean decodeUnknownEnumValues = Boolean.getBoolean(DECODE_UNKNOWN_ENUM_VALUES);
final boolean shouldSupportTypesPackageNames = Boolean.getBoolean(TYPES_PACKAGE_OVERRIDE);

final CodeGenerator codecGenerator = new CppGenerator(ir, decodeUnknownEnumValues, precedenceChecks(),
final CodeGenerator codecGenerator = new CppGenerator(
ir,
decodeUnknownEnumValues,
precedenceChecks(),
shouldSupportTypesPackageNames,
outputManager);

if (Boolean.getBoolean(CPP_GENERATE_DTOS))
{
final CodeGenerator dtoGenerator = new CppDtoGenerator(ir, outputManager);
final CodeGenerator dtoGenerator = new CppDtoGenerator(
ir,
shouldSupportTypesPackageNames,
outputManager);
return () ->
{
codecGenerator.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,65 @@ public class CppDtoGenerator implements CodeGenerator

private final Ir ir;
private final OutputManager outputManager;
private final boolean shouldSupportTypesPackageNames;
private final Map<String, String> namespaceByType = new HashMap<>();

/**
* Create a new C++ DTO {@link CodeGenerator}.
*
* @param ir for the messages and types.
* @param outputManager for generating the DTOs to.
* @param ir for the messages and types.
* @param shouldSupportTypesPackageNames generator support for types in their own package.
* @param outputManager for generating the DTOs to.
*/
public CppDtoGenerator(final Ir ir, final OutputManager outputManager)
public CppDtoGenerator(
final Ir ir,
final boolean shouldSupportTypesPackageNames,
final OutputManager outputManager)
{
Verify.notNull(ir, "ir");
Verify.notNull(outputManager, "outputManager");

this.ir = ir;
this.shouldSupportTypesPackageNames = shouldSupportTypesPackageNames;
this.outputManager = outputManager;
}

private String[] fetchTypesPackageName(final Token token, final Ir ir)
{
if (!shouldSupportTypesPackageNames)
{
return ir.namespaces();
}

if (token.packageName() != null)
{
return Ir.getNamespaces(token.packageName());
}

return ir.namespaces();
}

/**
* {@inheritDoc}
*/
public void generate() throws IOException
{
namespaceByType.clear();

if (shouldSupportTypesPackageNames)
{
for (final List<Token> tokens : ir.types())
{
final Token token = tokens.get(0);
final String packageName = token.packageName();

if (packageName != null)
{
namespaceByType.put(token.applicableTypeName(), packageName);
}
}
}

generateDtosForTypes();

for (final List<Token> tokens : ir.messages())
Expand Down Expand Up @@ -116,13 +154,14 @@ public void generate() throws IOException
final Set<String> referencedTypes = generateTypesToIncludes(beginTypeTokensInSchema);
referencedTypes.add(codecClassName);

final String[] namespaces = fetchTypesPackageName(msgToken, ir);
out.append(generateDtoFileHeader(
ir.namespaces(),
namespaces,
className,
referencedTypes));
out.append(generateDocumentation(BASE_INDENT, msgToken));
classBuilder.appendTo(out);
out.append(CppUtil.closingBraces(ir.namespaces().length));
out.append(CppUtil.closingBraces(namespaces.length));
out.append("#endif\n");
}
}
Expand Down Expand Up @@ -1732,7 +1771,8 @@ private void generateDtosForTypes() throws IOException

private void generateComposite(final List<Token> tokens) throws IOException
{
final String name = tokens.get(0).applicableTypeName();
final Token token = tokens.get(0);
final String name = token.applicableTypeName();
final String className = formatDtoClassName(name);
final String codecClassName = formatClassName(name);

Expand All @@ -1741,8 +1781,9 @@ private void generateComposite(final List<Token> tokens) throws IOException
final List<Token> compositeTokens = tokens.subList(1, tokens.size() - 1);
final Set<String> referencedTypes = generateTypesToIncludes(compositeTokens);
referencedTypes.add(codecClassName);
out.append(generateDtoFileHeader(ir.namespaces(), className, referencedTypes));
out.append(generateDocumentation(BASE_INDENT, tokens.get(0)));
final String[] namespaces = fetchTypesPackageName(token, ir);
out.append(generateDtoFileHeader(namespaces, className, referencedTypes));
out.append(generateDocumentation(BASE_INDENT, token));

final ClassBuilder classBuilder = new ClassBuilder(className, BASE_INDENT);

Expand All @@ -1754,14 +1795,15 @@ private void generateComposite(final List<Token> tokens) throws IOException
codecClassName + "::sbeSchemaVersion()", BASE_INDENT + INDENT);

classBuilder.appendTo(out);
out.append(CppUtil.closingBraces(ir.namespaces().length));
out.append(CppUtil.closingBraces(namespaces.length));
out.append("#endif\n");
}
}

private void generateChoiceSet(final List<Token> tokens) throws IOException
{
final String name = tokens.get(0).applicableTypeName();
final Token token = tokens.get(0);
final String name = token.applicableTypeName();
final String className = formatDtoClassName(name);
final String codecClassName = formatClassName(name);

Expand All @@ -1770,7 +1812,8 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
final List<Token> setTokens = tokens.subList(1, tokens.size() - 1);
final Set<String> referencedTypes = generateTypesToIncludes(setTokens);
referencedTypes.add(codecClassName);
out.append(generateDtoFileHeader(ir.namespaces(), className, referencedTypes));
final String[] namespaces = fetchTypesPackageName(token, ir);
out.append(generateDtoFileHeader(namespaces, className, referencedTypes));
out.append(generateDocumentation(BASE_INDENT, tokens.get(0)));

final ClassBuilder classBuilder = new ClassBuilder(className, BASE_INDENT);
Expand All @@ -1780,7 +1823,7 @@ private void generateChoiceSet(final List<Token> tokens) throws IOException
generateChoiceSetEncodeWith(classBuilder, className, codecClassName, setTokens, BASE_INDENT + INDENT);

classBuilder.appendTo(out);
out.append(CppUtil.closingBraces(ir.namespaces().length));
out.append(CppUtil.closingBraces(namespaces.length));
out.append("#endif\n");
}
}
Expand Down Expand Up @@ -1965,7 +2008,7 @@ private static CharSequence typeWithFieldOptionality(
}
}

private static CharSequence generateDtoFileHeader(
private CharSequence generateDtoFileHeader(
final CharSequence[] namespaces,
final String className,
final Collection<String> typesToInclude)
Expand Down Expand Up @@ -2010,6 +2053,32 @@ private static CharSequence generateDtoFileHeader(
sb.append(String.join(" {\nnamespace ", namespaces));
sb.append(" {\n\n");

if (shouldSupportTypesPackageNames && typesToInclude != null && !typesToInclude.isEmpty())
{
final Set<String> namespacesToUse = namespaceByType
.entrySet()
.stream()
.filter(e -> typesToInclude.contains(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toSet());

// remove the current namespace
namespacesToUse.remove(String.join(".", namespaces));

for (final String namespace : namespacesToUse)
{
sb
.append("using namespace ")
.append(namespace.replaceAll("\\.", "::"))
.append(";\n");
}

if (!namespacesToUse.isEmpty())
{
sb.append("\n");
}
}

return sb;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public class CppDtos implements TargetCodeGenerator
*/
public CodeGenerator newInstance(final Ir ir, final String outputDir)
{
return new CppDtoGenerator(ir, new NamespaceOutputManager(outputDir, ir.applicableNamespace()));
return new CppDtoGenerator(ir, false, new NamespaceOutputManager(outputDir, ir.applicableNamespace()));
}
}
Loading

0 comments on commit 0caf77e

Please sign in to comment.