Exemple #1
0
        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());
        }
Exemple #2
0
        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);
        }