-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JetStream API serialization with typed results
Replace JsonDocument-based responses with strongly-typed `NatsJSApiResult<T>` for improved type safety and error handling. Added new deserialization logic and tests to cover valid responses, errors, and edge cases like empty buffers.
- Loading branch information
Showing
4 changed files
with
127 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Runtime.CompilerServices; | ||
using NATS.Client.JetStream.Models; | ||
|
||
namespace NATS.Client.JetStream.Internal; | ||
|
||
internal readonly struct NatsJSApiResult<T> | ||
{ | ||
private readonly T? _value; | ||
private readonly ApiError? _error; | ||
private readonly Exception? _exception; | ||
|
||
public NatsJSApiResult(T value) | ||
{ | ||
_value = value; | ||
_error = null; | ||
_exception = null; | ||
} | ||
|
||
public NatsJSApiResult(ApiError error) | ||
{ | ||
_value = default; | ||
_error = error; | ||
_exception = null; | ||
} | ||
|
||
public NatsJSApiResult(Exception exception) | ||
{ | ||
_value = default; | ||
_error = null; | ||
_exception = exception; | ||
} | ||
|
||
public T Value => _value ?? ThrowValueIsNotSetException(); | ||
|
||
public ApiError Error => _error ?? ThrowErrorIsNotSetException(); | ||
|
||
public Exception Exception => _exception ?? ThrowExceptionIsNotSetException(); | ||
|
||
public bool Success => _error == null && _exception == null; | ||
|
||
public bool HasError => _error != null; | ||
|
||
public bool HasException => _exception != null; | ||
|
||
public static implicit operator NatsJSApiResult<T>(T value) => new(value); | ||
|
||
public static implicit operator NatsJSApiResult<T>(ApiError error) => new(error); | ||
|
||
public static implicit operator NatsJSApiResult<T>(Exception exception) => new(exception); | ||
|
||
private static T ThrowValueIsNotSetException() => throw CreateInvalidOperationException("Result value is not set"); | ||
|
||
private static ApiError ThrowErrorIsNotSetException() => throw CreateInvalidOperationException("Result error is not set"); | ||
|
||
private static Exception ThrowExceptionIsNotSetException() => throw CreateInvalidOperationException("Result exception is not set"); | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static Exception CreateInvalidOperationException(string message) => new InvalidOperationException(message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters