Skip to content

Commit

Permalink
Merge pull request #171 from Azure/daschult/RemoveJoda
Browse files Browse the repository at this point in the history
Replace Joda with standard Java 8 types
  • Loading branch information
RikkiGibson authored Mar 10, 2018
2 parents c906db9 + 3aec111 commit 760c502
Show file tree
Hide file tree
Showing 48 changed files with 1,652 additions and 1,907 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ This project enables Java code generation in [AutoRest](https://github.com/Azure
- With Argument (`true`): `int Plus(@NonNull Integer val1, @NonNull Integer val2)`
- *--java.required-parameter-client-methods*: Whether or not Service and Method Group client method overloads that omit optional parameters will be created.
- Default: `true`
- *--java.string-dates*: Whether or not DateTime types should be represented as Strings (generally for better/different precision).
- Default: `false`
- Without Argument: `public DateTime duration(DateTime startTime, DateTime endTime)`
- With Argument (`true`): `public String duration(String startTime, String endTime)`

# Contributing

Expand Down
35 changes: 17 additions & 18 deletions src/JavaCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class JavaCodeGenerator : CodeGenerator
"byte[]", "Byte[]",
"String",
"LocalDate",
"DateTime",
"OffsetDateTime",
"DateTimeRfc1123",
"Duration",
"Period",
Expand Down Expand Up @@ -154,7 +154,6 @@ public override Task Generate(AutoRestCodeModel codeModel)
package: codeModel.Namespace.ToLowerInvariant(),
shouldGenerateXmlSerialization: codeModel.ShouldGenerateXmlSerialization,
nonNullAnnotations: GetBoolSetting(autoRestSettings, "non-null-annotations", true),
stringDates: GetBoolSetting(autoRestSettings, "string-dates"),
clientTypePrefix: GetStringSetting(autoRestSettings, "client-type-prefix"),
generateClientInterfaces: GetBoolSetting(autoRestSettings, "generate-client-interfaces", true),
implementationSubpackage: GetStringSetting(autoRestSettings, "implementation-subpackage", "implementation"),
Expand Down Expand Up @@ -1428,13 +1427,13 @@ private static IType ParseType(AutoRestIModelType autoRestIModelType, JavaSettin
result = ArrayType.ByteArray;
break;
case AutoRestKnownPrimaryType.Date:
result = settings.StringDates ? ClassType.String : ClassType.JodaLocalDate;
result = ClassType.LocalDate;
break;
case AutoRestKnownPrimaryType.DateTime:
result = settings.StringDates ? ClassType.String : ClassType.JodaDateTime;
result = ClassType.DateTime;
break;
case AutoRestKnownPrimaryType.DateTimeRfc1123:
result = settings.StringDates ? ClassType.String : ClassType.DateTimeRfc1123;
result = ClassType.DateTimeRfc1123;
break;
case AutoRestKnownPrimaryType.Double:
result = PrimitiveType.Double;
Expand Down Expand Up @@ -1462,10 +1461,10 @@ private static IType ParseType(AutoRestIModelType autoRestIModelType, JavaSettin
}
break;
case AutoRestKnownPrimaryType.TimeSpan:
result = ClassType.JodaPeriod;
result = ClassType.Duration;
break;
case AutoRestKnownPrimaryType.UnixTime:
result = settings.StringDates ? (IType)ClassType.String : PrimitiveType.UnixTimeLong;
result = PrimitiveType.UnixTimeLong;
break;
case AutoRestKnownPrimaryType.Uuid:
result = ClassType.UUID;
Expand Down Expand Up @@ -2684,7 +2683,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
string propertyConversion = null;
switch (sourceTypeName.ToLower())
{
case "datetime":
case "offsetdatetime":
switch (targetTypeName.ToLower())
{
case "datetimerfc1123":
Expand All @@ -2696,7 +2695,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
case "datetimerfc1123":
switch (targetTypeName.ToLower())
{
case "datetime":
case "offsetdatetime":
propertyConversion = $"{expression}.dateTime()";
break;
}
Expand Down Expand Up @@ -2742,7 +2741,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
{
switch (sourceTypeName.ToLower())
{
case "datetime":
case "offsetdatetime":
switch (targetTypeName.ToLower())
{
case "datetimerfc1123":
Expand All @@ -2754,7 +2753,7 @@ public static JavaFile GetModelJavaFile(ServiceModel model, JavaSettings setting
case "datetimerfc1123":
switch (targetTypeName.ToLower())
{
case "datetime":
case "offsetdatetime":
propertyConversion = $"{expression}.dateTime()";
break;
}
Expand Down Expand Up @@ -3032,7 +3031,7 @@ private static IType ConvertToClientType(IType modelType)
}
else if (modelType == ClassType.DateTimeRfc1123)
{
clientType = ClassType.JodaDateTime;
clientType = ClassType.DateTime;
}
else if (modelType == PrimitiveType.UnixTimeLong)
{
Expand Down Expand Up @@ -3180,7 +3179,7 @@ private static string AutoRestIModelTypeName(AutoRestIModelType autoRestModelTyp
result = "LocalDate";
break;
case AutoRestKnownPrimaryType.DateTime:
result = "DateTime";
result = "OffsetDateTime";
break;
case AutoRestKnownPrimaryType.DateTimeRfc1123:
result = "DateTimeRfc1123";
Expand Down Expand Up @@ -3303,14 +3302,14 @@ private static void ParameterConvertClientTypeToWireType(JavaBlock block, JavaSe
{
if (parameterIsRequired)
{
block.Line($"Long {target} = {source}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;");
block.Line($"Long {target} = {source}.toInstant().getEpochSecond();");
}
else
{
block.Line($"Long {target} = null;");
block.If($"{source} != null", ifBlock =>
{
ifBlock.Line($"{target} = {source}.toDateTime(DateTimeZone.UTC).getMillis() / 1000;");
ifBlock.Line($"{target} = {source}.toInstant().getEpochSecond();");
});
}
}
Expand Down Expand Up @@ -4597,10 +4596,10 @@ private static IEnumerable<string> GetExpressionsToValidate(RestAPIMethod restAP
parameterType != ClassType.Double &&
parameterType != ClassType.BigDecimal &&
parameterType != ClassType.String &&
parameterType != ClassType.JodaDateTime &&
parameterType != ClassType.JodaLocalDate &&
parameterType != ClassType.DateTime &&
parameterType != ClassType.LocalDate &&
parameterType != ClassType.DateTimeRfc1123 &&
parameterType != ClassType.JodaPeriod &&
parameterType != ClassType.Duration &&
parameterType != ClassType.Boolean &&
parameterType != ClassType.ServiceClientCredentials &&
parameterType != ClassType.AzureTokenCredentials &&
Expand Down
7 changes: 0 additions & 7 deletions src/JavaSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public JavaSettings(
string package,
bool shouldGenerateXmlSerialization,
bool nonNullAnnotations,
bool stringDates,
string clientTypePrefix,
bool generateClientInterfaces,
string implementationSubpackage,
Expand All @@ -62,7 +61,6 @@ public JavaSettings(
Package = package;
ShouldGenerateXmlSerialization = shouldGenerateXmlSerialization;
NonNullAnnotations = nonNullAnnotations;
StringDates = stringDates;
ClientTypePrefix = clientTypePrefix;
GenerateClientInterfaces = generateClientInterfaces;
ImplementationSubpackage = implementationSubpackage;
Expand Down Expand Up @@ -100,11 +98,6 @@ public bool AddCredentials
/// </summary>
public bool NonNullAnnotations { get; }

/// <summary>
/// Whether or not DateTime types should be represented as Strings (generally for better/different precision).
/// </summary>
public bool StringDates { get; }

/// <summary>
/// The prefix that will be added to each generated client type.
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Model/ClassType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class ClassType : IType
public static readonly ClassType Double = new ClassType("java.lang", "Double", defaultValueExpressionConverter: (string defaultValueExpression) => double.Parse(defaultValueExpression).ToString());
public static readonly ClassType String = new ClassType("java.lang", "String", defaultValueExpressionConverter: (string defaultValueExpression) => CodeNamer.Instance.QuoteValue(defaultValueExpression));
public static readonly ClassType Base64Url = new ClassType("com.microsoft.rest.v2", "Base64Url");
public static readonly ClassType JodaLocalDate = new ClassType("org.joda.time", "LocalDate", defaultValueExpressionConverter: (string defaultValueExpression) => $"LocalDate.parse(\"{defaultValueExpression}\")");
public static readonly ClassType JodaDateTime = new ClassType("org.joda.time", "DateTime", defaultValueExpressionConverter: (string defaultValueExpression) => $"DateTime.parse(\"{defaultValueExpression}\")");
public static readonly ClassType JodaPeriod = new ClassType("org.joda.time", "Period", defaultValueExpressionConverter: (string defaultValueExpression) => $"Period.parse(\"{defaultValueExpression}\")");
public static readonly ClassType DateTimeRfc1123 = new ClassType("com.microsoft.rest.v2", "DateTimeRfc1123", defaultValueExpressionConverter: (string defaultValueExpression) => $"DateTime.parse(\"{defaultValueExpression}\")");
public static readonly ClassType LocalDate = new ClassType("java.time", "LocalDate", defaultValueExpressionConverter: (string defaultValueExpression) => $"LocalDate.parse(\"{defaultValueExpression}\")");
public static readonly ClassType DateTime = new ClassType("java.time", "OffsetDateTime", defaultValueExpressionConverter: (string defaultValueExpression) => $"OffsetDateTime.parse(\"{defaultValueExpression}\")");
public static readonly ClassType Duration = new ClassType("java.time", "Duration", defaultValueExpressionConverter: (string defaultValueExpression) => $"Duration.parse(\"{defaultValueExpression}\")");
public static readonly ClassType DateTimeRfc1123 = new ClassType("com.microsoft.rest.v2", "DateTimeRfc1123", defaultValueExpressionConverter: (string defaultValueExpression) => $"new DateTimeRfc1123(\"{defaultValueExpression}\")");
public static readonly ClassType BigDecimal = new ClassType("java.math", "BigDecimal");
public static readonly ClassType UUID = new ClassType("java.util", "UUID");
public static readonly ClassType Object = new ClassType("java.lang", "Object");
Expand All @@ -29,7 +29,7 @@ public class ClassType : IType
public static readonly ClassType CloudException = new ClassType("com.microsoft.azure.v2", "CloudException");
public static readonly ClassType RestException = new ClassType("com.microsoft.azure.v2", "RestException");
public static readonly ClassType UnixTime = new ClassType("com.microsoft.rest.v2", "UnixTime");
public static readonly ClassType UnixTimeDateTime = new ClassType("org.joda.time", "DateTime", new[] { "org.joda.time.DateTimeZone" });
public static readonly ClassType UnixTimeDateTime = new ClassType("java.time", "OffsetDateTime");
public static readonly ClassType UnixTimeLong = new ClassType("java.lang", "Long");
public static readonly ClassType AzureEnvironment = new ClassType("com.microsoft.azure.v2", "AzureEnvironment");
public static readonly ClassType HttpPipeline = new ClassType("com.microsoft.rest.v2.http", "HttpPipeline");
Expand Down
Loading

0 comments on commit 760c502

Please sign in to comment.