Skip to content

Commit

Permalink
Add file visibility option (#435)
Browse files Browse the repository at this point in the history
* Add file visibility option

* Rev version
  • Loading branch information
jamescourtney committed Mar 14, 2024
1 parent 9808c65 commit 2d0766c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
</PropertyGroup>

<PropertyGroup>
<Version>7.5.1</Version>
<PackageVersion>7.5.1</PackageVersion>
<Version>7.6.0</Version>
<PackageVersion>7.6.0</PackageVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<Authors>James Courtney</Authors>
<Description>FlatSharp is a fast, idiomatic implementation of the FlatBuffer binary format.</Description>
<Copyright>2023</Copyright>
<Copyright>2024</Copyright>
<RepositoryUrl>https://github.com/jamescourtney/FlatSharp/</RepositoryUrl>
<PackageTags>flatbuffers serialization flatbuffer flatsharp</PackageTags>
<PackageReleaseNotes>Release notes at https://github.com/jamescourtney/FlatSharp/releases</PackageReleaseNotes>
Expand Down
4 changes: 3 additions & 1 deletion src/FlatSharp.Compiler/CloneMethodsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ internal static class CloneMethodsGenerator
writer.AppendLine($"namespace {@namespace}");
using (writer.WithBlock())
{
writer.AppendLine($"internal static class {className}");
string visibility = options.FileVisibility ? "file" : "internal";

writer.AppendLine($"{visibility} static class {className}");
using (writer.WithBlock())
{
foreach (var seenType in seenTypes)
Expand Down
3 changes: 3 additions & 0 deletions src/FlatSharp.Compiler/CompilerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public record CompilerOptions
[Option("nullable-warnings", Default = false, HelpText = "Emit full nullable annotations and enable warnings.")]
public bool? NullableWarnings { get; set; }

[Option("file-visibility", Default = false, HelpText = "Use file visibility for FlatSharp-generated types. Requires C# 11 or later.")]
public bool FileVisibility { get; set; }

[Option("gen-poolable", Hidden = false, Default = false, HelpText = "EXPERIMENTAL: Generate extra code to enable object pooling for allocation reductions.")]
public bool GeneratePoolableObjects { get; set; }

Expand Down
4 changes: 4 additions & 0 deletions src/FlatSharp.Compiler/FlatSharp.Compiler.targets
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
<CompilerCommand>$(CompilerCommand) --mutation-testing-mode</CompilerCommand>
</PropertyGroup>

<PropertyGroup Condition=" '$(FlatSharpFileVisibility)' == 'true' ">
<CompilerCommand>$(CompilerCommand) --file-visibility</CompilerCommand>
</PropertyGroup>

<Message Text="$(CompilerCommand)" Importance="high" />
<Exec Command="$(CompilerCommand)" CustomErrorRegularExpression=".*" />

Expand Down
2 changes: 1 addition & 1 deletion src/FlatSharp.Compiler/SchemaModel/RootModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal void WriteCode(CodeWriter writer, CompileContext context)

private static void WriteHelperClass(Type type, CompileContext context, CodeWriter writer)
{
var options = new FlatBufferSerializerOptions();
var options = new FlatBufferSerializerOptions { EnableFileVisibility = context.Options.FileVisibility };
var generator = new RoslynSerializerGenerator(options, context.TypeModelContainer);
string helper = generator.ImplementHelperClass(context.TypeModelContainer.CreateTypeModel(type), context.Options.Deserializers);

Expand Down
5 changes: 5 additions & 0 deletions src/FlatSharp/FlatBufferSerializerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ public record class FlatBufferSerializerOptions
/// is not wholly sufficient to enable them.
/// </summary>
public bool EnableValueStructMemoryMarshalDeserialization { get; set; } = true;

/// <summary>
/// Prefer 'file' visibility over 'internal'.
/// </summary>
public bool EnableFileVisibility { get; set; }
}
4 changes: 2 additions & 2 deletions src/FlatSharp/Serialization/RoslynSerializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public int GetMaxSize({CSharpHelpers.GetGlobalCompilableTypeName(rootType)} root
string code = $@"
namespace {resolvedName.@namespace}
{{
internal class {resolvedName.name} : {nameof(IGeneratedSerializer<byte>)}<{rootType.GetGlobalCompilableTypeName()}>
{(this.options.EnableFileVisibility ? "file" : "internal")} class {resolvedName.name} : {nameof(IGeneratedSerializer<byte>)}<{rootType.GetGlobalCompilableTypeName()}>
{{
// Method generated to help AOT compilers make good decisions about generics.
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -650,7 +650,7 @@ namespace {ns}
// Ensures that extension methods, etc are available.
using {typeModel.ClrType.Namespace};
internal static class {name}
{(this.options.EnableFileVisibility ? "file" : "internal")} static class {name}
{{
{string.Join("\r\n", methods)}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<RunAnalyzersDuringLiveAnalysis>False</RunAnalyzersDuringLiveAnalysis>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<FlatSharpFileVisibility>true</FlatSharpFileVisibility>
</PropertyGroup>

<PropertyGroup Condition=" '$(BuildAot)' == 'true' ">
Expand Down

0 comments on commit 2d0766c

Please sign in to comment.