Skip to content

Commit

Permalink
Merge pull request #948 from tbombach/issue-909
Browse files Browse the repository at this point in the history
Adding support for Unix time formatted dates in C#
  • Loading branch information
tbombach committed Apr 22, 2016
2 parents d1d781c + dccbc73 commit ab9cb34
Show file tree
Hide file tree
Showing 27 changed files with 1,734 additions and 44 deletions.
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;
}

/// <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

0 comments on commit ab9cb34

Please sign in to comment.