Skip to content

Commit

Permalink
Adding a new primary type for unix time and updating namers to handle…
Browse files Browse the repository at this point in the history
… this type
  • Loading branch information
tbombach committed Apr 14, 2016
1 parent 56a20fa commit 1833d26
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 3 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
}
}
7 changes: 6 additions & 1 deletion AutoRest/Generators/CSharp/CSharp/CSharpCodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ protected virtual IType NormalizePrimaryType(PrimaryType primaryType)
{
primaryType.Name = "ServiceClientCredentials";
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
primaryType.Name = "DateTime";
}
else if (primaryType.Type == KnownPrimaryType.Uuid)
{
primaryType.Name = "Guid";
Expand Down Expand Up @@ -401,7 +405,8 @@ public override string EscapeDefaultValue(string defaultValue, IType type)
primaryType.Type == KnownPrimaryType.DateTimeRfc1123 ||
primaryType.Type == KnownPrimaryType.TimeSpan ||
primaryType.Type == KnownPrimaryType.ByteArray ||
primaryType.Type == KnownPrimaryType.Base64Url)
primaryType.Type == KnownPrimaryType.Base64Url ||
primaryType.Type == KnownPrimaryType.UnixTime)
{

return "SafeJsonConvert.DeserializeObject<" + primaryType.Name.TrimEnd('?') +
Expand Down
5 changes: 5 additions & 0 deletions AutoRest/Generators/CSharp/CSharp/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ public static string ToString(this IType type, string clientReference, string re
{
serializationSettings = "new Base64UrlJsonConverter()";
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
serializationSettings = "new UnixTimeJsonConverter()";
}
}

return string.Format(CultureInfo.InvariantCulture,
Expand Down Expand Up @@ -268,6 +272,7 @@ public static bool IsValueType(this IType type)
|| primaryType.Type == KnownPrimaryType.Long
|| primaryType.Type == KnownPrimaryType.TimeSpan
|| primaryType.Type == KnownPrimaryType.DateTimeRfc1123
|| primaryType.Type == KnownPrimaryType.UnixTime
|| primaryType.Type == KnownPrimaryType.Uuid));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ public string GetSerializationSettingsReference(IType serializationType)
{
return "new Base64UrlJsonConverter()";
}
else if (serializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
{
return "new UnixTimeJsonConverter()";
}
return ClientReference + ".SerializationSettings";
}

Expand All @@ -371,14 +379,22 @@ public string GetDeserializationSettingsReference(IType deserializationType)
{
return "new DateJsonConverter()";
}
if (deserializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
else if (deserializationType.IsPrimaryType(KnownPrimaryType.Base64Url) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.Base64Url) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.Base64Url))
{
return "new Base64UrlJsonConverter()";
}
else if (deserializationType.IsPrimaryType(KnownPrimaryType.UnixTime) ||
(sequenceType != null && sequenceType.ElementType is PrimaryType
&& ((PrimaryType)sequenceType.ElementType).Type == KnownPrimaryType.UnixTime) ||
(dictionaryType != null && dictionaryType.ValueType is PrimaryType
&& ((PrimaryType)dictionaryType.ValueType).Type == KnownPrimaryType.UnixTime))
{
return "new UnixTimeJsonConverter()";
}

