private TsPropertySymbol GetPropertySymbol(PropertyInfo propertyInfo, DtoTypeSymbolMap dtoSymbolMap) { TsPropertySymbol result = TsPropertySymbol.LoadFrom(propertyInfo, this.symbolLookup, this.options); TsTypeSymbol unwrappedTypeSymbol = result.Type.UnwrapArray(out int arrayRank); if (dtoPropertyTypeSymbolMap.TryGetValue(unwrappedTypeSymbol, out TsTypeSymbol replacementType)) { // We need a type replacement. if (result.Type.IsArray) { replacementType = TsTypeSymbol.CreateArraySymbol(replacementType, arrayRank); } result = new TsPropertySymbol(result.Name, replacementType, result.PropertyMetadata); } else if ((unwrappedTypeSymbol.IsClass || unwrappedTypeSymbol.IsInterface) && this.symbolLookup.ContainsType(unwrappedTypeSymbol.TypeMetadata.Type)) { // We need a DTO Interface for this type. TsTypeSymbol dtoInterfaceType = this.GetOrCreateDtoInterface(unwrappedTypeSymbol, dtoSymbolMap); if (result.Type.IsArray) { dtoInterfaceType = TsTypeSymbol.CreateArraySymbol(dtoInterfaceType, arrayRank); } result = new TsPropertySymbol(result.Name, dtoInterfaceType, result.PropertyMetadata); } return(result); }
/// <summary> /// Writes the symbol dto transformation. /// </summary> /// <param name="propertySymbol">The property symbol.</param> /// <param name="valueAccessor">The value accessor.</param> /// <param name="variableName">The name of the variable to write.</param> /// <param name="interfaceTransformLookup">The interface transform lookup.</param> /// <param name="writer">The writer.</param> /// <exception cref="InvalidOperationException">The specified type does not require DTO transformation: {type.Name}</exception> /// <exception cref="NotSupportedException">Unable to write transformation for the specified type: {type.Name}</exception> public static void WriteSymbolDtoTransformation(TsPropertySymbol propertySymbol, string valueAccessor, string variableName, DtoInterfaceTransformLookup interfaceTransformLookup, SourceWriter writer) { if (!RequiresDtoTransform(propertySymbol.Type)) { throw new InvalidOperationException($"The specified type does not require DTO transformation: {propertySymbol.Type.Name}."); } bool isArray = propertySymbol.Type.IsArray; bool isOptional = propertySymbol.IsOptional; TsTypeSymbol type = propertySymbol.Type.UnwrapArray(); writer.Write($"const {variableName} = "); if (isOptional) { writer.Write($"{valueAccessor} === {TsTypeSymbol.Undefined.Name} ? {TsTypeSymbol.Undefined.Name} : "); } if (type.IsInterface && type.HasDtoInterface) { DtoInterfaceTransformMetadata dtoInterfaceMetadata = interfaceTransformLookup[type.DtoInterface]; if (isArray) { writer.WriteLine($"{valueAccessor}.map({dtoInterfaceMetadata.TransformMethodAccessor});"); } else { writer.WriteLine($"{dtoInterfaceMetadata.TransformMethodAccessor}({valueAccessor});"); } } else if (type.IsClass && type.HasDtoInterface) { if (isArray) { writer.WriteLine($"{valueAccessor}.map({type.Name}.fromDto);"); } else { writer.WriteLine($"{type.Name}.fromDto({valueAccessor});"); } } else if (type == TsTypeSymbol.Date) { if (isArray) { writer.WriteLine($"{valueAccessor}.map(s => new Date(s));"); } else { writer.WriteLine($"new Date({valueAccessor});"); } } else { throw new NotSupportedException($"Unable to write transformation for the specified type: {type.Name}."); } }
/// <summary> /// Loads a <see cref="TsTypeSymbol" /> from the specified <see cref="TsTypeMetadata" /> using the <see cref="TsSymbolLookup" />. /// </summary> /// <param name="typeMetadata">The type metadata.</param> /// <param name="symbolLookup">The symbol lookup.</param> /// <param name="options">The options.</param> /// <returns>A <see cref="TsTypeSymbol" />.</returns> internal static TsTypeSymbol LoadFrom(TsTypeMetadata typeMetadata, TsSymbolLookup symbolLookup, ISymbolLoadOptions options) { if (typeMetadata.Type.IsGenericType) { throw new NotSupportedException("Generic types are not currently supported."); } TsTypeSymbol baseTypeSymbol = null; if (typeMetadata.Type.IsClass && !typeMetadata.Flatten && typeMetadata.Type.BaseType != null && typeMetadata.Type.BaseType != typeof(object)) { baseTypeSymbol = symbolLookup.ResolveSymbol(typeMetadata.Type.BaseType); } List <TsTypeSymbol> interfaceTypeSymbols = new List <TsTypeSymbol>(); if (!typeMetadata.Flatten) { foreach (Type interfaceType in typeMetadata.Type.GetInterfaces()) { if (symbolLookup.TryResolveSymbol(interfaceType, out TsTypeSymbol interfaceTypeSymbol)) { interfaceTypeSymbols.Add(interfaceTypeSymbol); } } } List <TsPropertySymbol> propertySymbols = new List <TsPropertySymbol>(); foreach (PropertyInfo property in typeMetadata.GetProperties()) { propertySymbols.Add(TsPropertySymbol.LoadFrom(property, symbolLookup, options)); } TsTypeSymbol symbol = new TsTypeSymbol ( name: typeMetadata.Name, symbolKind: GetSymbolKind(typeMetadata), baseTypeSymbol, interfaceTypeSymbols, propertySymbols, typeMetadata ); return(symbol); }
private static string GetPropertyTypeIdentifier(TsPropertySymbol property) { return(GetTypeIdentifier(property.Type)); }