/// <summary> /// Generate value code for typescript in including value check /// </summary> /// <param name="type"></param> /// <param name="value"></param> /// <returns></returns> public string GetValue(Type type, object value, int indentation = 0) { var category = _typeDictionary[type].ProcessingCategory; if (category == ProcessingCategory.Primitive) { if (value == null) { return(null); } return(GetPrimitive(value)); } if (category == ProcessingCategory.Enum) { return(GetEnumValue(_nameConvertor.GetName(type), value)); } if (category == ProcessingCategory.Collection) { return(GenerateArray(value)); } if (category == ProcessingCategory.Dictionary) { return(GenerateDictionary(value, indentation)); } if (category == ProcessingCategory.Object) { return(GenerateObject(value, indentation)); } return(""); }
public string Generate(Type type) { var stringBuilder = new StringBuilder(); var names = Enum.GetNames(type); var values = Enum.GetValues(type).Cast <int>().ToArray(); // add typescript enum stringBuilder.AppendLine($"export enum {_nameConvertor.GetName(type)} {{"); for (int i = 0; i < names.Length; i++) { stringBuilder.AppendLine($" {names[i]} = {values[i]}{(i == names.Length - 1 ? string.Empty : ",")}"); } stringBuilder.AppendLine("}"); // add typescript enum array stringBuilder.AppendLine($"export const {_nameConvertor.GetName(type)}Array = ["); for (int i = 0; i < names.Length; i++) { stringBuilder.AppendLine($" {names[i].ToTypeScript()},"); } stringBuilder.AppendLine("];"); return(stringBuilder.ToString());; }
public string Generate(Type type) { var stringBuilder = new StringBuilder(); var typeScriptType = _nameConvertor.GetName(type); stringBuilder.AppendLine($"export class {typeScriptType} {{"); stringBuilder.AppendLine(" constructor("); object instance = null; // create new instance if type contains parameterless constractor if (type.GetConstructor(Type.EmptyTypes) != null) { instance = Activator.CreateInstance(type); } var properties = TypeExtensions.GetProperties(type).ToList().OrderBy(x => x.Name); foreach (var property in properties) { if (property.IsTypeScriptIgnored()) { continue; } var propertyType = property.PropertyType; var propertyName = _nameConvertor.GetName(propertyType); var propertyValue = instance == null ? null : property.GetValue(instance); var valueCode = _valueConvertor.GetValue(propertyType, propertyValue); var noValueCode = string.IsNullOrEmpty(valueCode); var nullableCode = _typeHelper.IsNullable(propertyType) && noValueCode ? "?" : ""; stringBuilder.Append($" public {property.Name.ToCamelCase()}{nullableCode}: "); if (!noValueCode) { stringBuilder.AppendLine($"{ propertyName} = {valueCode},"); } else { stringBuilder.AppendLine($"{ propertyName},"); } } stringBuilder.AppendLine(" ) { }"); stringBuilder.AppendLine("}"); return(stringBuilder.ToString()); }
public string Generate(Type type) { var stringBuilder = new StringBuilder(); var typeScriptType = _nameConvertor.GetName(type); stringBuilder.AppendLine($"export interface {typeScriptType} {{"); object instance = null; // create new instance if type contains parameterless constractor if (type.GetConstructor(Type.EmptyTypes) != null) { instance = Activator.CreateInstance(type); } var properties = TypeExtensions.GetProperties(type).ToList().OrderBy(x => x.Name); foreach (var property in properties) { if (property.IsTypeScriptIgnored()) { continue; } var propertyType = property.PropertyType; var propertyName = _nameConvertor.GetName(propertyType); var nullableCode = _typeHelper.IsNullable(propertyType) ? "?" : ""; stringBuilder.AppendLine($" {property.Name.ToCamelCase()}{nullableCode}: {propertyName};"); } stringBuilder.AppendLine("}"); return(stringBuilder.ToString()); }
public string Generate(Type type) { var stringBuilder = new StringBuilder(); var name = _nameConvertor.GetName(type).ToCamelCase(); stringBuilder.AppendLine($"export const {name} = {{"); object instance = null; // create new instance if type contains parameterless constractor if (type.GetConstructor(Type.EmptyTypes) != null) { instance = Activator.CreateInstance(type); } var properties = TypeExtensions.GetProperties(type); foreach (var property in properties) { if (property.IsTypeScriptIgnored()) { continue; } var propertyName = property.Name.ToCamelCase(); var propertyType = property.PropertyType; var propertyValue = instance == null ? null : property.GetValue(instance); var valueCode = _valueConvertor.GetValue(propertyType, propertyValue, 2); if (!string.IsNullOrEmpty(valueCode)) { stringBuilder.AppendLine($" {propertyName}: {valueCode},"); } } stringBuilder.AppendLine("}"); return(stringBuilder.ToString()); }