-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrew Butler <andrew.butler@boomin.com>
- Loading branch information
Showing
8 changed files
with
89 additions
and
97 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
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace HttpPatch.Schema | ||
namespace HttpPatch.Schema; | ||
|
||
public class PatchDocument | ||
{ | ||
public class PatchDocument | ||
{ | ||
[MinLength(1)] | ||
[Required] | ||
public IEnumerable<PatchOperation> Operations { get; set; } | ||
} | ||
[MinLength(1)] | ||
[Required] | ||
public IEnumerable<PatchOperation> Operations { get; set; } = null!; | ||
} |
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 |
---|---|---|
@@ -1,50 +1,50 @@ | ||
using System.Buffers; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace HttpPatch.Schema | ||
namespace HttpPatch.Schema; | ||
|
||
public class PatchOperation | ||
{ | ||
public class PatchOperation | ||
{ | ||
[JsonPropertyName("op")] | ||
[JsonConverter(typeof(EnumMemberJsonConverter<PatchOperationType>))] | ||
public PatchOperationType Type { get; set; } | ||
[JsonPropertyName("op")] | ||
[JsonConverter(typeof(EnumMemberJsonConverter<PatchOperationType>))] | ||
[Required] | ||
public PatchOperationType Type { get; set; } | ||
|
||
public string Path { get; set; } | ||
[Required] | ||
public string Path { get; set; } = null!; | ||
|
||
public object Value { get; set; } | ||
public object? Value { get; set; } | ||
|
||
public T ValueAs<T>(T defaultValue = default) | ||
public T? ValueAs<T>(T? defaultValue = default) | ||
{ | ||
if (Value == null) | ||
{ | ||
if (Value == null) | ||
{ | ||
return defaultValue; | ||
} | ||
return defaultValue; | ||
} | ||
|
||
if (!(Value is JsonElement)) | ||
{ | ||
throw new JsonException("Value is not an JsonElement."); | ||
} | ||
if (Value is not JsonElement valueElement) | ||
{ | ||
throw new JsonException("Value is not an JsonElement."); | ||
} | ||
|
||
var element = (JsonElement)Value; | ||
|
||
if (typeof(T).IsEnum) | ||
{ | ||
var value = element.GetString(); | ||
if (typeof(T).IsEnum) | ||
{ | ||
var value = valueElement.GetString(); | ||
|
||
if (!Enum.TryParse(typeof(T), value, true, out var enumValue)) | ||
{ | ||
throw new PatchOperationValidationException($"'{value}' is not a valid value for {typeof(T).Name}"); | ||
} | ||
return (T)enumValue; | ||
if (!Enum.TryParse(typeof(T), value, true, out var enumValue)) | ||
{ | ||
throw new PatchOperationValidationException($"'{value}' is not a valid value for {typeof(T).Name}"); | ||
} | ||
return (T?)enumValue; | ||
} | ||
|
||
var bufferWriter = new ArrayBufferWriter<byte>(1024); | ||
using var writer = new Utf8JsonWriter(bufferWriter); | ||
element.WriteTo(writer); | ||
writer.Flush(); | ||
var bufferWriter = new ArrayBufferWriter<byte>(1024); | ||
using var writer = new Utf8JsonWriter(bufferWriter); | ||
valueElement.WriteTo(writer); | ||
writer.Flush(); | ||
|
||
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, new JsonSerializerOptions {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}); | ||
} | ||
return JsonSerializer.Deserialize<T?>(bufferWriter.WrittenSpan, new JsonSerializerOptions {PropertyNamingPolicy = JsonNamingPolicy.CamelCase}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
namespace HttpPatch.Schema | ||
namespace HttpPatch.Schema; | ||
|
||
public enum PatchOperationType | ||
{ | ||
public enum PatchOperationType | ||
{ | ||
Remove, | ||
Replace | ||
} | ||
Remove, | ||
Replace | ||
} |
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 |
---|---|---|
@@ -1,27 +1,26 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace HttpPatch.Schema | ||
namespace HttpPatch.Schema; | ||
|
||
[Serializable] | ||
public class PatchOperationValidationException : Exception | ||
{ | ||
[Serializable] | ||
public class PatchOperationValidationException : Exception | ||
public PatchOperationValidationException() | ||
{ | ||
public PatchOperationValidationException() | ||
{ | ||
} | ||
} | ||
|
||
public PatchOperationValidationException(string message) : base(message) | ||
{ | ||
} | ||
public PatchOperationValidationException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public PatchOperationValidationException(string message, Exception inner) : base(message, inner) | ||
{ | ||
} | ||
public PatchOperationValidationException(string message, Exception inner) : base(message, inner) | ||
{ | ||
} | ||
|
||
protected PatchOperationValidationException( | ||
SerializationInfo info, | ||
StreamingContext context | ||
) : base(info, context) | ||
{ | ||
} | ||
protected PatchOperationValidationException( | ||
SerializationInfo info, | ||
StreamingContext context | ||
) : base(info, context) | ||
{ | ||
} | ||
} |
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