Beispiel #1
0
        private string CreateISvcMethods(DotNet2TS dotNet2TS)
        {
            StringBuilder sbISvcMeth = new StringBuilder(512);

            sbISvcMeth.AppendLine("export interface ISvcMethods");
            sbISvcMeth.AppendLine("{");
            StringBuilder            sbArgs     = new StringBuilder(255);
            List <MethodDescription> svcMethods = _metadata.GetInvokeMethods().OrderBy(m => m.methodName).ToList();

            svcMethods.ForEach(methodInfo =>
            {
                sbArgs.Length = 0;
                if (methodInfo.parameters.Count() > 0)
                {
                    sbArgs.AppendLine("(args: {");

                    methodInfo.parameters.ForEach(paramInfo =>
                    {
                        sbArgs.Append("\t\t");
                        sbArgs.AppendFormat(_CreateParamSignature(paramInfo, dotNet2TS));
                        sbArgs.AppendLine();
                    });

                    if (methodInfo.methodResult)
                    {
                        sbArgs.Append("\t}) => RIAPP.IPromise<");
                        sbArgs.Append(dotNet2TS.RegisterType(methodInfo.GetMethodData().MethodInfo.ReturnType.GetTaskResultType()));
                        sbArgs.Append(">");
                    }
                    else
                    {
                        sbArgs.Append("\t}) => RIAPP.IPromise<void>");
                    }
                }
                else
                {
                    if (methodInfo.methodResult)
                    {
                        sbArgs.Append("() => RIAPP.IPromise<");
                        sbArgs.Append(dotNet2TS.RegisterType(methodInfo.GetMethodData().MethodInfo.ReturnType.GetTaskResultType()));
                        sbArgs.Append(">");
                    }
                    else
                    {
                        sbArgs.Append("() => RIAPP.IPromise<void>");
                    }
                }

                sbISvcMeth.AppendFormat("\t{0}: {1};", methodInfo.methodName, sbArgs.ToString());
                sbISvcMeth.AppendLine();
            });

            sbISvcMeth.AppendLine("}");

            return(TrimEnd(sbISvcMeth.ToString()));
        }
Beispiel #2
0
        private string CreateDictionary(string name, string keyName, string itemName, string aspectName,
                                        string valsName, string properties, List <PropertyInfo> propList, DotNet2TS dotNet2TS)
        {
            PropertyInfo pkProp = propList.Where(propInfo => keyName == propInfo.Name).SingleOrDefault();

            if (pkProp == null)
            {
                throw new Exception(string.Format("Dictionary item does not have a property with a name {0}", keyName));
            }

            string pkVals = pkProp.Name.ToCamelCase() + ": " + dotNet2TS.RegisterType(pkProp.PropertyType);

            Dictionary <string, Func <TemplateParser.Context, string> > dic = new Dictionary <string, Func <TemplateParser.Context, string> >
            {
                { "DICT_NAME", (context) => name },
                { "ITEM_TYPE_NAME", (context) => string.Format("_{0}", itemName) },
                { "ASPECT_NAME", (context) => aspectName },
                { "VALS_NAME", (context) => valsName },
                { "INTERFACE_NAME", (context) => itemName },
                { "PROPS", (context) => properties },
                { "KEY_NAME", (context) => keyName },
                { "PK_VALS", (context) => pkVals }
            };

            return(_dictionaryTemplate.ToString(dic));
        }
Beispiel #3
0
 private string _CreateParamSignature(ParamMetadata paramInfo, DotNet2TS dotNet2TS)
 {
     return(string.Format("{0}{1}: {2}{3};", paramInfo.name, paramInfo.isNullable ? "?" : "",
                          paramInfo.dataType == DataType.None
             ? dotNet2TS.RegisterType(paramInfo.GetParameterType())
             : DotNet2TS.DataTypeToTypeName(paramInfo.dataType),
                          paramInfo.dataType != DataType.None && paramInfo.isArray ? "[]" : ""));
 }
