コード例 #1
0
        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}"); }));
        }
コード例 #2
0
        // 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);
        }
コード例 #3
0
        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}"); }));
        }
コード例 #4
0
        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());
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 public static IOption <TypescriptBaseClass> ToTypescriptBaseClass(this Type baseType, TypescriptModel model)
 {
     return(baseType.Match()
            .With <IOption <TypescriptBaseClass> >(typeof(Object), new None <TypescriptBaseClass>())
            .Else(NewTypescriptBaseClass));
 }
コード例 #7
0
 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)
     }));
 }
コード例 #8
0
        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());
            }
        }
コード例 #9
0
 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)
     }));
 }
コード例 #10
0
 protected virtual IEnumerable <TypescriptInterfaceContent> GetInterfaceContent(Type type, TypescriptModel model)
 {
     return(GetTypescriptProperties(type, model).Select(property => new TypescriptInterfaceContent(property)));
 }
コード例 #11
0
        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);
        }
コード例 #12
0
 private TypescriptType GetTypeForInnerType(Type type, TypescriptModel model)
 {
     return(GetTypeFor(type.GenericTypeArguments.Single(), model));
 }
コード例 #13
0
 public static TypescriptInterfaceContentList GetInterfaceContent(this Type type, ITypescriptTypeCreator typeCreator, TypescriptModel model)
 {
     return(new TypescriptInterfaceContentList(type.GetTypescriptProperties(typeCreator, model).Select(x => x.ToTypescriptInterfaceContent())));
 }
コード例 #14
0
 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));
 }
コード例 #15
0
        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());
            }
        }
コード例 #16
0
 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());
 }
コード例 #17
0
        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());
            }
        }