#nullable enable namespace Bts.DataStreamCore; using BCG; using bts; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; public interface IDataStreamMetadata { public string Name { get; } public IEnumerable AlternateNames { get; } } public class DataStreamMetadata : IDataStreamMetadata { public string? Name { get; set; } public List? AlternateNames { get; set; } string IDataStreamMetadata.Name => Name ?? string.Empty; IEnumerable IDataStreamMetadata.AlternateNames => AlternateNames ?? new(0); public static DataStreamMetadata FromType(Type type) { var friendlyName = type.GetCustomAttribute()?.friendlyName; var rv = new DataStreamMetadata() { Name = friendlyName ?? type.Name, AlternateNames = new(2) { Log.notnull(type.FullName) } }; if (friendlyName is not null) { rv.AlternateNames.Add(type.Name); } return rv; } } public static class DataStreamMetadataExtensions { public static bool IsMatch(this IDataStreamMetadata dsm, string name, bool checkAlternateNames = true, bool ignoreCase = true, bool ignoreUnderscores = true) { var strComp = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; var nameWithoutUnderscores = new Lazy(() => RemoveUnderscores(name)); return IsNameMatch(dsm.Name) || (checkAlternateNames && dsm.AlternateNames.Any(IsNameMatch)); static string RemoveUnderscores(string s) => s.Replace("_", ""); bool IsNameMatch(string otherName) => string.Equals(name, otherName, strComp) || (ignoreUnderscores && string.Equals(nameWithoutUnderscores.Value, RemoveUnderscores(otherName), strComp)); } }