Files
caliverse_server/BrokerApiCore/Common/Guard.cs
2025-05-01 07:20:41 +09:00

140 lines
4.4 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using ServerCore;
using ServerBase;
using ServerCommon;
namespace BrokerCore.Common;
public static class Guard
{
public static class Against
{
// 기존 메서드를 유지하면서 zero allocation 버전 추가
public static void resultFail(Result result, Func<string>? messageFactory = null)
{
if (result.isFail())
{
throw new ResultFailException(result, messageFactory?.Invoke());
}
}
// Func<string> 활용 지연 평가 버전 추가
// public static void resultFail(Result result, Func<string> messageFactory)
// {
// if (result.isFail())
// {
// throw new ResultFailException(result, messageFactory());
// }
// }
public static void resultFail(Result result, ServerErrorCode code, string message)
{
if (result.isFail())
{
throw new ResultFailException(code, message);
}
}
// Func<string> 활용 지연 평가 버전 추가
public static void resultFail(Result result, ServerErrorCode code, Func<string> messageFactory)
{
if (result.isFail())
{
throw new ResultFailException(code, messageFactory());
}
}
public static void isNull([NotNull] object? value, ServerErrorCode code, string message)
{
if (value is null)
{
throw new ApiException(code, message);
}
}
// Func<string> 활용 지연 평가 버전 추가
public static void isNull([NotNull] object? value, ServerErrorCode code, Func<string> messageFactory)
{
if (value is null)
{
throw new ApiException(code, messageFactory());
}
}
public static void isFalse(bool value, ServerErrorCode code, string message)
{
if (!value)
{
throw new ApiException(code, message);
}
}
// Func<string> 활용 지연 평가 버전 추가
public static void isFalse(bool value, ServerErrorCode code, Func<string> messageFactory)
{
if (!value)
{
throw new ApiException(code, messageFactory());
}
}
public static void isTrue(bool value, ServerErrorCode code, string message)
{
if (value)
{
throw new ApiException(code, message);
}
}
// Func<string> 활용 지연 평가 버전 추가
public static void isTrue(bool value, ServerErrorCode code, Func<string> messageFactory)
{
if (value)
{
throw new ApiException(code, messageFactory());
}
}
public static void isNullOrEmptyOrWhiteSpace([NotNull] string? value, ServerErrorCode code, string message)
{
if (value is null || string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
{
throw new ApiException(code, message);
}
}
// Func<string> 활용 지연 평가 버전 추가
public static void isNullOrEmptyOrWhiteSpace([NotNull] string? value, ServerErrorCode code, Func<string> messageFactory)
{
if (value is null || string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
{
throw new ApiException(code, messageFactory());
}
}
// ReadOnlySpan<char> 버전 추가
public static void isNullOrEmptyOrWhiteSpace([NotNull] string? value, ServerErrorCode code, ReadOnlySpan<char> message)
{
if (value is null || string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
{
throw new ApiException(code, message.ToString());
}
}
public static void throwException(ServerErrorCode code, string message)
{
throw new ApiException((int)code, message);
}
// Func<string> 활용 지연 평가 버전 추가
public static void throwException(ServerErrorCode code, Func<string> messageFactory)
{
throw new ApiException((int)code, messageFactory());
}
}
}