Merge pull request #72 from onerain88/chore

Chore
oneRain 2020-05-28 16:24:45 +08:00 committed by GitHub
commit 29392036e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 150 additions and 70 deletions

View File

@ -30,6 +30,9 @@
<Compile Include="..\Common\Json\LCJsonConverter.cs">
<Link>Json\LCJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Common\Exception\LCException.cs">
<Link>Exception\LCException.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">

View File

@ -13,5 +13,6 @@
<Folder Include="Log\" />
<Folder Include="Http\" />
<Folder Include="Task\" />
<Folder Include="Exception\" />
</ItemGroup>
</Project>

View File

@ -1,11 +1,20 @@
using System;
namespace LeanCloud.Storage {
namespace LeanCloud {
/// <summary>
/// LeanCloud 异常
/// </summary>
public class LCException : Exception {
/// <summary>
/// 错误码
/// </summary>
public int Code {
get; set;
}
/// <summary>
/// 错误信息
/// </summary>
public new string Message {
get; set;
}
@ -14,5 +23,9 @@ namespace LeanCloud.Storage {
Code = code;
Message = message;
}
public override string ToString() {
return $"{Code} - {Message}";
}
}
}

View File

@ -52,7 +52,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
attrs["name"] = name;
}
if (properties != null) {
attrs = properties.Union(attrs)
attrs = properties.Union(attrs.Where(kv => !properties.ContainsKey(kv.Key)))
.ToDictionary(k => k.Key, v => v.Value);
}
conv.Attr = new JsonObjectMessage {

View File

@ -15,9 +15,6 @@
<Compile Include="..\Storage\LCCloud.cs">
<Link>LCCloud.cs</Link>
</Compile>
<Compile Include="..\Storage\LCException.cs">
<Link>LCException.cs</Link>
</Compile>
<Compile Include="..\Storage\LCFile.cs">
<Link>LCFile.cs</Link>
</Compile>

View File

@ -1,5 +1,6 @@
using NUnit.Framework;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using LeanCloud;
using LeanCloud.Storage;
@ -74,5 +75,19 @@ namespace Storage.Test {
TestContext.WriteLine(account.ObjectId);
Assert.NotNull(account.ObjectId);
}
[Test]
public async Task Serialization() {
await LCUser.Login("hello", "world");
LCQuery<LCObject> query = new LCQuery<LCObject>("Account") {
IncludeACL = true
};
query.OrderByDescending("createdAt");
ReadOnlyCollection<LCObject> accounts = await query.Find();
foreach (LCObject account in accounts) {
TestContext.WriteLine($"public read access: {account.ACL.PublicReadAccess}");
TestContext.WriteLine($"public write access: {account.ACL.PublicWriteAccess}");
}
}
}
}

View File

@ -1,5 +1,5 @@
using NUnit.Framework;
using LeanCloud.Storage;
using LeanCloud;
namespace Storage.Test {
public class ExceptionTest {

View File

@ -68,10 +68,25 @@ namespace LeanCloud.Storage.Internal.Codec {
}
public static LCGeoPoint DecodeGeoPoint(IDictionary data) {
double latitude = double.Parse(data["latitude"].ToString());
double longitude = double.Parse(data["longitude"].ToString());
double latitude = Convert.ToDouble(data["latitude"]);
double longitude = Convert.ToDouble(data["longitude"]);
LCGeoPoint geoPoint = new LCGeoPoint(latitude, longitude);
return geoPoint;
}
public static LCACL DecodeACL(Dictionary<string, object> dict) {
LCACL acl = new LCACL();
foreach (KeyValuePair<string, object> kv in dict) {
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);
}
if (access.TryGetValue("write", out object wa)) {
acl.writeAccess[key] = Convert.ToBoolean(wa);
}
}
return acl;
}
}
}

View File

