Change ReplacePlaceholders to return NULL for unresolved
Previously, ReplacePlaceholders threw PlaceholderResolutionException when a placeholder could not be resolved. Now, unresolved placeholders are replaced with "NULL" instead. All exception references and related tests have been updated to reflect this new behavior. Documentation has also been revised accordingly.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using ReC.Application.Common.Exceptions;
|
||||
|
||||
namespace ReC.Application.Common.Dto;
|
||||
|
||||
@@ -14,10 +13,8 @@ public static partial class PlaceholderExtensions
|
||||
/// Replaces placeholders in the format <c>{#ANY_STRING#COLUMN_NAME}</c> with the corresponding
|
||||
/// property value resolved via <see cref="GetValueByColumnName{T}"/> from the provided objects.
|
||||
/// Values are converted to SQL-compatible string representations.
|
||||
/// If a placeholder cannot be resolved, it is replaced with <c>NULL</c>.
|
||||
/// </summary>
|
||||
/// <exception cref="PlaceholderResolutionException">
|
||||
/// Thrown when a placeholder's column name cannot be resolved from any of the provided objects.
|
||||
/// </exception>
|
||||
public static string ReplacePlaceholders(this string str, params object?[] objects)
|
||||
{
|
||||
return PlaceholderRegex().Replace(str, match =>
|
||||
@@ -37,7 +34,7 @@ public static partial class PlaceholderExtensions
|
||||
return ToSqlLiteral(value);
|
||||
}
|
||||
|
||||
throw new PlaceholderResolutionException(placeholder, columnName, str);
|
||||
return "NULL";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using ReC.Application.Common.Dto;
|
||||
using ReC.Application.Common.Exceptions;
|
||||
|
||||
namespace ReC.Tests.Application.Behaviors;
|
||||
|
||||
@@ -211,46 +210,40 @@ public class InvokeActionTests
|
||||
#region ReplacePlaceholders - Exception Tests
|
||||
|
||||
[Test]
|
||||
public void ReplacePlaceholders_UnresolvableColumn_ThrowsPlaceholderResolutionException()
|
||||
public void ReplacePlaceholders_UnresolvableColumn_ReturnsNull()
|
||||
{
|
||||
var input = "WHERE X = {#INT#NON_EXISTING}";
|
||||
|
||||
var ex = Assert.Throws<PlaceholderResolutionException>(() =>
|
||||
input.ReplacePlaceholders(_foo, _bar, _fuz));
|
||||
|
||||
Assert.That(ex!.ColumnName, Is.EqualTo("NON_EXISTING"));
|
||||
Assert.That(ex.Placeholder, Is.EqualTo("{#INT#NON_EXISTING}"));
|
||||
Assert.That(ex.Input, Is.EqualTo(input));
|
||||
var result = input.ReplacePlaceholders(_foo, _bar, _fuz);
|
||||
Assert.That(result, Is.EqualTo("WHERE X = NULL"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReplacePlaceholders_NoObjectsProvided_ThrowsPlaceholderResolutionException()
|
||||
public void ReplacePlaceholders_NoObjectsProvided_ReturnsNull()
|
||||
{
|
||||
var input = "WHERE X = {#INT#BAZ}";
|
||||
|
||||
Assert.Throws<PlaceholderResolutionException>(() =>
|
||||
input.ReplacePlaceholders());
|
||||
var result = input.ReplacePlaceholders();
|
||||
Assert.That(result, Is.EqualTo("WHERE X = NULL"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReplacePlaceholders_ObjectWithoutColumnAttributes_ThrowsPlaceholderResolutionException()
|
||||
public void ReplacePlaceholders_ObjectWithoutColumnAttributes_ReturnsNull()
|
||||
{
|
||||
var model = new NoColumnModel { Id = 1, Name = "Test" };
|
||||
var input = "WHERE X = {#INT#Id}";
|
||||
|
||||
Assert.Throws<PlaceholderResolutionException>(() =>
|
||||
input.ReplacePlaceholders(model));
|
||||
var result = input.ReplacePlaceholders(model);
|
||||
Assert.That(result, Is.EqualTo("WHERE X = NULL"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReplacePlaceholders_MixedResolvableAndUnresolvable_ThrowsOnUnresolvable()
|
||||
public void ReplacePlaceholders_MixedResolvableAndUnresolvable_ReturnsNullForUnresolvable()
|
||||
{
|
||||
var input = "WHERE BAZ = {#INT#BAZ} AND X = {#INT#UNKNOWN}";
|
||||
|
||||
var ex = Assert.Throws<PlaceholderResolutionException>(() =>
|
||||
input.ReplacePlaceholders(_foo, _bar, _fuz));
|
||||
|
||||
Assert.That(ex!.ColumnName, Is.EqualTo("UNKNOWN"));
|
||||
var result = input.ReplacePlaceholders(_foo, _bar, _fuz);
|
||||
Assert.That(result, Is.EqualTo("WHERE BAZ = 2 AND X = NULL"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user