/// <summary>
        /// Tries to map the given instance into generic destination type using an specific key service.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="keyService">The key service.</param>
        /// <returns></returns>
        public TDestination TryToMap <TSource, TDestination>(TSource source, object keyService)
        {
            object src = source;

            if (src == null)
            {
                return(default(TDestination));
            }

            var serviceMapper =
                this.mapperResolver.FirstOrDefault(
                    transformer => transformer.Match(keyService, typeof(TSource), typeof(TDestination)));

            if (serviceMapper == null)
            {
                return(default(TDestination));
            }

            ISimpleMapper <TSource, TDestination> mapper = serviceMapper.ServiceAs <ISimpleMapper <TSource, TDestination> >();

            if (mapper != null)
            {
                return(mapper.Map(source));
            }

            ISourceMapper mp = serviceMapper.ServiceAs <ISourceMapper>();

            if (mp == null)
            {
                return(default(TDestination));
            }
            return((TDestination)mp.Map(source));
        }
Ejemplo n.º 2
0
        public void Test()
        {
            ISourceMapper <Student, Person> mapper1 = FactoryMapper.DynamicResolutionMapper <Student, Person>();
            ISourceMapper mapper2 = FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(PersonDetails));
            ISourceMapper mapper3 = FactoryMapper.DynamicResolutionMapper(typeof(IPersonHeader), typeof(PersonDetails));


            IList <IPropertyMapper <Student, Person> > propMappers = new List <IPropertyMapper <Student, Person> >
            {
                new PropertyMapper <Student, Person>((student, person) => person.Name          = student.Name, "Name", "Name")
                , new PropertyMapper <Student, Person>((student, person) => person.AnnoNascita = student.AnnoNascita)
                , new PropertyMapper <Student, Person>((student, person) => person.Parent      = student.Father)
            };

            SourceMapper <Student, Person> mapper4 = new SourceMapper <Student, Person>(propMappers, null, null);
            StudentDetails example = new StudentDetails();

            ServiceTransformer <ISourceMapper <Student, Person> > srv1 = new ServiceTransformer <ISourceMapper <Student, Person> >("default", mapper4);

            var res1 = srv1.Match <ISourceMapper <Student, Person> >("default");
            var res2 = srv1.Match <SourceMapper <Student, Person> >("default");

            var res3 = srv1.Match <ISourceMapper <Student, Person> >("ss");
            var res4 = srv1.Match <SourceMapper <Student, Person> >("ss");
            var res5 = srv1.Match("default", example.GetType(), typeof(Person));

            Assert.IsTrue(res1);
            Assert.IsFalse(res2);
            Assert.IsFalse(res3);
            Assert.IsFalse(res4);
            Assert.IsTrue(res5);
        }
        public VBScriptStringContentProvider(string code, ISourceMapper mapper)
        {
            ContractUtils.RequiresNotNull(code, "code");

            _code   = code;
            _mapper = mapper;
        }
Ejemplo n.º 4
0
        //[Test]
        public void TestReflection()
        {
            TransformerObserver  a  = new TransformerObserver();
            ITransformerResolver aa = a;

            ISourceMapper <Person, PersonaGiuridica> mapper0 = FactoryMapper.DynamicResolutionMapper <Person, PersonaGiuridica>();
            SourceMapper <Person, PersonaGiuridica>  mapper  = new SourceMapper <Person, PersonaGiuridica>(new List <IPropertyMapper <Person, PersonaGiuridica> >(), null, null);
            //aa.Register<ISourceMapper<Person, PersonaGiuridica>>(mapper);
            //aa.Register(mapper0);


            object obj1 = new Person();
            object obj2 = new Person();

            Type t1 = typeof(IPersonHeader);
            Type t2 = typeof(Person);

            try
            {
                object instance = 5.5;
                //int i = (int) instance;

                byte bb = Convert.ToByte(instance);

                byte   b = (byte)instance;
                double d = (double)instance;
            }
            catch (Exception)
            {
                Assert.IsFalse(true, "Cast invalid");
            }

            Compare <IPersonHeader>(obj1, obj2);
            //Compare<long>(1, 10);
        }
