Created
October 10, 2017 00:25
-
-
Save michaelaird/cfd15cebe92585556be2a14abff9dff4 to your computer and use it in GitHub Desktop.
Create api proxy with dependencies
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
${ | |
using Typewriter.Extensions.WebApi; | |
using Typewriter.Extensions.Types; | |
string ReturnType(Method m) => m.Type.Name == "IHttpActionResult" ? "void" : m.Type.Name; | |
string ServiceName(Class c) => c.Name.Replace("Controller", "Gateway"); | |
string ParentServiceName(Method m) => ServiceName((Class)m.Parent); | |
static List<String> VisitedTypes = new List<String>(); | |
static List<String> VisitedEnums = new List<String>(); | |
Template(Settings settings) | |
{ | |
settings.PartialRenderingMode = PartialRenderingMode.Combined; | |
} | |
string TypeMap(Type @type) | |
{ | |
switch(@type.Name) | |
{ | |
case "LocalDate": | |
case "LocalDateTime": | |
return "string"; | |
default: | |
return @type.Name; | |
} | |
} | |
bool IsInherited(Class @class) | |
{ | |
return @class.BaseClass != null; | |
} | |
bool IsInherited(Type @type) | |
{ | |
return @type.BaseClass != null; | |
} | |
IEnumerable<Type> VisitTypeDependencies(Class @class) | |
{ | |
foreach(Method m in @class.Methods) | |
{ | |
Type returnType = m.Type.Unwrap(); | |
if (returnType.IsDefined && !returnType.IsEnum) | |
{ | |
if (!VisitedTypes.Contains(returnType.FullName)) | |
{ | |
VisitedTypes.Add(returnType.FullName); | |
yield return returnType; | |
foreach(Type t in RecurseProperties(returnType)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
foreach(Parameter p in m.Parameters) | |
{ | |
Type parameterType = p.Type.Unwrap(); | |
if (VisitedTypes.Contains(parameterType.FullName)) | |
{ | |
continue; | |
} | |
if (parameterType.IsDefined && !parameterType.IsEnum) | |
{ | |
VisitedTypes.Add(parameterType.FullName); | |
yield return parameterType; | |
} | |
foreach(Type t in RecurseProperties(parameterType)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
} | |
IEnumerable<Type> VisitEnumDependencies(Class @class) | |
{ | |
foreach(Method m in @class.Methods) | |
{ | |
if (m.Type.IsDefined && m.Type.IsEnum) | |
{ | |
if (!VisitedEnums.Contains(m.Type.FullName)) | |
{ | |
VisitedEnums.Add(m.Type.FullName); | |
yield return m.Type; | |
} | |
} | |
foreach(Parameter p in m.Parameters) | |
{ | |
if (VisitedEnums.Contains(p.Type.FullName)) | |
{ | |
continue; | |
} | |
if (p.Type.IsDefined && p.Type.IsEnum) | |
{ | |
VisitedEnums.Add(p.Type.FullName); | |
yield return p.Type; | |
} | |
foreach(Type t in RecurseEnums(p.Type)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
} | |
IEnumerable<Type> RecurseProperties(Type @type) | |
{ | |
if (@type.BaseClass != null) | |
{ | |
// ???? | |
} | |
foreach(Property prop in @type.Properties) | |
{ | |
Type propertyType = prop.Type.Unwrap(); | |
if (VisitedTypes.Contains(propertyType.FullName)) | |
{ | |
continue; | |
} | |
if (propertyType.IsDefined && !propertyType.IsEnum) | |
{ | |
VisitedTypes.Add(propertyType.FullName); | |
yield return propertyType; | |
foreach(Type t in RecurseProperties(propertyType)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
foreach(Field f in @type.Fields) | |
{ | |
Type fieldType = f.Type.Unwrap(); | |
if (VisitedTypes.Contains(fieldType)) | |
{ | |
continue; | |
} | |
if (fieldType.IsDefined && !fieldType.IsEnum) | |
{ | |
VisitedTypes.Add(fieldType.FullName); | |
yield return fieldType; | |
foreach(Type t in RecurseProperties(fieldType)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
} | |
IEnumerable<Type> RecurseEnums(Type @type) | |
{ | |
foreach(Property prop in @type.Properties) | |
{ | |
if (VisitedEnums.Contains(prop.Type.FullName)) | |
{ | |
continue; | |
} | |
if (prop.Type.IsDefined) | |
{ | |
if (prop.Type.IsEnum) | |
{ | |
VisitedEnums.Add(prop.Type.FullName); | |
yield return prop.Type; | |
} | |
foreach(Type t in RecurseEnums(prop.Type)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
foreach(Field f in @type.Fields) | |
{ | |
if (VisitedEnums.Contains(f.Type.FullName)) | |
{ | |
continue; | |
} | |
if (f.Type.IsDefined) | |
{ | |
if (f.Type.IsEnum) | |
{ | |
VisitedEnums.Add(f.Type.FullName); | |
yield return f.Type; | |
} | |
foreach(Type t in RecurseEnums(f.Type)) | |
{ | |
yield return t; | |
} | |
} | |
} | |
} | |
} | |
namespace BigBrain.Gateways.Cabinizer { | |
$Classes(c => c.Name == "XXXXController" )[ | |
export class $ServiceName { | |
$Methods[ | |
// $HttpMethod: $Url | |
private static Route$Name = ($Parameters(p => p.Type.IsPrimitive)[$name: $Type][, ]) => `/$Url`; | |
public static $Name($Parameters[$name: $Type][, ]): JQueryPromise<$ReturnType> { | |
let url: string = $ParentServiceName.Route$Name($Parameters(p => p.Type.IsPrimitive)[$name][, ]); | |
return $ParentServiceName.MakeRequestGeneric<$ReturnType>($RequestData, url, '$HttpMethod'); | |
}] | |
private static MakeRequestGeneric<T>(data: Object, url: string, method: 'get' | 'put' | 'post' | 'delete'): JQueryPromise<T> | |
{ | |
let options: JQueryAjaxSettings = <JQueryAjaxSettings>{}; | |
options.data = JSON.stringify(data); | |
options.dataType = 'json'; | |
options.contentType = 'application/json'; | |
options.type = method; | |
let request: JQueryPromise<T> = jQuery.ajax(url, options); | |
return request; | |
} | |
} | |
$VisitTypeDependencies[ | |
export interface $Name$IsInherited[ extends $BaseClass] {$Properties[ | |
$Name: $Type[$TypeMap]]$Fields[ | |
$Name: $Type[$TypeMap]] | |
} | |
] | |
$VisitEnumDependencies[ | |
export const enum $Name | |
{ $Constants[ | |
$Name = $Value,] | |
} | |
] | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this, it was helpful for a similar issue I was stuck trying to solve