Ejemplo n.º 1
0
        private TypescriptTypeReference MapType(TypescriptTypeReference r)
        {
            if (r == null)
            {
                return(null);
            }
            TypescriptTypeReference result;

            if (r.Raw != null)
            {
                result = new TypescriptTypeReference(MapRaw(r.Raw));
            }
            else if (r.TypeName != null)
            {
                result = new TypescriptTypeReference(r.TypeName);
            }
            else if (r.ReferencedType != null && r.ReferencedType is DeclarationBase)
            {
                result = new TypescriptTypeReference(MapDeclaration((DeclarationBase)r.ReferencedType));
            }
            else if (r.ReferencedType != null && r.ReferencedType is ArrayType)
            {
                var array = (ArrayType)r.ReferencedType;
                result = new TypescriptTypeReference(new ArrayType(MapType(array.ElementType)));
            }
            else
            {
                result = new TypescriptTypeReference(r.ReferencedType);
            }
            foreach (var item in r.GenericParameters)
            {
                result.GenericParameters.Add(MapType(item));
            }
            return(result);
        }
Ejemplo n.º 2
0
 public static RawStatements GenerateRawLiteral(object value, TypescriptTypeReference targetType)
 {
     //can use JsonConvert.Serialize object (with casting to target type)
     if (value is string)
     {
         return(new RawStatements("'" + value.ToString().Replace("'", "''") + "'"));
     }
     if (value is bool)
     {
         return(new RawStatements((bool)value ? "true" : "false"));
     }
     if (value is float)
     {
         return(GenerateRawLiteral((double)(float)value, targetType));
     }
     if (value is int)
     {
         return(GenerateRawLiteral((double)(int)value, targetType));
     }
     if (value is double)
     {
         return(new RawStatements(((double)value).ToString(CultureInfo.InvariantCulture)));
     }
     return(null);
 }
Ejemplo n.º 3
0
 public static TypescriptTypeReference ExtractArrayElement(this TypescriptTypeReference item)
 {
     if (item.ReferencedType != null && item.ReferencedType is ArrayType)
     {
         return(((ArrayType)item.ReferencedType).ElementType);
     }
     throw new InvalidOperationException("Not an array type");
 }
Ejemplo n.º 4
0
 private TypescriptTypeReference MapTypeToClass(TypescriptTypeReference r)
 {
     BeginInterfaceMapping(isInterface: false);
     try
     {
         return(MapType(r));
     }
     finally
     {
         EndInterfaceMapping();
     }
 }
        public void GenerateControllers(IEnumerable <ControllerModel> controllers, ReflectionGeneratorBase reflectionGenerator, TypescriptModule targetModule)
        {
            var proxyClass = new ClassType("GeneratedProxy");

            if (!String.IsNullOrEmpty(ProxyBaseName))
            {
                proxyClass.Extends = new TypescriptTypeReference(ProxyBaseName);
            }
            targetModule.Members.Add(new DeclarationModuleElement(proxyClass)
            {
                IsExporting = true
            });
            foreach (var controller in controllers)
            {
                var cls       = new ClassType(controller.Name + "Proxy");
                var proxyType = new TypescriptTypeReference(GeneratedClassName);
                cls.Members.Add(new PropertyMember("_parent")
                {
                    MemberType    = proxyType,
                    Accessibility = AccessibilityEnum.Private
                });
                //cls.Members.Add(new RawStatements("constructor(parent: ", proxyType, ") {\n\tthis._parent = parent;\n}"));
                cls.Members.Add(new FunctionMember("constructor", new RawStatements("this._parent = parent;"))
                {
                    Accessibility = null,
                    Parameters    = { new FunctionParameter("parent")
                                      {
                                          ParameterType = proxyType
                                      } },
                });
                foreach (var am in controller.Actions)
                {
                    cls.Members.Add(GenerateAction(am, reflectionGenerator));
                }
                targetModule.Members.Add(cls);
                targetModule.Members.Last().Comment = controller.Comment;

                proxyClass.Members.Add(new PropertyMember(controller.Name.Replace("Controller", ""))
                {
                    MemberType     = cls,
                    Accessibility  = AccessibilityEnum.Public,
                    Initialization = new RawStatements("new ", cls, "(this)")
                });
            }
            targetModule.Members.Add(new RawStatements("export var proxy = new ", proxyClass, "();"));
        }
Ejemplo n.º 6
0
 public virtual void VisitTypeReference(TypescriptTypeReference obj)
 {
     if (!String.IsNullOrEmpty(obj.TypeName))
     {
         VisitTypeReferenceNamed(obj, obj.TypeName);
     }
     else if (obj.Raw != null)
     {
         VisitTypeReferenceRaw(obj, obj.Raw);
     }
     else
     {
         VisitReference(obj.ReferencedType);
     }
     if (obj.GenericParameters.Count > 0)
     {
         foreach (var item in obj.GenericParameters.ToArray())
         {
             VisitTypeReference(item);
         }
     }
 }
Ejemplo n.º 7
0
        public virtual TypescriptTypeReference RewriteTypeReference(TypescriptTypeReference obj)
        {
            if (obj == null)
            {
                return(null);
            }
            TypescriptTypeReference result;

            if (!String.IsNullOrEmpty(obj.TypeName))
            {
                result = new TypescriptTypeReference(obj.TypeName);
            }
            else if (obj.Raw != null)
            {
                result = new TypescriptTypeReference(RewriteRaw(obj.Raw));
            }
            else
            {
                result = new TypescriptTypeReference(RewriteReference(obj.ReferencedType));
            }
            result.ExtraData = RewriteExtraData(obj);
            result.GenericParameters.AddRange(obj.GenericParameters.Select(RewriteTypeReference).Where(x => x != null));
            return(result);
        }
Ejemplo n.º 8
0
 public virtual RawStatements GenerateLiteral(object value, TypescriptTypeReference targetType)
 {
     return(GenerationStrategy.GenerateRawLiteral(value, targetType));
 }
Ejemplo n.º 9
0
 public virtual void VisitTypeReferenceNamed(TypescriptTypeReference obj, string name)
 {
 }
Ejemplo n.º 10
0
 public virtual void VisitTypeReferenceRaw(TypescriptTypeReference obj, RawStatements raw)
 {
     VisitRaw(raw);
 }