Skip to content
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

#1709 Generate C# TimeSpan as a DayJs duration #1714

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DateCodeGenerationTests
'myTimeSpan': { 'type': 'string', 'format': 'time-span' }
}
}";

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down Expand Up @@ -148,6 +148,26 @@ public async Task When_date_handling_is_dayjs_then_dayjs_property_is_generated_i
Assert.Contains("data[\"myDate\"] = this.myDate ? this.myDate.format('YYYY-MM-DD') : <any>undefined;", code);
}

[Fact]
public async Task When_date_handling_is_dayjs_then_duration_property_is_generated_in_class()
{
//// Arrange
var schema = await JsonSchema.FromJsonAsync(Json);

//// Act
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeStyle = TypeScriptTypeStyle.Class,
DateTimeType = TypeScriptDateTimeType.DayJS
});
var code = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains("myTimeSpan: dayjs.Duration", code);
Assert.Contains("this.myTimeSpan = _data[\"myTimeSpan\"] ? dayjs.duration(((val: string) => { const [days, rest] = val.split('.'); const [hours, minutes, seconds, milliseconds] = rest.split(/[:.]/); return dayjs.duration({ days: parseInt(days), hours: parseInt(hours), minutes: parseInt(minutes), seconds: parseInt(seconds), milliseconds: parseInt(milliseconds), }); })(_data[\"myTimeSpan\"].toString())) : <any>undefined;", code);
Assert.Contains("data[\"myTimeSpan\"] = this.myTimeSpan ? this.myTimeSpan.format('D.HH:mm:ss.SSS') : <any>undefined;", code);
}

[Fact]
public async Task When_date_handling_is_date_then_date_property_is_generated_in_class()
{
Expand All @@ -168,7 +188,7 @@ public async Task When_date_handling_is_date_then_date_property_is_generated_in_
Assert.Contains("data[\"myDate\"] = this.myDate ? formatDate(this.myDate) : <any>undefined;", code);
Assert.Contains("function formatDate(", code);
}

[Fact]
public async Task When_date_handling_is_date_then_date_property_is_generated_in_class_with_local_timezone_conversion()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private static object CreateModel(DataConversionParameters parameters)
IsNewableObject = IsNewableObject(parameters.Schema, parameters),
IsDate = IsDate(typeSchema.Format, parameters.Settings.DateTimeType),
IsDateTime = IsDateTime(typeSchema.Format, parameters.Settings.DateTimeType),
ConstructDateTimeWith = ConstructDateTimeWith(parameters.Value, typeSchema.Format, parameters.Settings.DateTimeType),

// Dictionary
IsDictionary = typeSchema.IsDictionary,
Expand Down Expand Up @@ -144,6 +145,11 @@ private static string GetStringToDateTime(DataConversionParameters parameters, J
return "DateTime.fromISO";

case TypeScriptDateTimeType.DayJS:
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "dayjs.duration";
}

return "dayjs";

default:
Expand Down Expand Up @@ -201,7 +207,7 @@ private static string GetDateTimeToString(DataConversionParameters parameters, J
case TypeScriptDateTimeType.DayJS:
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "format('d.hh:mm:ss.SSS')";
return "format('D.HH:mm:ss.SSS')";
}

return "toISOString()";
Expand Down Expand Up @@ -270,6 +276,17 @@ private static bool IsDateTime(string? format, TypeScriptDateTimeType type)
return false;
}

private static string ConstructDateTimeWith(string? value, string? format, TypeScriptDateTimeType type)
{
if (type == TypeScriptDateTimeType.DayJS &&
format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return $"((val: string) => {{ const [days, rest] = val.split('.'); const [hours, minutes, seconds, milliseconds] = rest.split(/[:.]/); return dayjs.duration({{ days: parseInt(days), hours: parseInt(hours), minutes: parseInt(minutes), seconds: parseInt(seconds), milliseconds: parseInt(milliseconds), }}); }})({value}.toString())";
}

return value + ".toString()";
}


private static bool IsDate(string? format, TypeScriptDateTimeType type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if ({{ Value }}) {
{% if IsDate -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateOnlyCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateOnlyCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
{% elsif IsDateTime -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
{{ Variable }} = {{ Value }} ? {{ StringToDateCode }}({{ ConstructDateTimeWith }}) : {% if HasDefaultValue %}{{ StringToDateCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
{% else -%}
{% if HasDefaultValue or NullValue != "undefined" -%}
{{ Variable }} = {{ Value }} !== undefined ? {{ Value }} : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private string ResolveString(JsonSchema schema, string? typeNameHint)

if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "dayjs.Dayjs";
return "dayjs.Duration";
}
}

Expand Down
Loading