75 lines
2.8 KiB
C#
75 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using Sozsoft.Platform;
|
|
|
|
namespace Sozsoft.Mcp;
|
|
|
|
/// <summary>
|
|
/// query_list'in DevExtreme bicimindeki (JSON) sort/group/summary girdilerini ekranin select
|
|
/// ucunun bekledigi metin bicimine (<c>Alan desc|Alan2 asc</c>) cevirir. Arayuzde ayni donusumu
|
|
/// <c>ui/src/views/list/Utils.ts</c> yapar; select ucu JSON'u tanimaz ve ORDER BY'a oldugu gibi
|
|
/// ekler. JSON olmayan girdi zaten metin bicimindedir, dokunulmaz. Bozuk girdi
|
|
/// <see cref="JsonException"/> ya da <see cref="InvalidOperationException"/> atar.
|
|
/// </summary>
|
|
internal static class McpLoadOptions
|
|
{
|
|
public static string? ToSort(string? json)
|
|
=> Convert(json, item => $"{Selector(item)} {Direction(item)}");
|
|
|
|
public static string? ToGroup(string? json)
|
|
=> Convert(json, item =>
|
|
{
|
|
var isExpanded = item.TryGetProperty("isExpanded", out var expanded) && expanded.ValueKind == JsonValueKind.True;
|
|
var interval = item.TryGetProperty("groupInterval", out var value) && value.ValueKind == JsonValueKind.String
|
|
? $" {value.GetString()}"
|
|
: string.Empty;
|
|
|
|
return $"{Selector(item)} {Direction(item)} {(isExpanded ? "true" : "false")}{interval}";
|
|
});
|
|
|
|
public static string? ToSummary(string? json)
|
|
=> Convert(json, item => $"{Selector(item)} {Text(item, "summaryType")}");
|
|
|
|
private static string? Convert(string? json, Func<JsonElement, string> map)
|
|
{
|
|
var value = json?.Trim();
|
|
if (string.IsNullOrEmpty(value))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!value.StartsWith('['))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
using var document = JsonDocument.Parse(value);
|
|
|
|
var parts = document.RootElement.EnumerateArray()
|
|
.Where(item => item.ValueKind == JsonValueKind.Object)
|
|
.Select(map)
|
|
.ToList();
|
|
|
|
return parts.Count == 0 ? null : string.Join(PlatformConsts.MultiValueDelimiter, parts);
|
|
}
|
|
|
|
private static string Selector(JsonElement item) => Text(item, "selector");
|
|
|
|
private static string Direction(JsonElement item)
|
|
=> item.TryGetProperty("desc", out var desc) && desc.ValueKind == JsonValueKind.True ? "desc" : "asc";
|
|
|
|
private static string Text(JsonElement item, string name)
|
|
{
|
|
var text = item.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String ? value.GetString() : null;
|
|
|
|
// Bosluk ve ayirici metin biciminin sozdizimidir; alan adinda bulunamazlar.
|
|
if (string.IsNullOrWhiteSpace(text) || text.Any(c => char.IsWhiteSpace(c) || c == PlatformConsts.MultiValueDelimiter))
|
|
{
|
|
throw new JsonException(name);
|
|
}
|
|
|
|
return text;
|
|
}
|
|
}
|