Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Date primitive (#22)
* Add support for Date primitive * Improve Code Coverage * Removed constructor from DateOnlyConverter and MapDateOnlyConverter Removed System.Runtime references Added Null forgiving operator in unit tests * Updated the converter classes and unit test to remove null references warnings * Updated comments and string null check Co-authored-by: NancyGargMS <v-nancygarg@DESKTOP-OPTHJ0J>
- Loading branch information
1 parent
dea17cb
commit b2f3765
Showing
22 changed files
with
480 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Generator.CustomModels; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Converts an RFC 3339 Date into a DateOnly. | ||
/// </summary> | ||
public class DateOnlyConverter : JsonConverter<DateOnly> | ||
{ | ||
private readonly string serializationFormat = "yyyy-MM-dd"; | ||
/// <inheritdoc/> | ||
public override DateOnly Read(ref Utf8JsonReader reader, | ||
Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var value = reader.GetString(); | ||
|
||
if(string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return new DateOnly(); | ||
} | ||
return DateOnly.Parse(value); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, DateOnly value, | ||
JsonSerializerOptions options) | ||
=> writer.WriteStringValue(value.ToString(serializationFormat)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Generator.TemplateProject/Custom/MapDateOnlyConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Generator.CustomModels; | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Converts a map of RFC 3339 into an <see cref="IDictionary{String, DateOnly}"/>. | ||
/// </summary> | ||
public class MapDateOnlyConverter : JsonConverter<IDictionary<string, DateOnly>> | ||
{ | ||
private readonly string serializationFormat = "yyyy-MM-dd"; | ||
/// <inheritdoc/> | ||
public override IDictionary<string, DateOnly> Read(ref Utf8JsonReader reader, | ||
Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartObject) | ||
{ | ||
throw new JsonException("First token was not the start of a JSON object."); | ||
} | ||
|
||
var dictionary = new Dictionary<string, DateOnly>(); | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
return dictionary; | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
var key = reader.GetString(); | ||
|
||
reader.Read(); | ||
if (reader.TokenType == JsonTokenType.Null || string.IsNullOrWhiteSpace(key)) | ||
{ | ||
continue; | ||
} | ||
|
||
var value = reader.GetString(); | ||
|
||
if(!string.IsNullOrWhiteSpace(value)) | ||
{ | ||
var parsedValue = DateOnly.Parse(value); | ||
dictionary.Add(key, parsedValue); | ||
} | ||
} | ||
|
||
throw new JsonException("Final token was not the end of a JSON object."); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, IDictionary<string, DateOnly> dictionary, | ||
JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
foreach ((string key, DateOnly value) in dictionary) | ||
{ | ||
writer.WritePropertyName(options.PropertyNamingPolicy?.ConvertName(key) ?? key); | ||
writer.WriteStringValue(value.ToString(serializationFormat)); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Microsoft.DigitalWorkplace.DigitalTwins.Models.Generator; | ||
|
||
internal class DateOnlyProperty : Property | ||
{ | ||
internal DateOnlyProperty(DTNamedEntityInfo entity, ModelGeneratorOptions options) : base(options) | ||
{ | ||
Name = entity.Name; | ||
JsonName = entity.Name; | ||
Obsolete = entity.IsObsolete(); | ||
Type = "DateOnly?"; | ||
} | ||
|
||
internal override void WriteTo(StreamWriter streamWriter) | ||
{ | ||
streamWriter.WriteLine($"{indent}{indent}[JsonConverter(typeof(DateOnlyConverter))]"); | ||
base.WriteTo(streamWriter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Generator.Tests.Generated; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
|
||
/// <summary> | ||
/// Converts an RFC 3339 Date into a DateOnly. | ||
/// </summary> | ||
public class DateOnlyConverter : JsonConverter<DateOnly> | ||
{ | ||
private readonly string serializationFormat = "yyyy-MM-dd"; | ||
/// <inheritdoc/> | ||
public override DateOnly Read(ref Utf8JsonReader reader, | ||
Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var value = reader.GetString(); | ||
|
||
if(string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return new DateOnly(); | ||
} | ||
return DateOnly.Parse(value); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void Write(Utf8JsonWriter writer, DateOnly value, | ||
JsonSerializerOptions options) | ||
=> writer.WriteStringValue(value.ToString(serializationFormat)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/Generator.Tests.Generated/Generator.Tests.Generated.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.