-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dump configuration in development mode
- Loading branch information
1 parent
8872757
commit 1c59268
Showing
3 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/PhoneBox.Server/Configuration/ConfigurationSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Configuration.Json; | ||
|
||
namespace PhoneBox.Server | ||
{ | ||
internal static class ConfigurationSerializer | ||
{ | ||
public static string DumpConfiguration(IConfigurationRoot root) | ||
{ | ||
JsonObject jsonObject = new JsonObject(); | ||
DumpConfiguration(jsonObject, parentNode: null, parentKey: null, root.GetChildren(), root); | ||
string json = jsonObject.ToJsonString(new JsonSerializerOptions { WriteIndented = true }); | ||
return json; | ||
} | ||
private static void DumpConfiguration(JsonNode node, JsonNode? parentNode, string? parentKey, IEnumerable<IConfigurationSection> children, IConfigurationRoot root) | ||
{ | ||
foreach (IConfigurationSection child in children) | ||
{ | ||
if (child.Key == "") | ||
{ | ||
// :ASPNETCORE_BROWSER_TOOLS | ||
// This key is treated as a section because it begins with : | ||
continue; | ||
} | ||
|
||
ConfigurationValueResolutionResult result = ResolveConfigurationValue(root, child.Path, out string? value); | ||
JsonNode childNode = DumpConfiguration(result, child.Key, parentKey, value, node, parentNode); | ||
DumpConfiguration(childNode, node, child.Key, child.GetChildren(), root); | ||
} | ||
} | ||
private static JsonNode DumpConfiguration(ConfigurationValueResolutionResult result, string key, string? parentKey, string? value, JsonNode node, JsonNode? parentNode) | ||
{ | ||
switch (result) | ||
{ | ||
case ConfigurationValueResolutionResult.NotInterested: | ||
break; | ||
|
||
case ConfigurationValueResolutionResult.NotFound: | ||
{ | ||
JsonObject childNode = new JsonObject(); | ||
node[key] = childNode; | ||
return childNode; | ||
} | ||
|
||
case ConfigurationValueResolutionResult.Found: | ||
{ | ||
if (Int32.TryParse(key, out int intValue)) | ||
{ | ||
if (parentNode![parentKey!] is not JsonArray array) | ||
{ | ||
array = new JsonArray(); | ||
parentNode![parentKey!] = array; | ||
} | ||
array.Insert(intValue, value!); | ||
} | ||
else | ||
{ | ||
node[key] = value!; | ||
} | ||
|
||
break; | ||
} | ||
|
||
default: | ||
throw new ArgumentOutOfRangeException(nameof(result), result, null); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
private static ConfigurationValueResolutionResult ResolveConfigurationValue(IConfigurationRoot root, string key, out string? value) | ||
{ | ||
foreach (IConfigurationProvider configurationProvider in root.Providers.Reverse()) | ||
{ | ||
if (!configurationProvider.TryGet(key, out value)) | ||
continue; | ||
|
||
bool isAppSettingsJsonProvider = configurationProvider is JsonConfigurationProvider jsonConfigurationProvider && Regex.IsMatch(jsonConfigurationProvider.Source.Path, @"^appsettings(.+)?\.json$"); | ||
return isAppSettingsJsonProvider ? ConfigurationValueResolutionResult.Found : ConfigurationValueResolutionResult.NotInterested; | ||
} | ||
|
||
value = null; | ||
return ConfigurationValueResolutionResult.NotFound; | ||
} | ||
|
||
private enum ConfigurationValueResolutionResult | ||
{ | ||
None, | ||
NotInterested, | ||
NotFound, | ||
Found | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters