88
99namespace StructId ;
1010
11- public enum TypeCheck
11+ public enum ReferenceCheck
1212{
1313 /// <summary>
1414 /// The check involves ensuring the type exists in the compilation.
@@ -20,18 +20,18 @@ public enum TypeCheck
2020 ValueIsType ,
2121}
2222
23- public abstract class TemplateGenerator ( string valueType , string stringTemplate , string typeTemplate , TypeCheck interfaceCheck = TypeCheck . ValueIsType ) : IIncrementalGenerator
23+ public abstract class TemplateGenerator ( string referenceType , string stringTemplate , string typeTemplate , ReferenceCheck referenceCheck = ReferenceCheck . ValueIsType ) : IIncrementalGenerator
2424{
25- record struct TemplateArgs ( string TargetNamespace , INamedTypeSymbol StructId , INamedTypeSymbol ValueType , INamedTypeSymbol InterfaceType , INamedTypeSymbol StringType ) ;
25+ record struct TemplateArgs ( string TargetNamespace , INamedTypeSymbol StructId , INamedTypeSymbol ValueType , INamedTypeSymbol ReferenceType , INamedTypeSymbol StringType ) ;
2626
27- public void Initialize ( IncrementalGeneratorInitializationContext context )
27+ public virtual void Initialize ( IncrementalGeneratorInitializationContext context )
2828 {
2929 var targetNamespace = context . AnalyzerConfigOptionsProvider
3030 . Select ( ( x , _ ) => x . GlobalOptions . TryGetValue ( "build_property.StructIdNamespace" , out var ns ) ? ns : "StructId" ) ;
3131
3232 // Locate the required types
3333 var types = context . CompilationProvider
34- . Select ( ( x , _ ) => ( InterfaceType : x . GetTypeByMetadataName ( valueType ) , StringType : x . GetTypeByMetadataName ( "System.String" ) ) ) ;
34+ . Select ( ( x , _ ) => ( ReferenceType : x . GetTypeByMetadataName ( referenceType ) , StringType : x . GetTypeByMetadataName ( "System.String" ) ) ) ;
3535
3636 var ids = context . CompilationProvider
3737 . SelectMany ( ( x , _ ) => x . Assembly . GetAllTypes ( ) . OfType < INamedTypeSymbol > ( ) )
@@ -40,11 +40,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4040
4141 var combined = ids . Combine ( types )
4242 // NOTE: we never generate for compilations that don't have the specified value interface type
43- . Where ( x => x . Right . InterfaceType != null || x . Right . StringType == null )
43+ . Where ( x => x . Right . ReferenceType != null || x . Right . StringType == null )
4444 . Combine ( targetNamespace )
4545 . Select ( ( x , _ ) =>
4646 {
47- var ( ( structId , ( interfaceType , stringType ) ) , targetNamespace ) = x ;
47+ var ( ( structId , ( referenceType , stringType ) ) , targetNamespace ) = x ;
4848
4949 // The value type is either a generic type argument for IStructId<T>, or the string type
5050 // for the non-generic IStructId
@@ -53,15 +53,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5353 . TypeArguments . OfType < INamedTypeSymbol > ( ) . FirstOrDefault ( ) ??
5454 stringType ! ;
5555
56- return new TemplateArgs ( targetNamespace , structId , valueType , interfaceType ! , stringType ! ) ;
56+ return new TemplateArgs ( targetNamespace , structId , valueType , referenceType ! , stringType ! ) ;
5757 } ) ;
5858
59- if ( interfaceCheck == TypeCheck . ValueIsType )
60- combined = combined . Where ( x => x . ValueType . Is ( x . InterfaceType ) ) ;
59+ if ( referenceCheck == ReferenceCheck . ValueIsType )
60+ combined = combined . Where ( x => x . ValueType . Is ( x . ReferenceType ) ) ;
6161
6262 context . RegisterImplementationSourceOutput ( combined , GenerateCode ) ;
6363 }
64-
6564 void GenerateCode ( SourceProductionContext context , TemplateArgs args )
6665 {
6766 var ns = args . StructId . ContainingNamespace . Equals ( args . StructId . ContainingModule . GlobalNamespace , SymbolEqualityComparer . Default )
0 commit comments