Skip to content

Commit 2f8aaf3

Browse files
Mpdreamzrusscam
authored andcommitted
remove hardcoded \r\n from code generator strings (#4158)
1 parent b63a4f3 commit 2f8aaf3

File tree

12 files changed

+57
-48
lines changed

12 files changed

+57
-48
lines changed

src/Auxiliary/Elasticsearch.Net.VirtualizedCluster/Audit/Auditor.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ public async Task<Auditor> TraceElasticsearchExceptionOnResponse(ClientCall call
123123

124124
Action call = () => { Response = _cluster.ClientCall(callTrace?.RequestOverrides); };
125125
call();
126-
126+
127127
if (Response.ApiCall.Success) throw new Exception("Expected call to not be valid");
128-
128+
129129
var exception = Response.ApiCall.OriginalException as ElasticsearchClientException;
130130
if (exception == null) throw new Exception("OriginalException on response is not expected ElasticsearchClientException");
131131
assert(exception);
@@ -175,9 +175,10 @@ public async Task<Auditor> TraceUnexpectedException(ClientCall callTrace, Action
175175

176176
private Auditor AssertAuditTrails(ClientCall callTrace, int nthCall)
177177
{
178+
var nl = Environment.NewLine;
178179
if (AuditTrail.Count != AsyncAuditTrail.Count)
179-
throw new Exception($"{nthCall} has a mismatch between sync and async. \r\nasync:{AuditTrail}\r\nsync:{AsyncAuditTrail}");
180-
180+
throw new Exception($"{nthCall} has a mismatch between sync and async. {nl}async:{AuditTrail}{nl}sync:{AsyncAuditTrail}");
181+
181182
AssertTrailOnResponse(callTrace, AuditTrail, true, nthCall);
182183
AssertTrailOnResponse(callTrace, AuditTrail, false, nthCall);
183184

@@ -226,7 +227,7 @@ private static void AssertTrailOnResponse(ClientCall callTrace, List<Elasticsear
226227
var actualAuditTrail = auditTrail.Aggregate(new StringBuilder(Environment.NewLine),
227228
(sb, a) => sb.AppendLine($"-> {a}"),
228229
sb => sb.ToString());
229-
230+
230231
var traceEvents =callTrace.Select(c => c.Event).ToList();
231232
var auditEvents = auditTrail.Select(a => a.Event).ToList();
232233
if (!traceEvents.SequenceEqual(auditEvents))
@@ -239,19 +240,19 @@ private static void AssertTrailOnResponse(ClientCall callTrace, List<Elasticsear
239240
var nthAuditTrailItem = (i + 1).ToOrdinal();
240241
var because = $"thats the {{0}} specified on the {nthAuditTrailItem} item in the {nthClientCall} client call's {typeOfTrail}";
241242
var c = callTrace[i];
242-
if (audit.Event != c.Event)
243+
if (audit.Event != c.Event)
243244
throw new Exception(string.Format(because, "event"));
244-
if (c.Port.HasValue && audit.Node.Uri.Port != c.Port.Value)
245+
if (c.Port.HasValue && audit.Node.Uri.Port != c.Port.Value)
245246
throw new Exception(string.Format(because, "port"));
246-
247+
247248
c.SimpleAssert?.Invoke(audit);
248249
c.AssertWithBecause?.Invoke(string.Format(because, "custom assertion"), audit);
249250
}
250251

251-
if (callTrace.Count != auditTrail.Count)
252+
if (callTrace.Count != auditTrail.Count)
252253
throw new Exception($"callTrace has {callTrace.Count} items. Actual auditTrail {actualAuditTrail}");
253254
}
254-
255+
255256
private static TException TryCall<TException>(Action call, Action<TException> assert) where TException : ElasticsearchClientException
256257
{
257258
TException exception = null;

src/CodeGeneration/ApiGenerator/Domain/Code/HighLevel/Requests/Constructor.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using ApiGenerator.Configuration;
45
using ApiGenerator.Domain.Specification;
@@ -7,7 +8,7 @@ namespace ApiGenerator.Domain.Code.HighLevel.Requests
78
{
89
public class Constructor
910
{
10-
private const string Indent = "\r\n\t\t";
11+
private static readonly string Indent = $"{Environment.NewLine}\t\t";
1112
public string AdditionalCode { get; set; } = string.Empty;
1213
public bool Parameterless { get; set; }
1314
public string Body { get; set; }
@@ -28,15 +29,15 @@ public static IEnumerable<Constructor> RequestConstructors(CsharpNames names, Ur
2829
var generateGeneric = CodeConfiguration.GenericOnlyInterfaces.Contains(names.RequestInterfaceName) || !inheritsFromPlainRequestBase;
2930
return GenerateConstructors(url, inheritsFromPlainRequestBase, generateGeneric, names.RequestName, generic);
3031
}
31-
32-
private static string FirstGeneric(string fullGenericString) =>
32+
33+
private static string FirstGeneric(string fullGenericString) =>
3334
fullGenericString?.Replace("<", "").Replace(">", "").Split(",").First().Trim();
3435

3536
private static IEnumerable<Constructor> GenerateConstructors(
3637
UrlInformation url,
3738
bool inheritsFromPlainRequestBase,
3839
bool generateGeneric,
39-
string typeName,
40+
string typeName,
4041
string generic
4142
)
4243
{
@@ -64,8 +65,8 @@ string generic
6465
ctors.AddRange(from path in paths.Where(path => path.HasResolvableArguments)
6566
let baseArgs = path.AutoResolveBaseArguments(generic)
6667
let constructorArgs = path.AutoResolveConstructorArguments
67-
let baseOrThis = inheritsFromPlainRequestBase
68-
? "this"
68+
let baseOrThis = inheritsFromPlainRequestBase
69+
? "this"
6970
: "base"
7071
let generated = $"public {typeName}({constructorArgs}) : {baseOrThis}({baseArgs})"
7172
select new Constructor
@@ -84,7 +85,7 @@ string generic
8485
{
8586
Parameterless = string.IsNullOrEmpty(docPathConstArgs),
8687
Generated = $"public {typeName}({docPathConstArgs}) : this({docPathBaseArgs})",
87-
88+
8889
AdditionalCode = $"partial void DocumentFromPath({generic} document);",
8990
Description = docPath.GetXmlDocs(Indent, skipResolvable: true, documentConstructor: true),
9091
Body = "=> DocumentFromPath(documentWithId);"

src/CodeGeneration/ApiGenerator/Generator/CodeGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static string Property(string type, string name, string key, string sette
1919
if (!string.IsNullOrWhiteSpace(obsolete)) A($"[Obsolete(\"Scheduled to be removed in 7.0, {obsolete}\")]");
2020

2121
A(PropertyGenerator(type, name, key, setter));
22-
return string.Join("\r\n\t\t", components);
22+
return string.Join($"{Environment.NewLine}\t\t", components);
2323

2424
void A(string s)
2525
{
@@ -36,7 +36,7 @@ public static string Constructor(Constructor c)
3636
A(generated);
3737
if (!c.Body.IsNullOrEmpty()) A(c.Body);
3838
if (!c.AdditionalCode.IsNullOrEmpty()) A(c.AdditionalCode);
39-
return string.Join("\r\n\t\t", components);
39+
return string.Join($"{Environment.NewLine}\t\t", components);
4040

4141
void A(string s)
4242
{

src/CodeGeneration/ApiGenerator/Views/HighLevel/Descriptors/XmlDocs.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@inherits ApiGenerator.CodeTemplatePage<List<string>>
77
@{
88
List<string> summary = Model;
9-
var description = summary.Count == 1 ? summary.First() : string.Join("\r\n\t\t", summary.Select(d => "/// " + d));
9+
var description = summary.Count == 1 ? summary.First() : string.Join($"{Environment.NewLine}\t\t", summary.Select(d => "/// " + d));
1010
if (summary.Count == 1)
1111
{
1212
<text> ///<summary>@Raw(description)</summary></text>

src/CodeGeneration/ApiGenerator/Views/LowLevel/Enums.Generated.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
var enumCsharp = string.Format("[EnumMember(Value = \"{0}\")] {1}{2}", value, enumValue, i.HasValue ? " = 1 << " + i.Value : null);
1919
if (GlobalOverrides.ObsoleteEnumMembers.TryGetValue(enumName, out var d) && d.TryGetValue(value, out var obsolete))
2020
{
21-
return string.Format("[Obsolete(\"{0}\")]\r\n\t\t{1}", obsolete, enumCsharp);
21+
return string.Format("[Obsolete(\"{0}\")]{2}\t\t{1}", obsolete, enumCsharp, Environment.NewLine);
2222
}
2323
return enumCsharp;
2424
}
@@ -62,7 +62,7 @@ namespace Elasticsearch.Net
6262
<text>
6363
@(isFlag ? "[Flags, StringEnum]" : "[StringEnum]")public enum @e.Name
6464
{
65-
@Raw(string.Join(",\r\n\t\t", e.Options.OrderBy(s => s == "_all" ? 1 : 0).Select((s, i) => CreateEnum(e.Name, s, isFlag ? (int?)i : null))))
65+
@Raw(string.Join(","+ Environment.NewLine + "\t\t", e.Options.OrderBy(s => s == "_all" ? 1 : 0).Select((s, i) => CreateEnum(e.Name, s, isFlag ? (int?)i : null))))
6666
}</text>
6767
}
6868

@@ -124,7 +124,7 @@ namespace Elasticsearch.Net
124124
{
125125
<text> switch (enumValue)
126126
{
127-
@Raw(string.Join("\r\n\t\t\t\t", e.Options.Select(o => CreateCase(e.Name, o))))
127+
@Raw(string.Join(Environment.NewLine + "\t\t\t\t", e.Options.Select(o => CreateCase(e.Name, o))))
128128
}
129129
throw new ArgumentException($"'{enumValue.ToString()}' is not a valid value for enum '@(e.Name)'");
130130
}</text>

src/CodeGeneration/DocGenerator/AsciiDoc/GeneratedAsciidocVisitor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ public override void VisitDocument(Document document)
7575
var originalFile = Regex.Replace(_source.FullName.Replace("\\", "/"), @"^(.*Tests/)",
7676
$"{github}/tree/{Program.BranchName}/src/Tests/Tests/");
7777

78+
var eol = Environment.NewLine;
7879
_newDocument.Insert(0, new Comment
7980
{
8081
Style = CommentStyle.MultiLine,
81-
Text = $"IMPORTANT NOTE\r\n==============\r\nThis file has been generated from {originalFile}. \r\n" +
82-
"If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,\r\n" +
82+
Text = $"IMPORTANT NOTE{eol}=============={eol}This file has been generated from {originalFile}. {eol}" +
83+
$"If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,{eol}" +
8384
"please modify the original csharp file found at the link and submit the PR with that change. Thanks!"
8485
});
8586

src/CodeGeneration/DocGenerator/AsciiDoc/RawAsciidocVisitor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public override void VisitDocument(Document document)
3131
var github = "https://github.com/elastic/elasticsearch-net";
3232
var originalFile = Regex.Replace(_source.FullName.Replace("\\", "/"),
3333
@"^(.*Tests/)", $"{github}/tree/{Program.BranchName}/src/Tests/Tests/");
34+
var eol = Environment.NewLine;
3435
document.Insert(0, new Comment
3536
{
3637
Style = CommentStyle.MultiLine,
37-
Text = $"IMPORTANT NOTE\r\n==============\r\nThis file has been generated from {originalFile}. \r\n" +
38-
"If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,\r\n" +
38+
Text = $"IMPORTANT NOTE{eol}=============={eol}This file has been generated from {originalFile}. {eol}" +
39+
$"If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,{eol}" +
3940
"please modify the original csharp file found at the link and submit the PR with that change. Thanks!"
4041
});
4142

src/CodeGeneration/DocGenerator/XmlDocs/XmlDocsVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class XmlDocsVisitor : Visitor
1717
{
1818
// AsciiDocNet does not currently have a type for list item continuations, so mimic here
1919
// for the moment
20-
private const string ListItemContinuation = "\r\n+\r\n";
20+
private static readonly string ListItemContinuation = Environment.NewLine + "+" + Environment.NewLine;
2121
private readonly Type _type;
2222
private LabeledListItem _labeledListItem;
2323

src/Elasticsearch.Net/Responses/ResponseStatics.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public static string DebugInformationBuilder(IApiCallDetails r, StringBuilder sb
3030

3131
var response = r.ResponseBodyInBytes?.Utf8String() ?? ResponseAlreadyCaptured;
3232
var request = r.RequestBodyInBytes?.Utf8String() ?? RequestAlreadyCaptured;
33-
sb.AppendLine($"# Request:\r\n{request}");
34-
sb.AppendLine($"# Response:\r\n{response}");
33+
sb.AppendLine($"# Request:{Environment.NewLine}{request}");
34+
sb.AppendLine($"# Response:{Environment.NewLine}{response}");
3535

3636
return sb.ToString();
3737
}
@@ -42,7 +42,7 @@ public static void DebugAuditTrailExceptions(List<Audit> auditTrail, StringBuild
4242

4343
var auditExceptions = auditTrail.Select((audit, i) => new { audit, i }).Where(a => a.audit.Exception != null);
4444
foreach (var a in auditExceptions)
45-
sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:\r\n{a.audit.Exception}");
45+
sb.AppendLine($"# Audit exception in step {a.i + 1} {a.audit.Event.GetStringValue()}:{Environment.NewLine}{a.audit.Exception}");
4646
}
4747

4848
public static void DebugAuditTrail(List<Audit> auditTrail, StringBuilder sb)

src/Tests/Tests.Core/Extensions/DiffExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using System.Text;
34
using System.Text.RegularExpressions;
@@ -81,8 +82,9 @@ private static string CreateDiff(string expected, string actual, string message,
8182

8283
private static string AppendCsharpApproximation(string expected, string actual, string diff)
8384
{
84-
diff += "\r\n C# approximation of actual ------ ";
85-
diff += "\r\n new ";
85+
var eol = Environment.NewLine;
86+
diff += $"{eol} C# approximation of actual ------ ";
87+
diff += $"{eol} new ";
8688
var approx = Regex.Replace(actual, @"^(?=.*:.*)[^:]+:", (s) => s
8789
.Value.Replace("\"", "")
8890
.Replace(":", " =")
@@ -93,8 +95,8 @@ private static string AppendCsharpApproximation(string expected, string actual,
9395
approx = Regex.Replace(approx, @"^\s*\],?.*$", s => s.Value.Replace("]", "}"), RegexOptions.Multiline);
9496
diff += approx + ";";
9597

96-
diff += "\r\n C# approximation of expected ------ ";
97-
diff += "\r\n new ";
98+
diff += $"{eol} C# approximation of expected ------ ";
99+
diff += $"{eol} new ";
98100
approx = Regex.Replace(expected, @"^(?=.*:.*)[^:]+:", (s) => s
99101
.Value.Replace("\"", "")
100102
.Replace(":", " =")

0 commit comments

Comments
 (0)