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 methodInnerCode = mapCodeFile.InnerMethodAssignment .GetCode(cv.SrcParam, cv.DestParam) .RemoveDoubleBraces(); var forCode = StatementTemplates.For(methodInnerCode, new ForDeclarationContext(cv.SrcCollection, cv.DestCollection, cv.SrcParam, cv.DestParam)); string methodCode = StatementTemplates.Method(forCode, new MethodDeclarationContext(cv.Method, new VariableContext(destCollType, cv.DestCollection), new VariableContext(srcCollType, cv.SrcCollection), new VariableContext(destCollType, cv.DestCollection))); var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method); files.Add(typePair, file); } return(files.ToImmutableDictionary()); }
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); }