return ClientReference + ".DeserializationSettings";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ namespace @(Settings.Namespace).Models
{
@:[JsonConverter(typeof(Base64UrlJsonConverter))]
}
if (property.Type.IsPrimaryType(KnownPrimaryType.UnixTime))
{
@:[JsonConverter(typeof(UnixTimeJsonConverter))]
}
@:[JsonProperty(PropertyName = "@property.SerializedName")]
@:public @property.Type.Name @property.Name { get; @(property.IsReadOnly ? "private " : "")set; }
@EmptyLine
Expand All @@ -145,6 +149,10 @@ namespace @(Settings.Namespace).Models
{
@:[JsonConverter(typeof(Base64UrlJsonConverter))]
}
if (property.Type.IsPrimaryType(KnownPrimaryType.UnixTime))
{
@:[JsonConverter(typeof(UnixTimeJsonConverter))]
}
@:[JsonProperty(PropertyName = "@property.SerializedName")]
@:public static @property.Type.Name @property.Name { get; private set; }
@EmptyLine
Expand Down
4 changes: 4 additions & 0 deletions AutoRest/Generators/Java/Java/TypeModels/PrimaryTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ private void Initialize(PrimaryType primaryType)
Name = "Period";
_imports.Add("org.joda.time.Period");
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
Name = "long";
}
else if (primaryType.Type == KnownPrimaryType.Uuid)
{
Name = "UUID";
Expand Down
4 changes: 4 additions & 0 deletions AutoRest/Generators/NodeJS/NodeJS/NodeJsCodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ private static IType NormalizePrimaryType(PrimaryType primaryType)
{
primaryType.Name = "moment.duration";
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
primaryType.Name = "Number";
}
else if (primaryType.Type == KnownPrimaryType.Uuid)
{
primaryType.Name = "Uuid";
Expand Down
4 changes: 4 additions & 0 deletions AutoRest/Generators/Python/Python/PythonCodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ private static IType NormalizePrimaryType(PrimaryType primaryType)
{
primaryType.Name = "Decimal";
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
primaryType.Name = "long";
}
else if (primaryType.Type == KnownPrimaryType.Object) // Revisit here
{
primaryType.Name = "object";
Expand Down
4 changes: 4 additions & 0 deletions AutoRest/Generators/Ruby/Ruby/RubyCodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ private IType NormalizePrimaryType(PrimaryType primaryType)
{
primaryType.Name = "Duration";
}
else if (primaryType.Type == KnownPrimaryType.UnixTime)
{
primaryType.Name = "Bignum";
}
else if (primaryType.Type == KnownPrimaryType.Object)
{
primaryType.Name = "Object";
Expand Down
4 changes: 4 additions & 0 deletions AutoRest/Modelers/Swagger/Model/SwaggerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public PrimaryType ToType()
{
return new PrimaryType(KnownPrimaryType.Long);
}
if (string.Equals("unixtime", Format, StringComparison.OrdinalIgnoreCase))
{
return new PrimaryType(KnownPrimaryType.UnixTime);
}
return new PrimaryType(KnownPrimaryType.Int);
case DataType.Boolean:
return new PrimaryType(KnownPrimaryType.Boolean);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Rest.Serialization
{
public class UnixTimeJsonConverter : JsonConverter
{
public static readonly DateTime EpochDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

/// <summary>
/// Converts a byte array to a Base64Url encoded string
/// </summary>
/// <param name="input">The byte array to convert</param>
/// <returns>The Base64Url encoded form of the input</returns>
private static long? ToUnixTime(DateTime dateTime)
{
return (long?)dateTime.Subtract(EpochDate).TotalSeconds;
}

/// <summary>
/// Converts a Base64Url encoded string to a byte array
/// </summary>
/// <param name="input">The Base64Url encoded string</param>
/// <returns>The byte array represented by the enconded string</returns>
private static DateTime FromUnixTime(long? seconds)
{
if (seconds.HasValue)
{
return EpochDate.AddSeconds(seconds.Value);
}
return EpochDate;
}

public override bool CanConvert(Type objectType)
{
if (objectType == typeof(DateTime))
return true;

return false;
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (objectType != typeof(DateTime))
{
return serializer.Deserialize(reader, objectType);
}
else
{
var value = serializer.Deserialize<long?>(reader);
if (value.HasValue)
{
return FromUnixTime(value);
}
}

return null;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value.GetType() != typeof(DateTime))
{
JToken.FromObject(value).WriteTo(writer);
}
else
{
JToken.FromObject(ToUnixTime((DateTime)value)).WriteTo(writer);
}
}
}
}

0 comments on commit 1833d26

Please sign in to comment.