private ImmutableDictionary <TypePair, CodeFile> CreateCodeFilesDictionary() { var files = new Dictionary <TypePair, CodeFile>(); var cv = NameConventionsStorage.Map; foreach (var kvp in ExplicitTypeMaps) { TypePair typePair = kvp.Key; TypeMap map = kvp.Value; string srcType = typePair.SourceType.FullName.NormalizeTypeName(); string destType = typePair.DestinationType.FullName.NormalizeTypeName(); var assignment = MethodInnerCodeBuilder.GetAssignment(map); string methodInnerCode = assignment.GetCode(cv.SrcParam, cv.DestParam); string methodCode = StatementTemplates.Method(methodInnerCode, new MethodDeclarationContext(cv.Method, new VariableContext(destType, cv.DestParam), new VariableContext(srcType, cv.SrcParam), new VariableContext(destType, cv.DestParam))); var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method, assignment); files.Add(typePair, file); } return(files.ToImmutableDictionary()); }
public ImmutableDictionary <TypePair, CodeFile> CreateCodeFilesDictionary( ImmutableDictionary <TypePair, CodeFile> parentFiles) { var files = new Dictionary <TypePair, CodeFile>(); var cv = NameConventionsStorage.Map; foreach (var kvp in ExplicitTypeMaps) { TypePair typePair = kvp.Key; var mapCodeFile = parentFiles[typePair]; string srcType = typePair.SourceType.FullName.NormalizeTypeName(); string destType = typePair.DestinationType.FullName.NormalizeTypeName(); string arg1 = $"{cv.SrcParam} as {srcType}"; string arg2 = StatementTemplates.New(destType); var methodCall = StatementTemplates.MethodCall(mapCodeFile.GetClassAndMethodName(), arg1, arg2); string methodCode = StatementTemplates.Method(string.Empty, new MethodDeclarationContext(cv.Method, new VariableContext(destType, methodCall), new VariableContext(typeof(object).Name, cv.SrcParam))); var file = TextBuilderHelper.CreateFile(typePair, methodCode, cv.Method); files.Add(typePair, file); } return(files.ToImmutableDictionary()); }
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 AssignReferenceTypes(PropertyNameContext ctx) { Recorder recorder = new Recorder(); //has parameterless ctor if (ctx.DestType.HasParameterlessCtor()) { recorder.AppendLine($"{{1}} = {StatementTemplates.New(ctx.DestTypeFullName)};"); } else { throw new HappyMapperException(ErrorMessages.NoParameterlessCtor(ctx.DestType)); } string template; //typepair isn't in template cache if (!TemplateCache.TryGetValue(ctx.TypePair, out template)) { var nodeMap = GetTypeMap(ctx.TypePair); var assignment = ProcessTypeMap(nodeMap); template = assignment.RelativeTemplate; } recorder.AppendLine(template); return(recorder.ToAssignment().RelativeTemplate); }
public static CodeFile CreateFile( TypePair typePair, string methodCode, string methodName, Assignment assignment = default(Assignment)) { var Convention = NameConventionsStorage.Mapper; string shortClassName = Convention.CreateUniqueMapperMethodNameWithGuid(typePair); string fullClassName = $"{Convention.Namespace}.{shortClassName}"; string classCode = StatementTemplates.Class(methodCode, Convention.Namespace, shortClassName); return(new CodeFile(classCode, fullClassName, methodName, typePair, assignment)); }
public string BuildCode() { List <string> members = new List <string>(); IterateStatements((tm, statement) => { string memberCode = CreateMemberCode(statement, tm); memberCode = memberCode.RemoveDoubleBraces(); members.Add(memberCode); } ); string code = StatementTemplates.Class(members, Convention.Namespace, Convention.ClassShortName); return(code); }
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()); }
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); }