chore: 实现 operation

oneRain 2020-02-20 12:44:33 +08:00
parent e4381667c8
commit 8132a465ba
13 changed files with 294 additions and 27 deletions

View File

@ -17,7 +17,7 @@ namespace LeanCloud.Storage.Internal.Codec {
return EncodeDictionary(dict); return EncodeDictionary(dict);
} else if (obj is LCObject lcObj) { } else if (obj is LCObject lcObj) {
return EncodeLCObject(lcObj); return EncodeLCObject(lcObj);
} else if (obj is LCOperation op) { } else if (obj is ILCOperation op) {
return EncodeOperation(op); return EncodeOperation(op);
} else if (obj is ILCQueryCondition cond) { } else if (obj is ILCQueryCondition cond) {
return EncodeQueryCondition(cond); return EncodeQueryCondition(cond);
@ -74,7 +74,7 @@ namespace LeanCloud.Storage.Internal.Codec {
}; };
} }
static object EncodeOperation(LCOperation operation) { static object EncodeOperation(ILCOperation operation) {
return null; return null;
} }

View File

@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
namespace LeanCloud.Storage.Internal.Operation {
internal interface ILCOperation {
ILCOperation MergeWithPrevious(ILCOperation previousOp);
Dictionary<string, object> Encode();
object Apply(object oldValue, string key);
IEnumerable GetNewObjectList();
}
}

View File

@ -1,7 +1,49 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCAddOperation { internal class LCAddOperation : ILCOperation {
public LCAddOperation() { 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.");
}
Dictionary<string, object> ILCOperation.Encode() {
return new Dictionary<string, object> {
{ "op", "Add" },
{ "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;
} }
} }
} }

View File

@ -1,7 +1,43 @@
using System; using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCAddRelationOperation { internal class LCAddRelationOperation : ILCOperation {
public LCAddRelationOperation() { List<LCObject> valueList;
internal LCAddRelationOperation(IEnumerable<LCObject> objects) {
valueList = new List<LCObject>(objects);
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCSetOperation || previousOp is LCDeleteOperation) {
return previousOp;
}
if (previousOp is LCAddRelationOperation addRelationOp) {
valueList.AddRange(addRelationOp.valueList);
return this;
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
public Dictionary<string, object> Encode() {
return new Dictionary<string, object> {
{ "__op", "AddRelation" },
{ "objects", LCEncoder.Encode(valueList) }
};
}
public object Apply(object oldValue, string key) {
LCRelation<LCObject> relation = new LCRelation<LCObject>();
relation.targetClass = valueList[0].ClassName;
return relation;
}
public IEnumerable GetNewObjectList() {
return valueList;
} }
} }
} }

View File

@ -1,7 +1,46 @@
using System; using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCAddUniqueOperation { internal class LCAddUniqueOperation : ILCOperation {
public LCAddUniqueOperation() { internal HashSet<object> values;
internal LCAddUniqueOperation(IEnumerable<object> values) {
this.values = new HashSet<object>(values);
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCSetOperation || previousOp is LCDeleteOperation) {
return previousOp;
}
if (previousOp is LCAddUniqueOperation addUniqueOp) {
values.UnionWith(addUniqueOp.values);
return this;
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
public Dictionary<string, object> Encode() {
return new Dictionary<string, object> {
{ "__op", "AddUnique" },
{ "objects", LCEncoder.Encode(values.ToList()) }
};
}
public object Apply(object oldValue, string key) {
HashSet<object> set = new HashSet<object>();
if (oldValue != null) {
set.UnionWith(oldValue as IEnumerable<object>);
}
set.UnionWith(values);
return set;
}
public IEnumerable GetNewObjectList() {
return values;
} }
} }
} }

View File

@ -1,7 +1,28 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCDecrementOperation { internal class LCDecrementOperation : ILCOperation {
public LCDecrementOperation() {
internal LCDecrementOperation() {
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
throw new NotImplementedException();
}
public Dictionary<string, object> Encode() {
throw new NotImplementedException();
}
public object Apply(object oldValue, string key) {
throw new NotImplementedException();
}
public IEnumerable GetNewObjectList() {
throw new NotImplementedException();
} }
} }
} }