@ -83,18 +83,25 @@ namespace LeanCloud.Storage.Internal.Codec {
}
public static object EncodeACL(LCACL acl) {
HashSet<string> readers = acl.readers;
HashSet<string> writers = acl.writers;
HashSet<string> union = new HashSet<string>(readers);
union.UnionWith(writers);
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (string k in union) {
dict[k] = new Dictionary<string, object> {
{ "read", readers.Contains(k) },
{ "write", writers.Contains(k) }
};
HashSet<string> keys = new HashSet<string>();
if (acl.readAccess.Count > 0) {
keys.UnionWith(acl.readAccess.Keys);
}
return dict;
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)) {
access["read"] = ra;
}
if (acl.writeAccess.TryGetValue(key, out bool wa)) {
access["write"] = wa;
}
result[key] = access;
}
return result;
}
public static object EncodeRelation(LCRelation<LCObject> relation) {

View File

@ -40,11 +40,16 @@ namespace LeanCloud.Storage.Internal.Object {
} else if (key == "objectId") {
objectData.ObjectId = value.ToString();
} else if (key == "createdAt" && DateTime.TryParse(value.ToString(), out DateTime createdAt)) {
objectData.CreatedAt = createdAt;
objectData.CreatedAt = createdAt.ToLocalTime();
} else if (key == "updatedAt" && DateTime.TryParse(value.ToString(), out DateTime updatedAt)) {
objectData.UpdatedAt = updatedAt;
objectData.UpdatedAt = updatedAt.ToLocalTime();
} else {
objectData.CustomPropertyDict[key] = LCDecoder.Decode(value);
if (key == "ACL" &&
value is Dictionary<string, object> dic) {
objectData.CustomPropertyDict[key] = LCDecoder.DecodeACL(dic);
} else {
objectData.CustomPropertyDict[key] = LCDecoder.Decode(value);
}
}
}
return objectData;
@ -61,10 +66,10 @@ namespace LeanCloud.Storage.Internal.Object {
dict["objectId"] = objectData.ObjectId;
}
if (objectData.CreatedAt != null) {
dict["createdAt"] = objectData.CreatedAt;
dict["createdAt"] = objectData.CreatedAt.ToUniversalTime();
}
if (objectData.UpdatedAt != null) {
dict["updatedAt"] = objectData.UpdatedAt;
dict["updatedAt"] = objectData.UpdatedAt.ToUniversalTime();
}
if (objectData.CustomPropertyDict != null) {
foreach (KeyValuePair<string, object> kv in objectData.CustomPropertyDict) {

View File

@ -24,6 +24,10 @@ namespace LeanCloud.Storage.Internal.Query {
get; set;
}
public bool IncludeACL {
get; set;
}
public LCCompositionalCondition(string composition = And) {
this.composition = composition;
Skip = 0;
@ -217,6 +221,9 @@ namespace LeanCloud.Storage.Internal.Query {
if (selectedKeys != null && selectedKeys.Count > 0) {
dict["keys"] = string.Join(",", selectedKeys);
}
if (IncludeACL) {
dict["returnACL"] = "true";
}
return dict;
}

View File

@ -10,22 +10,32 @@ namespace LeanCloud.Storage {
const string RoleKeyPrefix = "role:";
internal HashSet<string> readers;
internal HashSet<string> writers;
internal Dictionary<string, bool> readAccess = new Dictionary<string, bool>();
internal Dictionary<string, bool> writeAccess = new Dictionary<string, bool>();
public static LCACL CreateWithOwner(LCUser owner) {
if (owner == null) {
throw new ArgumentNullException(nameof(owner));
}
LCACL acl = new LCACL();
acl.SetUserReadAccess(owner, true);
acl.SetUserWriteAccess(owner, true);
return acl;
}
public bool PublicReadAccess {
get {
return GetAccess(readers, PublicKey);
return GetAccess(readAccess, PublicKey);
} set {
SetAccess(readers, PublicKey, value);
SetAccess(readAccess, PublicKey, value);
}
}
public bool PublicWriteAccess {
get {
return GetAccess(writers, PublicKey);
return GetAccess(writeAccess, PublicKey);
} set {
SetAccess(writers, PublicKey, value);
SetAccess(writeAccess, PublicKey, value);
}
}
@ -33,28 +43,28 @@ namespace LeanCloud.Storage {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
}
return GetAccess(readers, userId);
return GetAccess(readAccess, userId);
}
public void SetUserIdReadAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
}
SetAccess(readers, userId, value);
SetAccess(readAccess, userId, value);
}
public bool GetUserIdWriteAccess(string userId) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
}
return GetAccess(writers, userId);
return GetAccess(writeAccess, userId);
}
public void SetUserIdWriteAccess(string userId, bool value) {
if (string.IsNullOrEmpty(userId)) {
throw new ArgumentNullException(nameof(userId));
}
SetAccess(writers, userId, value);
SetAccess(writeAccess, userId, value);
}
public bool GetUserReadAccess(LCUser user) {
@ -90,7 +100,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role));
}
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
return GetAccess(readers, roleKey);
return GetAccess(readAccess, roleKey);
}
public void SetRoleReadAccess(LCRole role, bool value) {
@ -98,7 +108,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role));
}
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
SetAccess(readers, roleKey, value);
SetAccess(readAccess, roleKey, value);
}
public bool GetRoleWriteAccess(LCRole role) {
@ -106,7 +116,7 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role));
}
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
return GetAccess(writers, roleKey);
return GetAccess(writeAccess, roleKey);
}
public void SetRoleWriteAccess(LCRole role, bool value) {
@ -114,34 +124,16 @@ namespace LeanCloud.Storage {
throw new ArgumentNullException(nameof(role));
}
string roleKey = $"{RoleKeyPrefix}{role.ObjectId}";
SetAccess(writers, roleKey, value);
SetAccess(writeAccess, roleKey, value);
}
public LCACL() {
readers = new HashSet<string>();
writers = new HashSet<string>();
bool GetAccess(Dictionary<string, bool> access, string key) {
return access.ContainsKey(key) ?
access[key] : false;
}
public static LCACL CreateWithOwner(LCUser owner) {
if (owner == null) {
throw new ArgumentNullException(nameof(owner));
}
LCACL acl = new LCACL();
acl.SetUserReadAccess(owner, true);
acl.SetUserWriteAccess(owner, true);
return acl;
}
bool GetAccess(HashSet<string> set, string key) {
return set.Contains(key);
}
void SetAccess(HashSet<string> set, string key, bool value) {
if (value) {
set.Add(key);
} else {
set.Remove(key);
}
void SetAccess(Dictionary<string, bool> access, string key, bool value) {
access[key] = value;
}
}
}

