1   /*
2    * Copyright 2018 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  package com.linecorp.centraldogma.internal.jsonpatch;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import com.fasterxml.jackson.core.JsonGenerator;
24  import com.fasterxml.jackson.core.JsonPointer;
25  import com.fasterxml.jackson.databind.JsonNode;
26  import com.fasterxml.jackson.databind.SerializerProvider;
27  import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
28  import com.fasterxml.jackson.databind.node.ArrayNode;
29  import com.fasterxml.jackson.databind.node.MissingNode;
30  import com.fasterxml.jackson.databind.node.ObjectNode;
31  
32  /**
33   * JSON Path {@code remove} operation.
34   *
35   * <p>This operation only takes one pointer ({@code path}) as an argument. It
36   * is an error condition if no JSON value exists at that pointer.</p>
37   */
38  public final class RemoveIfExistsOperation extends JsonPatchOperation {
39  
40      @JsonCreator
41      public RemoveIfExistsOperation(@JsonProperty("path") final JsonPointer path) {
42          super("removeIfExists", path);
43      }
44  
45      @Override
46      JsonNode apply(final JsonNode node) {
47          if (path.toString().isEmpty()) {
48              return MissingNode.getInstance();
49          }
50  
51          final JsonNode found = node.at(path);
52          if (found.isMissingNode()) {
53              return node;
54          }
55  
56          final JsonNode parentNode = node.at(path.head());
57          final String raw = path.last().getMatchingProperty();
58          if (parentNode.isObject()) {
59              ((ObjectNode) parentNode).remove(raw);
60          } else if (parentNode.isArray()) {
61              ((ArrayNode) parentNode).remove(Integer.parseInt(raw));
62          }
63          return node;
64      }
65  
66      @Override
67      public void serialize(final JsonGenerator jgen,
68                            final SerializerProvider provider) throws IOException {
69          jgen.writeStartObject();
70          jgen.writeStringField("op", "removeIfExists");
71          jgen.writeStringField("path", path.toString());
72          jgen.writeEndObject();
73      }
74  
75      @Override
76      public void serializeWithType(final JsonGenerator jgen,
77                                    final SerializerProvider provider, final TypeSerializer typeSer)
78              throws IOException {
79          serialize(jgen, provider);
80      }
81  
82      @Override
83      public String toString() {
84          return "op: " + op + "; path: \"" + path + '"';
85      }
86  }