Skip to content

Comments

Add PolyType support for efficient serialization integration#1

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-6f261f1c-67b8-49c6-a33b-c3e4980c812b
Draft

Add PolyType support for efficient serialization integration#1
Copilot wants to merge 5 commits intomainfrom
copilot/fix-6f261f1c-67b8-49c6-a33b-c3e4980c812b

Conversation

Copy link

Copilot AI commented Aug 27, 2025

This PR implements support for PolyType integration as requested in issue SteveDunn#834. The implementation conditionally emits additional code when PolyType.TypeShapeAttribute is available in the compilation, enabling seamless integration between Vogen value objects and PolyType-based serialization libraries like Nerdbank.MessagePack.

Problem Statement

PolyType is a high-performance type modeling library that uses source generation to expose type graphs at runtime. However, PolyType's source generator cannot see code emitted by Vogen's source generator, creating a compatibility issue. This forces users to either:

  • Use separate assemblies for data models and serialization (suboptimal)
  • Accept inefficient serialized schemas with unnecessary wrapper layers

Solution

When PolyType.TypeShapeAttribute is detected in the compilation, Vogen now automatically generates:

  1. TypeShape attribute on the partial value object declaration
  2. PolyTypeMarshaler nested class that implements IMarshaler<T, TUnderlying>

This enables PolyType to efficiently serialize/deserialize the underlying primitive value directly, bypassing the wrapper type and producing optimal schemas.

Example

For a value object like:

[ValueObject<int>]
public partial struct CustomerId { }

When PolyType is available, Vogen now generates:

[global::PolyType.TypeShapeAttribute(Marshaler = typeof(PolyTypeMarshaler), Kind = global::PolyType.TypeShapeKind.None)]
public partial struct CustomerId
{
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    public class PolyTypeMarshaler : global::PolyType.IMarshaler<CustomerId, int>
    {
        int global::PolyType.IMarshaler<CustomerId, int>.Marshal(CustomerId value) => value.Value;
        CustomerId global::PolyType.IMarshaler<CustomerId, int>.Unmarshal(int value) => From(value);
    }
}

Implementation Details

  • Conditional Generation: Code only appears when PolyType.TypeShapeAttribute is found in the compilation
  • Zero Configuration: No user flags or attributes required - automatically detects PolyType availability
  • Universal Support: Works with all Vogen type declarations (class, struct, record class, record struct)
  • Type Safety: Uses proper global:: prefixes and follows existing Vogen patterns
  • Format Agnostic: Works with any PolyType-based serialization library, not just MessagePack

Benefits

  • Enables high-performance serialization with PolyType without requiring separate assemblies
  • Produces optimal serialized schemas by marshaling the underlying primitive directly
  • Supports all current and future PolyType use cases
  • Maintains full backward compatibility - no breaking changes

This resolves the core incompatibility between PolyType and Vogen source generators, enabling developers to use both libraries together seamlessly for efficient, type-safe serialization.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 27, 2025 16:05
- Add TypeShapeAttribute detection to VogenKnownSymbols
- Add utility methods for generating PolyType attribute and marshaler
- Integrate PolyType generation into all four generators (Class, Struct, RecordClass, RecordStruct)
- PolyType code only generated when PolyType.TypeShapeAttribute is available in compilation

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
- Add snapshot tests for PolyType functionality
- Create validation script demonstrating expected behavior
- Tests verify conditional generation based on PolyType availability

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Copilot AI changed the title [WIP] Author a pull request that resolves @SteveDunn/Vogen/issues/834 As described in the issue, only emit the additional code in the source generator for compilations that include PolyType.TypeShapeAttribute as a metadata type in the compilation. Add PolyType support for efficient serialization integration Aug 27, 2025
Copilot AI requested a review from AArnott August 27, 2025 16:10
Removed unnecessary whitespace and comments in PolyTypeTests.cs
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