File tree Expand file tree Collapse file tree
samples/generators/SourceKit.Generators.Grpc.Samples/protos
src/generators/SourceKit.Generators.Grpc Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ message ProtoModel {
2121 map <int32 , string > map_value = 10 ;
2222
2323 InnerMessage m = 11 ;
24-
24+
2525 message InnerMessage {
2626 InnerEnum enum = 1 ;
2727 }
@@ -33,4 +33,8 @@ message ProtoModel {
3333}
3434
3535message EmptyMessage {
36+ }
37+
38+ enum OuterEnum {
39+ UNSPECIFIED = 0 ;
3640}
Original file line number Diff line number Diff line change @@ -4,11 +4,15 @@ internal static class Constants
44{
55 public const string ProtobufNamespace = "Google.Protobuf" ;
66 public const string ProtobufCollectionsNamespace = $ "{ ProtobufNamespace } .Collections";
7+ public const string ProtobufReflectionNamespace = $ "{ ProtobufNamespace } .Reflection";
78
89 public const string ProtobufMessageInterfaceFullyQualifiedName = $ "{ ProtobufNamespace } .IMessage";
910
11+ public const string ProtobufOriginalNameAttributeFullyQualifiedName =
12+ $ "{ ProtobufReflectionNamespace } .OriginalNameAttribute";
13+
1014 public const string ProtobufRepeatedFieldFullyQualifiedName = $ "{ ProtobufCollectionsNamespace } .RepeatedField`1";
1115 public const string ProtobufMapFieldFullyQualifiedName = $ "{ ProtobufCollectionsNamespace } .MapField`2";
1216
1317 public const string FilenameSuffix = "SourceKit.Generators.Grpc.g.cs" ;
14- }
18+ }
Original file line number Diff line number Diff line change @@ -13,20 +13,41 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
1313 INamedTypeSymbol ? messageInterfaceSymbol = context . SemanticModel . Compilation
1414 . GetTypeByMetadataName ( Constants . ProtobufMessageInterfaceFullyQualifiedName ) ;
1515
16- if ( messageInterfaceSymbol is null )
16+ INamedTypeSymbol ? enumAttributeSymbol = context . SemanticModel . Compilation
17+ . GetTypeByMetadataName ( Constants . ProtobufOriginalNameAttributeFullyQualifiedName ) ;
18+
19+ if ( messageInterfaceSymbol is null || enumAttributeSymbol is null )
1720 return ;
1821
1922 ISymbol ? symbolInfo = context . SemanticModel . GetDeclaredSymbol ( context . Node ) ;
2023
2124 if ( symbolInfo is not INamedTypeSymbol symbol )
2225 return ;
2326
24- if ( symbol . AllInterfaces . Contains ( messageInterfaceSymbol , SymbolEqualityComparer . Default ) is false )
27+ if ( IsProtoClass ( symbol ) || IsProtoEnum ( symbol ) )
2528 return ;
2629
2730 if ( symbol . ContainingType is not null )
2831 return ;
2932
3033 _symbols . Add ( symbol ) ;
34+
35+ bool IsProtoClass ( INamedTypeSymbol type )
36+ {
37+ return type . TypeKind is TypeKind . Class
38+ && type . AllInterfaces . Contains ( messageInterfaceSymbol , SymbolEqualityComparer . Default ) is false ;
39+ }
40+
41+ bool IsProtoEnum ( INamedTypeSymbol type )
42+ {
43+ if ( type . TypeKind is not TypeKind . Enum )
44+ return false ;
45+
46+ return type
47+ . GetMembers ( )
48+ . All ( member => member . GetAttributes ( )
49+ . Any ( attr => attr
50+ . AttributeClass ? . Equals ( enumAttributeSymbol , SymbolEqualityComparer . Default ) is true ) ) ;
51+ }
3152 }
32- }
53+ }
You can’t perform that action at this time.
0 commit comments