Skip to content

Add union type support - #123

Merged
prozolic merged 4 commits into
mainfrom
pr/dotnet-union-type-support-f827fd
Oct 2, 2026
Merged

prozolic merged 4 commits into
mainfrom
pr/dotnet-union-type-support-f827fd

Conversation

@prozolic

@prozolic prozolic commented Sep 28, 2026 •

Copy link
Copy Markdown
Owner

This PR adds union type support.

Support:

  • union declarations
  • Custom union types marked with [System.Runtime.CompilerServices.Union]
  • Custom unions with typed storage (TryGetValue(out T) and optional HasValue)
  • Union member provider types(IUnionMembers, IUnionMembers.Create(value))
  • Class-based unions ([Union])

usage

[TomlUnion<long>]
public union IntOrText(long, string);

[TomlSerializedObject]
public partial class Setting
{
    [TomlValueOnSerialized]
    public IntOrText Num { get; set; }
}

var setting = new Setting() { Num = 123L };
using var bytes = CsTomlSerializer.Serialize(setting);

Related

#121

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Critical generated-code failures and additional union validation, accessibility, and nullable-handling issues remain unresolved.

Review effort: Lite
Findings: 3 High severity · 1 Medium severity

Open (4)
What changed in this PR

Adds source-generated serialization support for custom and C# union types.

Changes:

  • Adds union analysis, metadata, diagnostics, formatter generation, and registration.
  • Adds custom, generic, class-based, typed-storage, and .NET 11 union tests.
  • Updates NativeAOT samples, project targets, and CI for .NET 11.
File Summary
tests/​CsToml.Tests/​CsToml.Tests.csproj Adds .NET 11 test targeting.
tests/​CsToml.Generator.Tests/​Generator/​UnionTest.cs Tests custom union behavior.
tests/​CsToml.Generator.Tests/​Generator/​Net11/​UnionNet11Test.cs Tests .NET 11 union syntax.
tests/​CsToml.Generator.Tests/​Declarations/​UnionDeclarations.cs Defines custom union fixtures.
tests/​CsToml.Generator.Tests/​Declarations/​UnionAttributePolyfill.cs Provides union attribute support for older targets.
tests/​CsToml.Generator.Tests/​Declarations/​Net11/​UnionNet11Declarations.cs Defines .NET 11 union fixtures.
tests/​CsToml.Generator.Tests/​CsToml.Generator.Tests.csproj Adds .NET 11 test targeting.
tests/​CsToml.Extensions.Configuration.Tests/​CsToml.Extensions.Configuration.Tests.csproj Adds .NET 11 test targeting.
src/​CsToml.Generator/​UnionSymbolAnalyzer.cs Detects unions and providers; provider validation and formatter-name collision handling require changes.
src/​CsToml.Generator/​UnionMetaFactory.cs Builds union formatter metadata; nested unpinned cases and containing generic parameters require changes.
src/​CsToml.Generator/​UnionMeta.cs Defines union metadata models.
src/​CsToml.Generator/​TypeMeta.cs Integrates unions into object metadata; invalid pins can produce a duplicate diagnostic.
src/​CsToml.Generator/​TomlValueFormatterResolverEmitter.cs Emits union formatter registrations.
src/​CsToml.Generator/​TomlSerializationKind.cs Adds union serialization classification.
src/​CsToml.Generator/​SymbolUtility.cs Resolves union pins and reachable types.
src/​CsToml.Generator/​Generator.cs Emits union formatters and registration; class-union formatter typing and standalone accessibility require changes.
src/​CsToml.Generator/​FormatterTypeMetaData.cs Classifies union serialization types; nullable unions can lose pinned-case serialization style.
src/​CsToml.Generator/​DiagnosticDescriptors.cs Adds union diagnostics.
sandbox/​ConsoleNativeAOT/​Union.cs Adds a NativeAOT union sample.
sandbox/​ConsoleNativeAOT/​Program.cs Exercises union serialization.
sandbox/​ConsoleNativeAOT/​ConsoleNativeAOT.csproj Retargets the sample to .NET 11.
.github/​workflows/​build.yml Installs the .NET 11 SDK in CI.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/CsToml.Generator/Generator.cs
Comment thread src/CsToml.Generator/UnionMetaFactory.cs Outdated
Comment thread src/CsToml.Generator/UnionSymbolAnalyzer.cs
Comment thread src/CsToml.Generator/UnionSymbolAnalyzer.cs
@prozolic
prozolic marked this pull request as ready for review October 2, 2026 14:57
@prozolic
prozolic merged commit a7ac724 into main Oct 2, 2026
1 check passed
@prozolic
prozolic deleted the pr/dotnet-union-type-support-f827fd branch October 2, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants