csharp-sdk-upm/Storage/Internal/Operation/LCAddOperation.cs

50 lines
1.6 KiB
C#
Raw Normal View History

2020-02-19 18:50:51 +08:00
using System;
2020-02-20 12:44:33 +08:00
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
2020-02-19 18:50:51 +08:00
namespace LeanCloud.Storage.Internal.Operation {
2020-02-20 12:44:33 +08:00
internal class LCAddOperation : ILCOperation {
internal List<object> valueList;
internal LCAddOperation(IEnumerable<object> values) {
valueList = new List<object>(values);
}
ILCOperation ILCOperation.MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCSetOperation || previousOp is LCDeleteOperation) {
return previousOp;
}
if (previousOp is LCAddOperation addOp) {
valueList.AddRange(addOp.valueList);
return this;
}
if (previousOp is LCAddUniqueOperation addUniqueOp) {
valueList.AddRange(addUniqueOp.values);
return this;
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
object ILCOperation.Encode() {
2020-02-20 12:44:33 +08:00
return new Dictionary<string, object> {
{ "__op", "Add" },
2020-02-20 12:44:33 +08:00
{ "objects", LCEncoder.Encode(valueList) }
};
}
object ILCOperation.Apply(object oldValue, string key) {
List<object> list = new List<object>();
if (oldValue != null) {
list.AddRange(oldValue as IEnumerable<object>);
}
list.AddRange(valueList);
return list;
}
IEnumerable ILCOperation.GetNewObjectList() {
return valueList;
2020-02-19 18:50:51 +08:00
}
}
}