1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  /*
17   * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
18   *
19   * This software is dual-licensed under:
20   *
21   * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
22   *   later version;
23   * - the Apache Software License (ASL) version 2.0.
24   *
25   * The text of this file and of both licenses is available at the root of this
26   * project or, if you have the jar distribution, in directory META-INF/, under
27   * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
28   *
29   * Direct link to the sources:
30   *
31   * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
32   * - ASL 2.0: https://www.apache.org/licenses/LICENSE-2.0.txt
33   */
34  
35  package com.linecorp.centraldogma.internal.jsonpatch;
36  
37  import com.fasterxml.jackson.annotation.JsonCreator;
38  import com.fasterxml.jackson.annotation.JsonProperty;
39  import com.fasterxml.jackson.core.JsonPointer;
40  import com.fasterxml.jackson.databind.JsonNode;
41  import com.fasterxml.jackson.databind.node.ArrayNode;
42  import com.fasterxml.jackson.databind.node.ObjectNode;
43  
44  /**
45   * JSON Patch {@code move} operation.
46   *
47   * <p>For this operation, {@code from} points to the value to move, and {@code
48   * path} points to the new location of the moved value.</p>
49   *
50   * <p>It is an error condition if {@code from} does not point to a JSON value.
51   * </p>
52   *
53   * <p>The specification adds another rule that the {@code from} path must not be
54   * an immediate parent of {@code path}. Unfortunately, that doesn't really work.
55   * Consider this patch:</p>
56   *
57   * <pre>
58   *     { "op": "move", "from": "/0", "path": "/0/x" }
59   * </pre>
60   *
61   * <p>Even though {@code /0} is an immediate parent of {@code /0/x}, when this
62   * patch is applied to:</p>
63   *
64   * <pre>
65   *     [ "victim", {} ]
66   * </pre>
67   *
68   * <p>it actually succeeds and results in the patched value:</p>
69   *
70   * <pre>
71   *     [ { "x": "victim" } ]
72   * </pre>
73   */
74  public final class MoveOperation extends DualPathOperation {
75  
76      @JsonCreator
77      MoveOperation(@JsonProperty("from") final JsonPointer from,
78                    @JsonProperty("path") final JsonPointer path) {
79          super("move", from, path);
80      }
81  
82      @Override
83      JsonNode apply(final JsonNode node) {
84          if (from.equals(path)) {
85              return node;
86          }
87          if (node.at(from).isMissingNode()) {
88              throw new JsonPatchException("non-existent source path: " + from);
89          }
90  
91          final JsonNode sourceParent = ensureSourceParent(node, from);
92  
93          // Remove
94          final String raw = from.last().getMatchingProperty();
95          final JsonNode source;
96          if (sourceParent.isObject()) {
97              source = ((ObjectNode) sourceParent).remove(raw);
98          } else {
99              source = ((ArrayNode) sourceParent).remove(Integer.parseInt(raw));
100         }
101 
102         // Add
103         if (path.toString().isEmpty()) {
104             return source;
105         }
106 
107         final JsonNode targetParent = ensureTargetParent(node, path);
108         return targetParent.isArray() ? AddOperation.addToArray(path, node, source)
109                                       : AddOperation.addToObject(path, node, source);
110     }
111 }