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