Beispiel #1
0
 /// <summary>
 /// Applies the Remove patch operation to the target
 /// </summary>
 /// <param name="target">The object to apply the operation to.</param>
 public override void Apply(TModel target)
 {
     this.Apply(
         target,
         (type, parent, current) =>
     {
         if (type.IsList())
         {
             var list = (IList)parent;
             int index;
             if (int.TryParse(current, out index) &&
                 list.Count > index)
             {
                 list.RemoveAt(index);
             }
             else
             {
                 // We can't remove outside the rangeof the list.
                 throw new PatchOperationFailedException(PatchResources.IndexOutOfRange(this.Path));
             }
         }
         else if (type.IsDictionary())
         {
             ((IDictionary)parent).Remove(current);
         }
         else
         {
             type.SetMemberValue(current, parent, this.Value);
         }
     });
 }
Beispiel #2
0
 /// <summary>
 /// Applies the Add patch operation to the target
 /// </summary>
 /// <param name="target">The object to apply the operation to.</param>
 public override void Apply(TModel target)
 {
     this.Apply(
         target,
         (type, parent, current) =>
     {
         // Empty current means replace the whole object.
         if (string.IsNullOrEmpty(current))
         {
             parent = this.Value;
         }
         else if (type.IsList())
         {
             var list = (IList)parent;
             if (current == EndOfIndex)
             {
                 list.Add(this.Value);
             }
             else
             {
                 int index;
                 // When index == list.Count it's the same
                 // as doing an index append to the end.
                 if (int.TryParse(current, out index) &&
                     list.Count >= index)
                 {
                     list.Insert(index, this.Value);
                 }
                 else
                 {
                     // We can't insert beyond the length of the list.
                     throw new PatchOperationFailedException(PatchResources.IndexOutOfRange(this.Path));
                 }
             }
         }
         else if (type.IsDictionary())
         {
             ((IDictionary)parent)[current] = this.Value;
         }
         else
         {
             type.SetMemberValue(current, parent, this.Value);
         }
     });
 }
        /// <summary>
        /// Applies the Test patch operation to the target
        /// </summary>
        /// <param name="target">The object to apply the operation to.</param>
        public override void Apply(TModel target)
        {
            this.Apply(
                target,
                (type, parent, current) =>
            {
                object memberValue = null;
                if (type.IsList())
                {
                    var list = (IList)parent;
                    int index;
                    if (int.TryParse(current, out index) &&
                        list.Count > index)
                    {
                        memberValue = list[index];
                    }
                    else
                    {
                        // We can't insert beyond the length of the list.
                        throw new PatchOperationFailedException(PatchResources.IndexOutOfRange(this.Path));
                    }
                }
                else if (type.IsDictionary())
                {
                    var fieldDictionary = ((IDictionary)parent);

                    if (!fieldDictionary.Contains(current))
                    {
                        throw new InvalidPatchFieldNameException(PatchResources.InvalidFieldName(current));
                    }
                    memberValue = fieldDictionary[current];
                }
                else
                {
                    memberValue = type.GetMemberValue(current, parent);
                }

                var success = false;
                if (memberValue != null)
                {
                    if (memberValue is IList)
                    {
                        // TODO: Implement
                        throw new PatchOperationFailedException(PatchResources.TestNotImplementedForList());
                    }
                    else if (memberValue is IDictionary)
                    {
                        // TODO: Implement
                        throw new PatchOperationFailedException(PatchResources.TestNotImplementedForDictionary());
                    }
                    else if (memberValue.GetType().IsAssignableOrConvertibleFrom(this.Value))
                    {
                        // We convert the objects since we need the values unboxed.
                        var convertedMemberValue = ConvertUtility.ChangeType(memberValue, memberValue.GetType());
                        var convertedValue       = ConvertUtility.ChangeType(this.Value, memberValue.GetType());

                        success = convertedMemberValue.Equals(convertedValue);
                    }
                    else
                    {
                        success = memberValue.Equals(this.Value);
                    }
                }
                else
                {
                    success = object.Equals(memberValue, this.Value);
                }

                if (!success)
                {
                    throw new TestPatchOperationFailedException(PatchResources.TestFailed(this.Path, memberValue, this.Value));
                }
            });
        }