* LCObject.cs:

* LCAddOperation.cs:
* LCRemoveOperation.cs:

* ObjectTest.cs: chore: 支持操作未赋值字段
oneRain 2020-05-06 12:06:35 +08:00
parent 94f1c9feca
commit 7bd2cd21d3
4 changed files with 42 additions and 9 deletions

View File

@ -5,6 +5,8 @@ using System.Collections.Generic;
using LeanCloud; using LeanCloud;
using LeanCloud.Storage; using LeanCloud.Storage;
using static NUnit.Framework.TestContext;
namespace Storage.Test { namespace Storage.Test {
public class ObjectTest { public class ObjectTest {
[SetUp] [SetUp]
@ -144,5 +146,25 @@ namespace Storage.Test {
TestContext.WriteLine(hello["content"]); TestContext.WriteLine(hello["content"]);
Assert.IsNull(hello["content"]); Assert.IsNull(hello["content"]);
} }
[Test]
public async Task OperateNullProperty() {
LCObject obj = new LCObject("Hello");
obj.Increment("intValue", 123);
obj.Increment("intValue", 321);
obj.Add("intList", 1);
obj.Add("intList", 2);
obj.Add("intList", 3);
await obj.Save();
WriteLine(obj["intValue"]);
Assert.AreEqual(obj["intValue"], 444);
List<object> intList = obj["intList"] as List<object>;
WriteLine(intList.Count);
Assert.AreEqual(intList.Count, 3);
Assert.AreEqual(intList[0], 1);
Assert.AreEqual(intList[1], 2);
Assert.AreEqual(intList[2], 3);
}
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using LeanCloud.Storage.Internal.Codec; using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
@ -16,11 +17,15 @@ namespace LeanCloud.Storage.Internal.Operation {
return previousOp; return previousOp;
} }
if (previousOp is LCAddOperation addOp) { if (previousOp is LCAddOperation addOp) {
valueList.AddRange(addOp.valueList); List<object> list = new List<object>(addOp.valueList);
list.AddRange(valueList);
valueList = list;
return this; return this;
} }
if (previousOp is LCAddUniqueOperation addUniqueOp) { if (previousOp is LCAddUniqueOperation addUniqueOp) {
valueList.AddRange(addUniqueOp.values); List<object> list = addUniqueOp.values.ToList();
list.AddRange(valueList);
valueList = list;
return this; return this;
} }
throw new ArgumentException("Operation is invalid after previous operation."); throw new ArgumentException("Operation is invalid after previous operation.");

View File

@ -17,7 +17,10 @@ namespace LeanCloud.Storage.Internal.Operation {
return previousOp; return previousOp;
} }
if (previousOp is LCRemoveOperation removeOp) { if (previousOp is LCRemoveOperation removeOp) {
valueList.AddRange(removeOp.valueList); List<object> list = new List<object>(removeOp.valueList);
list.AddRange(valueList);
valueList = list;
return this;
} }
throw new ArgumentException("Operation is invalid after previous operation."); throw new ArgumentException("Operation is invalid after previous operation.");
} }
@ -30,7 +33,10 @@ namespace LeanCloud.Storage.Internal.Operation {
} }
public object Apply(object oldValue, string key) { public object Apply(object oldValue, string key) {
List<object> list = new List<object>(oldValue as IEnumerable<object>); List<object> list = new List<object>();
if (oldValue != null) {
list.AddRange(oldValue as IEnumerable<object>);
}
list.RemoveAll(item => valueList.Contains(item)); list.RemoveAll(item => valueList.Contains(item));
return list; return list;
} }

View File

@ -420,11 +420,6 @@ namespace LeanCloud.Storage {
} }
void ApplyOperation(string key, ILCOperation op) { void ApplyOperation(string key, ILCOperation op) {
if (operationDict.TryGetValue(key, out ILCOperation previousOp)) {
operationDict[key] = op.MergeWithPrevious(previousOp);
} else {
operationDict[key] = op;
}
if (op is LCDeleteOperation) { if (op is LCDeleteOperation) {
estimatedData.Remove(key); estimatedData.Remove(key);
} else { } else {
@ -434,6 +429,11 @@ namespace LeanCloud.Storage {
estimatedData[key] = op.Apply(null, key); estimatedData[key] = op.Apply(null, key);
} }
} }
if (operationDict.TryGetValue(key, out ILCOperation previousOp)) {
operationDict[key] = op.MergeWithPrevious(previousOp);
} else {
operationDict[key] = op;
}
} }
internal void Merge(LCObjectData objectData) { internal void Merge(LCObjectData objectData) {