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

Adding support for Unix time formatted dates in C# #948

Merged
merged 7 commits into from
Apr 22, 2016
Merged
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
3 changes: 2 additions & 1 deletion AutoRest/AutoRest.Core/ClientModel/KnownPrimaryType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum KnownPrimaryType
Boolean,
Credentials,
Uuid,
Base64Url
Base64Url,
UnixTime
}
}
51 changes: 50 additions & 1 deletion AutoRest/AutoRest.Core/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static string EscapeXmlComment(this string comment)
}

/// <summary>
/// Returns true is the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// Returns true if the type is a PrimaryType with KnownPrimaryType matching typeToMatch.
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
Expand All @@ -263,5 +263,54 @@ public static bool IsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
}
return false;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markcowl Mark, could you take a quick look at the changes I made in this last commit whenever it's convenient for you?

For context, MethodTemplateModel.GetSerializationSettingsReference() is getting too complex and failing a build rule for code complexity (and it's hard to read). I simplified this logic by adding methods to check if an IType either is a KnownPrimaryType or is a dictionary/sequence that is based on the KnownPrimaryType (previously these checks were repeated for each primary type that had special serialization behavior).

Tests are passing and the serialization is working the same as before. I'd like another set of eyes on it before I merge to make sure I kept the behavior the same with respect to the null checks. Thanks!

/// <summary>
/// Returns true if the <paramref name="type"/> is a PrimaryType with KnownPrimaryType matching <paramref name="typeToMatch"/>
/// or a DictionaryType with ValueType matching <paramref name="typeToMatch"/> or a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsOrContainsPrimaryType(this IType type, KnownPrimaryType typeToMatch)
{
if (type == null)
{
return false;
}

if (type.IsPrimaryType(typeToMatch) ||
type.IsDictionaryContainingType(typeToMatch) ||
type.IsSequenceContainingType(typeToMatch))
{
return true;
}
return false;
}

/// <summary>
/// Returns true if the <paramref name="type"/> is a DictionaryType with ValueType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsDictionaryContainingType(this IType type, KnownPrimaryType typeToMatch)
{
DictionaryType dictionaryType = type as DictionaryType;
PrimaryType dictionaryPrimaryType = dictionaryType?.ValueType as PrimaryType;
return dictionaryPrimaryType != null && dictionaryPrimaryType.IsPrimaryType(typeToMatch);
}

/// <summary>
/// Returns true if the <paramref name="type"/>is a SequenceType matching <paramref name="typeToMatch"/>
/// </summary>
/// <param name="type"></param>
/// <param name="typeToMatch"></param>
/// <returns></returns>
public static bool IsSequenceContainingType(this IType type, KnownPrimaryType typeToMatch)
{
SequenceType sequenceType = type as SequenceType;
PrimaryType sequencePrimaryType = sequenceType?.ElementType as PrimaryType;
return sequencePrimaryType != null && sequencePrimaryType.IsPrimaryType(typeToMatch);
}
}
}
5 changes: 5 additions & 0 deletions AutoRest/Generators/CSharp/CSharp.Tests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ public void IntegerTests()
client.IntModel.PutMin32(Int32.MinValue);
client.IntModel.PutMax64(Int64.MaxValue);
client.IntModel.PutMin64(Int64.MinValue);
client.IntModel.PutUnixTimeDate(new DateTime(2016, 4, 13, 0, 0, 0));
client.IntModel.GetNull();
Assert.Throws<SerializationException>(() => client.IntModel.GetInvalid());
Assert.Throws<SerializationException>(() => client.IntModel.GetOverflowInt32());
Assert.Throws<SerializationException>(() => client.IntModel.GetOverflowInt64());
Assert.Throws<SerializationException>(() => client.IntModel.GetUnderflowInt32());
Assert.Throws<SerializationException>(() => client.IntModel.GetUnderflowInt64());
Assert.Throws<SerializationException>(() => client.IntModel.GetInvalidUnixTime());
Assert.Null(client.IntModel.GetNullUnixTime());
Assert.Equal(new DateTime(2016, 4, 13, 0, 0, 0), client.IntModel.GetUnixTime());
}

[Fact]
Expand Down Expand Up @@ -1351,6 +1355,7 @@ public void UrlPathTests()
client.Paths.Base64Url(Encoding.UTF8.GetBytes("lorem"));
var testArray = new List<string> { "ArrayPath1", @"begin!*'();:@ &=+$,/?#[]end", null, "" };
client.Paths.ArrayCsvInPath(testArray);
client.Paths.UnixTimeUrl(new DateTime(2016, 4, 13, 0, 0, 0));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,47 @@ public partial interface IIntModel
/// The cancellation token.
/// </param>
Task<HttpOperationResponse> PutMin64WithHttpMessagesAsync(long intBody, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Get datetime encoded as Unix time value
/// </summary>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<DateTime?>> GetUnixTimeWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Put datetime encoded as Unix time
/// </summary>
/// <param name='intBody'>
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse> PutUnixTimeDateWithHttpMessagesAsync(DateTime intBody, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Get invalid Unix time value
/// </summary>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<DateTime?>> GetInvalidUnixTimeWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Get null Unix time value
/// </summary>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<HttpOperationResponse<DateTime?>> GetNullUnixTimeWithHttpMessagesAsync(Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
}
}
Loading