The "JsonValueOrArrayConverter<T>" doesn't work the way I thought it would. When it is associated with a property via the JsonConverter attribute, dotnet json parsing code throws an exception if "T" isn't assignable to the property type.
59 lines
1.6 KiB
C#
59 lines
1.6 KiB
C#
using mbts.utils;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace BCG;
|
|
|
|
public class PubSubRoute : IEquatable<PubSubRoute>, IComparable<PubSubRoute> {
|
|
[JsonPropertyName("server_address")]
|
|
public string? ServerAddress { get; set; }
|
|
|
|
[JsonPropertyName("tag")]
|
|
public string? TagRegExp { get; set; }
|
|
//route names are used in intermediary code (i.e. while substations are being deployed)
|
|
//so that MC Hammer can advertise them.
|
|
[JsonPropertyName("name")]
|
|
public string? Name { get; set; }
|
|
|
|
static readonly IComparer<PubSubRoute> comparer = new ComparerBuilder<PubSubRoute>() {
|
|
p => p.ServerAddress,
|
|
p => p.TagRegExp,
|
|
};
|
|
|
|
Regex? myRegex;
|
|
|
|
public PubSubRoute() { }
|
|
|
|
public PubSubRoute(string theServerAddress, string theTagRegExp) {
|
|
ServerAddress = theServerAddress;
|
|
TagRegExp = theTagRegExp;
|
|
}
|
|
|
|
[MemberNotNull(nameof(TagRegExp), nameof(myRegex))]
|
|
public bool IsMatch(string tag) {
|
|
if (TagRegExp is null) {
|
|
throw new InvalidOperationException();
|
|
}
|
|
myRegex ??= new Regex(TagRegExp);
|
|
return myRegex.IsMatch(tag);
|
|
}
|
|
|
|
public int CompareTo(PubSubRoute? other) =>
|
|
other is null ? -1 : comparer.Compare(this, other);
|
|
|
|
public bool Equals(PubSubRoute? other) =>
|
|
other is not null
|
|
&& ServerAddress == other.ServerAddress
|
|
&& TagRegExp == other.TagRegExp;
|
|
|
|
public override bool Equals(object? obj) =>
|
|
Equals(obj as PubSubRoute);
|
|
|
|
public override int GetHashCode() =>
|
|
HashCode.Combine(ServerAddress, TagRegExp);
|
|
}
|