From 518b51fcf6b5a8515a70eafcb36dddc4f9ab7a9f Mon Sep 17 00:00:00 2001 From: Gervasio Marchand Date: Tue, 20 Dec 2022 16:34:43 -0800 Subject: [PATCH] Fix NRE when Tags is null and error if both tags and connection string are specified (#9) --- src/Config.cs | 8 +++++++- src/Extensions.cs | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/Extensions.cs diff --git a/src/Config.cs b/src/Config.cs index 0b23d65..2b1044b 100644 --- a/src/Config.cs +++ b/src/Config.cs @@ -50,6 +50,12 @@ public class Config Console.WriteLine("Warning: Sites is deprecated, please use Instances instead"); } + data.Tags ??= Array.Empty(); + if (data.MastodonPostgresConnectionString.HasValue() && data.Tags.Length > 0) + { + throw new Exception("You can't specify both MastodonPostgresConnectionString and Tags"); + } + Instance = new Config(importedPath, data.FakeRelayUrl, apiKey, data.MastodonPostgresConnectionString, data.Tags.ToImmutableArray(), data.GetImmutableSites()); } @@ -60,7 +66,7 @@ public class Config public string? FakeRelayApiKey { get; set; } public string? MastodonPostgresConnectionString { get; set; } public string[]? Instances { get; set; } - public string[] Tags { get; set; } + public string[]? Tags { get; set; } public InternalSiteData[]? Sites { get; set; } public ImmutableArray GetImmutableSites() diff --git a/src/Extensions.cs b/src/Extensions.cs new file mode 100644 index 0000000..9eed7c5 --- /dev/null +++ b/src/Extensions.cs @@ -0,0 +1,8 @@ +namespace GetMoarFediverse; + +public static class Extensions +{ + public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s); + + public static bool HasValue(this string s) => !s.IsNullOrEmpty(); +}