Skip to content

Commit

Permalink
feat(typeaccessor): add isnull custom implementation (#26)
Browse files Browse the repository at this point in the history
* add isnull implementation

* add check on Ssytem.DBNull property

* check global namespace
  • Loading branch information
DeagleGross committed Jul 15, 2023
1 parent 88f7a44 commit 9070118
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private void Generate(SourceProductionContext context, (Compilation Compilation,
accessorSb.WriteGetName(typeSymbolName, members);
accessorSb.WriteIndexer(typeSymbolName, members);
accessorSb.WriteIsNullable(members);
accessorSb.WriteIsNull(typeSymbolName, members);
accessorSb.WriteGetType(members);
accessorSb.WriteGetValue(typeSymbolName, members);
accessorSb.WriteSetValue(typeSymbolName, members);
Expand Down Expand Up @@ -412,6 +413,50 @@ public void WriteIsNullable(MemberData[] members)
.Outdent().Append(";").NewLine();
}

public void WriteIsNull(string userTypeName, MemberData[] members)
{
_sb.Append("public override bool IsNull(").Append(userTypeName).Append(" obj, int index) => index switch")
.Indent().NewLine();

var strBuilder = new StringBuilder();
foreach (var item in members.Where(x => !x.IsNullable))
{
strBuilder.Append(item.Number).Append(" or ");
}
if (strBuilder.Length > 0)
{
strBuilder.Length -= 4;
_sb.Append(strBuilder.ToString()).Append(" => false,").NewLine();
}

foreach (var member in members.Where(x => x.IsNullable))
{
if (IsDBNull(member.TypeSymbol))
{
// if member is of type DBNull, then it is always null => simply return true
_sb.Append(member.Number).Append(" => true,").NewLine();
continue;
}

_sb.Append(member.Number).Append(" => obj.").Append(member.Name).Append(" is null");
if (member.TypeSymbol.IsSystemObject())
{
_sb.Append(" or global::System.DBNull");
}
_sb.Append(",").NewLine();
}

_sb.Append("_ => base.IsNull(obj, index)")
.Outdent().Append(";").NewLine();

bool IsDBNull(ITypeSymbol typeSymbol)
{
return typeSymbol.ContainingNamespace.ContainingNamespace?.IsGlobalNamespace == true
&& typeSymbol.ContainingNamespace.Name == "System"
&& typeSymbol.Name == "DBNull";
}
}

public void WriteGetType(MemberData[] members)
{
_sb.Append("public override global::System.Type GetType(int index) => index switch")
Expand Down Expand Up @@ -507,7 +552,7 @@ private MemberData[] ConstructTypeMembers(ITypeSymbol typeSymbol)
Type = member.ToDisplayString(),
TypeSymbol = property.Type,
Number = memberNumber++,
IsNullable = property.NullableAnnotation == NullableAnnotation.Annotated
IsNullable = property.Type.IsNullable()
});
}
if (type is IFieldSymbol field)
Expand All @@ -526,6 +571,7 @@ private MemberData[] ConstructTypeMembers(ITypeSymbol typeSymbol)
return members.ToArray();
}

[DebuggerDisplay("{TypeSymbol} {Name}")]
struct MemberData
{
public int Number;
Expand Down
18 changes: 18 additions & 0 deletions src/Dapper.AOT.Analyzers/Internal/Roslyn/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public static bool IsPrimitiveType(this ITypeSymbol? typeSymbol)
};
}

public static bool IsNullable(this ITypeSymbol typeSymbol)
{
if (typeSymbol.NullableAnnotation == NullableAnnotation.Annotated) return true;
return typeSymbol.TypeKind is
TypeKind.Class or TypeKind.Array or
TypeKind.Delegate or TypeKind.Dynamic or
TypeKind.Interface;
}

/// <returns>
/// Returns true, if <paramref name="typeSymbol"/> represents an <see cref="object"/> type.
/// </returns>
public static bool IsSystemObject(this ITypeSymbol? typeSymbol)
{
if (typeSymbol is null) return false;
return typeSymbol.SpecialType == SpecialType.System_Object;
}

/// <returns>
/// True, if passed <param name="typeSymbol"/> represents array. False otherwise
/// </returns>
Expand Down
5 changes: 4 additions & 1 deletion test/Dapper.AOT.Test/Accessors/Data/Accessor.input.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dapper;
using System;
using Dapper;

[module: DapperAot]

