chore: open internal api

oneRain 2021-03-05 12:22:58 +08:00
parent 36ed2980fb
commit fec7d89ee9
5 changed files with 46 additions and 40 deletions

View File

@ -80,10 +80,10 @@ namespace LeanCloud.Storage.Internal.Codec {
string key = kv.Key; string key = kv.Key;
Dictionary<string, object> access = kv.Value as Dictionary<string, object>; Dictionary<string, object> access = kv.Value as Dictionary<string, object>;
if (access.TryGetValue("read", out object ra)) { if (access.TryGetValue("read", out object ra)) {
acl.readAccess[key] = Convert.ToBoolean(ra); acl.ReadAccess[key] = Convert.ToBoolean(ra);
} }
if (access.TryGetValue("write", out object wa)) { if (access.TryGetValue("write", out object wa)) {
acl.writeAccess[key] = Convert.ToBoolean(wa); acl.WriteAccess[key] = Convert.ToBoolean(wa);
} }
} }
return acl; return acl;

View File

@ -84,19 +84,19 @@ namespace LeanCloud.Storage.Internal.Codec {
public static object EncodeACL(LCACL acl) { public static object EncodeACL(LCACL acl) {
HashSet<string> keys = new HashSet<string>(); HashSet<string> keys = new HashSet<string>();
if (acl.readAccess.Count > 0) { if (acl.ReadAccess.Count > 0) {
keys.UnionWith(acl.readAccess.Keys); keys.UnionWith(acl.ReadAccess.Keys);
} }
if (acl.writeAccess.Count > 0) { if (acl.WriteAccess.Count > 0) {
keys.UnionWith(acl.writeAccess.Keys); keys.UnionWith(acl.WriteAccess.Keys);
} }
Dictionary<string, object> result = new Dictionary<string, object>(); Dictionary<string, object> result = new Dictionary<string, object>();
foreach (string key in keys) { foreach (string key in keys) {
Dictionary<string, bool> access = new Dictionary<string, bool>(); Dictionary<string, bool> access = new Dictionary<string, bool>();
if (acl.readAccess.TryGetValue(key, out bool ra)) { if (acl.ReadAccess.TryGetValue(key, out bool ra)) {
access["read"] = ra; access["read"] = ra;
} }
if (acl.writeAccess.TryGetValue(key, out bool wa)) { if (acl.WriteAccess.TryGetValue(key, out bool wa)) {
access["write"] = wa; access["write"] = wa;
} }
result[key] = access; result[key] = access;

View File

@ -1,8 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic;
namespace LeanCloud.Storage.Internal.Operation { namespace LeanCloud.Storage.Internal.Operation {
internal interface ILCOperation { public interface ILCOperation {
ILCOperation MergeWithPrevious(ILCOperation previousOp); ILCOperation MergeWithPrevious(ILCOperation previousOp);
object Encode(); object Encode();

View File

@ -10,8 +10,13 @@ namespace LeanCloud.Storage {
const string RoleKeyPrefix = "role:"; const string RoleKeyPrefix = "role:";
internal Dictionary<string, bool> readAccess = new Dictionary<string, bool>(); public Dictionary<string, bool> ReadAccess {
internal Dictionary<string, bool> writeAccess = new Dictionary<string, bool>(); get;
} = new Dictionary<string, bool>();
public Dictionary<string, bool> WriteAccess {
get;
} = new Dictionary<string, bool>();
public static LCACL CreateWithOwner(LCUser owner) { public static LCACL CreateWithOwner(LCUser owner) {
if (owner == null) { if (owner == null) {
@ -25,17 +30,17 @@ namespace LeanCloud.Storage {
public bool PublicReadAccess { public bool PublicReadAccess {
get { get {
return GetAccess(readAccess, PublicKey); return GetAccess(ReadAccess, PublicKey);
} set { } set {
SetAccess(readAccess, PublicKey, value); SetAccess(ReadAccess, PublicKey, value);
} }
} }
public bool PublicWriteAccess { public bool PublicWriteAccess {
get { get {
return GetAccess(writeAccess, PublicKey); return GetAccess(WriteAccess, PublicKey);
} set { } set {
SetAccess(writeAccess, PublicKey, value); SetAccess(WriteAccess, PublicKey, value);
} }
} }
@ -43,28 +48,28 @@ namespace LeanCloud.Storage {
if (string.IsNullOrEmpty(userId)) { if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId)); throw new ArgumentNullException(nameof(userId));
} }
return GetAccess(readAccess, userId); return GetAccess(ReadAccess, userId);
} }
public void SetUserIdReadAccess(string userId, bool value) { public void SetUserIdReadAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) { if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId)); throw new ArgumentNullException(nameof(userId));
} }
SetAccess(readAccess, userId, value); SetAccess(ReadAccess, userId, value);
} }
public bool GetUserIdWriteAccess(string userId) { public bool GetUserIdWriteAccess(string userId) {
if (string.IsNullOrEmpty(userId)) { if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId)); throw new ArgumentNullException(nameof(userId));
} }
return GetAccess(writeAccess, userId); return GetAccess(WriteAccess, userId);
} }
public void SetUserIdWriteAccess(string userId, bool value) { public void SetUserIdWriteAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) { if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId)); throw new ArgumentNullException(nameof(userId));
} }
SetAccess(writeAccess, userId, value); SetAccess(WriteAccess, userId, value);
} }
public bool GetUserReadAccess(LCUser user) { public bool GetUserReadAccess(LCUser user) {
@ -100,7 +105,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role)); throw new ArgumentNullException(nameof(role));
} }
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}"; string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
return GetAccess(readAccess, roleKey); return GetAccess(ReadAccess, roleKey);
} }
public void SetRoleReadAccess(LCRole role, bool value) { public void SetRoleReadAccess(LCRole role, bool value) {
@ -108,7 +113,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role)); throw new ArgumentNullException(nameof(role));
} }
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}"; string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
SetAccess(readAccess, roleKey, value); SetAccess(ReadAccess, roleKey, value);
} }
public bool GetRoleWriteAccess(LCRole role) { public bool GetRoleWriteAccess(LCRole role) {
@ -116,7 +121,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role)); throw new ArgumentNullException(nameof(role));
} }
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}"; string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
return GetAccess(writeAccess, roleKey); return GetAccess(WriteAccess, roleKey);
} }
public void SetRoleWriteAccess(LCRole role, bool value) { public void SetRoleWriteAccess(LCRole role, bool value) {
@ -124,7 +129,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role)); throw new ArgumentNullException(nameof(role));
} }
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}"; string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
SetAccess(writeAccess, roleKey, value); SetAccess(WriteAccess, roleKey, value);
} }
bool GetAccess(Dictionary<string, bool> access, string key) { bool GetAccess(Dictionary<string, bool> access, string key) {

View File

@ -16,7 +16,9 @@ namespace LeanCloud.Storage {
/// <summary> /// <summary>
/// Last synced data. /// Last synced data.
/// </summary> /// </summary>
LCObjectData data; public LCObjectData Data {
get;
}
/// <summary> /// <summary>
/// Estimated data. /// Estimated data.
@ -33,25 +35,25 @@ namespace LeanCloud.Storage {
public string ClassName { public string ClassName {
get { get {
return data.ClassName; return Data.ClassName;
} }
} }
public string ObjectId { public string ObjectId {
get { get {
return data.ObjectId; return Data.ObjectId;
} }
} }
public DateTime CreatedAt { public DateTime CreatedAt {
get { get {
return data.CreatedAt; return Data.CreatedAt;
} }
} }
public DateTime UpdatedAt { public DateTime UpdatedAt {
get { get {
return data.UpdatedAt; return Data.UpdatedAt;
} }
} }
@ -75,11 +77,11 @@ namespace LeanCloud.Storage {
if (string.IsNullOrEmpty(className)) { if (string.IsNullOrEmpty(className)) {
throw new ArgumentNullException(nameof(className)); throw new ArgumentNullException(nameof(className));
} }
data = new LCObjectData(); Data = new LCObjectData();
estimatedData = new Dictionary<string, object>(); estimatedData = new Dictionary<string, object>();
operationDict = new Dictionary<string, ILCOperation>(); operationDict = new Dictionary<string, ILCOperation>();
data.ClassName = className; Data.ClassName = className;
isNew = true; isNew = true;
} }
@ -88,7 +90,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(objectId)); throw new ArgumentNullException(nameof(objectId));
} }
LCObject obj = Create(className); LCObject obj = Create(className);
obj.data.ObjectId = objectId; obj.Data.ObjectId = objectId;
obj.isNew = false; obj.isNew = false;
return obj; return obj;
} }
@ -454,7 +456,7 @@ namespace LeanCloud.Storage {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public override string ToString() { public override string ToString() {
Dictionary<string, object> originalData = LCObjectData.Encode(data); Dictionary<string, object> originalData = LCObjectData.Encode(Data);
Dictionary<string, object> currentData = estimatedData.Union(originalData.Where(kv => !estimatedData.ContainsKey(kv.Key))) Dictionary<string, object> currentData = estimatedData.Union(originalData.Where(kv => !estimatedData.ContainsKey(kv.Key)))
.ToDictionary(k => k.Key, v => v.Value); .ToDictionary(k => k.Key, v => v.Value);
return JsonConvert.SerializeObject(currentData); return JsonConvert.SerializeObject(currentData);
@ -490,17 +492,17 @@ namespace LeanCloud.Storage {
} }
public void Merge(LCObjectData objectData) { public void Merge(LCObjectData objectData) {
data.ClassName = objectData.ClassName ?? data.ClassName; Data.ClassName = objectData.ClassName ?? Data.ClassName;
data.ObjectId = objectData.ObjectId ?? data.ObjectId; Data.ObjectId = objectData.ObjectId ?? Data.ObjectId;
data.CreatedAt = objectData.CreatedAt != null ? objectData.CreatedAt : data.CreatedAt; Data.CreatedAt = objectData.CreatedAt != null ? objectData.CreatedAt : Data.CreatedAt;
data.UpdatedAt = objectData.UpdatedAt != null ? objectData.UpdatedAt : data.UpdatedAt; Data.UpdatedAt = objectData.UpdatedAt != null ? objectData.UpdatedAt : Data.UpdatedAt;
// 先将本地的预估数据直接替换 // 先将本地的预估数据直接替换
data.CustomPropertyDict = estimatedData; Data.CustomPropertyDict = estimatedData;
// 再将服务端的数据覆盖 // 再将服务端的数据覆盖
foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) { foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) {
string key = kv.Key; string key = kv.Key;
object value = kv.Value; object value = kv.Value;
data.CustomPropertyDict[key] = value; Data.CustomPropertyDict[key] = value;
} }
// 最后重新生成预估数据,用于后续访问和操作 // 最后重新生成预估数据,用于后续访问和操作
@ -512,7 +514,7 @@ namespace LeanCloud.Storage {
void RebuildEstimatedData() { void RebuildEstimatedData() {
estimatedData = new Dictionary<string, object>(); estimatedData = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> kv in data.CustomPropertyDict) { foreach (KeyValuePair<string, object> kv in Data.CustomPropertyDict) {
string key = kv.Key; string key = kv.Key;
object value = kv.Value; object value = kv.Value;
if (value is IList list) { if (value is IList list) {