Skip to content

Commit

Permalink
Prevent string resource lookup if not required (#265)
Browse files Browse the repository at this point in the history
Signed-off-by: Campbell Harding-Deason <campbell.harding-deason@firstaml.com>
  • Loading branch information
campbellhardingdeason authored Sep 1, 2023
1 parent 0c0169f commit afb9b85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/CloudNative.CloudEvents/CloudEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions src/CloudNative.CloudEvents/Core/Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public static void CheckArgument([DoesNotReturnIf(false)] bool condition, string
}
}

/// <summary>
/// Validates an argument-dependent condition, throwing an exception if the check fails.
/// </summary>
/// <param name="condition">The condition to validate; this method will throw an <see cref="ArgumentException"/> if this is false.</param>
/// <param name="paramName">The name of the parameter being validated. May be null.</param>
/// <param name="messageFunc">A func that returns the message to use in the exception, if one is thrown.</param>
internal static void CheckArgument([DoesNotReturnIf(false)] bool condition, string paramName, Func<string> messageFunc)
{
if (!condition)
{
throw new ArgumentException(messageFunc(), paramName);
}
}

/// <summary>
/// Validates an argument-dependent condition, throwing an exception if the check fails.
/// </summary>
Expand Down

0 comments on commit afb9b85

Please sign in to comment.