Skip to content

Add ToString methods for better enum string conversion #3458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 103 additions & 10 deletions sdk/src/Core/Amazon.Auth/AccessControlPolicy/ConditionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static partial class ConditionFactory
/// <summary>
/// Enumeration of the supported ways an ARN comparison can be evaluated.
/// </summary>
public enum ArnComparisonType
public enum ArnComparisonType
{
/// <summary>Exact matching</summary>
ArnEquals,
Expand Down Expand Up @@ -177,7 +177,7 @@ public enum NumericComparisonType
/// <summary>
/// Enumeration of the supported ways a string comparison can be evaluated.
/// </summary>
public enum StringComparisonType
public enum StringComparisonType
{
/// <summary>
/// Case-sensitive exact string matching
Expand Down Expand Up @@ -220,9 +220,9 @@ public enum StringComparisonType
/// <param name="type">The type of comparison to perform.</param>
/// <param name="value">The second ARN to compare against. When using ArnLike or ArnNotLike this may contain the
/// multi-character wildcard (*) or the single-character wildcard</param>
public static Condition NewCondition(ArnComparisonType type, string key, string value)
public static Condition NewCondition(ArnComparisonType type, string key, string value)
{
return new Condition(type.ToString(), key, value);
return new Condition(ToString(type), key, value);
}

/// <summary>
Expand All @@ -234,7 +234,7 @@ public static Condition NewCondition(ArnComparisonType type, string key, string
/// <param name="value">The boolean to compare against.</param>
public static Condition NewCondition(string key, bool value)
{
return new Condition("Bool", key, value.ToString().ToLowerInvariant());
return new Condition("Bool", key, value ? "true" : "false");
}

/// <summary>
Expand All @@ -251,7 +251,7 @@ public static Condition NewCondition(string key, bool value)
[Obsolete("Invoking this method results in non-UTC DateTimes not being marshalled correctly. Use NewConditionUtc instead.", false)]
public static Condition NewCondition(DateComparisonType type, DateTime date)
{
return new Condition(type.ToString(), CURRENT_TIME_CONDITION_KEY, date.ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture));
return new Condition(ToString(type), CURRENT_TIME_CONDITION_KEY, date.ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture));
}

/// <summary>
Expand All @@ -265,7 +265,7 @@ public static Condition NewCondition(DateComparisonType type, DateTime date)
/// <param name="date">The date to compare against.</param>
public static Condition NewConditionUtc(DateComparisonType type, DateTime date)
{
return new Condition(type.ToString(), CURRENT_TIME_CONDITION_KEY, date.ToUniversalTime().ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture));
return new Condition(ToString(type), CURRENT_TIME_CONDITION_KEY, date.ToUniversalTime().ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture));
}

/// <summary>
Expand Down Expand Up @@ -298,7 +298,7 @@ public static Condition NewIpAddressCondition(string ipAddressRange)
/// <param name="ipAddressRange">The CIDR IP range involved in the policy condition.</param>
public static Condition NewCondition(IpAddressComparisonType type, string ipAddressRange)
{
return new Condition(type.ToString(), SOURCE_IP_CONDITION_KEY, ipAddressRange);
return new Condition(ToString(type), SOURCE_IP_CONDITION_KEY, ipAddressRange);
}

