public TypescriptType GetTypeFor(Type type, TypescriptModel model) { var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type); return(type.Match() .With(typeMatch => typeMatch.IsGenericParameter, typeMatch => NewGenericParameter(typeMatch)) .With(typeMatch => primitiveTypes.ContainsKey(typeMatch), typeMatch => NewPrimitiveType(typeMatch)) .With(typeMatch => typeMatch.IsClass, typeMatch => classCreator.GetTypeFor(typeMatch, model)) .With(typeMatch => typeMatch.IsEnum, typeMatch => enumCreator.GetTypeFor(typeMatch, model)) .With(typeMatch => typeMatch.IsInterface, typeMatch => interfaceCreator.GetTypeFor(typeMatch, model)) .Else(typeMatch => { throw new ArgumentOutOfRangeException($"unknown type {type.Name}"); })); }
// TODO create interface for this? public TypescriptModel CreateTypescriptModelFor(IEnumerable <Type> types, bool IncludeDependencies = true) { // TODO: maybe change the way this currently works and return the types throughout the chain without actually adding them to the model. if (IncludeDependencies == false) { throw new NotImplementedException(); } var model = new TypescriptModel(); types.ToList().ForEach(x => GetTypeFor(x, model)); return(model); }
public TypescriptType GetTypeFor(Type type, TypescriptModel model) { var isEnumerable = typeof(IEnumerable).IsAssignableFrom(type); return(type.Match() .With(IsGenericNullable, typeMatch => GetTypeForInnerType(typeMatch, model)) .With(IsEnumerable, CreateCollection) .With(typeMatch => StandardMappings.ContainsKey(typeMatch), typeMatch => StandardMappings[typeMatch]) .With(typeMatch => typeMatch.IsGenericParameter, typeMatch => typeMatch.ToTypescriptGenericParameter()) .With(typeMatch => typeMatch.IsTypescriptPrimitiveType(), typeMatch => typeMatch.ToTypescriptPrimitiveType()) .With(typeMatch => typeMatch.IsClass, typeMatch => typeMatch.ClassTypeToTypescriptInterface(this, model)) .With(typeMatch => typeMatch.IsEnum, typeMatch => typeMatch.EnumTypeToTypescriptEnum()) .With(typeMatch => typeMatch.IsInterface, typeMatch => typeMatch.InterfaceTypeToTypescriptInterface(this, model)) .Else(typeMatch => { throw new ArgumentOutOfRangeException($"unknown type {type?.Name}"); })); }
public TypescriptType GetTypeFor(Type type, TypescriptModel model) { if (model.knownTypes.ContainsKey(type)) { return(model.knownTypes[type]); } else { var newInterface = new TypescriptInterface { Name = NameWithoutGeneric(type) }; model.knownTypes.Add(type, newInterface.ToTypescriptType()); // TODO: implement inherrited interfaces. newInterface. = GetBaseClassFor(type.BaseType, model); newInterface.Content = new TypescriptInterfaceContentList(GetInterfaceContent(type, model)); newInterface.GenricTypeParameters = TypescriptTypeCreatorBase.GetGenericTypeParametersFor(type); return(newInterface.ToTypescriptType()); } }
public static TypescriptGenericTypeArguments GetGenericTypeArgumentsFor(ITypescriptTypeCreator typeCreator, Type baseType, TypescriptModel model) { var result = new TypescriptGenericTypeArguments(); foreach (var genericTypeArgument in baseType.GetGenericArguments()) { var tsType = typeCreator.GetTypeFor(genericTypeArgument, model); tsType.Match( primitive => result.Add(primitive), tsClass => result.Add(tsClass), tsInterface => result.Add(tsInterface), tsEnumerable => { throw new Exception("enum cannot be a generic type parameter"); }, genericTypeParameter => result.Add(genericTypeParameter)); } return(result); }
public static IOption <TypescriptBaseClass> ToTypescriptBaseClass(this Type baseType, TypescriptModel model) { return(baseType.Match() .With <IOption <TypescriptBaseClass> >(typeof(Object), new None <TypescriptBaseClass>()) .Else(NewTypescriptBaseClass)); }
public static IEnumerable <TypescriptProperty> GetTypescriptProperties(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model) { return(type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).Select(property => new TypescriptProperty { Name = property.Name, Accesability = TypescriptAccesModifier.@public, DefaultValue = new None <String>(), Type = typeCreator.GetTypeFor(property.PropertyType, model) })); }
public static TypescriptType ClassTypeToTypescriptClass(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model) { // TODO Check if type is indeed a class.. if (model.knownTypes.ContainsKey(type)) { return(model.knownTypes[type]); } else { var newClass = new TypescriptClass { Name = type.NameWithoutGeneric() }; model.knownTypes.Add(type, newClass.ToTypescriptType()); newClass.BaseClass = type.BaseType.ToTypescriptBaseClass(model); newClass.Content = type.GetTypescriptProperties(typeCreator, model).ToClassContent(); newClass.GenricTypeParameters = TypescriptTypeCreatorBase.GetGenericTypeParametersFor(type); return(newClass.ToTypescriptType()); } }
protected virtual IEnumerable <TypescriptProperty> GetTypescriptProperties(Type type, TypescriptModel model) { return(type.GetProperties().Select(property => new TypescriptProperty { Name = property.Name, Accesability = TypescriptAccesModifier.@public, DefaultValue = new None <String>(), Type = typeCreator.GetTypeFor(property.PropertyType, model) })); }
protected virtual IEnumerable <TypescriptInterfaceContent> GetInterfaceContent(Type type, TypescriptModel model) { return(GetTypescriptProperties(type, model).Select(property => new TypescriptInterfaceContent(property))); }
private static TypescriptInterfaceBaseTypes ClassBaseClassAndInterfacesAsBaseInterfaces(this Type classType, ITypescriptTypeCreator typeCreator, TypescriptModel model) { var interfaceBaseInterfaces = classType.TypescriptImplementedInterfaces(); var classBaseClassAsInterface = classType.BaseType.ClassBaseClassToTypescriptInterfaceBase(typeCreator, model); classBaseClassAsInterface.IfNotNullDo(x => interfaceBaseInterfaces.Add(x)); return(interfaceBaseInterfaces); }
private TypescriptType GetTypeForInnerType(Type type, TypescriptModel model) { return(GetTypeFor(type.GenericTypeArguments.Single(), model)); }
public static TypescriptInterfaceContentList GetInterfaceContent(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model) { return(new TypescriptInterfaceContentList(type.GetTypescriptProperties(typeCreator, model).Select(x => x.ToTypescriptInterfaceContent()))); }
private static IOption <TypescriptInterfaceBaseType> ClassBaseClassToTypescriptInterfaceBase(this Type baseType, ITypescriptTypeCreator typeCreator, TypescriptModel model) { return(baseType.Match() .With <IOption <TypescriptInterfaceBaseType> >(typeof(Object), new None <TypescriptInterfaceBaseType>()) .Else(NewTypescriptInterfaceBase)); }
public static TypescriptType ClassTypeToTypescriptInterface(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model) { // TODO validate parameters. the input hsould be a class type... if (model.knownTypes.ContainsKey(type)) { return(model.knownTypes[type]); } else { var newInterface = new TypescriptInterface { Name = type.NameWithoutGeneric() }; model.knownTypes.Add(type, newInterface.ToTypescriptType()); // TODO: implement inherrited interfaces. newInterface. = GetBaseClassFor(type.BaseType, model); newInterface.BaseType = type.ClassBaseClassAndInterfacesAsBaseInterfaces(typeCreator, model); newInterface.Content = type.GetInterfaceContent(typeCreator, model); newInterface.GenricTypeParameters = TypescriptTypeCreatorBase.GetGenericTypeParametersFor(type); return(newInterface.ToTypescriptType()); } }
protected virtual IOption <TypescriptBaseClass> GetBaseClassFor(Type baseType, TypescriptModel model) { if (baseType == typeof(Object)) { return(new None <TypescriptBaseClass>()); } return(new TypescriptBaseClass { Name = NameWithoutGeneric(baseType), GenericArguments = TypescriptTypeCreatorBase.GetGenericTypeArgumentsFor(typeCreator, baseType, model) }.ToOption()); }
public static TypescriptType InterfaceTypeToTypescriptInterface(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model) { // TODO Check and refactor this and method below. if (model.knownTypes.ContainsKey(type)) { return(model.knownTypes[type]); } else { var newInterface = new TypescriptInterface { Name = type.NameWithoutGeneric() }; model.knownTypes.Add(type, newInterface.ToTypescriptType()); // TODO: implement inherrited interfaces. newInterface. = GetBaseClassFor(type.BaseType, model); newInterface.BaseType = type.TypescriptImplementedInterfaces(); newInterface.Content = type.GetInterfaceContent(typeCreator, model); newInterface.GenricTypeParameters = TypescriptTypeCreatorBase.GetGenericTypeParametersFor(type); return(newInterface.ToTypescriptType()); } }