Ejemplo n.º 5
0
        public override ScriptCode CompileSourceCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
        {
            using (var reader = sourceUnit.GetReader()) {
                try
                {
                    switch (sourceUnit.Kind)
                    {
                    case SourceCodeKind.SingleStatement:
                    case SourceCodeKind.Expression:
                    case SourceCodeKind.AutoDetect:
                    case SourceCodeKind.InteractiveCode:
                        return(new VBScriptCode(
                                   _vbscript, _vbscript.ParseExprToLambda(reader),
                                   sourceUnit));

                    case SourceCodeKind.Statements:
                    case SourceCodeKind.File:
                        return(new VBScriptCode(
                                   _vbscript,
                                   _vbscript.ParseFileToLambda(sourceUnit.Path, reader),
                                   sourceUnit));

                    default:
                        throw Assert.Unreachable;
                    }
                }
                catch (VBScriptCompilerException ex)
                {
                    VBScriptSourceCodeReader vbscriptReader = reader as VBScriptSourceCodeReader;
                    if (vbscriptReader != null)
                    {
                        ISourceMapper mapper = vbscriptReader.SourceMapper;
                        if (mapper != null)
                        {
                            foreach (VBScriptSyntaxError error in ex.SyntaxErrors)
                            {
                                DocSpan docSpan = mapper.Map(error.Span);
                                error.FileName = docSpan.Uri;
                                error.Span     = docSpan.Span;
                            }
                        }
                    }
                    throw ex;
                }
                catch (Exception e)
                {
                    // Real language implementation would have a specific type
                    // of exception.  Also, they would pass errorSink down into
                    // the parser and add messages while doing tighter error
                    // recovery and continuing to parse.
                    errorSink.Add(sourceUnit, e.Message, SourceSpan.None, 0,
                                  Severity.FatalError);
                    return(null);
                }
            }
        }
Ejemplo n.º 6
0
        public void TestNonGenericMapper1()
        {
            ISourceMapper    mapper = FactoryMapper.DynamicResolutionMapper(typeof(PersonaGiuridica), typeof(PersonDetails));
            PersonaGiuridica person = new PersonaGiuridica
            {
                Code        = "150",
                Name        = "Sergio",
                Surname     = "Hill",
                AnnoNascita = 1980,
                Parent      = new Person {
                    Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950
                }
            };

            var result = mapper.Map(person);

            Assert.IsNotNull(result);
        }
Ejemplo n.º 7
0
        public void TestMapperNonPublicMembers()
        {
            ISourceMapper <PersonaGiuridica, PersonDetails> mapper = FactoryMapper.DynamicResolutionMapper <PersonaGiuridica, PersonDetails>();
            PersonaGiuridica person = new PersonaGiuridica
            {
                Code        = "150",
                Name        = "Sergio",
                Surname     = "Hill",
                AnnoNascita = 1980,
                Parent      = new Person {
                    Name = "fatherName", Surname = "fatherSurname", AnnoNascita = 1950
                }
            };

            var result = mapper.Map(person);

            Assert.IsNotNull(result);
        }
Ejemplo n.º 8
0
        public void TestMapper()
        {
            TransformerObserver             observer = new TransformerObserver();
            ISourceMapper <Student, Person> mapper1  = FactoryMapper.DynamicResolutionMapper <Student, Person>();

            observer.RegisterMapper(mapper1);
            Student st = new Student {
                Name = "mario", Surname = "monti", AnnoNascita = 19
            };

            var res = observer.TryToMap <Student, Person>(st);

            Assert.IsNotNull(res);
            Assert.AreEqual(st.Name, res.Name);
            Assert.AreEqual(st.Surname, res.Surname);
            Assert.IsNull(st.Father);
            Assert.IsNull(res.Parent);
        }
        /// <summary>
        /// Tries to map the given instance into destination type using an specific key service.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destinationType">Type of the destination.</param>
        /// <param name="keyService">The key service.</param>
        /// <returns></returns>
        public object TryToMap(object source, Type destinationType, object keyService)
        {
            if (source == null)
            {
                return(null);
            }

            var serviceMapper = this.mapperResolver.FirstOrDefault(
                transformer => transformer.Match(keyService, source.GetType(), destinationType));

            if (serviceMapper == null)
            {
                return(null);
            }

            ISourceMapper mapper = serviceMapper.ServiceAs <ISourceMapper>();

            return(mapper.Map(source));
        }