Beispiel #4
0
        private string CreateListItem(string itemName, string aspectName, string valsName,
                                      List <PropertyInfo> propInfos, DotNet2TS dotNet2TS)
        {
            StringBuilder sbProps = new StringBuilder(512);

            propInfos.ForEach(propInfo =>
            {
                sbProps.AppendLine(string.Format("\tget {0}():{1} {{ return <{1}>this._aspect._getProp('{0}'); }}",
                                                 propInfo.Name, dotNet2TS.RegisterType(propInfo.PropertyType)));
                sbProps.AppendLine(string.Format("\tset {0}(v:{1}) {{ this._aspect._setProp('{0}', v); }}",
                                                 propInfo.Name, dotNet2TS.RegisterType(propInfo.PropertyType)));
            });

            Dictionary <string, Func <TemplateParser.Context, string> > dic = new Dictionary <string, Func <TemplateParser.Context, string> >
            {
                { "LIST_ITEM_NAME", (context) => string.Format("_{0}", itemName) },
                { "VALS_NAME", (context) => valsName },
                { "INTERFACE_NAME", (context) => itemName },
                { "ASPECT_NAME", (context) => aspectName },
                { "ITEM_PROPS", (context) => sbProps.ToString() }
            };

            return(_listItemTemplate.ToString(dic));
        }
Beispiel #5
0
 private void ProcessMethodArgs(DotNet2TS dotNet2TS)
 {
     foreach (MethodDescription methodInfo in _metadata.GetInvokeMethods())
     {
         if (methodInfo.parameters.Count() > 0)
         {
             methodInfo.parameters.ForEach(paramInfo =>
             {
                 //if this is complex type parse parameter to create its typescript interface
                 if (paramInfo.dataType == DataType.None)
                 {
                     dotNet2TS.RegisterType(paramInfo.GetParameterType());
                 }
             });
         }
     }
 }
Beispiel #6
0
        private string CreateClientType(Type type, DotNet2TS dotNet2TS)
        {
            DictionaryAttribute dictAttr = type.GetCustomAttributes(typeof(DictionaryAttribute), false)
                                           .OfType <DictionaryAttribute>()
                                           .FirstOrDefault();
            ListAttribute listAttr = type.GetCustomAttributes(typeof(ListAttribute), false).OfType <ListAttribute>().FirstOrDefault();

            if (dictAttr != null && dictAttr.KeyName == null)
            {
                throw new ArgumentException("DictionaryAttribute KeyName property must not be null");
            }

            StringBuilder sb       = new StringBuilder(512);
            string        dictName = null;
            string        listName = null;

            if (dictAttr != null)
            {
                dictName = dictAttr.DictionaryName == null
                    ? $"{type.Name}Dict"
                    : dictAttr.DictionaryName;
            }

            if (listAttr != null)
            {
                listName = listAttr.ListName == null ? $"{type.Name}List" : listAttr.ListName;
            }

            bool   isListItem = dictAttr != null || listAttr != null;
            string valsName   = dotNet2TS.RegisterType(type);

            //can return here if no need to create Dictionary or List
            if (!type.IsClass || !isListItem)
            {
                return(sb.ToString());
            }

            string itemName               = $"{type.Name}ListItem";
            string aspectName             = $"T{type.Name}ItemAspect";
            List <PropertyInfo> propInfos = type.GetProperties().ToList();
            string list_properties        = string.Empty;

            #region Define fn_Properties

            Func <List <PropertyInfo>, string> fn_Properties = props =>
            {
                StringBuilder sbProps = new StringBuilder(256);

                sbProps.Append("[");
                bool isFirst = true;

                props.ForEach(propInfo =>
                {
                    if (!isFirst)
                    {
                        sbProps.Append(",");
                    }

                    DataType dataType = DataType.None;

                    try
                    {
                        dataType = propInfo.PropertyType.IsArrayType() ? DataType.None : dotNet2TS.DataTypeFromDotNetType(propInfo.PropertyType);
                    }
                    catch (UnsupportedTypeException)
                    {
                        dataType = DataType.None;
                    }

                    sbProps.Append("{");
                    sbProps.Append(string.Format("name:'{0}',dtype:{1}", propInfo.Name, (int)dataType));
                    sbProps.Append("}");
                    isFirst = false;
                });

                sbProps.Append("]");

                return(sbProps.ToString());
            };

            #endregion

            if (dictAttr != null || listAttr != null)
            {
                sb.AppendLine(CreateListItem(itemName, aspectName, valsName, propInfos, dotNet2TS));
                sb.AppendLine();
                list_properties = fn_Properties(propInfos);
            }

            if (dictAttr != null)
            {
                sb.AppendLine(CreateDictionary(dictName, dictAttr.KeyName, itemName, aspectName, valsName,
                                               list_properties, propInfos, dotNet2TS));
                sb.AppendLine();
            }

            if (listAttr != null)
            {
                sb.AppendLine(CreateList(listName, itemName, aspectName, valsName, list_properties));
                sb.AppendLine();
            }


            return(sb.ToString());
        }