* Change DataStreamPublisher json fields to match cs class property names verbatim * Rename Publishers -> PublisherNodes in DataStreamPublisherConfig
52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
namespace Bts.DataStreamPublisher;
|
|
|
|
using CommandLine;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
public class DataStreamPublisherConfig {
|
|
/// <summary>
|
|
/// The tables to publish on startup. Each key/value represents a table name/query to publish.
|
|
/// </summary>
|
|
public List<PublisherNode>? PublisherNodes { get; set; }
|
|
|
|
public List<string>? CsvDirs { get; set; }
|
|
|
|
[Option(
|
|
"config",
|
|
MetaValue = "PATH",
|
|
HelpText = "Path to json file representing these options (DataStreamPublisherConfig)")]
|
|
[JsonIgnore]
|
|
public string? ConfigFile { get; set; }
|
|
|
|
public static DataStreamPublisherConfig Default() =>
|
|
new() { };
|
|
|
|
public void MergeWith(DataStreamPublisherConfig other) {
|
|
PublisherNodes ??= other.PublisherNodes;
|
|
ConfigFile ??= other.ConfigFile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Merges options with those defined in `ConfigFile` (does not overwrite
|
|
/// non-null existing values).
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// JsonSerializer.Deserialize returns null
|
|
/// </exception>
|
|
public void MergeWithConfigFile() {
|
|
if (ConfigFile is null) {
|
|
return;
|
|
}
|
|
var cfgString = File.ReadAllText(ConfigFile);
|
|
var parsedCfg = JsonSerializer.Deserialize<DataStreamPublisherConfig>(cfgString) ??
|
|
throw new InvalidOperationException($"Failed to parse json from {ConfigFile}");
|
|
MergeWith(parsedCfg);
|
|
ConfigFile = null;
|
|
}
|
|
}
|