@Override protected com.linecorp.centraldogma.common.MergeQuery<?> doBackward(MergeQuery mergeQuery) { switch (mergeQuery.type) { case IDENTITY: return com.linecorp.centraldogma.common.MergeQuery.ofJson(convertMergeSources(mergeQuery)); case JSON_PATH: return com.linecorp.centraldogma.common.MergeQuery.ofJsonPath(convertMergeSources(mergeQuery), mergeQuery.expressions); } throw new Error(); }
@Override int weigh(MergedEntry<?> value) { int weight = 0; final List<MergeSource> mergeSources = query.mergeSources(); weight += mergeSources.size(); for (MergeSource mergeSource : mergeSources) { weight += mergeSource.path().length(); } final List<String> expressions = query.expressions(); weight += expressions.size(); for (String expression : expressions) { weight += expression.length(); } if (value != null) { weight += value.contentAsText().length(); } return weight; }
@Override protected MergeQuery doForward(com.linecorp.centraldogma.common.MergeQuery<?> mergeQuery) { switch (mergeQuery.type()) { case IDENTITY: return new MergeQuery(QueryType.IDENTITY, convertMergeSources(mergeQuery), ImmutableList.of()); case JSON_PATH: return new MergeQuery(QueryType.JSON_PATH, convertMergeSources(mergeQuery), mergeQuery.expressions()); } throw new Error(); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. * * @param mergeSources the paths of JSON files being merged and indicates whether it is optional */ static MergeQuery<JsonNode> ofJson(MergeSource... mergeSources) { return ofJson(ImmutableList.copyOf(requireNonNull(mergeSources, "mergeSources"))); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. Then, the specified * <a href="https://github.com/json-path/JsonPath/blob/master/README.md">JSON path expressions</a> * are applied to the content of the {@link MergedEntry}. * * @param mergeSources the paths of JSON files being merged and indicates whether it is optional * @param jsonPaths the JSON path expressions to apply */ static MergeQuery<JsonNode> ofJsonPath(Iterable<MergeSource> mergeSources, String... jsonPaths) { return ofJsonPath(mergeSources, ImmutableList.copyOf(requireNonNull(jsonPaths, "jsonPaths"))); }
CacheableMergeQueryCall(Repository repo, Revision revision, MergeQuery<?> query) { super(repo); this.revision = requireNonNull(revision, "revision"); this.query = requireNonNull(query, "query"); // Only JSON files can currently be merged. query.mergeSources().forEach(path -> validateJsonFilePath(path.path(), "path")); hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo); assert !revision.isRelative(); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. * * @param mergeSources the paths of JSON files being merged and indicates whether it is optional */ static MergeQuery<JsonNode> ofJson(MergeSource... mergeSources) { return ofJson(ImmutableList.copyOf(requireNonNull(mergeSources, "mergeSources"))); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. Then, the specified * <a href="https://github.com/json-path/JsonPath/blob/master/README.md">JSON path expressions</a> * are applied to the content of the {@link MergedEntry}. * * @param mergeSources the paths of JSON files being merged and indicates whether it is optional * @param jsonPaths the JSON path expressions to apply */ static MergeQuery<JsonNode> ofJsonPath(Iterable<MergeSource> mergeSources, String... jsonPaths) { return ofJsonPath(mergeSources, ImmutableList.copyOf(requireNonNull(jsonPaths, "jsonPaths"))); }
CacheableMergeQueryCall(Repository repo, Revision revision, MergeQuery<?> query) { super(repo); this.revision = requireNonNull(revision, "revision"); this.query = requireNonNull(query, "query"); // Only JSON files can currently be merged. query.mergeSources().forEach(path -> validateJsonFilePath(path.path(), "path")); hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo); assert !revision.isRelative(); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. Then, the specified expressions are applied to the content of the * {@link MergedEntry}. * * @param type the type of the {@link MergeQuery} * @param mergeSources the paths of JSON files being merged and indicates whether it is optional * @param expressions the expressions to apply to the content of the {@link MergedEntry} */ static MergeQuery<?> of(QueryType type, Iterable<MergeSource> mergeSources, Iterable<String> expressions) { requireNonNull(type, "type"); switch (type) { case IDENTITY: return ofJson(mergeSources); case JSON_PATH: return ofJsonPath(mergeSources, expressions); default: throw new IllegalArgumentException("Illegal query type: " + type.name()); } }
/** * Retrieves the merged entry of the specified {@link MergeSource}s at the specified revision. * Only JSON entry merge is currently supported. The JSON files are merged sequentially as specified in * the {@code mergeSources}. * * <p>Note that only {@link ObjectNode} is recursively merged traversing the children. Other node types are * simply replaced. * * @return the {@link MergedEntry} which contains the result of the merge */ default CompletableFuture<MergedEntry<?>> mergeFiles( String projectName, String repositoryName, Revision revision, Iterable<MergeSource> mergeSources) { @SuppressWarnings("unchecked") final CompletableFuture<MergedEntry<?>> future = (CompletableFuture<MergedEntry<?>>) (CompletableFuture<?>) mergeFiles( projectName, repositoryName, revision, MergeQuery.ofJson(mergeSources)); return future; }
return MergeQuery.ofJsonPath(mergeSourceBuilder.build(), jsonPathsBuilder.build());
@Override int weigh(MergedEntry<?> value) { int weight = 0; final List<MergeSource> mergeSources = query.mergeSources(); weight += mergeSources.size(); for (MergeSource mergeSource : mergeSources) { weight += mergeSource.path().length(); } final List<String> expressions = query.expressions(); weight += expressions.size(); for (String expression : expressions) { weight += expression.length(); } if (value != null) { weight += value.contentAsText().length(); } return weight; }
private static ImmutableList<MergeSource> convertMergeSources( com.linecorp.centraldogma.common.MergeQuery<?> mergeQuery) { return mergeQuery.mergeSources().stream() .map(mergeSource -> new MergeSource( mergeSource.path(), mergeSource.isOptional())) .collect(toImmutableList()); }
/** * Returns a newly-created {@link MergeQuery} that merges the JSON contents as specified in the * {@code mergeSources}. Then, the specified expressions are applied to the content of the * {@link MergedEntry}. * * @param type the type of the {@link MergeQuery} * @param mergeSources the paths of JSON files being merged and indicates whether it is optional * @param expressions the expressions to apply to the content of the {@link MergedEntry} */ static MergeQuery<?> of(QueryType type, Iterable<MergeSource> mergeSources, Iterable<String> expressions) { requireNonNull(type, "type"); switch (type) { case IDENTITY: return ofJson(mergeSources); case JSON_PATH: return ofJsonPath(mergeSources, expressions); default: throw new IllegalArgumentException("Illegal query type: " + type.name()); } }
/** * Retrieves the merged entry of the specified {@link MergeSource}s at the specified revision. * Only JSON entry merge is currently supported. The JSON files are merged sequentially as specified in * the {@code mergeSources}. * * <p>Note that only {@link ObjectNode} is recursively merged traversing the children. Other node types are * simply replaced. * * @return the {@link MergedEntry} which contains the result of the merge */ default CompletableFuture<MergedEntry<?>> mergeFiles( String projectName, String repositoryName, Revision revision, Iterable<MergeSource> mergeSources) { @SuppressWarnings("unchecked") final CompletableFuture<MergedEntry<?>> future = (CompletableFuture<MergedEntry<?>>) (CompletableFuture<?>) mergeFiles( projectName, repositoryName, revision, MergeQuery.ofJson(mergeSources)); return future; }
return MergeQuery.ofJsonPath(mergeSourceBuilder.build(), jsonPathsBuilder.build());
final List<JsonNode> jsonNodes = jsonNodesBuilder.build(); if (jsonNodes.isEmpty()) { throw new EntryNotFoundException(revision, concatenatePaths(query.mergeSources())); final List<String> expressions = query.expressions(); if (!Iterables.isEmpty(expressions)) { result = Jackson.extractTree(result, expressions);
requireNonNull(query, "query"); final List<MergeSource> mergeSources = query.mergeSources();
@Test public void mergeFiles() throws Exception { doAnswer(invocation -> { final AsyncMethodCallback<MergedEntry> callback = invocation.getArgument(4); callback.onComplete(new MergedEntry(new TRevision(1), TEntryType.JSON, "{\"foo\": \"bar\"}", ImmutableList.of("/a.json", "/b.json"))); return null; }).when(iface).mergeFiles(any(), any(), any(), any(), any()); assertThat(client.mergeFiles("project", "repo", new Revision(1), MergeQuery.ofJson(ImmutableList.of(MergeSource.ofOptional("/a.json"), MergeSource.ofRequired("/b.json")))) .get()) .isEqualTo(com.linecorp.centraldogma.common.MergedEntry.of( new Revision(1), EntryType.JSON, Jackson.readTree("{\"foo\": \"bar\"}"), ImmutableList.of("/a.json", "/b.json"))); verify(iface).mergeFiles(eq("project"), eq("repo"), any(), any(), any()); }