Skip to content

Commit

Permalink
Standardize naming from JsonPatchMerge -> JsonMergePatch (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored Apr 11, 2024
1 parent 3a466fb commit 2138cd9
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Tingle.AspNetCore.JsonPatch.Converters;

public class JsonPatchMergeDocumentConverter : JsonConverter<JsonPatchMergeDocument>
public class JsonMergePatchDocumentConverter : JsonConverter<JsonMergePatchDocument>
{
/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(JsonPatchMergeDocument);
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(JsonMergePatchDocument);

/// <inheritdoc/>
public override JsonPatchMergeDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override JsonMergePatchDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType is not JsonTokenType.StartObject)
Expand All @@ -21,13 +21,13 @@ public class JsonPatchMergeDocumentConverter : JsonConverter<JsonPatchMergeDocum
var no = new JsonNodeOptions { PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive, };
var node = JsonNode.Parse(ref reader, no)!.AsObject();
var operations = new List<Operations.Operation>();
JsonPatchMergeDocumentConverterHelper.PopulateOperations(operations, node);
JsonMergePatchDocumentConverterHelper.PopulateOperations(operations, node);

return new JsonPatchMergeDocument(operations, options);
return new JsonMergePatchDocument(operations, options);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, JsonPatchMergeDocument value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, JsonMergePatchDocument value, JsonSerializerOptions options)
{
// convert the operations to a JSON object
var operations = value.Operations ?? [];
Expand All @@ -39,7 +39,7 @@ public override void Write(Utf8JsonWriter writer, JsonPatchMergeDocument value,
if (type is Operations.OperationType.Add or Operations.OperationType.Replace)
{
var segments = operation.path.Trim('/').Split('/');
JsonPatchMergeDocumentConverterHelper.PopulateJsonObject(node, segments, operation.value, options);
JsonMergePatchDocumentConverterHelper.PopulateJsonObject(node, segments, operation.value, options);
}
}

Expand All @@ -48,13 +48,13 @@ public override void Write(Utf8JsonWriter writer, JsonPatchMergeDocument value,
}
}

public class JsonPatchMergeDocumentConverter<TModel> : JsonConverter<JsonPatchMergeDocument<TModel>> where TModel : class
public class JsonMergePatchDocumentConverter<TModel> : JsonConverter<JsonMergePatchDocument<TModel>> where TModel : class
{
/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(JsonPatchMergeDocument<TModel>);
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(JsonMergePatchDocument<TModel>);

/// <inheritdoc/>
public override JsonPatchMergeDocument<TModel>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override JsonMergePatchDocument<TModel>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null) return default;
if (reader.TokenType is not JsonTokenType.StartObject)
Expand All @@ -65,13 +65,13 @@ public class JsonPatchMergeDocumentConverter<TModel> : JsonConverter<JsonPatchMe
var no = new JsonNodeOptions { PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive, };
var node = JsonNode.Parse(ref reader, no)!.AsObject();
var operations = new List<Operations.Operation<TModel>>();
JsonPatchMergeDocumentConverterHelper.PopulateOperations(operations, node);
JsonMergePatchDocumentConverterHelper.PopulateOperations(operations, node);

return new JsonPatchMergeDocument<TModel>(operations, options);
return new JsonMergePatchDocument<TModel>(operations, options);
}

/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, JsonPatchMergeDocument<TModel> value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, JsonMergePatchDocument<TModel> value, JsonSerializerOptions options)
{
// convert the operations to a JSON object
var operations = value.Operations ?? [];
Expand All @@ -82,15 +82,15 @@ public override void Write(Utf8JsonWriter writer, JsonPatchMergeDocument<TModel>
var type = operation.OperationType;
var segments = operation.path.Trim('/').Split('/');
var opvalue = type is Operations.OperationType.Remove ? null : operation.value;
JsonPatchMergeDocumentConverterHelper.PopulateJsonObject(node, segments, opvalue, options);
JsonMergePatchDocumentConverterHelper.PopulateJsonObject(node, segments, opvalue, options);
}

// write the object
node.WriteTo(writer, options);
}
}

