Exemplo n.º 1
0
        public void SetValueFromPath(Type entityType, PathComponent[] pathComponents, object entity, object value, JsonPatchOperationType operationType)
        {
            try
            {
                PathComponent[] parent     = pathComponents.Take(pathComponents.Length - 1).ToArray();
                string          parentPath = PathComponent.GetFullPath(parent);

                object previous = GetValueFromPath(entityType, parent, entity);

                if (previous == null)
                {
                    throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is null.", parentPath));
                }

                var target = pathComponents.Last();

                TypeSwitch.On(target)
                .Case((PropertyPathComponent component) =>
                {
                    switch (operationType)
                    {
                    case JsonPatchOperationType.add:
                    case JsonPatchOperationType.replace:
                        component.GetPropertyInfo(previous).SetValue(previous, ConvertValue(value, component.ComponentType));
                        break;

                    case JsonPatchOperationType.remove:
                        component.GetPropertyInfo(previous).SetValue(previous, null);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("operationType");
                    }
                })
                .Case((CollectionPathComponent component) =>
                {
                    var list = previous as IList;
                    if (list == null)
                    {
                        throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is not a valid collection.", parentPath));
                    }

                    switch (operationType)
                    {
                    case JsonPatchOperationType.add:
                        list.Add(ConvertValue(value, component.ComponentType));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("operationType");
                    }
                })
                .Case((CollectionPathComponent component) =>
                {
                    var list = previous as IList;
                    if (list == null)
                    {
                        throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is not a valid collection.", parentPath));
                    }

                    switch (operationType)
                    {
                    case JsonPatchOperationType.add:
                        list.Add(ConvertValue(value, component.ComponentType));
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("operationType");
                    }
                })
                .Case((CollectionIndexPathComponent component) =>
                {
                    var list = previous as IList;
                    if (list == null)
                    {
                        throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is not a valid collection.", parentPath));
                    }

                    switch (operationType)
                    {
                    case JsonPatchOperationType.add:
                        list.Insert(component.CollectionIndex, ConvertValue(value, component.ComponentType));
                        break;

                    case JsonPatchOperationType.remove:
                        list.RemoveAt(component.CollectionIndex);
                        break;

                    case JsonPatchOperationType.replace:
                        list[component.CollectionIndex] = ConvertValue(value, component.ComponentType);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("operationType");
                    }
                });
            }
            catch (Exception e)
            {
                throw new JsonPatchException(string.Format(
                                                 "Failed to set value at path \"{0}\" while performing \"{1}\" operation: {2}",
                                                 PathComponent.GetFullPath(pathComponents), operationType, e.Message), e);
            }
        }
Exemplo n.º 2
0
 public static void SetValueFromPath(Type entityType, PathComponent[] pathComponents, object entity, object value, JsonPatchOperationType operationType)
 {
     PathResolver.SetValueFromPath(entityType, pathComponents, entity, value, operationType);
 }
Exemplo n.º 3
0
        public static void SetValueFromPath(Type entityType, PathComponent[] pathComponents, object entity, object value, JsonPatchOperationType operationType)
        {
            try
            {
                PathComponent[] parent = pathComponents.Take(pathComponents.Length - 1).ToArray();
                string parentPath = PathComponent.GetFullPath(parent);

                object previous = GetValueFromPath(entityType, parent, entity);

                if (previous == null)
                {
                    throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is null.", parentPath));
                }

                var target = pathComponents.Last();

                TypeSwitch.On(target)
                    .Case((PropertyPathComponent component) =>
                    {
                        switch (operationType)
                        {
                            case JsonPatchOperationType.add:
                            case JsonPatchOperationType.replace:
                                component.PropertyInfo.SetValue(previous, ConvertValue(value, component.ComponentType));
                                break;
                            case JsonPatchOperationType.remove:
                                component.PropertyInfo.SetValue(previous, null);
                                break;
                            default:
                                throw new ArgumentOutOfRangeException("operationType");
                        }
                    })
                    .Case((CollectionIndexPathComponent component) =>
                    {
                        var list = previous as IList;
                        if (list == null)
                        {
                            throw new JsonPatchException(string.Format("Value at parent path \"{0}\" is not a valid collection.", parentPath));
                        }

                        switch (operationType)
                        {
                            case JsonPatchOperationType.add:
                                list.Insert(component.CollectionIndex, ConvertValue(value, component.ComponentType));
                                break;
                            case JsonPatchOperationType.remove:
                                list.RemoveAt(component.CollectionIndex);
                                break;
                            case JsonPatchOperationType.replace:
                                list[component.CollectionIndex] = ConvertValue(value, component.ComponentType);
                                break;
                            default:
                                throw new ArgumentOutOfRangeException("operationType");
                        }
                    });
            }
            catch (Exception e)
            {
                throw new JsonPatchException(string.Format(
                    "Failed to set value at path \"{0}\" while performing \"{1}\" operation: {2}",
                    PathComponent.GetFullPath(pathComponents), operationType, e.Message), e);
            }
        }
Exemplo n.º 4
0
 public static void SetValueFromPath(Type entityType, string path, object entity, object value, JsonPatchOperationType operationType)
 {
     SetValueFromPath(entityType, ParsePath(path, entityType), entity, value, operationType);
 }
Exemplo n.º 5
0
 public static void SetValueFromPath(Type entityType, string path, object entity, object value, JsonPatchOperationType operationType)
 {
     SetValueFromPath(entityType, ParsePath(path, entityType), entity, value, operationType);
 }