Skip to content

Commit 8f1b429

Browse files
committed
More nullability fixes
1 parent ab57aae commit 8f1b429

6 files changed

Lines changed: 44 additions & 49 deletions

File tree

src/RepoDb/Mappers/ClassMapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using RepoDb.Attributes;
1+
using System.Collections.Concurrent;
2+
using RepoDb.Attributes;
23
using RepoDb.Exceptions;
34
using RepoDb.Extensions;
4-
using System.Collections.Concurrent;
55

66
namespace RepoDb;
77

@@ -94,7 +94,7 @@ public static void Add(Type type,
9494
/// </summary>
9595
/// <typeparam name="TEntity">The target type.</typeparam>
9696
/// <returns>The mapped name of the class.</returns>
97-
public static string Get<TEntity>()
97+
public static string? Get<TEntity>()
9898
where TEntity : class =>
9999
Get(typeof(TEntity));
100100

@@ -103,7 +103,7 @@ public static string Get<TEntity>()
103103
/// </summary>
104104
/// <param name="type">The target type.</param>
105105
/// <returns>The mapped name of the class.</returns>
106-
public static string Get(Type type)
106+
public static string? Get(Type type)
107107
{
108108
var key = type.GetHashCode();
109109

src/RepoDb/Mappers/IdentityMapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using RepoDb.Attributes;
1+
using System.Collections.Concurrent;
2+
using System.Linq.Expressions;
3+
using RepoDb.Attributes;
24
using RepoDb.Exceptions;
35
using RepoDb.Extensions;
4-
using System.Collections.Concurrent;
5-
using System.Linq.Expressions;
66

77
namespace RepoDb;
88

@@ -159,7 +159,7 @@ internal static void Add(Type type,
159159
/// </summary>
160160
/// <typeparam name="TEntity">The type of the data entity.</typeparam>
161161
/// <returns>An instance of the mapped <see cref="ClassProperty"/> object.</returns>
162-
public static ClassProperty Get<TEntity>()
162+
public static ClassProperty? Get<TEntity>()
163163
where TEntity : class =>
164164
Get(typeof(TEntity));
165165

@@ -168,7 +168,7 @@ public static ClassProperty Get<TEntity>()
168168
/// </summary>
169169
/// <param name="type">The target type.</param>
170170
/// <returns>An instance of the mapped <see cref="ClassProperty"/> object.</returns>
171-
public static ClassProperty Get(Type type)
171+
public static ClassProperty? Get(Type type)
172172
{
173173
// Validate
174174
ObjectExtension.ThrowIfNull(type, "Type");

src/RepoDb/Mappers/PrimaryMapper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using RepoDb.Attributes;
1+
using System.Collections.Concurrent;
2+
using System.Linq.Expressions;
3+
using RepoDb.Attributes;
24
using RepoDb.Exceptions;
35
using RepoDb.Extensions;
4-
using System.Collections.Concurrent;
5-
using System.Linq.Expressions;
66

77
namespace RepoDb;
88

@@ -159,7 +159,7 @@ internal static void Add(Type type,
159159
/// </summary>
160160
/// <typeparam name="TEntity">The type of the data entity.</typeparam>
161161
/// <returns>An instance of the mapped <see cref="ClassProperty"/> object.</returns>
162-
public static ClassProperty Get<TEntity>()
162+
public static ClassProperty? Get<TEntity>()
163163
where TEntity : class =>
164164
Get(typeof(TEntity));
165165

@@ -168,7 +168,7 @@ public static ClassProperty Get<TEntity>()
168168
/// </summary>
169169
/// <param name="type">The target type.</param>
170170
/// <returns>An instance of the mapped <see cref="ClassProperty"/> object.</returns>
171-
public static ClassProperty Get(Type type)
171+
public static ClassProperty? Get(Type type)
172172
{
173173
// Validate
174174
ObjectExtension.ThrowIfNull(type, "Type");

src/RepoDb/Mappers/PropertyMapper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ internal static void Add<TEntity>(PropertyInfo propertyInfo,
183183
/// <typeparam name="TEntity">The type of the data entity.</typeparam>
184184
/// <param name="expression">The expression to be parsed.</param>
185185
/// <returns>The mapped name of the property.</returns>
186-
public static string Get<TEntity>(Expression<Func<TEntity, object?>> expression)
186+
public static string? Get<TEntity>(Expression<Func<TEntity, object?>> expression)
187187
where TEntity : class =>
188188
Get<TEntity>(ExpressionExtension.GetProperty(expression));
189189

@@ -193,7 +193,7 @@ public static string Get<TEntity>(Expression<Func<TEntity, object?>> expression)
193193
/// <typeparam name="TEntity">The target .NET CLR type.</typeparam>
194194
/// <param name="propertyName">The name of the property.</param>
195195
/// <returns>The mapped name of the property.</returns>
196-
public static string Get<TEntity>(string propertyName)
196+
public static string? Get<TEntity>(string propertyName)
197197
where TEntity : class =>
198198
Get<TEntity>(TypeExtension.GetProperty<TEntity>(propertyName));
199199

@@ -203,7 +203,7 @@ public static string Get<TEntity>(string propertyName)
203203
/// <typeparam name="TEntity">The target .NET CLR type.</typeparam>
204204
/// <param name="field">The instance of <see cref="Field"/> object.</param>
205205
/// <returns>The mapped name of the property.</returns>
206-
public static string Get<TEntity>(Field field)
206+
public static string? Get<TEntity>(Field field)
207207
where TEntity : class =>
208208
Get<TEntity>(TypeExtension.GetProperty<TEntity>(field.FieldName));
209209

@@ -214,7 +214,7 @@ public static string Get<TEntity>(Field field)
214214
/// <typeparam name="TEntity">The target .NET CLR type.</typeparam>
215215
/// <param name="propertyInfo">The instance of <see cref="PropertyInfo"/>.</param>
216216
/// <returns>The mapped name of the property.</returns>
217-
internal static string Get<TEntity>(PropertyInfo propertyInfo)
217+
internal static string? Get<TEntity>(PropertyInfo propertyInfo)
218218
where TEntity : class =>
219219
Get(typeof(TEntity), propertyInfo);
220220

@@ -223,7 +223,7 @@ internal static string Get<TEntity>(PropertyInfo propertyInfo)
223223
/// </summary>
224224
/// <param name="propertyInfo">The instance of <see cref="PropertyInfo"/>.</param>
225225
/// <returns>The mapped name of the property.</returns>
226-
internal static string Get(PropertyInfo propertyInfo) =>
226+
internal static string? Get(PropertyInfo propertyInfo) =>
227227
Get(propertyInfo.DeclaringType!, propertyInfo);
228228

229229
/// <summary>
@@ -232,7 +232,7 @@ internal static string Get(PropertyInfo propertyInfo) =>
232232
/// <param name="entityType">The target .NET CLR type.</param>
233233
/// <param name="propertyInfo">The instance of <see cref="PropertyInfo"/>.</param>
234234
/// <returns>The mapped name of the property.</returns>
235-
internal static string Get(Type entityType,
235+
internal static string? Get(Type entityType,
236236
PropertyInfo propertyInfo)
237237
{
238238
// Validate

src/RepoDb/QueryField/ParseExpression.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,17 @@ internal static QueryGroup Parse<TEntity>(BinaryExpression expression)
209209
/// <param name="value"></param>
210210
/// <returns></returns>
211211
private static object? ToEnumValue(Type enumType,
212-
object? value) =>
213-
(value != null ?
212+
object? value)
213+
{
214+
return (value != null ?
214215
ToEnumValue(enumType, Enum.GetName(enumType, value)) : null) ?? value;
215216

216-
/// <summary>
217-
///
218-
/// </summary>
219-
/// <param name="enumType"></param>
220-
/// <param name="name"></param>
221-
/// <returns></returns>
222-
private static object? ToEnumValue(Type enumType,
223-
string name) =>
224-
!string.IsNullOrEmpty(name) && Enum.IsDefined(enumType, name) ?
225-
Enum.Parse(enumType, name) : null;
217+
static object? ToEnumValue(Type enumType, string? name)
218+
{
219+
return !string.IsNullOrEmpty(name) && Enum.IsDefined(enumType, name) ?
220+
Enum.Parse(enumType, name) : null;
221+
}
222+
}
226223

227224
/*
228225
* Member
@@ -311,17 +308,15 @@ internal static QueryField ParseEquals<TEntity>(MethodCallExpression expression)
311308
var property = GetProperty<TEntity>(expression) ?? throw new InvalidOperationException($"Can't parse '{expression}' to entity property");
312309

313310
// Value
314-
if (expression?.Object != null)
311+
if (expression?.Object?.Type == StaticType.String)
315312
{
316-
if (expression.Object?.Type == StaticType.String)
317-
{
318-
var value = Converter.ToType<string>(expression.Arguments.First().GetValue());
319-
return new QueryField(property.AsField(), value);
320-
}
313+
var value = Converter.ToType<string>(expression.Arguments.First().GetValue());
314+
return new QueryField(property.AsField(), value);
315+
}
316+
else
317+
{
318+
throw new InvalidOperationException($"Can't parse '{expression}' to query");
321319
}
322-
323-
// Return
324-
return null;
325320
}
326321

327322
/// <summary>

src/RepoDb/Reflection/Compiler.DataEntityPropertySetter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ partial class Compiler
1818
// Get the entity property
1919
var property = PropertyCache.Get(entityType).GetByFieldName(field.FieldName)?.PropertyInfo;
2020

21+
if (property == null)
22+
{
23+
// If the property is not found, then return a no-op function
24+
return (_, _) => { };
25+
}
26+
2127
// Return the function
2228
return CompileDataEntityPropertySetter(entityType,
2329
property,
24-
property?.PropertyType ?? field.Type);
30+
property.PropertyType);
2531
}
2632

2733
/// <summary>
@@ -32,15 +38,9 @@ partial class Compiler
3238
/// <param name="targetType"></param>
3339
/// <returns></returns>
3440
private static Action<object, object?> CompileDataEntityPropertySetter(Type entityType,
35-
PropertyInfo? property,
41+
PropertyInfo property,
3642
Type targetType)
3743
{
38-
// Check the property first
39-
if (property == null)
40-
{
41-
return (_, _) => { };
42-
}
43-
4444
// Make sure we can write
4545
if (property.CanWrite == false)
4646
{

0 commit comments

Comments
 (0)