public ImmutableDictionary <TypePair, CodeFile> CreateCodeFilesDictionary( ImmutableDictionary <TypePair, CodeFile> parentFiles) { var files = new Dictionary <TypePair, CodeFile>(); var cv = NameConventionsStorage.MapCollection; foreach (var kvp in ExplicitTypeMaps) { TypePair typePair = kvp.Key; var mapCodeFile = parentFiles[typePair]; string srcCollType = cv.GetCollectionType(typePair.SourceType.FullName); string destCollType = cv.GetCollectionType(typePair.DestinationType.FullName); //string filler = CreationTemplates.NewObject(typePair.DestinationType); var builder = new StringBuilder(); builder.AppendLine($"var {cv.SrcParam} = {cv.SrcCollection} as {srcCollType};"); builder.AppendLine($"var {cv.DestParam} = {cv.DestCollection} as {destCollType};"); //builder.AppendLine($"{cv.DestParam}.Add({cv.SrcParam}.Count, () => {filler});"); builder.AppendLine(CreationTemplates.Add(cv.DestParam, $"{cv.SrcParam}.Count", typePair.DestinationType)); string methodCall = StatementTemplates.MethodCall( mapCodeFile.GetClassAndMethodName(), cv.SrcParam, cv.DestParam); builder.AppendLine(methodCall); string methodCode = StatementTemplates.Method(builder.ToString(), new MethodDeclarationContext(cv.Method, null, new VariableContext(typeof(object).Name, cv.SrcCollection), new VariableContext(typeof(object).Name, cv.DestCollection)) ); var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method); files.Add(typePair, file); } return(files.ToImmutableDictionary()); }
public static string Add(string collection, string countCode, Type fillerType) { string filler = CreationTemplates.NewObject(fillerType); return($"{collection}.Add({countCode}, () => {filler});"); }
private string AssignCollections(IPropertyNameContext ctx) { var recorder = new Recorder(); var itemSrcType = ctx.SrcType.GenericTypeArguments[0]; var itemDestType = ctx.DestType.GenericTypeArguments[0]; using (var block = new Block(recorder, "if", "{1} == null")) { string newCollection = CreationTemplates.NewCollection(ctx.DestType, "{0}.Count"); recorder.AppendLine($"{{1}} = {newCollection};"); } using (var block = new Block(recorder, "else")) { recorder.AppendLine("{1}.Clear();"); } //fill new (or cleared) collection with the new set of items recorder.AppendLine(CreationTemplates.Add("{1}", "{0}.Count", itemDestType)); //inner cycle variables (on each iteration itemSrcName is mapped to itemDestName). string itemSrcName = "src_" + NamingTools.NewGuid(4); string itemDestName = "dest_" + NamingTools.NewGuid(4); var typePair = new TypePair(itemSrcType, itemDestType); Assignment itemAssignment = new Assignment(); string cachedTemplate; if (TemplateCache.TryGetValue(typePair, out cachedTemplate)) { itemAssignment.RelativeTemplate = cachedTemplate; } else if (itemSrcType.IsCollectionType() && itemDestType.IsCollectionType()) { var innerContext = PropertyNameContextFactory.CreateWithoutPropertyMap( itemSrcType, itemDestType, itemSrcName, itemDestName); string innerTemplate = AssignCollections(innerContext); itemAssignment.RelativeTemplate = innerTemplate; } else { var nodeMap = GetTypeMap(typePair); itemAssignment = ProcessTypeMap(nodeMap); } string iterationCode = itemAssignment.GetCode(itemSrcName, itemDestName); string forCode = StatementTemplates.For(iterationCode, new ForDeclarationContext("{0}", "{1}", itemSrcName, itemDestName)); recorder.AppendLine(forCode); string template = recorder.ToAssignment().RelativeTemplate; return(template); }