diff --git a/src/Directory.Build.props b/src/Directory.Build.props index ace307cb..d7df55b9 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -18,12 +18,12 @@ - 7.5.1 - 7.5.1 + 7.6.0 + 7.6.0 $(Version) James Courtney FlatSharp is a fast, idiomatic implementation of the FlatBuffer binary format. - 2023 + 2024 https://github.com/jamescourtney/FlatSharp/ flatbuffers serialization flatbuffer flatsharp Release notes at https://github.com/jamescourtney/FlatSharp/releases diff --git a/src/FlatSharp.Compiler/CloneMethodsGenerator.cs b/src/FlatSharp.Compiler/CloneMethodsGenerator.cs index 56293579..4c75897c 100644 --- a/src/FlatSharp.Compiler/CloneMethodsGenerator.cs +++ b/src/FlatSharp.Compiler/CloneMethodsGenerator.cs @@ -59,7 +59,9 @@ public static string GenerateCloneMethodsForAssembly( 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) diff --git a/src/FlatSharp.Compiler/CompilerOptions.cs b/src/FlatSharp.Compiler/CompilerOptions.cs index d57e277b..6deb08c5 100644 --- a/src/FlatSharp.Compiler/CompilerOptions.cs +++ b/src/FlatSharp.Compiler/CompilerOptions.cs @@ -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; } diff --git a/src/FlatSharp.Compiler/FlatSharp.Compiler.targets b/src/FlatSharp.Compiler/FlatSharp.Compiler.targets index 3ea653fb..3e15aa29 100644 --- a/src/FlatSharp.Compiler/FlatSharp.Compiler.targets +++ b/src/FlatSharp.Compiler/FlatSharp.Compiler.targets @@ -165,6 +165,10 @@ $(CompilerCommand) --mutation-testing-mode + + $(CompilerCommand) --file-visibility + + diff --git a/src/FlatSharp.Compiler/SchemaModel/RootModel.cs b/src/FlatSharp.Compiler/SchemaModel/RootModel.cs index 4c45de7f..82c95823 100644 --- a/src/FlatSharp.Compiler/SchemaModel/RootModel.cs +++ b/src/FlatSharp.Compiler/SchemaModel/RootModel.cs @@ -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); diff --git a/src/FlatSharp/FlatBufferSerializerOptions.cs b/src/FlatSharp/FlatBufferSerializerOptions.cs index 397218fa..893e5e2d 100644 --- a/src/FlatSharp/FlatBufferSerializerOptions.cs +++ b/src/FlatSharp/FlatBufferSerializerOptions.cs @@ -87,4 +87,9 @@ public FlatBufferSerializerOptions( /// is not wholly sufficient to enable them. /// public bool EnableValueStructMemoryMarshalDeserialization { get; set; } = true; + + /// + /// Prefer 'file' visibility over 'internal'. + /// + public bool EnableFileVisibility { get; set; } } diff --git a/src/FlatSharp/Serialization/RoslynSerializerGenerator.cs b/src/FlatSharp/Serialization/RoslynSerializerGenerator.cs index f342480c..33c4fe7b 100644 --- a/src/FlatSharp/Serialization/RoslynSerializerGenerator.cs +++ b/src/FlatSharp/Serialization/RoslynSerializerGenerator.cs @@ -500,7 +500,7 @@ public int GetMaxSize({CSharpHelpers.GetGlobalCompilableTypeName(rootType)} root string code = $@" namespace {resolvedName.@namespace} {{ - internal class {resolvedName.name} : {nameof(IGeneratedSerializer)}<{rootType.GetGlobalCompilableTypeName()}> + {(this.options.EnableFileVisibility ? "file" : "internal")} class {resolvedName.name} : {nameof(IGeneratedSerializer)}<{rootType.GetGlobalCompilableTypeName()}> {{ // Method generated to help AOT compilers make good decisions about generics. [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] @@ -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)} }} diff --git a/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj b/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj index 5a7d98c7..9b352224 100644 --- a/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj +++ b/src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj @@ -14,6 +14,7 @@ False True latest + true