forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Char format for string (Azure#1252)
- Loading branch information
1 parent
0da7d34
commit 5069891
Showing
14 changed files
with
883 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
// | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace AutoRest.Core.ClientModel | ||
{ | ||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
public enum KnownFormat | ||
{ | ||
none, | ||
unknown, | ||
|
||
@char, | ||
int32, | ||
int64, | ||
@float, | ||
@double, | ||
@byte, | ||
binary, | ||
date, | ||
date_time, | ||
password, | ||
date_time_rfc1123, | ||
duration, | ||
uuid, | ||
base64url, | ||
@decimal, | ||
unixtime | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/core/AutoRest.Core/ClientModel/KnownFormatExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
// | ||
|
||
using System; | ||
|
||
namespace AutoRest.Core.ClientModel | ||
{ | ||
public static class KnownFormatExtensions | ||
{ | ||
public static KnownFormat Parse(string formatValue) | ||
{ | ||
if (string.IsNullOrWhiteSpace(formatValue)) | ||
{ | ||
return KnownFormat.none; | ||
} | ||
|
||
KnownFormat result; | ||
return Enum.TryParse(formatValue.Replace('-', '_'), true, out result) ? result : KnownFormat.unknown; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
src/generator/AutoRest.CSharp.Unit.Tests/AutoDynamic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
// 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 System.Collections; | ||
using System.Diagnostics; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace AutoRest.CSharp.Unit.Tests | ||
{ | ||
/// <summary> | ||
/// This is a class that creates a dynamic wrapper around any object and can allow | ||
/// deep inspection of anything (private or otherwise). | ||
/// Handy for testing. | ||
/// </summary> | ||
public class AutoDynamic : DynamicObject | ||
{ | ||
/// <summary> | ||
/// Specify the flags for accessing members | ||
/// </summary> | ||
private static readonly BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance | ||
| BindingFlags.Static | BindingFlags.Public | ||
| BindingFlags.IgnoreCase; | ||
|
||
/// <summary> | ||
/// The object we are going to wrap | ||
/// </summary> | ||
private readonly object _wrapped; | ||
|
||
/// <summary> | ||
/// Create a simple private wrapper | ||
/// </summary> | ||
public AutoDynamic(object o) | ||
{ | ||
_wrapped = o; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a JSON representation. | ||
/// </summary> | ||
/// <returns>a JSON string</returns> | ||
public string ToJson() | ||
{ | ||
return JsonConvert.SerializeObject( | ||
_wrapped, | ||
Formatting.Indented, | ||
new JsonSerializerSettings | ||
{ | ||
Converters = new JsonConverter[] {new StringEnumConverter()}, | ||
ContractResolver = new CamelCasePropertyNamesContractResolver(), | ||
NullValueHandling = NullValueHandling.Ignore, | ||
ObjectCreationHandling = ObjectCreationHandling.Reuse | ||
}); | ||
} | ||
|
||
private bool CheckResult(object result, out object outresult) | ||
{ | ||
if (result == null || result.GetType().GetTypeInfo().IsPrimitive | ||
|| result.GetType().GetTypeInfo().IsValueType || result is string) | ||
{ | ||
outresult = result; | ||
} | ||
else | ||
{ | ||
outresult = result.ToDynamic(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Try invoking a method | ||
/// </summary> | ||
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) | ||
{ | ||
if (_wrapped == null) | ||
{ | ||
result = null; | ||
return true; | ||
} | ||
var types = args.Select(a => a != null ? a.GetType() : typeof(object)); | ||
|
||
var method = _wrapped.GetType().GetMethod(binder.Name, types.ToArray()) | ||
?? _wrapped.GetType().GetMethod(binder.Name, flags); | ||
|
||
if (method == null) | ||
{ | ||
return base.TryInvokeMember(binder, args, out result); | ||
} | ||
|
||
return CheckResult(method.Invoke(_wrapped, args), out result); | ||
} | ||
|
||
public override bool TryConvert(ConvertBinder binder, out object result) | ||
{ | ||
if (_wrapped == null) | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
|
||
if (binder.ReturnType.IsInstanceOfType(_wrapped)) | ||
{ | ||
result = _wrapped; | ||
return true; | ||
} | ||
return base.TryConvert(binder, out result); | ||
} | ||
|
||
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) | ||
{ | ||
if (_wrapped == null) | ||
{ | ||
result = null; | ||
return false; | ||
} | ||
|
||
if (indexes.Length == 1 && indexes[0] is int) | ||
{ | ||
var index = (int) indexes[0]; | ||
try | ||
{ | ||
var arr = _wrapped as Array; | ||
if (arr != null) | ||
{ | ||
return CheckResult(arr.GetValue(index), out result); | ||
} | ||
} | ||
catch | ||
{ | ||
// nope... | ||
} | ||
} | ||
|
||
// is it asking for a property as a field | ||
foreach ( | ||
var prop in _wrapped.GetType().GetProperties(flags).Where(each => each.GetIndexParameters().Any())) | ||
{ | ||
try | ||
{ | ||
result = prop.GetValue(_wrapped, indexes); | ||
return true; | ||
} | ||
catch (TargetParameterCountException) | ||
{ | ||
} | ||
catch (TargetInvocationException) | ||
{ | ||
} | ||
} | ||
|
||
if (indexes.Length == 1 && indexes[0] is int) | ||
{ | ||
var index = (int) indexes[0]; | ||
try | ||
{ | ||
var ie = _wrapped as IEnumerable; | ||
if (ie != null) | ||
{ | ||
var e = ie.GetEnumerator(); | ||
|
||
while (index > 0 && e.MoveNext()) | ||
{ | ||
--index; | ||
} | ||
if (index == 0) | ||
{ | ||
if (e.MoveNext()) | ||
{ | ||
return CheckResult(e.Current, out result); | ||
} | ||
} | ||
} | ||
} | ||
catch | ||
{ | ||
} | ||
} | ||
return base.TryGetIndex(binder, indexes, out result); | ||
} | ||
|
||
/// <summary> | ||
/// Tries to get a property or field with the given name | ||
/// </summary> | ||
public override bool TryGetMember(GetMemberBinder binder, out object result) | ||
{ | ||
if (_wrapped == null) | ||
{ | ||
result = null; | ||
return true; | ||
} | ||
|
||
try | ||
{ | ||
//Try getting a property of that name | ||
var prop = _wrapped.GetType().GetProperty(binder.Name, flags); | ||
|
||
if (prop == null) | ||
{ | ||
//Try getting a field of that name | ||
var fld = _wrapped.GetType().GetField(binder.Name, flags); | ||
|
||
if (fld != null) | ||
{ | ||
return CheckResult(fld.GetValue(_wrapped), out result); | ||
} | ||
|
||
// check if this is an index into the | ||
if (TryGetIndex(null, new[] {binder.Name}, out result)) | ||
{ | ||
return true; | ||
} | ||
|
||
return base.TryGetMember(binder, out result); | ||
} | ||
return CheckResult(prop.GetValue(_wrapped, null), out result); | ||
} | ||
catch (Exception e) | ||
{ | ||
Debug.WriteLine($"{e.Message}/{e.StackTrace}"); | ||
result = null; | ||
return true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Tries to set a property or field with the given name | ||
/// </summary> | ||
public override bool TrySetMember(SetMemberBinder binder, object value) | ||
{ | ||
if (_wrapped == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var prop = _wrapped.GetType().GetProperty(binder.Name, flags); | ||
if (prop == null) | ||
{ | ||
var fld = _wrapped.GetType().GetField(binder.Name, flags); | ||
if (fld != null) | ||
{ | ||
fld.SetValue(_wrapped, value); | ||
return true; | ||
} | ||
return base.TrySetMember(binder, value); | ||
} | ||
|
||
prop.SetValue(_wrapped, value, null); | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.