Skip to content

Commit

Permalink
Перевод на Source Gen Json (#80)
Browse files Browse the repository at this point in the history
* #45 - тестовый workflow для проверки AOT publish на инфре GitHub Actions

* #45 - перевёл бекенд на json source gen

* fix

* #45 - перевёл SG на json source gen

* #45 - перевёл Infrastructure на json source gen

* Revert "#45 - тестовый workflow для проверки AOT publish на инфре GitHub Actions"

This reverts commit 3014c53.
  • Loading branch information
Stepami authored Aug 5, 2024
1 parent 8bdd77a commit 9f379c8
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 54 deletions.
1 change: 1 addition & 0 deletions ExtendedJavaScriptSubset.sln
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{04AB
samples\this.js = samples\this.js
samples\typeresolving.js = samples\typeresolving.js
samples\vec2d.js = samples\vec2d.js
samples\cycled.js = samples\cycled.js
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{FB8F6EE1-1942-46D6-954E-9A1647BBDF10}"
Expand Down
8 changes: 8 additions & 0 deletions samples/cycled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type CycledType = {
x: CycledType;
}
let obj: CycledType = {
x: null;
}
obj.x = obj
print(obj as string)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@

namespace HydraScript.Domain.BackEnd.Impl.Instructions.WithAssignment;

public class AsString(IValue value) : Simple(value)
public partial class AsString(IValue value) : Simple(value)
{
private static readonly AsStringSerializationContext AsStringJsonContext = new(new JsonSerializerOptions
{
WriteIndented = true,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals
});

public override IAddress Execute(IExecuteParams executeParams)
{
var frame = executeParams.Frames.Peek();
frame[Left!] = JsonSerializer.Serialize(
Right.right!.Get(frame),
new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.IgnoreCycles,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals
}
);
value: Right.right!.Get(frame)!,
AsStringJsonContext.Object);

return Address.Next;
}

protected override string ToStringInternal() =>
$"{Left} = {Right.right} as string";

[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(List<object>))]
[JsonSerializable(typeof(Dictionary<string, object>))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(double))]
[JsonSerializable(typeof(string))]
private partial class AsStringSerializationContext : JsonSerializerContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
</PackageReference>
</ItemGroup>

<PropertyGroup>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HydraScript.Infrastructure.LexerRegexGenerator;

public partial class PatternGenerator
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
Converters = { new TokenTypesReadConverter() }
};

private record TokenType(
string Tag,
string Pattern,
Expand All @@ -19,31 +12,7 @@ private record TokenType(
public string GetNamedRegex() => $"(?<{Tag}>{Pattern})";
}

[ExcludeFromCodeCoverage]
private class TokenTypesReadConverter : JsonConverter<IEnumerable<TokenType>>
{
public override IEnumerable<TokenType> Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
var root = JsonElement.ParseValue(ref reader);
var tokenTypes = root.EnumerateArray()
.Select(element =>
{
var tag = element.GetProperty("tag").GetString()!;
var pattern = element.GetProperty("pattern").GetString()!;
var priority = element.GetProperty("priority").GetInt32();

return new TokenType(tag, pattern, priority);
})
.OrderBy(x => x.Priority);
return tokenTypes;
}

public override void Write(
Utf8JsonWriter writer,
IEnumerable<TokenType> value,
JsonSerializerOptions options) => throw new NotSupportedException();
}
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(IEnumerable<TokenType>))]
private partial class PatternGeneratorContext : JsonSerializerContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ private static void GenerateCode(
{
foreach (var info in containerInfos)
{
var tokenTypes = JsonSerializer.Deserialize<IEnumerable<TokenType>>(
var tokenTypes = JsonSerializer.Deserialize(
info.Json,
JsonSerializerOptions)!
PatternGeneratorContext.Default.IEnumerableTokenType)!
.OrderBy(x => x.Priority)
.Concat([new TokenType("ERROR", @"\S+", int.MaxValue)]);
var pattern = string.Join('|', tokenTypes.Select(t => t.GetNamedRegex()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
<InternalsVisibleTo Include="HydraScript.Tests" />
</ItemGroup>

<PropertyGroup>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@

namespace HydraScript.Infrastructure;

internal class TokenTypesProvider : ITokenTypesProvider
internal partial class TokenTypesProvider : ITokenTypesProvider
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
Converters = { new TokenTypesReadConverter() }
};

public IEnumerable<TokenType> GetTokenTypes() =>
JsonSerializer.Deserialize<IEnumerable<TokenType>>(
JsonSerializer.Deserialize(
TokenTypesJson.String,
JsonSerializerOptions)!;
TokenTypesProviderContext.Default.IEnumerableTokenType)!;

[ExcludeFromCodeCoverage]
private class TokenTypesReadConverter : JsonConverter<IEnumerable<TokenType>>
Expand Down Expand Up @@ -52,4 +47,8 @@ public override void Write(

private record PrioritizedTokenType(TokenType TokenType, int Priority);
}

[JsonSourceGenerationOptions(Converters = [typeof(TokenTypesReadConverter)])]
[JsonSerializable(typeof(IEnumerable<TokenType>))]
private partial class TokenTypesProviderContext : JsonSerializerContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<Link>Samples\ceil.js</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\..\samples\cycled.js">
<Link>Samples\cycled.js</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="..\..\samples\defaultarray.js">
<Link>Samples\defaultarray.js</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void Invoke_NoError_ReturnCodeIsZero(string fileName)
"arraddremove.js",
"arreditread.js",
"ceil.js",
"cycled.js",
"defaultarray.js",
"equals.js",
"exprtest.js",
Expand Down

0 comments on commit 9f379c8

Please sign in to comment.