Skip to content

Commit e457940

Browse files
committed
stringify field value with culture-invariant or round-trip format
1 parent 4f15126 commit e457940

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MIT
33
using System;
44
using System.Collections.Generic;
5+
using System.Globalization;
56
using System.Linq;
67
using System.Reflection;
78
using System.Runtime.InteropServices;
@@ -339,9 +340,21 @@ GeneratorOptions options
339340
);
340341

341342
if (valueDeclaration == null) {
343+
var stringifiedValue = val switch {
344+
string s => s,
345+
DateTime dt => dt.ToString("o"),
346+
DateTimeOffset dto => dto.ToString("o"),
347+
IFormattable formattable => formattable.ToString(
348+
format: null,
349+
formatProvider: CultureInfo.InvariantCulture // TODO: specific culture
350+
),
351+
null => "null",
352+
_ => val.ToString(),
353+
};
354+
342355
sb
343356
.Append("; // = \"")
344-
.Append(CSharpFormatter.EscapeString((val ?? "null").ToString(), escapeDoubleQuote: true))
357+
.Append(CSharpFormatter.EscapeString(stringifiedValue, escapeDoubleQuote: true))
345358
.Append('"');
346359
}
347360
else {

tests/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.MemberDeclaration.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,25 @@ public class NonPrimitiveValueTypes {
242242
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F3 = System.DateTimeOffset.MinValue;")] public static readonly DateTimeOffset F3 = default(DateTimeOffset);
243243
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F4 = System.DateTimeOffset.MinValue;")] public static readonly DateTimeOffset F4 = DateTimeOffset.MinValue;
244244
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F5 = System.DateTimeOffset.MaxValue;")] public static readonly DateTimeOffset F5 = DateTimeOffset.MaxValue;
245-
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F6; // = \"2018/10/31 21:00:00 +09:00\"")] public static readonly DateTimeOffset F6 = new DateTimeOffset(2018, 10, 31, 21, 0, 0, 0, TimeSpan.FromHours(+9.0));
245+
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F6; // = \"2018-10-31T21:00:00.0000000+09:00\"")] public static readonly DateTimeOffset F6 = new DateTimeOffset(2018, 10, 31, 21, 0, 0, 0, TimeSpan.FromHours(+9.0));
246246

247247
// XXX: System.Threading.CancellationToken.None is a property
248248
[MemberDeclarationTestCase("public static readonly System.Threading.CancellationToken F7 = default(System.Threading.CancellationToken);")] public static readonly CancellationToken F7 = CancellationToken.None;
249249
[MemberDeclarationTestCase("public static readonly System.Threading.CancellationToken F8 = default(System.Threading.CancellationToken);")] public static readonly CancellationToken F8 = default(CancellationToken);
250250
[MemberDeclarationTestCase("public static readonly System.Threading.CancellationToken F9 = default;", UseDefaultLiteral = true)] public static readonly CancellationToken F9 = default(CancellationToken);
251251
}
252252

253+
public class NonPrimitiveValueTypes_Stringification {
254+
// DateTimeOffset
255+
[MemberDeclarationTestCase("public static readonly System.DateTimeOffset F_DateTimeOffset; // = \"2018-10-31T21:00:00.0000000+09:00\"")] public static readonly DateTimeOffset F_DateTimeOffset = new DateTimeOffset(2018, 10, 31, 21, 0, 0, 0, TimeSpan.FromHours(+9.0));
256+
257+
// DateTime
258+
[MemberDeclarationTestCase("public static readonly System.DateTime F_DateTime; // = \"2018-10-31T21:00:00.0000000Z\"")] public static readonly DateTime F_DateTime = new DateTime(2018, 10, 31, 21, 0, 0, 0, DateTimeKind.Utc);
259+
260+
// IFormattable
261+
[MemberDeclarationTestCase("public static readonly System.TimeSpan F_TimeSpan; // = \"1.23:45:06.7890000\"")] public static readonly TimeSpan F_TimeSpan = new TimeSpan(1, 23, 45, 6, 789);
262+
}
263+
253264
namespace NonPrimitiveValueTypes_FieldOfDeclaringType {
254265
public struct S1 {
255266
[MemberDeclarationTestCase("public static readonly S1 Empty; // = \"foo\"", MemberWithNamespace = false)]

0 commit comments

Comments
 (0)