Skip to content

Commit

Permalink
Added tools for building source generators based on Corvus.JsonSchema (
Browse files Browse the repository at this point in the history
…#513)

* Added tools library for people building source generators based on Corvus.JsonSchema

* Added self-contained version of CJEx.

* Removed package dependency on Community.HighPerformance.
Added README
Added Corvus.Json.JsonSchema libraries as direct inclusions.

* Fixed nuget packaging.
  • Loading branch information
mwadams authored Dec 17, 2024
1 parent f9614fd commit 4a6034e
Show file tree
Hide file tree
Showing 86 changed files with 10,286 additions and 372 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2072,13 +2072,15 @@ private static void SetSingleConstantValue(TypeDeclaration typeDeclaration)
// We don't have an existing constant value,
foundElement = value;
}
#if !BUILDING_SOURCE_GENERATOR
else if (JsonElement.DeepEquals(foundElement, value))
{
// The new constant is the same as the old value
// (typically because a composing type explicitly restates the
// constant from one of the composed types)
continue;
}
#endif
else
{
// We have more than keyword that explicitly provides a single constant value
Expand All @@ -2105,13 +2107,15 @@ private static void SetSingleConstantValue(TypeDeclaration typeDeclaration)
// an explicit declaration
foundElement = constantValue;
}
#if !BUILDING_SOURCE_GENERATOR
else if (JsonElement.DeepEquals(foundElement, constantValue))
{
// The new type is the same as the old type
// (typically because a composing type explicitly restates the
// type from one of the composed types)
continue;
}
#endif
else
{
// We have more than keyword that explicitly provides a format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json;
using CommunityToolkit.HighPerformance;

namespace Corvus.Json;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using CommunityToolkit.HighPerformance.Buffers;

namespace Corvus.Json
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,27 @@ public static bool ProcessRawText<TState, TResult>(
in Utf8Parser<TState, TResult> callback,
[NotNullWhen(true)] out TResult? result)
{
#if BUILDING_SOURCE_GENERATOR
PooledWriter? writerPair = null;
try
{
writerPair = WriterPool.Get();
(Utf8JsonWriter w, ArrayPoolBufferWriter<byte> writer) = writerPair.Get();
element.WriteTo(w);
w.Flush();
return callback(writer.WrittenSpan[1..^1], state, out result);
}
finally
{
if (writerPair is not null)
{
WriterPool.Return(writerPair);
}
}
#else
ReadOnlySpan<byte> rawValue = JsonMarshal.GetRawUtf8Value(element);
return callback(rawValue[1..^1], state, out result);
#endif
}

/// <summary>
Expand All @@ -58,8 +77,27 @@ public static bool ProcessRawValue<TState, TResult>(
in Utf8Parser<TState, TResult> callback,
[NotNullWhen(true)] out TResult? result)
{
#if BUILDING_SOURCE_GENERATOR
PooledWriter? writerPair = null;
try
{
writerPair = WriterPool.Get();
(Utf8JsonWriter w, ArrayPoolBufferWriter<byte> writer) = writerPair.Get();
element.WriteTo(w);
w.Flush();
return callback(writer.WrittenSpan, state, out result);
}
finally
{
if (writerPair is not null)
{
WriterPool.Return(writerPair);
}
}
#else
ReadOnlySpan<byte> rawValue = JsonMarshal.GetRawUtf8Value(element);
return callback(rawValue[1..^1], state, out result);
return callback(rawValue, state, out result);
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static bool TryGetRawText<TState, TResult>(this JsonElement element, in U
/// </exception>
public static bool TryGetRawText<TState, TResult>(this JsonElement element, in Utf8Parser<TState, TResult> parser, in TState state, bool decode, [NotNullWhen(true)] out TResult? value)
{
return element.ProcessRawValue(new Utf8ParserStateWrapper<TState, TResult>(parser, state, decode), ProcessRawText, out value);
return element.ProcessRawText(new Utf8ParserStateWrapper<TState, TResult>(parser, state, decode), ProcessRawText, out value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.InteropServices;
using CommunityToolkit.HighPerformance;

namespace Corvus.Json;

Expand Down
Loading

0 comments on commit 4a6034e

Please sign in to comment.