internal static class JsonPatchMergeDocumentConverterHelper
internal static class JsonMergePatchDocumentConverterHelper
{
internal static void PopulateJsonObject(JsonObject node, IReadOnlyList<string> segments, object? value, JsonSerializerOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

namespace Tingle.AspNetCore.JsonPatch.Converters;

internal class TypedJsonPatchMergeDocumentConverter : JsonConverterFactory
internal class TypedJsonMergePatchDocumentConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(JsonPatchMergeDocument<>);
return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(JsonMergePatchDocument<>);
}

public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
var modelType = typeToConvert.GetGenericArguments()[0];
var conveterType = typeof(JsonPatchMergeDocumentConverter<>).MakeGenericType(modelType);
var conveterType = typeof(JsonMergePatchDocumentConverter<>).MakeGenericType(modelType);
return (JsonConverter?)Activator.CreateInstance(conveterType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Tingle.AspNetCore.JsonPatch;

public interface IJsonPatchMergeDocument
public interface IJsonMergePatchDocument
{
JsonSerializerOptions SerializerOptions { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions src/Tingle.AspNetCore.JsonPatch/IMvcBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IMvcBuilder AddJsonPatch(this IMvcBuilder builder)
ServiceDescriptor.Transient<IApiDescriptionProvider, JsonPatchOperationsArrayProvider>());

services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, JsonPatchMergeDocumentProvider>());
ServiceDescriptor.Transient<IApiDescriptionProvider, JsonMergePatchDocumentProvider>());

services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, JsonPatchMvcOptionsSetup>());
Expand Down Expand Up @@ -55,9 +55,9 @@ public void Configure(MvcOptions options)

