Skip to content

Commit

Permalink
Return the right in implementation to match with return type in signa…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
Cyril Gandon committed Aug 22, 2016
1 parent da7b01a commit 1f6a5b6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Nimrod.Test/ToTypescriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void GetTypescriptType_Dictionary_Test()
{
Assert.AreEqual("{ [id: number] : number; }", typeof(Dictionary<int, int>).ToTypeScript());
Assert.AreEqual("{ [id: number] : string[]; }", typeof(Dictionary<int, List<string>>).ToTypeScript());
Assert.AreEqual("{ [id: string] : IException[]; }", typeof(Dictionary<string, List<Exception>>).ToTypeScript());
Assert.AreEqual("{ [id: string] : INonGenericClass[]; }", typeof(Dictionary<string, List<NonGenericClass>>).ToTypeScript());
}

public class Generic<T>
Expand Down
27 changes: 20 additions & 7 deletions Nimrod/Extensions/MethodExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,33 @@ public static string GetMethodSignature(this MethodInfo method, bool needNamespa
builder.Append($", {methodParameter.Name}: {param}");
}
builder.Append($", config?: {namespaceInsert}IRequestConfig)");
string returnType;
if (method.ReturnType.GetGenericArguments().Length == 1 && method.ReturnType.Name.StartsWith("Json"))
var returnType = method.GetReturnType();
builder.Append($": {namespaceInsert}IPromise<{returnType.ToTypeScript(needNamespace)}>");
return builder.ToString();
}

/// <summary>
/// Return method return's type of a controller
/// It can be the type itself, or the containing type
/// if it starts with 'Json' string
/// Example : JsonNetResult'SomeObject' will return SomeObject
/// </summary>
/// <param name="method">The method</param>
/// <returns></returns>
public static Type GetReturnType(this MethodInfo method)
{
var returnType = method.ReturnType;
if (returnType.Name.StartsWith("Json") && returnType.GetGenericArguments().Length == 1)
{
// the return type is generic, is should be something like Json<T>, so the promise will return a T
var genericArguments = method.ReturnType.GetGenericArguments()[0];
returnType = genericArguments.ToTypeScript(needNamespace);
var type = returnType.GetGenericArguments()[0];
return type;
}
else
{
// the return type is not wrapped, assume it is ApiController and use the return type
returnType = method.ReturnType.ToTypeScript(needNamespace);
return returnType;
}
builder.Append($": {namespaceInsert}IPromise<{returnType}>");
return builder.ToString();
}
}
}
13 changes: 10 additions & 3 deletions Nimrod/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,16 @@ static public string ToTypeScript(this Type type, bool includeNamespace, bool in
}
else if (!type.IsGenericType)
{
string iPrefix = type.IsEnum ? "" : "I";
string typeName = $"{iPrefix}{type.Name}";
return includeNamespace ? $"{type.Namespace}.{typeName}" : $"{typeName}";
if (type.IsSystem())
{
return "{}";
}
else
{
string iPrefix = type.IsEnum ? "" : "I";
string typeName = $"{iPrefix}{type.Name}";
return includeNamespace ? $"{type.Namespace}.{typeName}" : $"{typeName}";
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Nimrod/TypeDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static IEnumerable<Type> GetBaseTypes(Type type)
/// <returns></returns>
static public IEnumerable<Type> GetControllerActionParameterTypes(MethodInfo method)
{
var returnType = method.ReturnType.IsGenericType ? method.ReturnType.GetGenericArguments()[0] : method.ReturnType;
var returnType = method.GetReturnType();
yield return returnType;
foreach (var parameter in method.GetParameters())
{
Expand Down
6 changes: 3 additions & 3 deletions Nimrod/Writers/ControllerToTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private IEnumerable<string> GetImplementation()
var signature = method.GetMethodSignature(NeedNameSpace);
yield return $"public {signature} {{";

var genericArgString = $"<{method.ReturnType.ToTypeScript(NeedNameSpace, true)}>";

var entityName = this.Type.Name.Substring(0, this.Type.Name.Length - "Controller".Length);
var genericArgString = method.GetReturnType().ToTypeScript(NeedNameSpace, true);

if (httpVerb == HttpMethodAttribute.Get || httpVerb == HttpMethodAttribute.Delete)
{
Expand All @@ -81,7 +81,7 @@ private IEnumerable<string> GetImplementation()
yield return $"{methodParameter.Name}: {methodParameter.Name}{(needComma ? "," : "")}";
}
yield return "};";
yield return $"return restApi.{httpVerb}{genericArgString}('/{entityName}/{method.Name}', config);";
yield return $"return restApi.{httpVerb}<{genericArgString}>('/{entityName}/{method.Name}', config);";
}
else
{
Expand All @@ -93,7 +93,7 @@ private IEnumerable<string> GetImplementation()
yield return $"{methodParameter.Name}: {methodParameter.Name}{(needComma ? "," : "")}";
}
yield return "};";
yield return $"return restApi.{httpVerb}{genericArgString}('/{entityName}/{method.Name}', data, config);";
yield return $"return restApi.{httpVerb}<{genericArgString}>('/{entityName}/{method.Name}', data, config);";
}
yield return "}";
}
Expand Down
2 changes: 1 addition & 1 deletion Nimrod/Writers/Module/StaticToModuleTypeScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override IEnumerable<string> GetPromiseLines()
{
yield return "interface IPromise<T> {";
yield return " then<U>(onFulfilled ?: (value: T) => void, onRejected?: (error: any) => void): IPromise<U>;";
yield return " catch< U > (onRejected ?: (error: any) => void): IPromise<U>;";
yield return " catch<U>(onRejected ?: (error: any) => void): IPromise<U>;";
yield return "}";
yield return "export default IPromise;";
}
Expand Down

0 comments on commit 1f6a5b6

Please sign in to comment.