Ejemplo n.º 10
0
        ParseExprToLambda(TextReader reader)
        {
            var scanner    = new VB.Scanner(reader);
            var errorTable = new List <VB.SyntaxError>();
            var ast        = new VB.Parser().ParseScriptFile(scanner, errorTable);

            VBScriptSourceCodeReader sourceReader = reader as VBScriptSourceCodeReader;
            ISourceMapper            mapper       = null;

            if (sourceReader != null)
            {
                mapper = sourceReader.SourceMapper;
            }

            var scope = new AnalysisScope(
                null,
                "__snippet__",
                this,
                Expression.Parameter(typeof(VBScript), "vbscriptRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider), "fileModule"),
                mapper
                );

            List <Expression> body = new List <Expression>();

            body.Add(Expression.Convert(VBScriptGenerator.GenerateExpr(ast, scope), typeof(object)));

            if (scope.Errors.Count > 0)
            {
                throw new VBScriptCompilerException(scope.Errors);
            }

            var moduleFun = Expression.Lambda <Action <VBScript, IDynamicMetaObjectProvider> >(
                Expression.Block(body),
                scope.RuntimeExpr,
                scope.ModuleExpr
                );

            return(moduleFun.Compile());
        }
Ejemplo n.º 11
0
        public AnalysisScope(AnalysisScope parent,
                             string name,
                             VBScript runtime,
                             ParameterExpression runtimeParam,
                             ParameterExpression moduleParam,
                             ISourceMapper mapper)
        {
            _parent       = parent;
            _name         = name;
            _runtime      = runtime;
            _runtimeParam = runtimeParam;
            _moduleParam  = moduleParam;
            _mapper       = mapper;

            if (_moduleParam != null)
            {
                _functionTable = new Set <string>(StringComparer.InvariantCultureIgnoreCase);
                _errors        = new List <VBScriptSyntaxError>();
                _docInfos      = new Dictionary <string, SymbolDocumentInfo>();
            }

            _names = new Dictionary <string, ParameterExpression>();
        }
Ejemplo n.º 12
0
        ParseFileToLambda(string filename, TextReader reader)
        {
            var scanner    = new VB.Scanner(reader);
            var errorTable = new List <VB.SyntaxError>();

            var block = new VB.Parser().ParseScriptFile(scanner, errorTable);

            if (errorTable.Count > 0)
            {
                List <VBScriptSyntaxError> errors = new List <VBScriptSyntaxError>();
                foreach (VB.SyntaxError error in errorTable)
                {
                    errors.Add(new VBScriptSyntaxError(
                                   filename,
                                   SourceUtil.ConvertSpan(error.Span),
                                   (int)error.Type,
                                   error.Type.ToString())
                               );
                }
                throw new VBScriptCompilerException(errors);
            }

            VBScriptSourceCodeReader sourceReader = reader as VBScriptSourceCodeReader;
            ISourceMapper            mapper       = null;

            if (sourceReader != null)
            {
                mapper = sourceReader.SourceMapper;
            }

            var scope = new AnalysisScope(
                null,
                filename,
                this,
                Expression.Parameter(typeof(VBScript), "vbscriptRuntime"),
                Expression.Parameter(typeof(IDynamicMetaObjectProvider), "fileModule"),
                mapper);

            //Generate function table
            List <Expression> body = new List <Expression>();

            //Add the built in globals
            ParameterExpression err = Expression.Parameter(typeof(ErrObject), ERR_PARAMETER);

            scope.Names.Add(ERR_PARAMETER, err);
            body.Add(
                Expression.Assign(
                    err,
                    Expression.New(typeof(ErrObject))
                    )
                );

            if (Trace)
            {
                ParameterExpression trace = Expression.Parameter(typeof(ITrace), TRACE_PARAMETER);
                scope.Names.Add(TRACE_PARAMETER, trace);
                body.Add(
                    Expression.Assign(
                        trace,
                        Expression.Convert(
                            Expression.Dynamic(
                                scope.GetRuntime().GetGetMemberBinder(TRACE_PARAMETER),
                                typeof(object),
                                scope.GetModuleExpr()
                                ),
                            typeof(ITrace)
                            )
                        )
                    );
            }

            //Put module variables and functions into the scope
            VBScriptAnalyzer.AnalyzeFile(block, scope);

            //Generate the module level code other than the methods:
            if (block.Statements != null)
            {
                foreach (var s in block.Statements)
                {
                    if (s is VB.MethodDeclaration)
                    {
                        //Make sure methods are created first before being executed
                        body.Insert(0, VBScriptGenerator.GenerateExpr(s, scope));
                    }
                    else
                    {
                        Expression stmt = VBScriptGenerator.GenerateExpr(s, scope);
                        if (scope.VariableScope.IsOnErrorResumeNextOn)
                        {
                            stmt = VBScriptGenerator.WrapTryCatchExpression(stmt, scope);
                        }
                        Expression debugInfo      = null;
                        Expression clearDebugInfo = null;
                        if (Trace && s is VB.Statement && !(s is VB.BlockStatement))
                        {
                            debugInfo = VBScriptGenerator.GenerateDebugInfo(s, scope, out clearDebugInfo);
                            body.Add(debugInfo);
                        }

                        body.Add(stmt);

                        if (clearDebugInfo != null)
                        {
                            body.Add(clearDebugInfo);
                        }
                    }
                }
            }

            body.Add(Expression.Constant(null)); //Stop anything from returning

            if (scope.Errors.Count > 0)
            {
                throw new VBScriptCompilerException(scope.Errors);
            }

            //if (Debug)
            //{
            //    Expression registerRuntimeVariables = VBScriptGenerator.GenerateRuntimeVariablesExpression(scope);

            //    body.Insert(0, registerRuntimeVariables);
            //}

            var moduleFun = Expression.Lambda <Action <VBScript, IDynamicMetaObjectProvider> >(
                Expression.Block(
                    scope.Names.Values,
                    body),
                scope.RuntimeExpr,
                scope.ModuleExpr);

            //if (!Debug)
            //{
            return(moduleFun.Compile());
            //}
            //else
            //{
            //    Expression<Action<VBScript, IDynamicMetaObjectProvider>> lambda =  (Expression<Action<VBScript, IDynamicMetaObjectProvider>>)DebugContext.TransformLambda(moduleFun);
            //    return lambda.Compile();
            //}
        }
 public VBScriptSourceCodeReader(TextReader textReader, Encoding encoding, ISourceMapper mapper)
     : base(textReader, encoding)
 {
     _mapper = mapper;
 }