Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Azure/autorest
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Schulte committed Apr 22, 2016
2 parents a757cc4 + ab9cb34 commit f325514
Show file tree
Hide file tree
Showing 60 changed files with 2,231 additions and 266 deletions.
3 changes: 3 additions & 0 deletions AutoRest/AutoRest.Core.Tests/CodeGeneratorsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ public void OutputToSingleFile()
};

string path = Path.Combine(settings.OutputDirectory, "test.file.cs");
string existingContents = "this is dummy";
_fileSystem.VirtualStore[path] = new StringBuilder(existingContents);
var codeGenerator = new SampleCodeGenerator(settings);
codeGenerator.Generate(new ServiceClient()).GetAwaiter().GetResult();
Assert.DoesNotContain(existingContents, _fileSystem.VirtualStore[path].ToString());
Assert.Equal(4, _fileSystem.VirtualStore.Count);
Assert.True(_fileSystem.VirtualStore.ContainsKey(path));
Assert.True(_fileSystem.VirtualStore.ContainsKey("AutoRest.json"));
Expand Down
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
}
}
21 changes: 12 additions & 9 deletions AutoRest/AutoRest.Core/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Rest.Generator
public abstract class CodeGenerator
{
public const string EnumObject = "x-ms-enum";
private bool firstTimeWriteSingleFile = true;

protected CodeGenerator(Settings settings)
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public async Task Write(ITemplate template, string fileName)
/// <returns></returns>
public async Task Write(string template, string fileName)
{
string relativeFilePath = null;
string filePath = null;

if (Settings.OutputFileName != null)
{
Expand All @@ -110,17 +111,19 @@ public async Task Write(string template, string fileName)
ErrorManager.ThrowErrors();
}

relativeFilePath = Settings.OutputFileName;
filePath = Path.Combine(Settings.OutputDirectory, Settings.OutputFileName);

if (firstTimeWriteSingleFile)
{
// for SingleFileGeneration clean the file before writing only if its the first time
Settings.FileSystem.DeleteFile(filePath);
firstTimeWriteSingleFile = false;
}
}
else
{
relativeFilePath = fileName;
}
string filePath = Path.Combine(Settings.OutputDirectory, relativeFilePath);

// cleans file before writing unless single file
if (!(Settings.OutputFileName != null && IsSingleFileGenerationSupported))
{
filePath = Path.Combine(Settings.OutputDirectory, fileName);
// cleans file before writing
Settings.FileSystem.DeleteFile(filePath);
}
// Make sure the directory exist
Expand Down
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: 4 additions & 1 deletion AutoRest/AutoRest.Core/Utilities/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@

using System.IO;
using System.Net;
using System.Text;

namespace Microsoft.Rest.Generator.Utilities
{
public class FileSystem : IFileSystem
{
public void WriteFile(string path, string contents)
{
File.WriteAllText(path, contents);
File.WriteAllText(path, contents, Encoding.UTF8);
}

public string ReadFileAsText(string path)
{
using (var client = new WebClient())
{
client.Headers.Add("User-Agent: AutoRest");
client.Encoding = Encoding.UTF8;
return client.DownloadString(path);
}
}
Expand Down
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 f325514

Please sign in to comment.