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 : Refactors the TrySerializeValueParameter method for PatchOperation and makes it public #2398

Merged
merged 5 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions Microsoft.Azure.Cosmos/src/Patch/PatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.IO;
using Newtonsoft.Json;

/// <summary>
Expand All @@ -29,9 +30,16 @@ abstract class PatchOperation
[JsonProperty(PropertyName = PatchConstants.PropertyNames.Path)]
public abstract string Path { get; }

internal virtual bool TrySerializeValueParameter(
/// <summary>
/// Serializes the value parameter, if specified for the PatchOperation.
/// </summary>
/// <param name="cosmosSerializer">Serializer to be used.</param>
/// <param name="valueParam">Outputs the serialized stream if value parameter is specified, null otherwise.</param>
/// <returns>True if value is serialized, false otherwise.</returns>
/// <remarks>Output stream should be disposed after use.</remarks>
public virtual bool TrySerializeValueParameter(
CosmosSerializer cosmosSerializer,
out string valueParam)
out Stream valueParam)
{
valueParam = null;
return false;
Expand Down
14 changes: 4 additions & 10 deletions Microsoft.Azure.Cosmos/src/Patch/PatchOperationCore{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,14 @@ public PatchOperationCore(

public override string Path { get; }

internal override bool TrySerializeValueParameter(
public override bool TrySerializeValueParameter(
CosmosSerializer cosmosSerializer,
out string valueParam)
out Stream valueParam)
{
// Use the user serializer so custom conversions are correctly handled
using (Stream stream = cosmosSerializer.ToStream(this.Value))
{
using (StreamReader streamReader = new StreamReader(stream))
{
valueParam = streamReader.ReadToEnd();
}
}
valueParam = cosmosSerializer.ToStream(this.Value);

return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -87,8 +88,17 @@ public override void WriteJson(
writer.WritePropertyName(PatchConstants.PropertyNames.Path);
writer.WriteValue(operation.Path);

if (operation.TrySerializeValueParameter(this.userSerializer, out string valueParam))
if (operation.TrySerializeValueParameter(this.userSerializer, out Stream valueStream))
{
string valueParam;
using (valueStream)
{
using (StreamReader streamReader = new StreamReader(valueStream))
{
valueParam = streamReader.ReadToEnd();
j82w marked this conversation as resolved.
Show resolved Hide resolved
}
}

writer.WritePropertyName(PatchConstants.PropertyNames.Value);
writer.WriteRawValue(valueParam);
anujtoshniwal marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,11 @@
}
},
"Members": {
"Boolean TrySerializeValueParameter(Microsoft.Azure.Cosmos.CosmosSerializer, System.IO.Stream ByRef)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "Boolean TrySerializeValueParameter(Microsoft.Azure.Cosmos.CosmosSerializer, System.IO.Stream ByRef);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Microsoft.Azure.Cosmos.PatchOperation Add[T](System.String, T)": {
"Type": "Method",
"Attributes": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,26 @@ private static void ValidateOperations<T>(PatchOperation patchOperation, PatchOp
{
string expected;
CosmosSerializer cosmosSerializer = new CosmosJsonDotNetSerializer();
Stream stream = cosmosSerializer.ToStream(value);
using (StreamReader streamReader = new StreamReader(stream))
using (Stream stream = cosmosSerializer.ToStream(value))
{
expected = streamReader.ReadToEnd();
using (StreamReader streamReader = new StreamReader(stream))
{
expected = streamReader.ReadToEnd();
}
}

Assert.IsTrue(patchOperation.TrySerializeValueParameter(new CustomSerializer(), out string valueParam));
Assert.AreEqual(expected, valueParam);
Assert.IsTrue(patchOperation.TrySerializeValueParameter(new CustomSerializer(), out Stream valueParam));

string actual;
using (valueParam)
{
using (StreamReader streamReader = new StreamReader(valueParam))
{
actual = streamReader.ReadToEnd();
}
}

Assert.AreEqual(expected, actual);
}
}

Expand Down