Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
DeagleGross committed Aug 23, 2024
1 parent 0fad15d commit 9c8b1e6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ private void ValidateDapperMethod(in OperationAnalysisContext ctx, IOperation sq

// check the types
// resultType can be an array, so let's unwrap it to determine actual type
var resultType = invoke.GetResultType(flags)?.UnwrapArrayType();
if (resultType is not null && IdentifyDbType(resultType, out _) is null) // don't warn if handled as an inbuilt
var resultType = invoke.GetResultType(flags);
if (resultType is not null && IdentifyDbType(resultType, out _) is null && !IsSpecialCaseAllowedResultType(resultType)) // don't warn if handled as an inbuilt
{
var resultMap = MemberMap.CreateForResults(resultType, location);
if (resultMap is not null)
Expand Down
33 changes: 15 additions & 18 deletions src/Dapper.AOT.Analyzers/Internal/Inspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,21 @@ public static ITypeSymbol MakeNonNullable(ITypeSymbol type)
? type : type.WithNullableAnnotation(NullableAnnotation.None);
}

/// <summary>
/// There are special types we are allowing user to query,
/// which are not handled in the general checks
/// </summary>
public static bool IsSpecialCaseAllowedResultType(ITypeSymbol? type)
{
// byte[] or sbyte[] are basically blobs
if (type is IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Byte or SpecialType.System_SByte })
{
return true;
}

return false;
}

public static DbType? IdentifyDbType(ITypeSymbol? type, out string? readerMethod)
{
if (type is null)
Expand Down Expand Up @@ -1207,24 +1222,6 @@ public static bool IsDapperMethod(this IInvocationOperation operation, out Opera

public static bool TryGetConstantValue<T>(IOperation op, out T? value)
=> TryGetConstantValueWithSyntax(op, out value, out _, out _);

public static ITypeSymbol? UnwrapArrayType(this ITypeSymbol typeSymbol)
{
if (typeSymbol is not IArrayTypeSymbol arrayTypeSymbol)
{
return typeSymbol;
}

ITypeSymbol elementType;
IArrayTypeSymbol? currentArraySymbol = arrayTypeSymbol;
do
{
elementType = currentArraySymbol.ElementType;
currentArraySymbol = elementType as IArrayTypeSymbol;
} while (currentArraySymbol is not null);

return elementType;
}

public static ITypeSymbol? GetResultType(this IInvocationOperation invocation, OperationFlags flags)
{
Expand Down
5 changes: 2 additions & 3 deletions test/Dapper.AOT.Test/Verifiers/DAP037.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ from SomeTable
_ = conn.Query<HazExplicitConstructor>(sql, args);
_ = conn.Query<sbyte[]>(sql, args);
_ = conn.Query<byte[]>(sql, args);
_ = conn.Query<byte[][]>(sql, args);
_ = conn.Query<byte[][][]>(sql, args);
_ = conn.Query<int>(sql, args);
_ = conn.{|#2:Query<int[]>|}(sql, args);
}
}

Expand Down Expand Up @@ -82,6 +80,7 @@ static file class IsExternalInit {}
"""", DefaultConfig, [
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(0).WithArguments("NoSettable"),
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(1).WithArguments("ReadOnlyField"),
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(2).WithArguments(""),
]);

}

0 comments on commit 9c8b1e6

Please sign in to comment.