11using DynamoDBGenerator . SourceGenerator . Extensions ;
22using Microsoft . CodeAnalysis ;
3+
34namespace DynamoDBGenerator . SourceGenerator . Types ;
45
56public abstract record TypeIdentifier ( ITypeSymbol TypeSymbol )
67{
8+ private bool IsNullable { get ; } = TypeSymbol . IsNullable ( ) ;
9+
10+ public string ReturnNullOrThrow ( string dataMember )
11+ {
12+ return IsNullable
13+ ? "return null;"
14+ : $ "throw { Constants . DynamoDBGenerator . ExceptionHelper . NullExceptionMethod } ({ dataMember } );";
15+ }
16+
717 public ITypeSymbol TypeSymbol { get ; } = TypeSymbol ;
818}
919
1020public sealed record UnknownType ( ITypeSymbol TypeSymbol ) : TypeIdentifier ( TypeSymbol ) ;
1121
1222public sealed record KeyValueGeneric : TypeIdentifier
1323{
14-
1524 public enum SupportedType
1625 {
1726 LookUp = 1 ,
1827 Dictionary = 2
1928 }
2029
21- private KeyValueGeneric ( in ITypeSymbol typeSymbol , in ITypeSymbol tKey , in ITypeSymbol tValue , in SupportedType supportedType ) : base ( typeSymbol )
30+ private KeyValueGeneric ( in ITypeSymbol typeSymbol , in ITypeSymbol tKey , in ITypeSymbol tValue ,
31+ in SupportedType supportedType ) : base ( typeSymbol )
2232 {
2333 Type = supportedType ;
2434 TKey = tKey ;
2535 TValue = tValue ;
2636 }
37+
2738 public SupportedType Type { get ; }
2839
2940 // ReSharper disable once InconsistentNaming
@@ -37,13 +48,13 @@ private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in IType
3748 if ( typeSymbol is not INamedTypeSymbol type )
3849 return null ;
3950
40- if ( type is not { IsGenericType : true , TypeArguments . Length : 2 } )
51+ if ( type is not { IsGenericType : true , TypeArguments . Length : 2 } )
4152 return null ;
4253
4354 SupportedType ? supported = type switch
4455 {
45- { Name : "ILookup" } => SupportedType . LookUp ,
46- { Name : "Dictionary" or "IReadOnlyDictionary" or "IDictionary" } => SupportedType . Dictionary ,
56+ { Name : "ILookup" } => SupportedType . LookUp ,
57+ { Name : "Dictionary" or "IReadOnlyDictionary" or "IDictionary" } => SupportedType . Dictionary ,
4758 _ => null
4859 } ;
4960 return supported is null
@@ -54,7 +65,6 @@ private KeyValueGeneric(in ITypeSymbol typeSymbol, in ITypeSymbol tKey, in IType
5465
5566public sealed record SingleGeneric : TypeIdentifier
5667{
57-
5868 public enum SupportedType
5969 {
6070 Nullable = 1 ,
@@ -72,6 +82,7 @@ private SingleGeneric(ITypeSymbol type, ITypeSymbol innerType, in SupportedType
7282 Type = supportedType ;
7383 T = innerType ;
7484 }
85+
7586 public SupportedType Type { get ; }
7687 public ITypeSymbol T { get ; }
7788
@@ -84,25 +95,33 @@ private SingleGeneric(ITypeSymbol type, ITypeSymbol innerType, in SupportedType
8495 if ( typeSymbol is not INamedTypeSymbol type )
8596 return null ;
8697
87- if ( type is not { IsGenericType : true , TypeArguments . Length : 1 } )
98+ if ( type is not { IsGenericType : true , TypeArguments . Length : 1 } )
8899 return null ;
89100
90101 SupportedType ? supported = type switch
91102 {
92103 _ when type . TryGetNullableValueType ( ) is not null => SupportedType . Nullable ,
93- _ when type . OriginalDefinition . ToDisplayString ( ) == "System.Collections.Generic.List<T>" => SupportedType . List ,
94- { Name : "ISet" } => SupportedType . Set ,
95- _ when type . AllInterfaces . Any ( x => x is { Name : "ISet" } ) => SupportedType . Set ,
96- { Name : "IReadOnlySet" } => SupportedType . Set ,
97- _ when type . AllInterfaces . Any ( x => x is { Name : "IReadOnlySet" } ) => SupportedType . Set ,
98- { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_ICollection_T } => SupportedType . ICollection ,
99- _ when type . AllInterfaces . Any ( x => x is { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_ICollection_T } ) => SupportedType . ICollection ,
100- { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IReadOnlyCollection_T } => SupportedType . IReadOnlyCollection ,
101- _ when type . AllInterfaces . Any ( x => x is { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IReadOnlyCollection_T } ) => SupportedType . IReadOnlyCollection ,
102- { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IEnumerable_T } => SupportedType . IEnumerable ,
104+ _ when type . OriginalDefinition . ToDisplayString ( ) == "System.Collections.Generic.List<T>" => SupportedType
105+ . List ,
106+ { Name : "ISet" } => SupportedType . Set ,
107+ _ when type . AllInterfaces . Any ( x => x is { Name : "ISet" } ) => SupportedType . Set ,
108+ { Name : "IReadOnlySet" } => SupportedType . Set ,
109+ _ when type . AllInterfaces . Any ( x => x is { Name : "IReadOnlySet" } ) => SupportedType . Set ,
110+ { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_ICollection_T } => SupportedType
111+ . ICollection ,
112+ _ when type . AllInterfaces . Any ( x => x is
113+ { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_ICollection_T } ) =>
114+ SupportedType . ICollection ,
115+ { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IReadOnlyCollection_T } =>
116+ SupportedType . IReadOnlyCollection ,
117+ _ when type . AllInterfaces . Any ( x => x is
118+ { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IReadOnlyCollection_T } ) =>
119+ SupportedType . IReadOnlyCollection ,
120+ { OriginalDefinition . SpecialType : SpecialType . System_Collections_Generic_IEnumerable_T } => SupportedType
121+ . IEnumerable ,
103122 _ => null
104123 } ;
105124
106125 return supported is null ? null : new SingleGeneric ( type , type . TypeArguments [ 0 ] , supported . Value ) ;
107126 }
108- }
127+ }
0 commit comments