View File

@ -1,7 +1,28 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCDeleteOperation { internal class LCDeleteOperation : ILCOperation {
public LCDeleteOperation() { internal LCDeleteOperation() {
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
return this;
}
public Dictionary<string, object> Encode() {
return new Dictionary<string, object> {
{ "__op", "Delete" }
};
}
public object Apply(object oldValue, string key) {
return null;
}
public IEnumerable GetNewObjectList() {
return null;
} }
} }
} }

View File

@ -1,7 +0,0 @@
using System;
namespace LeanCloud.Storage.Internal.Operation {
public class LCOperation {
public LCOperation() {
}
}
}

View File

@ -1,7 +1,46 @@
using System; using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCRemoveOperation { internal class LCRemoveOperation : ILCOperation {
public LCRemoveOperation() { List<object> valueList;
internal LCRemoveOperation(IEnumerable values) {
valueList = new List<object> {
values.Cast<object>()
};
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCSetOperation || previousOp is LCDeleteOperation) {
return previousOp;
}
if (previousOp is LCRemoveOperation removeOp) {
valueList.AddRange(removeOp.valueList);
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
public Dictionary<string, object> Encode() {
return new Dictionary<string, object> {
{ "__op", "Remove" },
{ "objects", LCEncoder.Encode(valueList) }
};
}
public object Apply(object oldValue, string key) {
List<object> list = new List<object>();
if (oldValue != null) {
list.AddRange(oldValue as IEnumerable<object>);
}
return list;
}
public IEnumerable GetNewObjectList() {
return null;
} }
} }
} }

View File

@ -1,7 +1,42 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
public class LCRemoveRelationOperation { internal class LCRemoveRelationOperation : ILCOperation {
public LCRemoveRelationOperation() { List<LCObject> valueList;
internal LCRemoveRelationOperation(LCObject obj) {
valueList = new List<LCObject> { obj };
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
if (previousOp is LCSetOperation || previousOp is LCDeleteOperation) {
return previousOp;
}
if (previousOp is LCRemoveRelationOperation removeRelationOp) {
valueList.AddRange(removeRelationOp.valueList);
return this;
}
throw new ArgumentException("Operation is invalid after previous operation.");
}
public Dictionary<string, object> Encode() {
return new Dictionary<string, object> {
{ "__op", "RemoveRelation" },
{ "objects", LCEncoder.Encode(valueList) }
};
}
public object Apply(object oldValue, string key) {
LCRelation<LCObject> relation = new LCRelation<LCObject>();
relation.targetClass = valueList[0].ClassName;
return relation;
}
public IEnumerable GetNewObjectList() {
return null;
} }
} }
} }

View File

@ -1,7 +1,33 @@
using System; using System;
namespace LeanCloud.Storage.Internal { using System.Collections;
public class LCSetOperation { using System.Collections.Generic;
public LCSetOperation() { using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Storage.Internal.Operation {
internal class LCSetOperation : ILCOperation {
object value;
internal LCSetOperation(object value) {
this.value = value;
}
public ILCOperation MergeWithPrevious(ILCOperation previousOp) {
return this;
}
public Dictionary<string, object> Encode() {
return LCEncoder.Encode(value) as Dictionary<string, object>;
}
public object Apply(object oldValue, string key) {
return value;
}
public IEnumerable GetNewObjectList() {
if (value is IEnumerable enumerable) {
return enumerable;
}
return new List<object> { value };
} }
} }
} }

View File

@ -20,7 +20,7 @@ namespace LeanCloud.Storage {
/// <summary> /// <summary>
/// 操作字典 /// 操作字典
/// </summary> /// </summary>
Dictionary<string, LCOperation> operationDict; Dictionary<string, ILCOperation> operationDict;
public string ClassName { public string ClassName {
get { get {

View File

@ -1,4 +1,5 @@
using System; using System;
namespace LeanCloud.Storage { namespace LeanCloud.Storage {
public class LCRole : LCObject { public class LCRole : LCObject {
public LCRole() { public LCRole() {