From e5b9f2957541cba5f0a9960be92f023cba08a22e Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 18 Mar 2021 11:25:01 +0800 Subject: [PATCH] chore: for leanengine --- Storage/Storage/Internal/Codec/LCDecoder.cs | 2 +- .../Storage/Internal/Object/LCObjectData.cs | 4 +-- Storage/Storage/LCApplication.cs | 2 +- Storage/Storage/LCCloud.cs | 30 ++++++++++++++++++- Storage/Storage/LCObject.cs | 6 +++- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Storage/Storage/Internal/Codec/LCDecoder.cs b/Storage/Storage/Internal/Codec/LCDecoder.cs index 4d7a9ee..ec351ee 100644 --- a/Storage/Storage/Internal/Codec/LCDecoder.cs +++ b/Storage/Storage/Internal/Codec/LCDecoder.cs @@ -15,7 +15,7 @@ namespace LeanCloud.Storage.Internal.Codec { return DecodeBytes(dict); } else if (type == "Object") { return DecodeObject(dict); - } else if (type == "Pointer") { + } else if (type == "Pointer" || type == "Object") { return DecodeObject(dict); } else if (type == "Relation") { return DecodeRelation(dict); diff --git a/Storage/Storage/Internal/Object/LCObjectData.cs b/Storage/Storage/Internal/Object/LCObjectData.cs index bc5e90d..32ea0dd 100644 --- a/Storage/Storage/Internal/Object/LCObjectData.cs +++ b/Storage/Storage/Internal/Object/LCObjectData.cs @@ -65,10 +65,10 @@ namespace LeanCloud.Storage.Internal.Object { if (!string.IsNullOrEmpty(objectData.ObjectId)) { dict["objectId"] = objectData.ObjectId; } - if (objectData.CreatedAt != null) { + if (!objectData.CreatedAt.Equals(default)) { dict["createdAt"] = objectData.CreatedAt.ToUniversalTime(); } - if (objectData.UpdatedAt != null) { + if (!objectData.UpdatedAt.Equals(default)) { dict["updatedAt"] = objectData.UpdatedAt.ToUniversalTime(); } if (objectData.CustomPropertyDict != null) { diff --git a/Storage/Storage/LCApplication.cs b/Storage/Storage/LCApplication.cs index 2a297e9..593c346 100644 --- a/Storage/Storage/LCApplication.cs +++ b/Storage/Storage/LCApplication.cs @@ -9,7 +9,7 @@ namespace LeanCloud { /// public class LCApplication { // SDK 版本号,用于 User-Agent 统计 - internal const string SDKVersion = "0.6.4"; + public const string SDKVersion = "0.6.4"; // 接口版本号,用于接口版本管理 internal const string APIVersion = "1.1"; diff --git a/Storage/Storage/LCCloud.cs b/Storage/Storage/LCCloud.cs index a1cbfd1..830d3ca 100644 --- a/Storage/Storage/LCCloud.cs +++ b/Storage/Storage/LCCloud.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using System.Collections.Generic; using LeanCloud.Storage.Internal.Codec; +using LeanCloud.Storage.Internal.Object; namespace LeanCloud.Storage { /// @@ -46,11 +47,38 @@ namespace LeanCloud.Storage { Dictionary headers = new Dictionary { { PRODUCTION_KEY, IsProduction ? 1 : 0 } }; - object encodeParams = LCEncoder.Encode(parameters); + object encodeParams = Encode(parameters); Dictionary response = await LCApplication.HttpClient.Post>(path, headers: headers, data: encodeParams); return LCDecoder.Decode(response["result"]); } + + public static object Encode(object parameters) { + if (parameters is LCObject lcObj) { + return EncodeLCObject(lcObj); + } else if (parameters is IList list) { + List l = new List(); + foreach (LCObject obj in list) { + l.Add(EncodeLCObject(obj)); + } + return l; + } else if (parameters is IDictionary dict) { + Dictionary d = new Dictionary(); + foreach (KeyValuePair item in dict) { + d[item.Key] = EncodeLCObject(item.Value); + } + return d; + } + + return parameters; + } + + static object EncodeLCObject(LCObject obj) { + obj.ApplyCustomProperties(); + Dictionary dict = LCObjectData.Encode(obj.Data); + dict["__type"] = "Object"; + return dict; + } } } diff --git a/Storage/Storage/LCObject.cs b/Storage/Storage/LCObject.cs index 739fca2..7a43d8e 100644 --- a/Storage/Storage/LCObject.cs +++ b/Storage/Storage/LCObject.cs @@ -497,7 +497,7 @@ namespace LeanCloud.Storage { Data.CreatedAt = objectData.CreatedAt != null ? objectData.CreatedAt : Data.CreatedAt; Data.UpdatedAt = objectData.UpdatedAt != null ? objectData.UpdatedAt : Data.UpdatedAt; // 先将本地的预估数据直接替换 - Data.CustomPropertyDict = estimatedData; + ApplyCustomProperties(); // 再将服务端的数据覆盖 foreach (KeyValuePair kv in objectData.CustomPropertyDict) { string key = kv.Key; @@ -512,6 +512,10 @@ namespace LeanCloud.Storage { isNew = false; } + public void ApplyCustomProperties() { + Data.CustomPropertyDict = estimatedData; + } + void RebuildEstimatedData() { estimatedData = new Dictionary(); foreach (KeyValuePair kv in Data.CustomPropertyDict) {