Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #12

Merged
merged 22 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
增加将配置项反向转换为Json的方法。
Codespilot committed Jan 1, 2024
commit f7c0fa4fa4d7f8cf33c817558ac05cdf75290f22
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ public Task HandleAsync(SettingArchiveCreateCommand message, MessageContext cont
{
return ExecuteAsync(async () =>
{
var (appId, appCode, environment, nodes) = await GetNodesAsync(message.RootId, cancellationToken);
var data = nodes.ToDictionary(t => t.Key, t => t.Value);
var (appId, appCode, environment, items) = await GetItemsAsync(message.RootId, cancellationToken);
var data = items.ToDictionary(t => t.Key, t => t.Value);
var json = JsonConvert.SerializeObject(data);

var userName = context?.User?.Identity?.Name;
@@ -30,7 +30,7 @@ public Task HandleAsync(SettingArchiveCreateCommand message, MessageContext cont
});
}

private async Task<Tuple<long, string, string, List<SettingItem>>> GetNodesAsync(long id, CancellationToken cancellationToken = default)
private async Task<Tuple<long, string, string, List<SettingItem>>> GetItemsAsync(long id, CancellationToken cancellationToken = default)
{
var repository = UnitOfWork.Current.GetService<ISettingRepository>();

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

namespace Nerosoft.Starfish.Application;

public class JsonConfigurationFileParser
internal partial class JsonConfigurationFileParser
{
private const string KEY_DELIMITER = ":";

@@ -121,4 +121,81 @@ private void VisitValue(JsonElement value)
private void EnterContext(string context) => _paths.Push(_paths.Count > 0 ? _paths.Peek() + KEY_DELIMITER + context : context);

private void ExitContext() => _paths.Pop();
}

internal partial class JsonConfigurationFileParser
{
public static string InvertParsed(IDictionary<string, string> data)
{
var root = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

foreach (var entry in data)
{
var path = entry.Key.Split(":");
var current = root;

for (var i = 0; i < path.Length - 1; i++)
{
if (!current.TryGetValue(path[i], out var child))
{
child = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
current[path[i]] = child;
}

current = (Dictionary<string, object>)child;
}

current[path[^1]] = entry.Value;
}

var result = Rebuild(root);

return JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });

object Rebuild(object source)
{
switch (source)
{
case IDictionary<string, object> dict:
if (DetectArray(dict))
{
object array = ConvertToArray(dict);
array = Rebuild(array);
return array;
}
else
{
var keys = dict.Keys.Select(x => x).ToList();
foreach (var key in keys)
{
var val = dict[key];
dict[key] = Rebuild(val);
}

return dict;
}

case object[] array:
for (var index = 0; index < array.Length; index++)
{
array[index] = Rebuild(array[index]);
}

return array;
}

return source;
}

bool DetectArray(IDictionary<string, object> dictionary)
{
var keys = Enumerable.Range(0, dictionary.Count).Select(t => t.ToString());
return dictionary.Keys.All(key => keys.Contains(key));
}

object[] ConvertToArray(IDictionary<string, object> dictionary)
{
return dictionary.Values.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Nerosoft.Starfish.Application;

public class TextConfigurationFileParser
public partial class TextConfigurationFileParser
{
private TextConfigurationFileParser()
{
@@ -49,4 +49,18 @@ private Dictionary<string, string> ParseStream(Stream input)

return data;
}
}

public partial class TextConfigurationFileParser
{
public static string InvertParsed(IDictionary<string, string> data)
{
var builder = new StringBuilder();
foreach (var (key, value) in data)
{
builder.AppendLine($"{key}={value}");
}

return builder.ToString();
}
}
4 changes: 2 additions & 2 deletions Source/Starfish.Webapi/Controllers/DictionaryController.cs
Original file line number Diff line number Diff line change
@@ -60,8 +60,8 @@ public async Task<IActionResult> GetDatabaseTypeItemsAsync()
/// 获取支持的配置节点类型列表
/// </summary>
/// <returns></returns>
[HttpGet("setting-node-type")]
public async Task<IActionResult> GetSettingNodeTypeItemsAsync()
[HttpGet("setting-item-type")]
public async Task<IActionResult> GetSettingItemTypeItemsAsync()
{
var result = await _service.GetSettingItemTypeItemsAsync();
return Ok(result);