Skip to content

Commit 9eb914a

Browse files
authored
Merge pull request #115 from NeonGraal/yaml
Yaml
2 parents 8e7cc1d + 89d0071 commit 9eb914a

File tree

581 files changed

+81738
-54
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

581 files changed

+81738
-54
lines changed

Diff for: GqlPlus.sln

+36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2222
.editorconfig = .editorconfig
2323
.gitignore = .gitignore
2424
.prettierrc.yml = .prettierrc.yml
25+
autoverify.ps1 = autoverify.ps1
2526
collect-samples.ps1 = collect-samples.ps1
2627
.github\dependabot.yml = .github\dependabot.yml
2728
Directory.Build.props = Directory.Build.props
@@ -85,6 +86,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.GeneratorsTestBase"
8586
EndProject
8687
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.VerifyTestBase", "test\GqlPlus.VerifyTestBase\GqlPlus.VerifyTestBase.csproj", "{14F619F3-163D-43D5-B3A4-14817438274E}"
8788
EndProject
89+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Converter.Json", "src\GqlPlus.Converter.Json\GqlPlus.Converter.Json.csproj", "{B8D01714-3F6B-41D6-89F5-5F7056290ACF}"
90+
EndProject
91+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Converter.Json.ComponentTests", "test\GqlPlus.Converter.Json.ComponentTests\GqlPlus.Converter.Json.ComponentTests.csproj", "{3289A609-8CB0-495D-9D10-326D63BAA5F0}"
92+
EndProject
93+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Modeller.ComponentTestBase", "test\GqlPlus.Modeller.ComponentTestBase\GqlPlus.Modeller.ComponentTestBase.csproj", "{7A0D7D70-6533-442A-9FD1-29131DE207F4}"
94+
EndProject
95+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Converter.Yaml.ComponentTests", "test\GqlPlus.Converter.Yaml.ComponentTests\GqlPlus.Converter.Yaml.ComponentTests.csproj", "{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4}"
96+
EndProject
97+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GqlPlus.Converter.Yaml", "src\GqlPlus.Converter.Yaml\GqlPlus.Converter.Yaml.csproj", "{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA}"
98+
EndProject
8899
Global
89100
GlobalSection(SolutionConfigurationPlatforms) = preSolution
90101
Debug|Any CPU = Debug|Any CPU
@@ -167,6 +178,26 @@ Global
167178
{14F619F3-163D-43D5-B3A4-14817438274E}.Debug|Any CPU.Build.0 = Debug|Any CPU
168179
{14F619F3-163D-43D5-B3A4-14817438274E}.Release|Any CPU.ActiveCfg = Release|Any CPU
169180
{14F619F3-163D-43D5-B3A4-14817438274E}.Release|Any CPU.Build.0 = Release|Any CPU
181+
{B8D01714-3F6B-41D6-89F5-5F7056290ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
182+
{B8D01714-3F6B-41D6-89F5-5F7056290ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU
183+
{B8D01714-3F6B-41D6-89F5-5F7056290ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU
184+
{B8D01714-3F6B-41D6-89F5-5F7056290ACF}.Release|Any CPU.Build.0 = Release|Any CPU
185+
{3289A609-8CB0-495D-9D10-326D63BAA5F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
186+
{3289A609-8CB0-495D-9D10-326D63BAA5F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
187+
{3289A609-8CB0-495D-9D10-326D63BAA5F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
188+
{3289A609-8CB0-495D-9D10-326D63BAA5F0}.Release|Any CPU.Build.0 = Release|Any CPU
189+
{7A0D7D70-6533-442A-9FD1-29131DE207F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
190+
{7A0D7D70-6533-442A-9FD1-29131DE207F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
191+
{7A0D7D70-6533-442A-9FD1-29131DE207F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
192+
{7A0D7D70-6533-442A-9FD1-29131DE207F4}.Release|Any CPU.Build.0 = Release|Any CPU
193+
{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
194+
{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
195+
{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
196+
{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4}.Release|Any CPU.Build.0 = Release|Any CPU
197+
{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
198+
{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
199+
{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
200+
{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA}.Release|Any CPU.Build.0 = Release|Any CPU
170201
EndGlobalSection
171202
GlobalSection(SolutionProperties) = preSolution
172203
HideSolutionNode = FALSE
@@ -193,6 +224,11 @@ Global
193224
{44D55F97-4EBA-48C8-A72C-7FBC12A44719} = {B834B2ED-0D27-4281-8523-B706B42DCC15}
194225
{0336A900-AA73-4B44-99BD-B70A5B2D14D3} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
195226
{14F619F3-163D-43D5-B3A4-14817438274E} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
227+
{B8D01714-3F6B-41D6-89F5-5F7056290ACF} = {B834B2ED-0D27-4281-8523-B706B42DCC15}
228+
{3289A609-8CB0-495D-9D10-326D63BAA5F0} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
229+
{7A0D7D70-6533-442A-9FD1-29131DE207F4} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
230+
{8F67479F-A4B7-4574-B8EA-10C8E19DA2E4} = {3B90AC37-1A6B-41AF-8490-E9D48E429ACD}
231+
{DF7D0CA6-48AE-460D-BCFF-B274D6D004BA} = {B834B2ED-0D27-4281-8523-B706B42DCC15}
196232
EndGlobalSection
197233
GlobalSection(ExtensibilityGlobals) = postSolution
198234
SolutionGuid = {148B651C-DFAA-434F-B1ED-51E535FC2DE8}

Diff for: autoverify.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Get-ChildItem test -Filter "*.verified.*" -Recurse | Remove-Item -Force
22
dotnet test -e GQLPLUS_AUTOVERIFY=true -l "trx;LogFileName=TestResults.trx" -f net9.0
3-
prettier -w .
43
Write-Host "`n"
54
./make-summary.ps1 -NoCoverage

Diff for: src/GqlPlus.Converter.Json/AssemblyAttributes.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("GqlPlus.Modeller.ClassTests")]
4+
[assembly: InternalsVisibleTo("GqlPlus.Modeller.ComponentTests")]
5+
[assembly: InternalsVisibleTo("GqlPlus.Verifier.ComponentTests")]
6+
[assembly: InternalsVisibleTo("GqlPlus.TestBase")]
7+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Diff for: src/GqlPlus.Converter.Json/Convert/RenderJson.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace GqlPlus.Convert;
2+
3+
public static class RenderJson
4+
{
5+
private static JsonSerializerOptions Options { get; } = JsonOptions(true);
6+
7+
internal static JsonSerializerOptions Unindented { get; } = JsonOptions(false);
8+
9+
private static JsonSerializerOptions JsonOptions(bool indented)
10+
=> new() {
11+
Converters = {
12+
new RenderStructureJsonConverter(),
13+
RenderStructureJsonConverter.ValueConverter,
14+
},
15+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
16+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
17+
WriteIndented = indented,
18+
};
19+
20+
public static string ToJson(this Structured model)
21+
=> JsonSerializer.Serialize(model, Options);
22+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace GqlPlus.Convert;
2+
3+
internal abstract class RenderJsonConverter<T>
4+
: JsonConverter<T>
5+
{
6+
protected void WriteValue(Utf8JsonWriter writer, StructureValue value)
7+
{
8+
if (value is null || value.IsEmpty) {
9+
return;
10+
}
11+
12+
if (value.Identifier is not null) {
13+
writer.WriteStringValue(value.Identifier);
14+
} else if (value.Boolean is not null) {
15+
writer.WriteBooleanValue(value.Boolean.Value);
16+
} else if (value.Number is not null) {
17+
writer.WriteNumberValue(value.Number.Value);
18+
} else if (value.Text is not null) {
19+
writer.WriteStringValue(value.Text);
20+
}
21+
}
22+
23+
protected void StartTaggedValue(Utf8JsonWriter writer, string tag)
24+
{
25+
writer.WriteStartObject();
26+
writer.WriteString("$tag", tag);
27+
writer.WritePropertyName("value");
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
namespace GqlPlus.Convert;
2+
internal class RenderStructureJsonConverter
3+
: RenderJsonConverter<Structured>
4+
{
5+
internal static RenderValueJsonConverter ValueConverter { get; } = new();
6+
7+
public override Structured? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
8+
public override void Write(Utf8JsonWriter writer, Structured value, JsonSerializerOptions options)
9+
{
10+
if (value is null || value.IsEmpty) {
11+
return;
12+
}
13+
14+
bool plain = string.IsNullOrWhiteSpace(value.Tag);
15+
16+
if (value.List.Count > 0) {
17+
WriteList(writer, value.List, options);
18+
return;
19+
} else if (value.Map.Count > 0) {
20+
Structured first = value.Map.First().Value;
21+
if (value.Map.Count == 1 && !plain && string.IsNullOrWhiteSpace(first.Tag) && first.Value is not null) {
22+
StartTaggedValue(writer, value.Tag);
23+
WriteValue(writer, first.Value);
24+
writer.WriteEndObject();
25+
} else {
26+
WriteMap(writer, value.Map, value.Tag, options);
27+
}
28+
} else if (value.Value is not null) {
29+
ValueConverter.Write(writer, value.Value, options);
30+
}
31+
}
32+
33+
private void WriteMap(Utf8JsonWriter writer, Structured<StructureValue, Structured>.IDict map, string tag, JsonSerializerOptions options)
34+
{
35+
writer.WriteStartObject();
36+
37+
if (!string.IsNullOrWhiteSpace(tag)) {
38+
writer.WriteString("$tag", tag);
39+
}
40+
41+
IEnumerable<(string, Structured)> ordered = map
42+
.Select(kv => (key: kv.Key.AsString, kv.Value))
43+
.OrderBy(kv => kv.key);
44+
45+
foreach ((string key, Structured value) in ordered) {
46+
writer.WritePropertyName(key);
47+
Write(writer, value, options);
48+
}
49+
50+
writer.WriteEndObject();
51+
}
52+
53+
private void WriteList(Utf8JsonWriter writer, IList<Structured> list, JsonSerializerOptions options)
54+
{
55+
if (list.All(i => i.Value is not null)) {
56+
string result = JsonSerializer.Serialize(list, RenderJson.Unindented);
57+
writer.WriteRawValue(result.Replace(",", ", "));
58+
return;
59+
}
60+
61+
writer.WriteStartArray();
62+
foreach (Structured item in list) {
63+
Write(writer, item, options);
64+
}
65+
66+
writer.WriteEndArray();
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace GqlPlus.Convert;
2+
3+
internal class RenderValueJsonConverter
4+
: RenderJsonConverter<StructureValue>
5+
{
6+
public override StructureValue? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
7+
public override void Write(Utf8JsonWriter writer, StructureValue value, JsonSerializerOptions options)
8+
{
9+
if (value is null || value.IsEmpty) {
10+
return;
11+
}
12+
13+
if (string.IsNullOrWhiteSpace(value.Tag)) {
14+
WriteValue(writer, value);
15+
return;
16+
}
17+
18+
writer.WriteStartObject();
19+
writer.WriteString("$tag", value.Tag);
20+
writer.WritePropertyName("value");
21+
WriteValue(writer, value);
22+
writer.WriteEndObject();
23+
}
24+
}

Diff for: src/GqlPlus.Converter.Json/GlobalSuppressions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Design", "CA1033:Interface methods should be callable by child types")]
9+
[assembly: SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase")]
10+
[assembly: SuppressMessage("Naming", "CA1720:Identifier contains type name")]
11+
[assembly: SuppressMessage("Performance", "CA1819:Properties should not return arrays")]
12+
[assembly: SuppressMessage("Performance", "CA1822:Mark members as static")]
13+
14+
[assembly: SuppressMessage("Style", "IDE0058:Expression value is never used")]

Diff for: src/GqlPlus.Converter.Json/GlobalUsings.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global using System.Text.Json;
2+
global using System.Text.Json.Serialization;
3+
global using GqlPlus.Structures;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<PackageReference Include="System.Text.Json" Version="9.0.3" />
5+
</ItemGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\GqlPlus.Abstractions\GqlPlus.Abstractions.csproj" />
9+
</ItemGroup>
10+
11+
</Project>

Diff for: src/GqlPlus.Converter.Yaml/AssemblyAttributes.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("GqlPlus.Modeller.ClassTests")]
4+
[assembly: InternalsVisibleTo("GqlPlus.Modeller.ComponentTests")]
5+
[assembly: InternalsVisibleTo("GqlPlus.Verifier.ComponentTests")]
6+
[assembly: InternalsVisibleTo("GqlPlus.TestBase")]
7+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Diff for: src/GqlPlus.Converter.Yaml/Convert/RenderYaml.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using YamlDotNet.Serialization.NamingConventions;
2+
3+
namespace GqlPlus.Convert;
4+
5+
public static class RenderYaml
6+
{
7+
internal static int BestWidth = 60;
8+
internal static ISerializer YamlFull { get; }
9+
internal static ISerializer YamlWrapped { get; }
10+
11+
static RenderYaml()
12+
{
13+
SerializerBuilder builder = new SerializerBuilder()
14+
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull | DefaultValuesHandling.OmitEmptyCollections)
15+
.EnsureRoundtrip()
16+
.WithNamingConvention(CamelCaseNamingConvention.Instance);
17+
18+
YamlFull = builder
19+
.WithTypeConverter(RenderYamlFullConverter.Instance)
20+
.Build();
21+
22+
EmitterSettings settings = EmitterSettings
23+
.Default
24+
.WithBestWidth(BestWidth);
25+
26+
YamlWrapped = Serializer.FromValueSerializer(
27+
builder
28+
.WithTypeConverter(RenderYamlWrappedConverter.Instance)
29+
.BuildValueSerializer(),
30+
settings);
31+
}
32+
33+
public static string ToYaml(this Structured model, bool wrapped)
34+
=> (wrapped ? YamlWrapped : YamlFull).Serialize(model);
35+
36+
public static string YamlJoin(this IEnumerable<string> list, string start, string end)
37+
=> start + string.Join(", ", list ?? []) + end;
38+
}

0 commit comments

Comments
 (0)