<commit_before>﻿namespace Miruken.Map
{
    using System.Collections.Generic;
    using System.Linq;
    using Callback;
    using Callback.Policy;

    public class MapsAttribute : DefinitionAttribute
    {
        public MapsAttribute()
        {
            Strict = true;
        }

        public override CallbackPolicy CallbackPolicy => Policy;

        public override bool Approve(object callback, PolicyMethodBinding binding)
        {
            var mapping = (Mapping)callback;
            var format  = mapping.Format;
            if (format == null) return true;
            var dispatch = binding.Dispatcher;
            var formats  = dispatch.Attributes
                .OfType<IFormatMatching>().ToArray();
            if (Matches(format, formats)) return true;
            var sharedFormats = dispatch.Owner.Attributes
                .OfType<IFormatMatching>().ToArray();
            if (Matches(format, sharedFormats)) return true;
            return formats.Length == 0 && sharedFormats.Length == 0;
        }

        private static bool Matches(
            object format, IEnumerable<IFormatMatching> formats)
        {
            return formats.Any(f => f.Matches(format));
        }

        public static readonly CallbackPolicy Policy =
            BivariantPolicy.Create<Mapping>(m => m.Type, m => m.Source,
                x => x.MatchCallbackMethod(x.ReturnKey, x.Target)
                      .MatchMethod(x.ReturnKey.OrVoid, x.Callback)
            );
    }
}<commit_msg>Require Format match if provided<commit_after>﻿namespace Miruken.Map
{
    using System.Collections.Generic;
    using System.Linq;
    using Callback;
    using Callback.Policy;

    public class MapsAttribute : DefinitionAttribute
    {
        public MapsAttribute()
        {
            Strict = true;
        }

        public override CallbackPolicy CallbackPolicy => Policy;

        public override bool Approve(object callback, PolicyMethodBinding binding)
        {
            var mapping = (Mapping)callback;
            var format  = mapping.Format;
            if (format == null) return true;
            var dispatch = binding.Dispatcher;
            return Matches(format, dispatch.Attributes
                      .OfType<IFormatMatching>())
                || Matches(format, dispatch.Owner.Attributes
                      .OfType<IFormatMatching>());
        }

        private static bool Matches(
            object format, IEnumerable<IFormatMatching> formats)
        {
            return formats.Any(f => f.Matches(format));
        }

        public static readonly CallbackPolicy Policy =
            BivariantPolicy.Create<Mapping>(m => m.Type, m => m.Source,
                x => x.MatchCallbackMethod(x.ReturnKey, x.Target)
                      .MatchMethod(x.ReturnKey.OrVoid, x.Callback)
            );
    }
}
{'fraction_non_alphanumeric': 0.08835904628330996, 'fraction_numerical': 0.0007012622720897616, 'mean_word_length': 1.923155737704918, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '109572', 'n_tokens_mistral': 732, 'n_tokens_neox': 711, 'n_words': 172}