View File

@ -8,20 +8,31 @@ namespace LeanCloud.Storage {
/// </summary>
public static class LCCloud {
/// <summary>
/// 调用云函数,结果为 Dictionary 类型
/// 调用云函数
/// </summary>
/// <param name="name"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static async Task<Dictionary<string, object>> Run(string name, Dictionary<string, object> parameters = null) {
/// <returns>返回类型为 Dictionary<string, object></returns>
public static async Task<Dictionary<string, object>> Run(string name,
Dictionary<string, object> parameters = null) {
string path = $"functions/{name}";
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: parameters);
object encodeParams = LCEncoder.Encode(parameters);
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path,
data: encodeParams);
return response;
}
public static async Task<object> RPC(string name, Dictionary<string, object> parameters = null) {
/// <summary>
/// 调用 RPC 云函数
/// </summary>
/// <param name="name"></param>
/// <param name="parameters"></param>
/// <returns>返回类型为 LCObject 容器类型</returns>
public static async Task<object> RPC(string name, object parameters = null) {
string path = $"call/{name}";
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: parameters);
object encodeParams = LCEncoder.Encode(parameters);
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path,
data: encodeParams);
return LCDecoder.Decode(response["result"]);
}
}

View File

@ -462,7 +462,10 @@ namespace LeanCloud.Storage {
/// </summary>
/// <returns></returns>
public override string ToString() {
return JsonConvert.SerializeObject(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);
}
/// <summary>

View File

@ -333,6 +333,17 @@ namespace LeanCloud.Storage {
return this;
}
/// <summary>
/// 是否包含 ACL
/// </summary>
public bool IncludeACL {
get {
return Condition.IncludeACL;
} set {
Condition.IncludeACL = value;
}
}
/// <summary>
/// 跳过
/// </summary>

View File

@ -49,13 +49,13 @@ namespace LeanCloud.Storage {
public bool EmailVerified {
get {
return (bool)this["emailVerified"];
return Convert.ToBoolean(this["emailVerified"]);
}
}
public bool MobileVerified {
get {
return (bool)this["mobilePhoneVerified"];
return Convert.ToBoolean(this["mobilePhoneVerified"]);
}
}