/// <summary>
Expand All @@ -310,7 +310,7 @@ public static Condition NewCondition(IpAddressComparisonType type, string ipAddr
/// <param name="value">The second number to compare against.</param>
public static Condition NewCondition(NumericComparisonType type, string key, string value)
{
return new Condition(type.ToString(), key, value);
return new Condition(ToString(type), key, value);
}

/// <summary>
Expand All @@ -329,7 +329,7 @@ public static Condition NewCondition(NumericComparisonType type, string key, str
/// </param>
public static Condition NewCondition(StringComparisonType type, string key, string value)
{
return new Condition(type.ToString(), key, value);
return new Condition(ToString(type), key, value);
}

/// <summary>
Expand Down Expand Up @@ -376,5 +376,98 @@ public static Condition NewSecureTransportCondition()
{
return NewCondition(SECURE_TRANSPORT_CONDITION_KEY, true);
}

private static string ToString(ArnComparisonType type)
{
switch (type)
{
case ArnComparisonType.ArnEquals:
return nameof(ArnComparisonType.ArnEquals);
case ArnComparisonType.ArnLike:
return nameof(ArnComparisonType.ArnLike);
case ArnComparisonType.ArnNotEquals:
return nameof(ArnComparisonType.ArnNotEquals);
case ArnComparisonType.ArnNotLike:
return nameof(ArnComparisonType.ArnNotLike);
default:
return type.ToString();
}
}

private static string ToString(DateComparisonType type)
{
switch (type)
{
case DateComparisonType.DateEquals:
return nameof(DateComparisonType.DateEquals);
case DateComparisonType.DateGreaterThan:
return nameof(DateComparisonType.DateGreaterThan);
case DateComparisonType.DateGreaterThanEquals:
return nameof(DateComparisonType.DateGreaterThanEquals);
case DateComparisonType.DateLessThan:
return nameof(DateComparisonType.DateLessThan);
case DateComparisonType.DateLessThanEquals:
return nameof(DateComparisonType.DateLessThanEquals);
case DateComparisonType.DateNotEquals:
return nameof(DateComparisonType.DateNotEquals);
default:
return type.ToString();
}
}

private static string ToString(IpAddressComparisonType type)
{
switch (type)
{
case IpAddressComparisonType.IpAddress:
return nameof(IpAddressComparisonType.IpAddress);
case IpAddressComparisonType.NotIpAddress:
return nameof(IpAddressComparisonType.NotIpAddress);
default:
return type.ToString();
}
}

private static string ToString(NumericComparisonType type)
{
switch (type)
{
case NumericComparisonType.NumericEquals:
return nameof(NumericComparisonType.NumericEquals);
case NumericComparisonType.NumericGreaterThan:
return nameof(NumericComparisonType.NumericGreaterThan);
case NumericComparisonType.NumericGreaterThanEquals:
return nameof(NumericComparisonType.NumericGreaterThanEquals);
case NumericComparisonType.NumericLessThan:
return nameof(NumericComparisonType.NumericLessThan);
case NumericComparisonType.NumericLessThanEquals:
return nameof(NumericComparisonType.NumericLessThanEquals);
case NumericComparisonType.NumericNotEquals:
return nameof(NumericComparisonType.NumericNotEquals);
default:
return type.ToString();
}
}

private static string ToString(StringComparisonType type)
{
switch (type)
{
case StringComparisonType.StringEquals:
return nameof(StringComparisonType.StringEquals);
case StringComparisonType.StringEqualsIgnoreCase:
return nameof(StringComparisonType.StringEqualsIgnoreCase);
case StringComparisonType.StringLike:
return nameof(StringComparisonType.StringLike);
case StringComparisonType.StringNotEquals:
return nameof(StringComparisonType.StringNotEquals);
case StringComparisonType.StringNotEqualsIgnoreCase:
return nameof(StringComparisonType.StringNotEqualsIgnoreCase);
case StringComparisonType.StringNotLike:
return nameof(StringComparisonType.StringNotLike);
default:
return type.ToString();
}
}
}
}
52 changes: 52 additions & 0 deletions sdk/test/UnitTests/Custom/PolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Amazon.Auth.AccessControlPolicy;
using AWSSDK_DotNet.IntegrationTests.Utils;
using static Amazon.Auth.AccessControlPolicy.ConditionFactory;

namespace AWSSDK_DotNet.UnitTests
{
Expand Down Expand Up @@ -102,5 +104,55 @@ public void CheckIfStatementExists_DoesCaseInsensitiveComparisonOnActions()

Assert.IsTrue(result);
}

[TestMethod]
public void LookForArnComparisonTypeChanges()
{
var expectedHash = "93F200C066AF797AE2A923313D16DD1737AB26D8041C03F68E2BC9FAB03F464A";
AssertExtensions.AssertEnumUnchanged(
typeof(ArnComparisonType),
expectedHash,
"The Amazon.Auth.AccessControlPolicy.ConditionFactory.ToString(ArnComparisonType) method implementation may need to be updated.");
}

[TestMethod]
public void LookForDateComparisonTypeChanges()
{
var expectedHash = "929F27F8B4A0619A30D90F35429690BE334B14D4881EF47311FDEB5696D27DEF";
AssertExtensions.AssertEnumUnchanged(
typeof(DateComparisonType),
expectedHash,
"The Amazon.Auth.AccessControlPolicy.ConditionFactory.ToString(DateComparisonType) method implementation may need to be updated.");
}

[TestMethod]
public void LookForIpAddressComparisonTypeChanges()
{
var expectedHash = "440711FCA8408753BAEC4D1ECA39F813477A43DCA988924E0260A28359F53B78";
AssertExtensions.AssertEnumUnchanged(
typeof(IpAddressComparisonType),
expectedHash,
"The Amazon.Auth.AccessControlPolicy.ConditionFactory.ToString(DateComparisonType) method implementation may need to be updated.");
}

[TestMethod]
public void LookForNumericComparisonTypeChanges()
{
var expectedHash = "15808022DE81B0A1C62DBB9238EA542B891EDE6840278508C1FBFC4A6ACC829D";
AssertExtensions.AssertEnumUnchanged(
typeof(NumericComparisonType),
expectedHash,
"The Amazon.Auth.AccessControlPolicy.ConditionFactory.ToString(DateComparisonType) method implementation may need to be updated.");
}

[TestMethod]
public void LookForStringComparisonTypeChanges()
{
var expectedHash = "8466438D83763A3ADE2161964A9C3A68E247BC576A40956D5295382C74D8FB22";
AssertExtensions.AssertEnumUnchanged(
typeof(StringComparisonType),
expectedHash,
"The Amazon.Auth.AccessControlPolicy.ConditionFactory.ToString(DateComparisonType) method implementation may need to be updated.");
}
}
}