options.InputFormatters.Insert(
0,
new SystemTextJsonPatchMergeInputFormatter(
new SystemTextJsonMergePatchInputFormatter(
jsonOptions,
loggerFactory.CreateLogger<SystemTextJsonPatchMergeInputFormatter>()));
loggerFactory.CreateLogger<SystemTextJsonMergePatchInputFormatter>()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@

namespace Tingle.AspNetCore.JsonPatch;

[JsonConverter(typeof(JsonPatchMergeDocumentConverter))]
public class JsonPatchMergeDocument(JsonPatchDocument inner) : IJsonPatchMergeDocument
[JsonConverter(typeof(JsonMergePatchDocumentConverter))]
public class JsonMergePatchDocument(JsonPatchDocument inner) : IJsonMergePatchDocument
{
private readonly JsonPatchDocument inner = inner ?? throw new ArgumentNullException(nameof(inner));

[JsonIgnore]
public JsonSerializerOptions SerializerOptions { get { return inner.SerializerOptions; } set { inner.SerializerOptions = value; } }

public JsonPatchMergeDocument() : this([]) { }
public JsonMergePatchDocument() : this([]) { }

public JsonPatchMergeDocument(List<Operation> operations) : this(operations, new()) { }
public JsonMergePatchDocument(List<Operation> operations) : this(operations, new()) { }

public JsonPatchMergeDocument(JsonSerializerOptions serializerOptions) : this([], serializerOptions) { }
public JsonMergePatchDocument(JsonSerializerOptions serializerOptions) : this([], serializerOptions) { }

public JsonPatchMergeDocument(List<Operation> operations, JsonSerializerOptions serializerOptions)
public JsonMergePatchDocument(List<Operation> operations, JsonSerializerOptions serializerOptions)
: this(new JsonPatchDocument(operations, serializerOptions)) { }

internal List<Operation> Operations => inner.Operations;
IList<Operation> IJsonPatchMergeDocument.GetOperations() => ((IJsonPatchDocument)inner).GetOperations();
IList<Operation> IJsonMergePatchDocument.GetOperations() => ((IJsonPatchDocument)inner).GetOperations();

/// <summary>
/// Apply this JsonPatchMergeDocument
/// Apply this JsonMergePatchDocument
/// </summary>
/// <param name="objectToApplyTo">Object to apply the JsonPatchMergeDocument to</param>
/// <param name="objectToApplyTo">Object to apply the JsonMergePatchDocument to</param>
public void ApplyTo(object objectToApplyTo)
{
ArgumentNullException.ThrowIfNull(objectToApplyTo);
Expand All @@ -38,27 +38,27 @@ public void ApplyTo(object objectToApplyTo)
}

/// <summary>
/// Apply this JsonPatchMergeDocument
/// Apply this JsonMergePatchDocument
/// </summary>
/// <param name="objectToApplyTo">Object to apply the JsonPatchMergeDocument to</param>
/// <param name="objectToApplyTo">Object to apply the JsonMergePatchDocument to</param>
/// <param name="logErrorAction">Action to log errors</param>
public void ApplyTo(object objectToApplyTo, Action<JsonPatchError> logErrorAction)
{
ApplyTo(objectToApplyTo, new ObjectAdapter(SerializerOptions, logErrorAction, AdapterFactory.Default, create: true), logErrorAction);
}

/// <summary>
/// Apply this JsonPatchMergeDocument
/// Apply this JsonMergePatchDocument
/// </summary>
/// <param name="objectToApplyTo">Object to apply the JsonPatchMergeDocument to</param>
/// <param name="objectToApplyTo">Object to apply the JsonMergePatchDocument to</param>
/// <param name="adapter">IObjectAdapter instance to use when applying</param>
/// <param name="logErrorAction">Action to log errors</param>
public void ApplyTo(object objectToApplyTo, IObjectAdapter adapter, Action<JsonPatchError> logErrorAction) => inner.ApplyTo(objectToApplyTo, adapter, logErrorAction);

/// <summary>
/// Apply this JsonPatchMergeDocument
/// Apply this JsonMergePatchDocument
/// </summary>
/// <param name="objectToApplyTo">Object to apply the JsonPatchMergeDocument to</param>
/// <param name="objectToApplyTo">Object to apply the JsonMergePatchDocument to</param>
/// <param name="adapter">IObjectAdapter instance to use when applying</param>
public void ApplyTo(object objectToApplyTo, IObjectAdapter adapter) => inner.ApplyTo(objectToApplyTo, adapter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
namespace Tingle.AspNetCore.JsonPatch;

/// <summary>
/// Extensions for <see cref="JsonPatchMergeDocument{T}"/>
/// Extensions for <see cref="JsonMergePatchDocument{T}"/>
/// </summary>
public static class JsonPatchMergeDocumentExtensions
public static class JsonMergePatchDocumentExtensions
{
/// <summary>
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/> .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{TModel}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{TModel}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
/// <param name="immutableProperties">The properties that are not allowed to changed</param>
public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
public static void ApplyToSafely<T>(this JsonMergePatchDocument<T> patchDoc,
T objectToApplyTo,
ModelStateDictionary modelState,
IEnumerable<string> immutableProperties)
Expand All @@ -39,12 +39,12 @@ public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/> .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{TModel}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{TModel}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
/// <param name="prefix">The prefix to use when looking up values in <see cref="ModelStateDictionary"/>.</param>
/// <param name="immutableProperties">The properties that are not allowed to changed</param>
public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
public static void ApplyToSafely<T>(this JsonMergePatchDocument<T> patchDoc,
T objectToApplyTo,
ModelStateDictionary modelState,
string prefix,
Expand Down Expand Up @@ -82,10 +82,10 @@ public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/> .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{TModel}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{TModel}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
public static void ApplyToSafely<T>(this JsonMergePatchDocument<T> patchDoc,
T objectToApplyTo,
ModelStateDictionary modelState)
where T : class
Expand All @@ -103,11 +103,11 @@ public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/> .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{TModel}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{TModel}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{TModel}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
/// <param name="prefix">The prefix to use when looking up values in <see cref="ModelStateDictionary"/>.</param>
public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
public static void ApplyToSafely<T>(this JsonMergePatchDocument<T> patchDoc,
T objectToApplyTo,
ModelStateDictionary modelState,
string prefix)
Expand Down Expand Up @@ -149,10 +149,10 @@ public static void ApplyToSafely<T>(this JsonPatchMergeDocument<T> patchDoc,
/// <summary>
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/>.
/// </summary>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{T}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{T}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{T}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{T}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
public static void ApplyTo<T>(this JsonPatchMergeDocument<T> patchDoc, T objectToApplyTo, ModelStateDictionary modelState) where T : class
public static void ApplyTo<T>(this JsonMergePatchDocument<T> patchDoc, T objectToApplyTo, ModelStateDictionary modelState) where T : class
{
ArgumentNullException.ThrowIfNull(patchDoc);
ArgumentNullException.ThrowIfNull(objectToApplyTo);
Expand All @@ -164,11 +164,11 @@ public static void ApplyTo<T>(this JsonPatchMergeDocument<T> patchDoc, T objectT
/// <summary>
/// Applies JSON patch operations on object and logs errors in <see cref="ModelStateDictionary"/>.
/// </summary>
/// <param name="patchDoc">The <see cref="JsonPatchMergeDocument{T}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonPatchMergeDocument{T}"/> is applied.</param>
/// <param name="patchDoc">The <see cref="JsonMergePatchDocument{T}"/>.</param>
/// <param name="objectToApplyTo">The entity on which <see cref="JsonMergePatchDocument{T}"/> is applied.</param>
/// <param name="modelState">The <see cref="ModelStateDictionary"/> to add errors.</param>
/// <param name="prefix">The prefix to use when looking up values in <see cref="ModelStateDictionary"/>.</param>
public static void ApplyTo<T>(this JsonPatchMergeDocument<T> patchDoc, T objectToApplyTo, ModelStateDictionary modelState, string prefix) where T : class
public static void ApplyTo<T>(this JsonMergePatchDocument<T> patchDoc, T objectToApplyTo, ModelStateDictionary modelState, string prefix) where T : class
{
ArgumentNullException.ThrowIfNull(patchDoc);
ArgumentNullException.ThrowIfNull(objectToApplyTo);
Expand Down
Loading

0 comments on commit 2138cd9

Please sign in to comment.