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  package com.linecorp.centraldogma.common;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.List;
22  import java.util.function.Function;
23  
24  import javax.annotation.Nullable;
25  
26  import com.fasterxml.jackson.databind.JsonNode;
27  
28  /**
29   * A query on a file.
30   *
31   * @param <T> the content type of a file being queried
32   */
33  public interface Query<T> extends Function<T, T> {
34  
35      /**
36       * Returns a newly-created {@link Query} that retrieves the content as it is.
37       *
38       * @param path the path of a file being queried on
39       *
40       * @deprecated Use {@link #ofText(String)} or {@link #ofJson(String)}.
41       */
42      @Deprecated
43      static Query<Object> identity(String path) {
44          return new IdentityQuery<>(path, QueryType.IDENTITY);
45      }
46  
47      /**
48       * Returns a newly-created {@link Query} that retrieves the textual content as it is.
49       *
50       * @param path the path of a file being queried on
51       */
52      static Query<String> ofText(String path) {
53          return new IdentityQuery<>(path, QueryType.IDENTITY_TEXT);
54      }
55  
56      /**
57       * Returns a newly-created {@link Query} that retrieves the JSON content as it is.
58       *
59       * @param path the path of a file being queried on
60       */
61      static Query<JsonNode> ofJson(String path) {
62          return new IdentityQuery<>(path, QueryType.IDENTITY_JSON);
63      }
64  
65      /**
66       * Returns a newly-created {@link Query} that applies a series of
67       * <a href="https://github.com/json-path/JsonPath/blob/master/README.md">JSON path expressions</a>
68       * to the content.
69       *
70       * @param path the path of a file being queried on
71       * @param jsonPaths the JSON path expressions to apply
72       */
73      static Query<JsonNode> ofJsonPath(String path, String... jsonPaths) {
74          return new JsonPathQuery(path, jsonPaths);
75      }
76  
77      /**
78       * Returns a newly-created {@link Query} that applies a series of
79       * <a href="https://github.com/json-path/JsonPath/blob/master/README.md">JSON path expressions</a>
80       * to the content.
81       *
82       * @param path the path of a file being queried on
83       * @param jsonPaths the JSON path expressions to apply
84       */
85      static Query<JsonNode> ofJsonPath(String path, Iterable<String> jsonPaths) {
86          return new JsonPathQuery(path, jsonPaths);
87      }
88  
89      /**
90       * Returns a newly-created {@link Query} that applies a series of expressions to the content.
91       *
92       * @param type the type of the {@link Query}
93       * @param path the path of a file being queried on
94       * @param expressions the expressions to apply
95       */
96      static Query<?> of(QueryType type, String path, @Nullable String... expressions) {
97          requireNonNull(type, "type");
98          switch (type) {
99              case IDENTITY:
100                 return new IdentityQuery<>(path, QueryType.IDENTITY);
101             case IDENTITY_TEXT:
102                 return new IdentityQuery<>(path, QueryType.IDENTITY_TEXT);
103             case IDENTITY_JSON:
104                 return new IdentityQuery<>(path, QueryType.IDENTITY_JSON);
105             case JSON_PATH:
106                 requireNonNull(expressions, "expressions");
107                 return ofJsonPath(path, expressions);
108             default:
109                 throw new IllegalArgumentException("Illegal query type: " + type.name());
110         }
111     }
112 
113     /**
114      * Returns the path of the file being queried on.
115      */
116     String path();
117 
118     /**
119      * Returns the type of this {@link Query}.
120      */
121     QueryType type();
122 
123     /**
124      * Returns the list of the query expressions of this {@link Query}.
125      */
126     List<String> expressions();
127 }