private void ProcessPropertyTypePair(Coder coder, PropertyNameContext context, PropertyMap propertyMap) { var typePair = propertyMap.GetTypePair(); //typepair already in template cache string text; if (TemplateCache.TryGetValue(typePair, out text)) { coder.ApplyTemplate(context, text); return; } TypeMap nodeMap; //typepair explicitly mapped by user ExplicitTypeMaps.TryGetValue(typePair, out nodeMap); if (nodeMap == null) { if (!ImplicitTypeMaps.TryGetValue(typePair, out nodeMap)) { //create implicit map nodeMap = TypeMapFactory.CreateTypeMap(propertyMap.SrcType, propertyMap.DestType, Options); ImplicitTypeMaps.AddIfNotExist(nodeMap); } } var propAssignment = ProcessTypeMap(nodeMap, context.SrcFullMemberName, context.DestFullMemberName); coder.AttachPropertyAssignment(propAssignment, propertyMap); return; }
public void Create_ValidTypes_ContextCanBeCreated() { var propertyMap = PropertyMapFactory.CreateReal(typeof(A), typeof(B), nameof(A.P1), nameof(B.P1)); var context = new PropertyNameContext(propertyMap); Assert.IsNotNull(context); }
private Assignment ProcessTypeMap(TypeMap rootMap, string srcFieldName, string destFieldName) { var coder = new Coder(); foreach (PropertyMap propertyMap in rootMap.PropertyMaps) { RememberTypeLocations(propertyMap); var context = new PropertyNameContext(propertyMap, srcFieldName, destFieldName); if (propertyMap.Ignored) { continue; } //assign without explicit cast if (propertyMap.DestType.IsAssignableFrom(propertyMap.SrcType) || propertyMap.DestType.IsImplicitCastableFrom(propertyMap.SrcType)) { //TODO: need to determine explicit casts and produce cast operators coder.SimpleAssign(context); continue; } else { bool referenceType = propertyMap.DestType.IsClass; //TODO: perfomance degrades on each null check! Try to avoid it if possible! if (referenceType) { coder.NullCheck(context); coder.AttachRawCode(" else {{"); coder.AppendNoParameterlessCtorException(context, propertyMap.DestType); } ProcessPropertyTypePair(coder, context, propertyMap); if (referenceType) { coder.AttachRawCode("}}"); } } } var assignment = coder.GetAssignment(); TemplateCache.AddIfNotExist(rootMap.TypePair, assignment.RelativeTemplate); return(assignment); }
public void Condition_IsNull_NoCodeAppears() { string srcFieldName = "x"; string destFieldName = "y"; var propertyMap = CreatePropertyMap <A, B>("P1", "P1"); var context = new PropertyNameContext(propertyMap); var coder = new Recorder(); using (var condition = new ConditionPrinter(context, coder)) { } Assert.IsNullOrEmpty(coder.ToAssignment().RelativeTemplate); }
public void Condition_IsNotNull_PrintCode() { string srcFieldName = "x"; string destFieldName = "y"; var propertyMap = CreatePropertyMap <A, B>("P1", "P1"); var context = new PropertyNameContext(propertyMap); var coder = new Recorder(); Expression <Func <A, bool> > exp = src => src.P1 != 0; propertyMap.OriginalCondition = new OriginalStatement(exp); using (var condition = new ConditionPrinter(context, coder)) { } string template = coder.ToAssignment().RelativeTemplate.Replace(Environment.NewLine, ""); Assert.AreEqual("if ({0}.P1 != 0){{}}", template); }
private void ParseNode(IParseTree node) { if (node is SourceElementsContext) { SourceElementsContext elementsContext = node as SourceElementsContext; SourceElementContext[] srcElements = elementsContext.sourceElement(); foreach (SourceElementContext srcElement in srcElements) { StatementContext statementContext = srcElement.statement(); VariableStatementContext varStatementContext = statementContext.variableStatement(); FunctionDeclarationContext functionContext = statementContext.functionDeclaration(); ImportStatementContext importContext = statementContext.importStatement(); ExportStatementContext exportContext = statementContext.exportStatement(); if (varStatementContext != null) { VariableDeclarationListContext variableListContex = varStatementContext.variableDeclarationList(); if (variableListContex != null) { var variables = variableListContex.children; foreach (var variable in variables) { if (variable is VariableDeclarationContext) { VariableDeclarationContext declarationContext = variable as VariableDeclarationContext; string name = declarationContext.assignable().GetText(); ObjectLiteralExpressionContext[] exps = declarationContext.GetRuleContexts <ObjectLiteralExpressionContext>(); if (exps == null || exps.Length == 0) { this.WriteKeyValue("Field", name); } else { this.WriteKeyValue("Class", name); this.WriteBeginBrace(); foreach (ObjectLiteralExpressionContext exp in exps) { ObjectLiteralContext literalContext = exp.objectLiteral(); PropertyAssignmentContext[] properties = literalContext.propertyAssignment(); foreach (PropertyAssignmentContext property in properties) { PropertyNameContext propertyNameContext = property.GetRuleContext <PropertyNameContext>(0); FunctionExpressionContext funcExpContext = property.GetRuleContext <FunctionExpressionContext>(0); bool isFunction = funcExpContext != null; string propertyName = propertyNameContext.identifierName().GetText(); this.WriteKeyValue(isFunction ? "Function" : "Property", propertyName); } } this.WriteEndBrace(); } } } } } else if (functionContext != null) { string name = functionContext.Identifier().GetText(); this.WriteKeyValue("Function", name); } else if (importContext != null) { ImportFromBlockContext fromContext = importContext.importFromBlock(); ModuleItemsContext[] moduleItems = fromContext.GetRuleContexts <ModuleItemsContext>(); foreach (ModuleItemsContext module in moduleItems) { AliasNameContext[] aliasNames = module.aliasName(); foreach (AliasNameContext aliasName in aliasNames) { string name = aliasName.identifierName().LastOrDefault().GetText(); this.WriteKeyValue("Import", name); } } } else if (exportContext != null) { DeclarationContext[] declarations = exportContext.GetRuleContexts <DeclarationContext>(); ExportFromBlockContext[] exportBlocks = exportContext.GetRuleContexts <ExportFromBlockContext>(); foreach (DeclarationContext declaration in declarations) { FunctionDeclarationContext funcContext = declaration.functionDeclaration(); if (funcContext != null) { string name = funcContext.Identifier().GetText(); this.WriteKeyValue("Export", name); } } foreach (ExportFromBlockContext exportBlock in exportBlocks) { ModuleItemsContext[] moduleItems = exportBlock.GetRuleContexts <ModuleItemsContext>(); foreach (ModuleItemsContext module in moduleItems) { AliasNameContext[] aliasNames = module.aliasName(); foreach (AliasNameContext aliasName in aliasNames) { IdentifierNameContext[] identifierNames = aliasName.identifierName(); string name = identifierNames.LastOrDefault().Identifier().GetText(); this.WriteKeyValue("Export", name); } } } } } } }
public PropertyNameContext propertyName() { PropertyNameContext _localctx = new PropertyNameContext(Context, State); EnterRule(_localctx, 8, RULE_propertyName); try { EnterOuterAlt(_localctx, 1); { State = 82; label(); } } catch (RecognitionException re) { _localctx.exception = re; ErrorHandler.ReportError(this, re); ErrorHandler.Recover(this, re); } finally { ExitRule(); } return _localctx; }