Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed class SourceSchemaResult : IDisposable
private static ReadOnlySpan<byte> ExtensionsProperty => "extensions"u8;
private readonly SourceResultDocument _document;
private readonly bool _ownsDocument;
private bool _errorsParsed;

public SourceSchemaResult(
Path path,
Expand Down Expand Up @@ -45,9 +46,20 @@ public SourceResultElement Data
}

public SourceSchemaErrors? Errors
=> _document.Root.TryGetProperty(ErrorsProperty, out var errors)
? SourceSchemaErrors.From(errors)
: null;
{
get
{
if (!_errorsParsed)
{
field = _document.Root.TryGetProperty(ErrorsProperty, out var errors)
? SourceSchemaErrors.From(errors)
: null;
_errorsParsed = true;
}

return field;
}
}

internal SourceResultElement RawErrors
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected override async ValueTask<ExecutionStatus> OnExecuteAsync(
{
buffer[index++] = result;

if (result.Errors is not null)
if (result.HasErrors)
{
hasSomeErrors = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ public bool AddPartialResults(
// we need to track the result objects as they used rented memory.
_memory.Push(result);

if (result.Errors?.RootErrors is { Length: > 0 } rootErrors)
var errors = result.Errors;

if (errors?.RootErrors is { Length: > 0 } rootErrors)
{
_errors ??= [];
_errors.AddRange(rootErrors);
}

dataElement = GetDataElement(sourcePath, result.Data);
errorTrie = GetErrorTrie(sourcePath, result.Errors?.Trie);
errorTrie = GetErrorTrie(sourcePath, errors?.Trie);

result = ref Unsafe.Add(ref result, 1)!;
dataElement = ref Unsafe.Add(ref dataElement, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Unicode;
using HotChocolate.Text.Json;
Expand Down Expand Up @@ -157,12 +158,7 @@ internal ReadOnlySpan<byte> GetRawValue(Cursor cursor, bool includeQuotes)
var start = row.Location;
var endCursor = GetEndIndex(cursor, includeEndElement: false);
var endRow = _parsedData.Get(endCursor);
var endRowLength = endRow.SizeOrLength;

if (endRow.TokenType is JsonTokenType.EndObject or JsonTokenType.StartArray)
{
endRowLength = 1;
}
var endRowLength = GetEndRowLength(endRow);

return ReadRawValue(start, endRow.Location - start + endRowLength);
}
Expand All @@ -188,12 +184,7 @@ internal ReadOnlyMemory<byte> GetRawValueAsMemory(Cursor cursor, bool includeQuo
var start = row.Location;
var endCursor = GetEndIndex(cursor, includeEndElement: false);
var endRow = _parsedData.Get(endCursor);
var endRowLength = endRow.SizeOrLength;

if (endRow.TokenType is JsonTokenType.EndObject or JsonTokenType.StartArray)
{
endRowLength = 1;
}
var endRowLength = GetEndRowLength(endRow);

return ReadRawValueAsMemory(start, endRow.Location - start + endRowLength);
}
Expand All @@ -219,12 +210,7 @@ internal ValueRange GetRawValuePointer(Cursor cursor, bool includeQuotes)
var start = row.Location;
var endCursor = GetEndIndex(cursor, includeEndElement: false);
var endRow = _parsedData.Get(endCursor);
var endRowLength = endRow.SizeOrLength;

if (endRow.TokenType is JsonTokenType.EndObject or JsonTokenType.StartArray)
{
endRowLength = 1;
}
var endRowLength = GetEndRowLength(endRow);

return new ValueRange(start, endRow.Location - start + endRowLength);
}
Expand Down Expand Up @@ -256,7 +242,7 @@ private ReadOnlySpan<byte> GetPropertyRawValue(Cursor valueCursor)

var endCursor = GetEndIndex(valueCursor, includeEndElement: false);
var endRow = _parsedData.Get(endCursor);
var endOffset = endRow.Location + endRow.SizeOrLength;
var endOffset = endRow.Location + GetEndRowLength(endRow);
return ReadRawValue(start, endOffset - start);
}

Expand All @@ -282,4 +268,10 @@ internal Cursor GetEndIndex(Cursor cursor, bool includeEndElement)

return endId;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetEndRowLength(DbRow endRow)
=> endRow.TokenType is JsonTokenType.EndObject or JsonTokenType.EndArray
? 1
: endRow.SizeOrLength;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -252,4 +253,30 @@ public void Dispose()
_disposed = true;
}
}

