Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-level namespace support in C++ generator #347

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Core/Generators/CPlusPlus/CPlusPlusGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,15 @@ public override ValueTask<string> Compile(BebopSchema schema, GeneratorConfig co
builder.AppendLine("#include \"bebop.hpp\"");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a test workflow implemented to verify C++ changes here. We already have some basic testing setup in the lab: https://github.com/betwixt-labs/bebop/tree/master/Laboratory/C%2B%2B

So if you can setup a new test-cpp.yml in the workflows folder (https://github.com/betwixt-labs/bebop/tree/master/.github/workflows) and validate these changes there, I'll be happy to merge.

builder.AppendLine("");

string[] namespaces = {};
if (!string.IsNullOrWhiteSpace(Config.Namespace))
{
builder.AppendLine($"namespace {Config.Namespace} {{");
namespaces = Config.Namespace.Split("::");
}

foreach (string ns in namespaces)
{
builder.AppendLine($"namespace {ns} {{");
builder.AppendLine("");
}

Expand Down Expand Up @@ -510,9 +516,9 @@ public override ValueTask<string> Compile(BebopSchema schema, GeneratorConfig co
}
}

if (!string.IsNullOrWhiteSpace(Config.Namespace))
foreach (string ns in namespaces)
{
builder.AppendLine($"}} // namespace {Config.Namespace}");
builder.AppendLine($"}} // namespace {ns}");
builder.AppendLine("");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally fine with this, but it would be better to handle your desired behavior behind an option, that way those who are targeting C++17 and above can get a single level namespace you can see an example of that here:

if (Version.TryParse(config.GetOptionRawValue("langVersion"), out var langVersion))

I'll leave it to you to determine what an appropriate option name would be. For a refresher options are passed either via the bebop.json config or on the command line inside the generator string. ( "cpp:./GeneratedTestCode/test.g.hpp,namespace=Bebop::Codegen".

You'll need to test it works through both these means.


Expand Down
2 changes: 1 addition & 1 deletion Core/Meta/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public static string EscapeString(this string? value)
[GeneratedRegex(@"^.*[\*\?\[\]].*(\.[a-zA-Z0-9]+)?$")]
private static partial Regex LegalFileGlobRegex();

[GeneratedRegex(@"^[a-zA-Z]+(\.[a-zA-Z]+)*$")]
[GeneratedRegex(@"^[a-zA-Z]+(\.[a-zA-Z]+|::[a-zA-Z]+)*$")]
andrewmd5 marked this conversation as resolved.
Show resolved Hide resolved
private static partial Regex NamespaceRegex();
}
}