So we can create ViewStreams that depend on other services, config, etc. Previously, the PrimitiveManager relied purely on static construction of types through reflection.
46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
#nullable enable
|
|
namespace Bts.DataStreamCore;
|
|
|
|
using BCG;
|
|
|
|
using System;
|
|
using System.Reflection;
|
|
|
|
public interface IDataStreamMetadata {
|
|
public string Name { get; }
|
|
}
|
|
|
|
public class DataStreamMetadata : IDataStreamMetadata {
|
|
public DataStreamMetadata() { }
|
|
|
|
public DataStreamMetadata(string name) =>
|
|
Name = name;
|
|
|
|
public string? Name { get; set; }
|
|
|
|
string IDataStreamMetadata.Name => Name ?? string.Empty;
|
|
|
|
public static DataStreamMetadata FromType(Type type) {
|
|
var friendlyName = type.GetCustomAttribute<DataStreamUserRespresentation>()?.friendlyName;
|
|
var rv = new DataStreamMetadata() {
|
|
Name = friendlyName ?? type.Name,
|
|
};
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
public static class DataStreamMetadataExtensions {
|
|
public static bool IsMatch(this IDataStreamMetadata dsm, string name, bool ignoreCase = true, bool ignoreUnderscores = true) {
|
|
var strComp = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
|
|
var nameWithoutUnderscores = new Lazy<string>(() => RemoveUnderscores(name));
|
|
return IsNameMatch(dsm.Name);
|
|
|
|
static string RemoveUnderscores(string s) =>
|
|
s.Replace("_", "");
|
|
|
|
bool IsNameMatch(string otherName) =>
|
|
string.Equals(name, otherName, strComp)
|
|
|| (ignoreUnderscores && string.Equals(RemoveUnderscores(name), RemoveUnderscores(otherName), strComp));
|
|
}
|
|
}
|