<commit_before>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using AutoRest.Core.Logging;
using AutoRest.Core.Properties;
using AutoRest.Core.Validation;
using AutoRest.Swagger.Model;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace AutoRest.Swagger.Validation
{
    public class NetworkInterfacesApiWarning : TypedRule<Dictionary<string, Dictionary<string, Operation>>>
    {
        private static readonly List<string> NetworkApiNamespaces = new List<string>(new string[]{"microsoft.compute", "microsoft.network"});
        /// <summary>
        /// This rule passes if the paths contain reference to either Microsoft.Network Apis or 
        /// Microsoft.Compute Apis but not both
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public override bool IsValid(Dictionary<string, Dictionary<string, Operation>> paths)
        {
            Regex apiRegExp = new Regex(@"/([^/]+)+");
        
            string firstMatch = null;

            foreach (var path in paths.Keys)
            {
                // for each path, check if it exists in our list of namespaces and 
                // ensure it's unique for the given json
                // regex finds all strings of the form /<str> from the path 
                // firstMatch ensures there is only one kind of namespace referenced
                for (Match m = apiRegExp.Match(path); m.Success; m = m.NextMatch())
                {
                    string val = m.Groups[1].Value.ToLowerInvariant();
                    if (!NetworkApiNamespaces.Contains(val))
                    {
                        continue;
                    }
                    else
                    {
                        if (firstMatch == null)
                        {
                            firstMatch = val;
                        }
                        else if (firstMatch != val)
                        {
                            return false;
                        }
                    }
                }

            }
            return true;
        }

        /// <summary>
        /// The template message for this Rule. 
        /// </summary>
        /// <remarks>
        /// This may contain placeholders '{0}' for parameterized messages.
        /// </remarks>
        public override string MessageTemplate => Resources.NetworkInterfacesApiWarningMessage;

        /// <summary>
        /// The severity of this message (ie, debug/info/warning/error/fatal, etc)
        /// </summary>
        public override LogEntrySeverity Severity => LogEntrySeverity.Warning;

    }
}<commit_msg>Generic solution for unique resources<commit_after>﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using AutoRest.Core.Logging;
using AutoRest.Core.Properties;
using AutoRest.Core.Validation;
using AutoRest.Swagger.Model;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;

namespace AutoRest.Swagger.Validation
{
    public class NetworkInterfacesApiWarning : TypedRule<Dictionary<string, Dictionary<string, Operation>>>
    {
        // Ignore everything within /es except when we find microsoft.* and capture that group
        private readonly Regex apiRegExp = new Regex(@"(?:/[^/]+)+/(?i)microsoft(?-i).([^/]+)(?:/[^/]+)+");

        /// <summary>
        /// This rule passes if the paths contain reference to exactly one of the namespace resources
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        public override bool IsValid(Dictionary<string, Dictionary<string, Operation>> paths) 
            => paths.Keys.Select(path => { return apiRegExp.Match(path).Success ? apiRegExp.Match(path).Groups?[0]?.Value : null; }).Except(new List<string> { null }).Distinct().Count() <= 1;

        /// <summary>
        /// The template message for this Rule. 
        /// </summary>
        /// <remarks>
        /// This may contain placeholders '{0}' for parameterized messages.
        /// </remarks>
        public override string MessageTemplate => Resources.NetworkInterfacesApiWarningMessage;

        /// <summary>
        /// The severity of this message (ie, debug/info/warning/error/fatal, etc)
        /// </summary>
        public override LogEntrySeverity Severity => LogEntrySeverity.Warning;

    }
}
{'fraction_non_alphanumeric': 0.11144708423326134, 'fraction_numerical': 0.0010799136069114472, 'mean_word_length': 1.959105431309904, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 47, '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': '6001351', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1099, 'n_words': 345}