|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Diagnostics; |
| 7 | +using System.Text; |
7 | 8 | using Azure.Provisioning.Expressions; |
8 | 9 | using Azure.Provisioning.Primitives; |
9 | 10 |
|
@@ -93,6 +94,109 @@ public virtual void Remove(Provisionable resource) |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
| 97 | + private static bool IsAsciiLetterOrDigit(char ch) => |
| 98 | + 'a' <= ch && ch <= 'z' || |
| 99 | + 'A' <= ch && ch <= 'Z' || |
| 100 | + '0' <= ch && ch <= '9'; |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Checks whether an name is a valid bicep identifier name comprised of |
| 104 | + /// letters, digits, and underscores. |
| 105 | + /// </summary> |
| 106 | + /// <param name="identifierName">The proposed identifier name.</param> |
| 107 | + /// <returns>Whether the name is a valid bicep identifier name.</returns> |
| 108 | + public static bool IsValidIdentifierName(string? identifierName) |
| 109 | + { |
| 110 | + if (string.IsNullOrEmpty(identifierName)) { return false; } |
| 111 | + if (char.IsDigit(identifierName![0])) { return false; } |
| 112 | + foreach (char ch in identifierName) |
| 113 | + { |
| 114 | + if (!IsAsciiLetterOrDigit(ch) && ch != '_') |
| 115 | + { |
| 116 | + return false; |
| 117 | + } |
| 118 | + } |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Validates whether a given bicep identifier name is correctly formed of |
| 124 | + /// letters, numbers, and underscores. |
| 125 | + /// </summary> |
| 126 | + /// <param name="identifierName">The proposed bicep identifier name.</param> |
| 127 | + /// <param name="paramName">Optional parameter name to use for exceptions.</param> |
| 128 | + /// <exception cref="ArgumentNullException">Throws if null.</exception> |
| 129 | + /// <exception cref="ArgumentException">Throws if empty or invalid.</exception> |
| 130 | + public static void ValidateIdentifierName(string? identifierName, string? paramName = default) |
| 131 | + { |
| 132 | + paramName ??= nameof(identifierName); |
| 133 | + if (identifierName is null) |
| 134 | + { |
| 135 | + throw new ArgumentNullException(paramName, $"{paramName} cannot be null."); |
| 136 | + } |
| 137 | + else if (identifierName.Length == 0) |
| 138 | + { |
| 139 | + throw new ArgumentException($"{paramName} cannot be empty.", paramName); |
| 140 | + } |
| 141 | + else if (char.IsDigit(identifierName[0])) |
| 142 | + { |
| 143 | + throw new ArgumentException($"{paramName} cannot start with a number: \"{identifierName}\"", paramName); |
| 144 | + } |
| 145 | + |
| 146 | + foreach (var ch in identifierName) |
| 147 | + { |
| 148 | + if (!IsAsciiLetterOrDigit(ch) && ch != '_') |
| 149 | + { |
| 150 | + throw new ArgumentException($"{paramName} should only contain letters, numbers, and underscores: \"{identifierName}\"", paramName); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Normalizes a proposed bicep identifier name. Any invalid characters |
| 157 | + /// will be replaced with underscores. |
| 158 | + /// </summary> |
| 159 | + /// <param name="identifierName">The proposed bicep identifier name.</param> |
| 160 | + /// <returns>A valid bicep identifier name.</returns> |
| 161 | + /// <exception cref="ArgumentNullException">Throws if null.</exception> |
| 162 | + /// <exception cref="ArgumentException">Throws if empty.</exception> |
| 163 | + public static string NormalizeIdentifierName(string? identifierName) |
| 164 | + { |
| 165 | + if (IsValidIdentifierName(identifierName)) |
| 166 | + { |
| 167 | + return identifierName!; |
| 168 | + } |
| 169 | + |
| 170 | + if (identifierName is null) |
| 171 | + { |
| 172 | + // TODO: This may be relaxed in the future to generate an automatic |
| 173 | + // name rather than throwing |
| 174 | + throw new ArgumentNullException(nameof(identifierName), $"{nameof(identifierName)} cannot be null."); |
| 175 | + } |
| 176 | + else if (identifierName.Length == 0) |
| 177 | + { |
| 178 | + throw new ArgumentException($"{nameof(identifierName)} cannot be empty.", nameof(identifierName)); |
| 179 | + } |
| 180 | + |
| 181 | + StringBuilder builder = new(identifierName.Length); |
| 182 | + |
| 183 | + // Digits are not allowed as the first character, so prepend an |
| 184 | + // underscore if the identifierName starts with a digit |
| 185 | + if (char.IsDigit(identifierName[0])) |
| 186 | + { |
| 187 | + builder.Append('_'); |
| 188 | + } |
| 189 | + |
| 190 | + foreach (char ch in identifierName) |
| 191 | + { |
| 192 | + // TODO: Consider opening this up to other naming strategies if |
| 193 | + // someone can do something more intelligent for their usage/domain |
| 194 | + builder.Append(IsAsciiLetterOrDigit(ch) ? ch : '_'); |
| 195 | + } |
| 196 | + |
| 197 | + return builder.ToString(); |
| 198 | + } |
| 199 | + |
96 | 200 | /// <inheritdoc/> |
97 | 201 | protected internal override void Validate(ProvisioningContext? context = null) |
98 | 202 | { |
|
0 commit comments