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;
Dictionary<string, object> access = kv.Value as Dictionary<string, object>;
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)) {
acl.writeAccess[key] = Convert.ToBoolean(wa);
acl.WriteAccess[key] = Convert.ToBoolean(wa);
}
}
return acl;

View File

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

View File

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

View File

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

View File

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