11using Microsoft . CodeAnalysis ;
2+
23namespace DynamoDBGenerator . SourceGenerator . Extensions ;
34
45public static class NotNullEvaluationExtensions
56{
6- public static string NotNullTernaryExpression ( this ITypeSymbol typeSymbol , in string accessPattern , in string truthy ,
7+ public static string NotNullTernaryExpression ( this ITypeSymbol typeSymbol , in string accessPattern ,
8+ in string truthy ,
79 in string falsy )
810 {
911 var result = Expression ( in typeSymbol , in accessPattern ) is { } expression
@@ -18,47 +20,51 @@ private static string CreateException(in string accessPattern)
1820 return $ "throw { Constants . DynamoDBGenerator . ExceptionHelper . NullExceptionMethod } (nameof({ accessPattern } ));";
1921 }
2022
21- public static IEnumerable < string > NotNullIfStatement ( this ITypeSymbol typeSymbol , string accessPattern , string truthy )
23+ public static IEnumerable < string > NotNullIfStatement ( this ITypeSymbol typeSymbol , string accessPattern ,
24+ string truthy )
2225 {
2326 return NotNullIfStatement ( typeSymbol , accessPattern , obj : truthy ) ;
2427 }
25- public static IEnumerable < string > NotNullIfStatement ( this ITypeSymbol typeSymbol , string accessPattern , IEnumerable < string > truthy )
28+
29+ public static IEnumerable < string > NotNullIfStatement ( this ITypeSymbol typeSymbol , string accessPattern ,
30+ IEnumerable < string > truthy )
2631 {
2732 return NotNullIfStatement ( typeSymbol , accessPattern , obj : truthy ) ;
2833 }
29-
34+
3035 private static IEnumerable < string > NotNullIfStatement ( this ITypeSymbol typeSymbol , string accessPattern , object obj )
3136 {
3237 if ( Expression ( typeSymbol , accessPattern ) is not { } expression )
3338 {
34- if ( obj is string single )
35- yield return single ;
36- else if ( obj is IEnumerable < string > truthies )
37- foreach ( var x in truthies )
38- yield return x ;
39- else
40- throw new NotImplementedException ( $ "Method '{ nameof ( NotNullIfStatement ) } ' could not determine type '{ obj . GetType ( ) . Name } '") ;
39+ if ( obj is string single )
40+ yield return single ;
41+ else if ( obj is IEnumerable < string > truthies )
42+ foreach ( var x in truthies )
43+ yield return x ;
44+ else
45+ throw new NotImplementedException (
46+ $ "Method '{ nameof ( NotNullIfStatement ) } ' could not determine type '{ obj . GetType ( ) . Name } '") ;
4147 }
4248 else
4349 {
44-
45- var ifClause = obj switch
50+ var ifClause = obj switch
4651 {
47- string single => $ "if ({ expression } )". CreateScope ( single ) ,
48- IEnumerable < string > multiple => $ "if ({ expression } )". CreateScope ( multiple ) ,
49- _ => throw new NotImplementedException ( $ "Method '{ nameof ( NotNullIfStatement ) } ' could not determine type '{ obj . GetType ( ) . Name } '")
52+ string single => $ "if ({ expression } )". CreateScope ( single ) ,
53+ IEnumerable < string > multiple => $ "if ({ expression } )". CreateScope ( multiple ) ,
54+ _ => throw new NotImplementedException (
55+ $ "Method '{ nameof ( NotNullIfStatement ) } ' could not determine type '{ obj . GetType ( ) . Name } '")
5056 } ;
5157 var enumerable = typeSymbol . NullableAnnotation switch
5258 {
5359 NullableAnnotation . None or NullableAnnotation . Annotated => ifClause ,
54- NullableAnnotation . NotAnnotated => ifClause . Concat ( "else" . CreateScope ( CreateException ( in accessPattern ) ) ) ,
60+ NullableAnnotation . NotAnnotated => ifClause . Concat (
61+ "else" . CreateScope ( CreateException ( in accessPattern ) ) ) ,
5562 _ => throw new ArgumentOutOfRangeException ( typeSymbol . ToDisplayString ( ) )
5663 } ;
5764
5865 foreach ( var element in enumerable )
5966 yield return element ;
6067 }
61-
6268 }
6369
6470
@@ -73,7 +79,6 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo
7379
7480 static string ? OnValueType ( in ITypeSymbol typeSymbol , in string accessPattern )
7581 {
76-
7782 if ( typeSymbol . TryGetNullableValueType ( ) is not { } namedTypeSymbol )
7883 return null ;
7984
@@ -86,6 +91,4 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo
8691 : $ "{ accessPattern } is not null && { expression } ";
8792 }
8893 }
89-
90-
91- }
94+ }
0 commit comments