public virtual void WriteTypeContent(StringBuilder result, TsType type)
 {
     foreach (var property in type.Properties)
     {
         WriteProperty(result, property);
     }
 }
        public virtual void WriteType(StringBuilder result, TsType type)
        {
            result.Append($"    export {type.Type} {CleanUpTypeName(type.Name)}");

            if (type.BaseType != null)
            {
                result.Append($" extends {type.BaseType}");
            }

            if (type.Interfaces.Any())
            {
                var inheritanceType = type.Type == "interface" ? "extends" : "implements";
                result.Append($" {inheritanceType} {string.Join(", ", type.Interfaces)}");
            }

            result.Append(" {\r\n");
            WriteTypeContent(result, type);
            result.Append("    }\r\n");
        }
Exemple #3
0
        private static void MapType(TsGeneratorSettings settings, Dictionary <string, TsModule> modules, Type type)
        {
            var moduleName = settings.GetModuleName(type.Namespace);

            if (!modules.ContainsKey(moduleName))
            {
                modules.Add(moduleName, new TsModule(moduleName));
            }

            var tsModule = modules[moduleName];
            var typeName = settings.GetTypeName(type);

            if (tsModule.Types.Any(x => x.Name == typeName))
            {
                return;
            }

            var baseType       = type.IsInterface ? null : type.BaseType;
            var baseTypeString = settings.GetBaseType(baseType);

            var interfaces       = type.IsEnum ? new Type[0] : type.GetInterfaces().Where(x => !x.Name.Contains("`")).ToArray();
            var interfaceStrings = interfaces.Select(settings.GetBaseType).ToList();

            var tsType = new TsType(type.IsEnum ? "enum" : (type.IsInterface ? "interface" : "class"), typeName, baseTypeString, interfaceStrings, tsModule);

            if (baseTypeString != null)
            {
                MapType(settings, modules, baseType);
            }

            foreach (var @interface in interfaces)
            {
                MapType(settings, modules, @interface);
            }

            tsModule.Types.Add(tsType);

            foreach (var propertyInfo in type.GetProperties())
            {
                if (!settings.MapProperty(type, propertyInfo))
                {
                    continue;
                }

                var propertyName = settings.GetPropertyName(propertyInfo);
                var propertyType = settings.GetPropertyType(propertyInfo);
                tsType.Properties.Add(new TsProperty(propertyName, propertyType, tsType, null, false));

                foreach (var referencedType in settings.GetReferencedTypes(propertyInfo.PropertyType))
                {
                    MapType(settings, modules, referencedType);
                }
            }

            if (type.IsEnum)
            {
                foreach (var fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Static))
                {
                    if (!settings.MapField(fieldInfo))
                    {
                        continue;
                    }

                    var fieldName  = settings.GetEnumName(fieldInfo);
                    var fieldValue = fieldInfo.GetRawConstantValue();
                    tsType.Properties.Add(new TsProperty(fieldName, null, tsType, fieldValue, false));
                }
            }
            else
            {
                foreach (var fieldInfo in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
                {
                    if (!settings.MapField(fieldInfo))
                    {
                        continue;
                    }

                    var fieldName = settings.GetFieldName(fieldInfo);
                    var fieldType = settings.GetFieldType(fieldInfo);

                    object fieldValue;
                    switch (fieldType)
                    {
                    case "System.Guid":
                        fieldValue = !fieldInfo.IsLiteral && fieldInfo.IsInitOnly ? fieldInfo.GetValue(null).ToString() : null;
                        break;

                    default:
                        fieldValue = fieldInfo.IsLiteral && !fieldInfo.IsInitOnly ? fieldInfo.GetRawConstantValue() : null;
                        break;
                    }
                    tsType.Properties.Add(new TsProperty(fieldName, fieldType, tsType, fieldValue, fieldInfo.IsStatic));
                }
            }
        }