示例#1
0
        private PatchResult <JObject> TryPrivatePatch(JObject JSON,
                                                      JObject Patch)
        {
            foreach (var property in Patch)
            {
                if (property.Key == "id")
                {
                    return(PatchResult <JObject> .Failed(JSON,
                                                         "Patching the 'identification' of a connector is not allowed!"));
                }

                else if (property.Value is null)
                {
                    JSON.Remove(property.Key);
                }

                else if (property.Value is JObject subObject)
                {
                    if (JSON.ContainsKey(property.Key))
                    {
                        if (JSON[property.Key] is JObject oldSubObject)
                        {
                            //ToDo: Perhaps use a more generic JSON patch here!
                            // PatchObject.Apply(ToJSON(), EVSEPatch),
                            var patchResult = TryPrivatePatch(oldSubObject, subObject);

                            if (patchResult.IsSuccess)
                            {
                                JSON[property.Key] = patchResult.PatchedData;
                            }
                        }

                        else
                        {
                            JSON[property.Key] = subObject;
                        }
                    }

                    else
                    {
                        JSON.Add(property.Key, subObject);
                    }
                }

                //else if (property.Value is JArray subArray)
                //{
                //}

                else
                {
                    JSON[property.Key] = property.Value;
                }
            }

            return(PatchResult <JObject> .Success(JSON));
        }
示例#2
0
        /// <summary>
        /// Try to patch the JSON representaion of this connector.
        /// </summary>
        /// <param name="ConnectorPatch">The JSON merge patch.</param>
        /// <param name="AllowDowngrades">Allow to set the 'lastUpdated' timestamp to an earlier value.</param>
        public PatchResult <Connector> TryPatch(JObject ConnectorPatch,
                                                Boolean AllowDowngrades = false)
        {
            if (ConnectorPatch == null)
            {
                return(PatchResult <Connector> .Failed(this,
                                                       "The given connector patch must not be null!"));
            }

            lock (patchLock)
            {
                if (ConnectorPatch["last_updated"] is null)
                {
                    ConnectorPatch["last_updated"] = DateTime.UtcNow.ToIso8601();
                }

                else if (AllowDowngrades == false &&
                         ConnectorPatch["last_updated"].Type == JTokenType.Date &&
                         (ConnectorPatch["last_updated"].Value <DateTime>().ToIso8601().CompareTo(LastUpdated.ToIso8601()) < 1))
                {
                    return(PatchResult <Connector> .Failed(this,
                                                           "The 'lastUpdated' timestamp of the connector patch must be newer then the timestamp of the existing connector!"));
                }


                var patchResult = TryPrivatePatch(ToJSON(), ConnectorPatch);


                if (patchResult.IsFailed)
                {
                    return(PatchResult <Connector> .Failed(this,
                                                           patchResult.ErrorResponse));
                }

                if (TryParse(patchResult.PatchedData,
                             out Connector PatchedConnector,
                             out String ErrorResponse))
                {
                    return(PatchResult <Connector> .Success(PatchedConnector,
                                                            ErrorResponse));
                }