From afb9b857e778b4684fc00289fcb43879e6654976 Mon Sep 17 00:00:00 2001 From: Campbell Harding-Deason Date: Fri, 1 Sep 2023 17:46:53 +1200 Subject: [PATCH] Prevent string resource lookup if not required (#265) Signed-off-by: Campbell Harding-Deason --- src/CloudNative.CloudEvents/CloudEvent.cs | 8 ++++---- src/CloudNative.CloudEvents/Core/Validation.cs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/CloudNative.CloudEvents/CloudEvent.cs b/src/CloudNative.CloudEvents/CloudEvent.cs index 029199c..f9ef381 100644 --- a/src/CloudNative.CloudEvents/CloudEvent.cs +++ b/src/CloudNative.CloudEvents/CloudEvent.cs @@ -122,7 +122,7 @@ public object? this[CloudEventAttribute attribute] get { Validation.CheckNotNull(attribute, nameof(attribute)); - Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), Strings.ErrorCannotIndexBySpecVersionAttribute); + Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), () => Strings.ErrorCannotIndexBySpecVersionAttribute); // TODO: Is this validation definitely useful? It does mean we never return something // that's invalid for the attribute, which is potentially good... @@ -136,7 +136,7 @@ public object? this[CloudEventAttribute attribute] set { Validation.CheckNotNull(attribute, nameof(attribute)); - Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), Strings.ErrorCannotIndexBySpecVersionAttribute); + Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), () => Strings.ErrorCannotIndexBySpecVersionAttribute); string name = attribute.Name; var knownAttribute = GetAttribute(name); @@ -181,13 +181,13 @@ public object? this[string attributeName] { // TODO: Validate the attribute name is valid (e.g. not upper case)? Seems overkill. Validation.CheckNotNull(attributeName, nameof(attributeName)); - Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), Strings.ErrorCannotIndexBySpecVersionAttribute); + Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), () => Strings.ErrorCannotIndexBySpecVersionAttribute); return attributeValues.GetValueOrDefault(Validation.CheckNotNull(attributeName, nameof(attributeName))); } set { Validation.CheckNotNull(attributeName, nameof(attributeName)); - Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), Strings.ErrorCannotIndexBySpecVersionAttribute); + Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), () => Strings.ErrorCannotIndexBySpecVersionAttribute); var knownAttribute = GetAttribute(attributeName); diff --git a/src/CloudNative.CloudEvents/Core/Validation.cs b/src/CloudNative.CloudEvents/Core/Validation.cs index f98d590..cdd3e5c 100644 --- a/src/CloudNative.CloudEvents/Core/Validation.cs +++ b/src/CloudNative.CloudEvents/Core/Validation.cs @@ -40,6 +40,20 @@ public static void CheckArgument([DoesNotReturnIf(false)] bool condition, string } } + /// + /// Validates an argument-dependent condition, throwing an exception if the check fails. + /// + /// The condition to validate; this method will throw an if this is false. + /// The name of the parameter being validated. May be null. + /// A func that returns the message to use in the exception, if one is thrown. + internal static void CheckArgument([DoesNotReturnIf(false)] bool condition, string paramName, Func messageFunc) + { + if (!condition) + { + throw new ArgumentException(messageFunc(), paramName); + } + } + /// /// Validates an argument-dependent condition, throwing an exception if the check fails. ///