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 generic type transpilation #164

Merged
merged 5 commits into from
Feb 13, 2024

Conversation

backfromexile
Copy link
Contributor

@backfromexile backfromexile commented Feb 3, 2024

This PR adds functionality to the type transpiler to also transpile custom generic types correctly.
For example, the following C# class

[TranspilationSource]
public class Message<T> {
    public Guid Id { get; set; }
    public T Content { get; set; }
}

would be transpiled to this TypeScript type

export type Message<T> = {
    id: string;
    content: T;
}

This also works for nested generic types as long as the type definition is either marked as [TranspilationSource] or the type is known as one of the generic types with built-in support (e.g. certain collection and dictionary types, Nullable<T>).

Transpilation also works with concrete generic type arguments in inheritance chains.

I'd love to get feedback for this addition/change.

Copy link
Owner

@nenoNaninu nenoNaninu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Perhaps the use of ConstructUnboundGenericType() is due to the implementation within SourceTypeMapper.MapTo(), but I think ConstructedFrom is appropriate.

src/Tapper/RoslynExtensions.cs Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/CollectionTypeTypeMappers.tt Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/CollectionTypeTypeMappers.tt Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/DictionaryTypeMappers.tt Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/DictionaryTypeMappers.tt Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/NullableStructTypeMapper.cs Outdated Show resolved Hide resolved
src/Tapper/TypeMappers/NullableStructTypeMapper.cs Outdated Show resolved Hide resolved
@@ -14,8 +15,18 @@ public SourceTypeMapper(INamedTypeSymbol sourceTypes)

public string MapTo(ITypeSymbol typeSymbol, ITranspilationOptions options)
{
if (SymbolEqualityComparer.Default.Equals(typeSymbol, Assign))
if (SymbolEqualityComparer.Default.Equals(typeSymbol.GetUnboundedType(), Assign))
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if (SymbolEqualityComparer.Default.Equals(typeSymbol.GetUnboundedType(), Assign))
var symbol = (typeSymbol as INamedTypeSymbol)?.ConstructedFrom ?? typeSymbol;
if (SymbolEqualityComparer.Default.Equals(symbol, Assign))

Copy link
Contributor Author

@backfromexile backfromexile Feb 7, 2024

Choose a reason for hiding this comment

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

This won't work if we want to support custom type mappers (#165) for concrete generic types (e.g. a custom mapper/translator that works for Something<string> but not for Something<T>), so this will require a change for the implementation of #165.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is non-blocking for this PR though imo, just something we need to consider if we want to support custom type mappers in the future.

src/Tapper/TypeTranslators/DefaultMessageTypeTranslator.cs Outdated Show resolved Hide resolved
@backfromexile
Copy link
Contributor Author

Thanks for the PR! Perhaps the use of ConstructUnboundGenericType() is due to the implementation within SourceTypeMapper.MapTo(), but I think ConstructedFrom is appropriate.

I'll have a look, I personally found it easier to wrap my head around unbounded types than having generic and concrete generic types, but I'll change it if it's really just using ConstructedFrom instead.
I only have little experience with the compilation APIs so I appreciate your feedback!

Copy link
Owner

@nenoNaninu nenoNaninu left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in the review. LGTM!

@nenoNaninu nenoNaninu merged commit 082cc01 into nenoNaninu:main Feb 13, 2024
2 checks passed
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.

None yet

2 participants