Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch: Adds Move Operation #3389

Merged
merged 19 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Changes made to address comments'
  • Loading branch information
Amaan Haque committed Aug 16, 2022
commit e0b9a5332bb9f40106849ff168c93e91fcf127b5
10 changes: 5 additions & 5 deletions Microsoft.Azure.Cosmos/src/Patch/PatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public abstract class PatchOperation
/// Source location reference (used in case of move)
Amaan-Haque marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
[JsonProperty(PropertyName = PatchConstants.PropertyNames.From)]
public abstract string From { get; }
public virtual string From { get; set; } = null;
Amaan-Haque marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Serializes the value parameter, if specified for the PatchOperation.
Expand Down Expand Up @@ -140,16 +140,16 @@ public static PatchOperation Increment(
path,
value);
}

/// <summary>
/// Create <see cref="PatchOperation"/> to move an object/value.
/// </summary>
/// <param name="path">Target location reference.</param>
/// <param name="from">The source location of the object/value.</param>
/// <param name="path">Target location reference.</param>
/// <returns>PatchOperation instance for specified input.</returns>
public static PatchOperation Move(
ealsur marked this conversation as resolved.
Show resolved Hide resolved
string path,
string from)
string from,
string path)
{
return new PatchOperationCore<string>(
PatchOperationType.Move,
Expand Down
1 change: 0 additions & 1 deletion Microsoft.Azure.Cosmos/src/Patch/PatchOperationCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ public PatchOperationCore(
public override PatchOperationType OperationType { get; }

public override string Path { get; }
public override string From { get; }
}
}
33 changes: 17 additions & 16 deletions Microsoft.Azure.Cosmos/src/Patch/PatchOperationCore{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,46 @@ namespace Microsoft.Azure.Cosmos
internal sealed class PatchOperationCore<T> : PatchOperation<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="PatchOperationCore{T}"/> class.
/// </summary>
/// <param name="operationType">Specifies the type of Patch operation.</param>
/// <param name="path">Specifies the path to target location.</param>
/// <param name="value">Specifies the value to be used. In case of move operations it will be a string specifying the source
/// location.</param>
        /// Initializes a new instance of the <see cref="PatchOperationCore{T}"/> class.
        /// </summary>
        /// <param name="operationType">Specifies the type of Patch operation.</param>
        /// <param name="path">Specifies the path to target location.</param>
        /// <param name="value">Specifies the value to be used. In case of move operations it will be a string specifying the source
        /// location.</param>
public PatchOperationCore(
PatchOperationType operationType,
string path,
T value)
PatchOperationType operationType,
string path,
T value)
{
this.OperationType = operationType;
if (operationType == PatchOperationType.Move)
{
this.Path = string.IsNullOrWhiteSpace(path)
? throw new ArgumentNullException(nameof(path))
: path;

this.From = string.IsNullOrWhiteSpace(value.ToString())
if (!(value is String valueAsString))
{
throw new ArgumentException(
$"Parameter {nameof(value)} must be of type String for patch operation type {nameof(PatchOperationType.Move)}");
}
this.From = string.IsNullOrWhiteSpace(valueAsString)
? throw new ArgumentNullException(nameof(value))
: value.ToString();
: valueAsString;
}
else
{
this.Path = string.IsNullOrWhiteSpace(path)
? throw new ArgumentNullException(nameof(path))
: path;
this.Value = value;
this.Value = value;
}
}

public override T Value { get; }

public override PatchOperationType OperationType { get; }

public override string Path { get; }

public override string From { get; }

public override bool TrySerializeValueParameter(
CosmosSerializer cosmosSerializer,
out Stream valueParam)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public override void WriteJson(
writer.WritePropertyName(PatchConstants.PropertyNames.From);
writer.WriteValue(operation.From);
}

if (operation.TrySerializeValueParameter(this.userSerializer, out Stream valueStream))
else if (operation.TrySerializeValueParameter(this.userSerializer, out Stream valueStream))
{
string valueParam;
using (valueStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ public async Task BatchCustomSerializerUsedForPatchAsync()
List<PatchOperation> patchOperations = new List<PatchOperation>()
{
PatchOperation.Add("/date", patchDate),
PatchOperation.Move("/TodayDate", "/date")
PatchOperation.Move("/date", "/TodayDate")
Amaan-Haque marked this conversation as resolved.
Show resolved Hide resolved
};

BatchCore batch = (BatchCore)new BatchCore((ContainerInlineCore)customSerializationContainer, BatchTestBase.GetPartitionKey(this.PartitionKey1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2004,8 +2004,8 @@ public async Task ItemPatchSuccessTest()

patchOperations.Clear();
patchOperations.Add(PatchOperation.Add("/children/1/description","Child#1"));
patchOperations.Add(PatchOperation.Move("/description", "/children/0/description"));
patchOperations.Add(PatchOperation.Move("/children/0/description", "/children/1/description"));
patchOperations.Add(PatchOperation.Move("/children/0/description", "/description"));
patchOperations.Add(PatchOperation.Move("/children/1/description", "/children/0/description"));
// with content response
response = await containerInternal.PatchItemAsync<ToDoActivity>(
id: testItem.id,
Expand Down