So we can create ViewStreams that depend on other services, config, etc. Previously, the PrimitiveManager relied purely on static construction of types through reflection.
55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
#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<string> AlternateNames { get; }
|
|
}
|
|
|
|
public class DataStreamMetadata : IDataStreamMetadata {
|
|
public string? Name { get; set; }
|
|
public List<string>? AlternateNames { get; set; }
|
|
|
|
string IDataStreamMetadata.Name => Name ?? string.Empty;
|
|
IEnumerable<string> IDataStreamMetadata.AlternateNames =>
|
|
AlternateNames ?? new(0);
|
|
|
|
public static DataStreamMetadata FromType(Type type) {
|
|
var friendlyName = type.GetCustomAttribute<DataStreamUserRespresentation>()?.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<string>(() => 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));
|
|
}
|
|
}
|