-
Notifications
You must be signed in to change notification settings - Fork 382
[wip] add naming normalization for dates #11664
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
base: main
Are you sure you want to change the base?
Changes from all commits
42dd88a
1582644
9b0729d
8bde832
875ac14
aa00bd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,7 +63,10 @@ public sealed class ParameterProvider : IEquatable<ParameterProvider> | |
| public ParameterProvider(InputParameter inputParameter) | ||
| { | ||
| InputParameter = inputParameter; | ||
| Name = inputParameter.Name; | ||
| Name = inputParameter is InputMethodParameter && !inputParameter.IsExactName | ||
| && inputParameter.Type.IsDateTimeInputType() | ||
| ? inputParameter.Name.NormalizeDateTimeSuffix() | ||
| : inputParameter.Name; | ||
|
Comment on lines
+66
to
+69
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot validate this feedback
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validated and fixed: normalized non-exact method parameters now retain the original-name mapping during back-compat restoration. Added a regression test in 875ac14.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot wouldn't a simpler solution would be to simply have the back compat validate both the parameter name or the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Description = DocHelpers.GetFormattableDescription(inputParameter.Summary, inputParameter.Doc) ?? FormattableStringHelpers.Empty; | ||
| var type = CodeModelGenerator.Instance.TypeFactory.CreateCSharpType(inputParameter.Type) ?? throw new InvalidOperationException($"Failed to create CSharpType for {inputParameter.Type}"); | ||
| if (!inputParameter.IsRequired) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,27 @@ | |
| // Licensed under the MIT License. | ||
|
jorgerangel-msft marked this conversation as resolved.
|
||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Text; | ||
| using Microsoft.TypeSpec.Generator.Input; | ||
|
|
||
| namespace Microsoft.TypeSpec.Generator.Utilities | ||
| { | ||
| internal static class CSharpNameExtensions | ||
| { | ||
| private const string DateSuffix = "Date"; | ||
| private const string DateTimeSuffix = "DateTime"; | ||
| private const string FromName = "From"; | ||
| private const string LowercaseOnSuffix = "on"; | ||
| private const string OnSuffix = "On"; | ||
| private const string PointInTimeName = "PointInTime"; | ||
| private const string TimeStampSuffix = "TimeStamp"; | ||
| private const string TimeSuffix = "Time"; | ||
| private const string TimestampSuffix = "Timestamp"; | ||
| private const string ToName = "To"; | ||
| private const string AtSuffix = "At"; | ||
|
|
||
| private static readonly (string Source, string Replacement)[] _acronymRenamingRules = | ||
| [ | ||
| ("Ipv4", "IPv4"), | ||
|
|
@@ -20,9 +34,19 @@ private static readonly (string Source, string Replacement)[] _acronymRenamingRu | |
| ("Os", "OS") | ||
|
jorgerangel-msft marked this conversation as resolved.
|
||
| ]; | ||
|
|
||
| public static string NormalizeCSharpAcronyms(this string name) | ||
| private static readonly HashSet<string> _dateTimeNameExclusions = new(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| FromName, | ||
| ToName, | ||
| PointInTimeName | ||
| }; | ||
|
|
||
| public static string NormalizeCSharpAcronyms(this string name, bool normalizeDateTimeSuffix = false) | ||
| { | ||
| StringBuilder? normalizedName = null; | ||
| var suffixLength = normalizeDateTimeSuffix && !HasExcludedDateTimeNameComponent(name) | ||
| ? GetDateTimeSuffixLength(name) | ||
| : 0; | ||
| StringBuilder? normalizedName = suffixLength > 0 ? new(name.Length - suffixLength + OnSuffix.Length) : null; | ||
| int segmentStart = 0; | ||
| for (int index = 0; index < name.Length - 1; index++) | ||
| { | ||
|
|
@@ -53,10 +77,88 @@ public static string NormalizeCSharpAcronyms(this string name) | |
| return name; | ||
| } | ||
|
|
||
| normalizedName.Append(name, segmentStart, name.Length - segmentStart); | ||
| normalizedName.Append(name, segmentStart, name.Length - suffixLength - segmentStart); | ||
| if (suffixLength > 0) | ||
| { | ||
| normalizedName.Append(name.Length == suffixLength && char.IsLower(name[0]) ? LowercaseOnSuffix : OnSuffix); | ||
| } | ||
| return normalizedName.ToString(); | ||
| } | ||
|
|
||
| public static string NormalizeDateTimeSuffix(this string name) | ||
| { | ||
| if (HasExcludedDateTimeNameComponent(name)) | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| var suffixLength = GetDateTimeSuffixLength(name); | ||
| if (suffixLength == 0) | ||
| { | ||
| return name; | ||
| } | ||
|
|
||
| var prefix = name[..^suffixLength]; | ||
| var onSuffix = prefix.Length == 0 && char.IsLower(name[0]) ? LowercaseOnSuffix : OnSuffix; | ||
| return prefix + onSuffix; | ||
| } | ||
|
|
||
| private static bool HasExcludedDateTimeNameComponent(string name) | ||
| { | ||
| var lookup = _dateTimeNameExclusions.GetAlternateLookup<ReadOnlySpan<char>>(); | ||
| return (name.Length >= FromName.Length && lookup.Contains(name.AsSpan(0, FromName.Length))) || | ||
| (name.Length >= ToName.Length && lookup.Contains(name.AsSpan(0, ToName.Length))) || | ||
| (name.Length >= PointInTimeName.Length && lookup.Contains(name.AsSpan(^PointInTimeName.Length))); | ||
| } | ||
|
Comment on lines
+106
to
+112
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot we have "pointinttime" declared in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted the exclusions, including |
||
|
|
||
| private static int GetDateTimeSuffixLength(string name) | ||
| { | ||
| if (name.EndsWith(TimestampSuffix, StringComparison.Ordinal) || | ||
| name.EndsWith(TimeStampSuffix, StringComparison.Ordinal)) | ||
| { | ||
| return TimestampSuffix.Length; | ||
| } | ||
|
|
||
| if (name.Equals(TimestampSuffix, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return TimestampSuffix.Length; | ||
| } | ||
|
|
||
| if (name.Length > DateTimeSuffix.Length && name.EndsWith(DateTimeSuffix, StringComparison.Ordinal)) | ||
| { | ||
| return DateTimeSuffix.Length; | ||
| } | ||
|
|
||
| if (name.Length > TimeSuffix.Length && name.EndsWith(TimeSuffix, StringComparison.Ordinal)) | ||
| { | ||
| return TimeSuffix.Length; | ||
| } | ||
|
|
||
| if (name.Equals(DateSuffix, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return DateSuffix.Length; | ||
| } | ||
|
|
||
| if (name.EndsWith(DateSuffix, StringComparison.Ordinal)) | ||
| { | ||
| return DateSuffix.Length; | ||
| } | ||
|
|
||
| if (name.Length > AtSuffix.Length && name.EndsWith(AtSuffix, StringComparison.Ordinal)) | ||
| { | ||
| return AtSuffix.Length; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
Comment on lines
+114
to
+153
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot we should create const values for these strings.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted the date-time suffix and replacement strings into constants. aa00bd3 |
||
|
|
||
| public static bool IsDateTimeInputType(this InputType inputType) => inputType switch | ||
| { | ||
| InputDateTimeType => true, | ||
| InputPrimitiveType { Kind: InputPrimitiveTypeKind.PlainDate } => true, | ||
| InputNullableType nullableType => IsDateTimeInputType(nullableType.Type), | ||
| _ => false | ||
| }; | ||
| [return: NotNullIfNotNull(nameof(name))] | ||
| public static string? NormalizeCSharpUrlSuffix(this string? name) | ||
| => !string.IsNullOrEmpty(name) && name.EndsWith("Url", StringComparison.Ordinal) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.