1   /*
2    * Copyright 2021 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  package com.linecorp.centraldogma.client;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.concurrent.CompletableFuture;
21  
22  import com.fasterxml.jackson.databind.node.ObjectNode;
23  
24  import com.linecorp.centraldogma.common.MergeQuery;
25  import com.linecorp.centraldogma.common.MergedEntry;
26  import com.linecorp.centraldogma.common.Revision;
27  
28  /**
29   * Prepares to send a {@link CentralDogma#mergeFiles(String, String, Revision, MergeQuery)} request to the
30   * Central Dogma repository.
31   */
32  public final class MergeRequest<T> {
33  
34      private final CentralDogmaRepository centralDogmaRepo;
35      private final MergeQuery<T> mergeQuery;
36  
37      MergeRequest(CentralDogmaRepository centralDogmaRepo,
38                   MergeQuery<T> mergeQuery) {
39          this.centralDogmaRepo = centralDogmaRepo;
40          this.mergeQuery = mergeQuery;
41      }
42  
43      /**
44       * Retrieves the merged entry of the {@link MergeQuery} at the {@link Revision#HEAD}.
45       * Only JSON entry merge is currently supported. The JSON files are merged sequentially as specified in
46       * the {@link MergeQuery}.
47       *
48       * <p>Note that only {@link ObjectNode} is recursively merged traversing the children. Other node types are
49       * simply replaced.
50       *
51       * @return the {@link MergedEntry} which contains the result of the merge
52       */
53      public CompletableFuture<MergedEntry<T>> get() {
54          return get(Revision.HEAD);
55      }
56  
57      /**
58       * Retrieves the merged entry of the {@link MergeQuery} at the {@link Revision}.
59       * Only JSON entry merge is currently supported. The JSON files are merged sequentially as specified in
60       * the {@link MergeQuery}.
61       *
62       * <p>Note that only {@link ObjectNode} is recursively merged traversing the children. Other node types are
63       * simply replaced.
64       *
65       * @return the {@link MergedEntry} which contains the result of the merge
66       */
67      public CompletableFuture<MergedEntry<T>> get(Revision revision) {
68          requireNonNull(revision, "revision");
69          return centralDogmaRepo.centralDogma().mergeFiles(centralDogmaRepo.projectName(),
70                                                            centralDogmaRepo.repositoryName(),
71                                                            revision, mergeQuery);
72      }
73  }