Expand All @@ -19,6 +20,8 @@ public class Customer
public string Y;
public double? Z { get; set; }
public State State { get; set; }
public object Obj { get; set; }
public DBNull DbNullProp { get; set; }
}
public enum State
{
Expand Down
36 changes: 32 additions & 4 deletions test/Dapper.AOT.Test/Accessors/Data/Accessor.output.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#nullable enable
file static class DapperTypeAccessorGeneratedInterceptors
{
[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Accessors\\Data\\Accessor.input.cs", 10, 26)]
[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Accessors\\Data\\Accessor.input.cs", 11, 26)]
internal static global::Dapper.ObjectAccessor<global::Foo.Customer> Forwarded0(global::Foo.Customer obj, global::Dapper.TypeAccessor<global::Foo.Customer>? accessor)
=> global::Dapper.TypeAccessor.CreateAccessor(obj, accessor ?? DapperCustomTypeAccessor0.Instance);

[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Accessors\\Data\\Accessor.input.cs", 13, 26)]
[global::System.Runtime.CompilerServices.InterceptsLocationAttribute("Accessors\\Data\\Accessor.input.cs", 14, 26)]
internal static global::System.Data.Common.DbDataReader Forwarded1(global::System.Collections.Generic.IEnumerable<global::Foo.Customer> source, string[]? members, bool exact, global::Dapper.TypeAccessor<global::Foo.Customer>? accessor)
=> global::Dapper.TypeAccessor.CreateDataReader(source, members, exact, accessor ?? DapperCustomTypeAccessor0.Instance);

private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<global::Foo.Customer>
{
internal static readonly DapperCustomTypeAccessor0 Instance = new();
public override int MemberCount => 4;
public override int MemberCount => 6;
public override int? TryIndex(string name, bool exact = false)
{
if (exact)
Expand All @@ -23,6 +23,8 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
nameof(global::Foo.Customer.Y) => 1,
nameof(global::Foo.Customer.Z) => 2,
nameof(global::Foo.Customer.State) => 3,
nameof(global::Foo.Customer.Obj) => 4,
nameof(global::Foo.Customer.DbNullProp) => 5,
_ => base.TryIndex(name, exact)
};
}
Expand All @@ -34,6 +36,8 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
4228665076U when NormalizedEquals(name, "y") => 1,
4278997933U when NormalizedEquals(name, "z") => 2,
2016490230U when NormalizedEquals(name, "state") => 3,
3343205242U when NormalizedEquals(name, "obj") => 4,
492569847U when NormalizedEquals(name, "dbnullprop") => 5,
_ => base.TryIndex(name, exact)
};
}
Expand All @@ -44,6 +48,8 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
1 => nameof(global::Foo.Customer.Y),
2 => nameof(global::Foo.Customer.Z),
3 => nameof(global::Foo.Customer.State),
4 => nameof(global::Foo.Customer.Obj),
5 => nameof(global::Foo.Customer.DbNullProp),
_ => base.GetName(index)
};
public override object? this[global::Foo.Customer obj, int index]
Expand All @@ -54,6 +60,8 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
1 => obj.Y,
2 => obj.Z,
3 => obj.State,
4 => obj.Obj,
5 => obj.DbNullProp,
_ => base[obj, index]
};
set
Expand All @@ -64,22 +72,34 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
case 1: obj.Y = (string)value!; break;
case 2: obj.Z = (double?)value!; break;
case 3: obj.State = (Foo.State)value!; break;
case 4: obj.Obj = (object)value!; break;
case 5: obj.DbNullProp = (System.DBNull)value!; break;
default: base[obj, index] = value; break;
};
}
}
public override bool IsNullable(int index) => index switch
{
2 => true,
2 or 4 or 5 => true,
0 or 1 or 3 => false,
_ => base.IsNullable(index)
};
public override bool IsNull(global::Foo.Customer obj, int index) => index switch
{
0 or 1 or 3 => false,
2 => obj.Z is null,
4 => obj.Obj is null or global::System.DBNull,
5 => true,
_ => base.IsNull(obj, index)
};
public override global::System.Type GetType(int index) => index switch
{
0 => typeof(int),
1 => typeof(string),
2 => typeof(double?),
3 => typeof(Foo.State),
4 => typeof(object),
5 => typeof(System.DBNull),
_ => base.GetType(index)
};
public override TValue GetValue<TValue>(global::Foo.Customer obj, int index) => index switch
Expand All @@ -88,6 +108,8 @@ private sealed class DapperCustomTypeAccessor0 : global::Dapper.TypeAccessor<glo
1 when typeof(TValue) == typeof(string) => UnsafePun<string, TValue>(obj.Y),
2 when typeof(TValue) == typeof(double?) => UnsafePun<double?, TValue>(obj.Z),
3 when typeof(TValue) == typeof(Foo.State) || typeof(TValue) == typeof(int) => UnsafePun<Foo.State, TValue>(obj.State),
4 when typeof(TValue) == typeof(object) => UnsafePun<object, TValue>(obj.Obj),
5 when typeof(TValue) == typeof(System.DBNull) => UnsafePun<System.DBNull, TValue>(obj.DbNullProp),
_ => base.GetValue<TValue>(obj, index)
};
public override void SetValue<TValue>(global::Foo.Customer obj, int index, TValue value)
Expand All @@ -106,6 +128,12 @@ public override void SetValue<TValue>(global::Foo.Customer obj, int index, TValu
case 3 when typeof(TValue) == typeof(Foo.State) || typeof(TValue) == typeof(int):
obj.State = UnsafePun<TValue, Foo.State>(value);
break;
case 4 when typeof(TValue) == typeof(object):
obj.Obj = UnsafePun<TValue, object>(value);
break;
case 5 when typeof(TValue) == typeof(System.DBNull):
obj.DbNullProp = UnsafePun<TValue, System.DBNull>(value);
break;

}

Expand Down
14 changes: 14 additions & 0 deletions test/Dapper.AOT.Test/Internal/Roslyn/TypeSymbolExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ namespace Dapper.Internal.Roslyn
{
public class TypeSymbolExtensionsTests
{
[Fact]
public void CheckSystemObject()
{
var text = BuildDapperCodeText(
"""
_ = connection.Execute("def", new Customer());
""");

var argumentOperation = GetInvocationArgumentOperation(text);
var typeSymbol = GetConversionTypeSymbol(argumentOperation);

Assert.True(typeSymbol.IsNullable());
}

[Fact]
public void CheckCollectionType_TwoDimensionalArray()
{
Expand Down

0 comments on commit 9070118

Please sign in to comment.