Esempio n. 1
0
        public void ExactNameAndExplicitConversionTypeMatching()
        {
            var source = new A()
            {
                Double = 11
            };

            var mapper = new Mapper(cfg =>
            {
                var matching = new MatchingRules();

                cfg.Conventions.GetOrAdd <DefaultConvention>(convention =>
                {
                    convention.MatchingRules.GetOrAdd <ExactNameMatching>()
                    .GetOrAdd <TypeMatchingRule>(ruleCfg => ruleCfg.AllowExplicitConversions = false);
                });
            });

            var result = mapper.Map <B>(source);

            Assert.IsTrue(result.Double == 0);

            var mapper2 = new Mapper(cfg =>
            {
                cfg.Conventions.GetOrAdd <DefaultConvention>(convention =>
                {
                    convention.MatchingRules.GetOrAdd <ExactNameMatching>()
                    .GetOrAdd <TypeMatchingRule>(ruleCfg => ruleCfg.AllowExplicitConversions = true);
                });
            });

            result = mapper2.Map <B>(source);
            Assert.IsTrue(result.Double == 11);
        }
Esempio n. 2
0
 internal void SetEmptyValuesToNull()
 {
     ProviderStates = ProviderStates?.FirstOrDefault() != null ? ProviderStates : null;
     if (MatchingRules != null)
     {
         MatchingRules.SetEmptyValuesToNull();
     }
 }
Esempio n. 3
0
        protected MatchingRules FindRulesThatCanFire(Tag tagToMatch, Namespace data)
        {
            var result      = new MatchingRules();
            var rulesForTag = rules.GetRules(tagToMatch);

            if (rulesForTag != null)
            {
                int lastMatchingPriority = int.MinValue;
                foreach (RuleBase rule in rulesForTag)
                {
                    // Rules should be stored in decreasing priority, so as soon as we find a priority lower than
                    // a valid match, we know to stop looking.
                    if (rule.Priority < lastMatchingPriority)
                    {
                        break;
                    }

                    // If the tag has no hashtags, it doesn't care, if it does, at least one must be present.
                    if (tagToMatch.HashTags.Count > 0 && !tagToMatch.HashTags.Overlaps(rule.HashTags))
                    {
                        continue;
                    }

                    // The rule can have arbitrary additional requirements (like if-clauses).
                    if (!rule.CanFire(tagToMatch, data))
                    {
                        continue;
                    }

                    result.Add(rule);
                    result.totalFrequency += rule.Frequency;

                    // Only increase the matched priority.
                    if (rule.MinPriority > lastMatchingPriority)
                    {
                        lastMatchingPriority = rule.MinPriority;
                    }
                }
            }
            return(result);
        }
Esempio n. 4
0
 public string Run(TagBase tag, Namespace data = null)
 {
     if (!tag.IsRecursive)
     {
         return(RunNonRecursiveTag(tag as NonRecursiveTag, data));
     }
     else
     {
         Tag           tagToReplace  = tag as Tag;
         MatchingRules matchingRules = FindRulesThatCanFire(tagToReplace, data);
         if (matchingRules.Count == 0)
         {
             return(tagToReplace.ToString());
         }
         else
         {
             RuleBase ruleToFire = matchingRules.ChooseRuleToFire(random);
             return(FireRuleOnTag(ruleToFire, tagToReplace, data));
         }
     }
 }
Esempio n. 5
0
        public void SuffixMatching()
        {
            //Property -> DtoProperty
            var source2 = new TestClass()
            {
                Property = 11
            };
            var mapper2 = new Mapper(cfg =>
            {
                var matching = new MatchingRules();

                cfg.Conventions.GetOrAdd <DefaultConvention>(convention =>
                {
                    convention.MatchingRules.GetOrAdd <SuffixMatching>();
                });
            });

            var result2 = mapper2.Map <SuffixTestClass>(source2);

            Assert.IsTrue(source2.Property == result2.PropertyDto);

            //DtoProperty -> Property
            var source = new SuffixTestClass()
            {
                PropertyDto = 11
            };
            var mapper = new Mapper(cfg =>
            {
                var matching = new MatchingRules();

                cfg.Conventions.GetOrAdd <DefaultConvention>(convention =>
                {
                    convention.MatchingRules.GetOrAdd <SuffixMatching>();
                });
            });

            var result = mapper.Map <TestClass>(source);

            Assert.IsTrue(source.PropertyDto == result.Property);
        }