Skip to content

Commit afb9b85

Browse files
Prevent string resource lookup if not required (cloudevents#265)
Signed-off-by: Campbell Harding-Deason <[email protected]>
1 parent 0c0169f commit afb9b85

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/CloudNative.CloudEvents/CloudEvent.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public object? this[CloudEventAttribute attribute]
122122
get
123123
{
124124
Validation.CheckNotNull(attribute, nameof(attribute));
125-
Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), Strings.ErrorCannotIndexBySpecVersionAttribute);
125+
Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), () => Strings.ErrorCannotIndexBySpecVersionAttribute);
126126

127127
// TODO: Is this validation definitely useful? It does mean we never return something
128128
// that's invalid for the attribute, which is potentially good...
@@ -136,7 +136,7 @@ public object? this[CloudEventAttribute attribute]
136136
set
137137
{
138138
Validation.CheckNotNull(attribute, nameof(attribute));
139-
Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), Strings.ErrorCannotIndexBySpecVersionAttribute);
139+
Validation.CheckArgument(attribute.Name != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attribute), () => Strings.ErrorCannotIndexBySpecVersionAttribute);
140140

141141
string name = attribute.Name;
142142
var knownAttribute = GetAttribute(name);
@@ -181,13 +181,13 @@ public object? this[string attributeName]
181181
{
182182
// TODO: Validate the attribute name is valid (e.g. not upper case)? Seems overkill.
183183
Validation.CheckNotNull(attributeName, nameof(attributeName));
184-
Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), Strings.ErrorCannotIndexBySpecVersionAttribute);
184+
Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), () => Strings.ErrorCannotIndexBySpecVersionAttribute);
185185
return attributeValues.GetValueOrDefault(Validation.CheckNotNull(attributeName, nameof(attributeName)));
186186
}
187187
set
188188
{
189189
Validation.CheckNotNull(attributeName, nameof(attributeName));
190-
Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), Strings.ErrorCannotIndexBySpecVersionAttribute);
190+
Validation.CheckArgument(attributeName != CloudEventsSpecVersion.SpecVersionAttributeName, nameof(attributeName), () => Strings.ErrorCannotIndexBySpecVersionAttribute);
191191

192192
var knownAttribute = GetAttribute(attributeName);
193193

src/CloudNative.CloudEvents/Core/Validation.cs

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public static void CheckArgument([DoesNotReturnIf(false)] bool condition, string
4040
}
4141
}
4242

43+
/// <summary>
44+
/// Validates an argument-dependent condition, throwing an exception if the check fails.
45+
/// </summary>
46+
/// <param name="condition">The condition to validate; this method will throw an <see cref="ArgumentException"/> if this is false.</param>
47+
/// <param name="paramName">The name of the parameter being validated. May be null.</param>
48+
/// <param name="messageFunc">A func that returns the message to use in the exception, if one is thrown.</param>
49+
internal static void CheckArgument([DoesNotReturnIf(false)] bool condition, string paramName, Func<string> messageFunc)
50+
{
51+
if (!condition)
52+
{
53+
throw new ArgumentException(messageFunc(), paramName);
54+
}
55+
}
56+
4357
/// <summary>
4458
/// Validates an argument-dependent condition, throwing an exception if the check fails.
4559
/// </summary>

0 commit comments

Comments
 (0)