public override string ToString()
{
if (_usedChunks == 0)
{
return string.Empty;
}

var totalSize = 0;

for (var i = 0; i < _usedChunks; i++)
{
totalSize += _dataChunks[i].Length;
}

var buffer = new byte[totalSize];
var offset = 0;

for (var i = 0; i < _usedChunks; i++)
{
_dataChunks[i].CopyTo(buffer, offset);
offset += _dataChunks[i].Length;
}

return s_utf8Encoding.GetString(buffer).TrimEnd('\0');
Comment thread
tobias-tengler marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace HotChocolate.Fusion.Text.Json;

[DebuggerDisplay("{DebuggerDisplay,nq}")]
public readonly partial struct SourceResultElement
{
internal readonly SourceResultDocument _parent;
Expand Down Expand Up @@ -582,6 +583,43 @@ public ObjectEnumerator EnumerateObject()
return new ObjectEnumerator(this);
}

/// <inheritdoc />
public override string ToString()
{
switch (TokenType)
{
case JsonTokenType.None:
case JsonTokenType.Null:
return string.Empty;

case JsonTokenType.True:
return bool.TrueString;

case JsonTokenType.False:
return bool.FalseString;

case JsonTokenType.Number:
case JsonTokenType.StartArray:
case JsonTokenType.StartObject:
Debug.Assert(_parent != null);
return _parent.GetRawValueAsString(_cursor);

case JsonTokenType.String:
return GetString()!;

case JsonTokenType.Comment:
case JsonTokenType.EndArray:
case JsonTokenType.EndObject:
default:
Debug.Fail($"No handler for {nameof(JsonTokenType)}.{TokenType}");
return string.Empty;
}
}

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
=> ValueKind == JsonValueKind.Undefined ? "<Undefined>" : ToString();

private void CheckValidInstance()
{
if (_parent == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ internal bool EscapedNameEquals(ReadOnlySpan<byte> utf8Text)

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay
=> Value.ValueKind == JsonValueKind.Undefined ? "<Undefined>" : $"\"{ToString()}\"";
=> Value.ValueKind == JsonValueKind.Undefined ? "<Undefined>" : ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,11 @@ public string SomeField(IResolverContext context)
ErrorBuilder.New()
.SetMessage("Something went wrong")
.SetCode("SOME_ERROR")
.SetExtension("stringValue", "a-string")
.SetExtension("booleanValue", true)
.SetExtension("numberValue", 123)
.SetExtension("arrayValue", new [] { 1, 2, 3})
.SetExtension("emptyArrayValue", Array.Empty<string>())
.SetPath(context.Path)
.SetException(new Exception("Some exception"))
.Build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ response:
],
"extensions": {
"code": "SOME_ERROR",
"stringValue": "a-string",
"booleanValue": true,
"numberValue": 123,
"arrayValue": [
1,
2,
3
],
"emptyArrayValue": [],
"exception": {
"message": "Some exception",
"stackTrace": null
Expand Down Expand Up @@ -51,6 +60,15 @@ sourceSchemas:
],
"extensions": {
"code": "SOME_ERROR",
"stringValue": "a-string",
"booleanValue": true,
"numberValue": 123,
"arrayValue": [
1,
2,
3
],
"emptyArrayValue": [],
"exception": {
"message": "Some exception",
"stackTrace": null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,18 @@ public void GetRawText_ReturnsOriginalJson_Success()
Assert.Equal("\"hello\"", result.Root.GetProperty("string").GetRawText());
Assert.Contains("nested", result.Root.GetProperty("object").GetRawText());
}

[Fact]
public void GetRawText_Array_Success()
{
var json = "{\"arr\":[1,2,3],\"next\":4}"u8.ToArray();
var chunk = new byte[128 * 1024];
json.AsSpan().CopyTo(chunk);

var result = SourceResultDocument.Parse([chunk], json.Length, 1, pooledMemory: false);

var array = result.Root.GetProperty("arr");
Assert.Equal("[1,2,3]", array.GetRawText());
Assert.Equal("[1,2,3]", Encoding.UTF8.GetString(array.GetRawValueAsMemory().Span));
}
}
Loading