private static void RenderTsType(TsType type, StringBuilder builder) { var name = type.Name; builder.AppendLine($"export interface {name} {{"); if (type.Properties != null) { foreach (Property v in type.Properties) { builder.AppendLine($" {v.PropertyName}: {v.TypeName}{(v.IsNullable ? " | null" : "")}"); } } builder.AppendLine("}"); builder.AppendLine(""); if (type.DefaultValue != null) { builder.AppendLine($"export const default{name}: {name} = {{"); RenderProperties(type.Properties, builder, 0, 1); builder.AppendLine("}"); } builder.AppendLine(""); }
private static void RenderProperties(List <Property> properties, StringBuilder builder, int depth, int maxDepth) { foreach (Property property in properties) { TsType tsType = property.TsType.IsT0 ? property.TsType.AsT0 : null; if (tsType?.IsCollection == true) { builder.AppendLine($" {property.PropertyName}: [],"); } else if (tsType?.IsPrimitive != true) { builder.AppendLine($" {property.PropertyName}: {{}} as any,"); } else if (tsType != null && !tsType.IsPrimitive && tsType.HasCyclicDependency && !tsType.IsCollection) { if (depth >= maxDepth) { builder.Append("".PadRight(depth)); builder.AppendLine($" {property.PropertyName}: null as any,"); } else { builder.Append("".PadRight(depth)); builder.AppendLine($" {property.PropertyName}: {{"); RenderProperties(property.TsType.AsT0.Properties, builder, depth + 1, maxDepth); builder.Append(" ".PadRight(depth)); builder.AppendLine("},"); } } else { builder.Append("".PadRight(depth)); builder.AppendLine($" {property.PropertyName}: {property.DefaultValue ?? "null"},"); } } }
public override string ToString() { return(PropertyName + ": " + TsType.Match(v => v.Name, v => v.Name)); }