* LCObject.cs:
* LCAddOperation.cs: * LCRemoveOperation.cs: * ObjectTest.cs: chore: 支持操作未赋值字段
parent
94f1c9feca
commit
7bd2cd21d3
|
@ -5,6 +5,8 @@ using System.Collections.Generic;
|
|||
using LeanCloud;
|
||||
using LeanCloud.Storage;
|
||||
|
||||
using static NUnit.Framework.TestContext;
|
||||
|
||||
namespace Storage.Test {
|
||||
public class ObjectTest {
|
||||
[SetUp]
|
||||
|
@ -144,5 +146,25 @@ namespace Storage.Test {
|
|||
TestContext.WriteLine(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LeanCloud.Storage.Internal.Codec;
|
||||
|
||||
namespace LeanCloud.Storage.Internal.Operation {
|
||||
|
@ -16,11 +17,15 @@ namespace LeanCloud.Storage.Internal.Operation {
|
|||
return previousOp;
|
||||
}
|
||||
if (previousOp is LCAddOperation addOp) {
|
||||
valueList.AddRange(addOp.valueList);
|
||||
List<object> list = new List<object>(addOp.valueList);
|
||||
list.AddRange(valueList);
|
||||
valueList = list;
|
||||
return this;
|
||||
}
|
||||
if (previousOp is LCAddUniqueOperation addUniqueOp) {
|
||||
valueList.AddRange(addUniqueOp.values);
|
||||
List<object> list = addUniqueOp.values.ToList();
|
||||
list.AddRange(valueList);
|
||||
valueList = list;
|
||||
return this;
|
||||
}
|
||||
throw new ArgumentException("Operation is invalid after previous operation.");
|
||||
|
|
|
@ -17,7 +17,10 @@ namespace LeanCloud.Storage.Internal.Operation {
|
|||
return previousOp;
|
||||
}
|
||||
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.");
|
||||
}
|
||||
|
@ -30,7 +33,10 @@ namespace LeanCloud.Storage.Internal.Operation {
|
|||
}
|
||||
|
||||
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));
|
||||
return list;
|
||||
}
|
||||
|
|
|
@ -420,11 +420,6 @@ namespace LeanCloud.Storage {
|
|||
}
|
||||
|
||||
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) {
|
||||
estimatedData.Remove(key);
|
||||
} else {
|
||||
|
@ -434,6 +429,11 @@ namespace LeanCloud.Storage {
|
|||
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) {
|
||||
|
|
Loading…
Reference in New Issue