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

prettier way for naming and code suggestions #8

Open
5andr0 opened this issue Sep 11, 2024 · 0 comments · May be fixed by #11
Open

prettier way for naming and code suggestions #8

5andr0 opened this issue Sep 11, 2024 · 0 comments · May be fixed by #11

Comments

@5andr0
Copy link

5andr0 commented Sep 11, 2024

You asked for a prettier way to name your getters. You can actually use generics here with generic type constraints. You just have to put the methods in different extension classes.

public static class {typeNode.Identifier}DataReaderExtensions {
	public static List<{{typeNodeSymbol.FullName()}}> To<T>(this IDataReader dr) where T : {{typeNodeSymbol.FullName()}}
}

Also instead of populating a full List synchronously, you may want to return IAsyncEnumerable<T> and populate it with yield return and use async functions to read from reader (but you will need IAsyncEnumerable<{{typeNodeSymbol.FullName()}}> ToAsync<T>(this DbDataReader dr) instead of IDataReader to access async functions like ReadAsync and GetFieldValueAsync)

For compilers sanity, please use null checks:

if (context.SyntaxContextReceiver is not TargetTypeTracker targetTypeTracker)
{
	return;
}

	foreach (var typeNode in targetTypeTracker.TypesNeedingGening)
	{
		if (context.Compilation
			.GetSemanticModel(typeNode.SyntaxTree)
			.GetDeclaredSymbol(typeNode) is INamedTypeSymbol typeNodeSymbol)
...

If you can't use the new is c# feature, just change the <LangVersion>preview</LangVersion> in your generators csproj. You can use a higher c# version even with <TargetFramework>netstandard2.0</TargetFramework>

When you use c# 11+ it's better to use raw string literals for code to avoid the indents.
With $$""" you can use single brackets { } to appear in text and double brackets for variables {{typeNodeSymbol.FullName()}}

If you compile your nuget with github actions it will generate the code with \n newlines mixed with your hardcoded \r\n. It would be better to use something like

static readonly string nl = @"
"; 

and replace your \r\n with {{nl}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant