Skip to content

Commit 5d5c7b5

Browse files
committed
variables and template
1 parent f89447c commit 5d5c7b5

29 files changed

Lines changed: 1249 additions & 139 deletions

File tree

client/java-armeria-legacy/src/main/java/com/linecorp/centraldogma/client/armeria/legacy/LegacyCentralDogma.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ private static <T> Entry<T> entryAsText(Query<T> query, Revision normRev, String
281281
@Override
282282
public CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, String repositoryName,
283283
Revision revision, PathPattern pathPattern,
284-
boolean viewRaw) {
284+
boolean viewRaw, boolean applyTemplate) {
285285
requireNonNull(pathPattern, "pathPattern");
286286
return maybeNormalizeRevision(projectName, repositoryName, revision).thenCompose(normRev -> {
287287
final CompletableFuture<List<com.linecorp.centraldogma.internal.thrift.Entry>> future =
288288
run(callback -> {
289-
// viewRaw is not supported in LegacyCentralDogma.
289+
// viewRaw and applyTemplate are not supported in LegacyCentralDogma.
290290
client.getFiles(projectName, repositoryName,
291291
RevisionConverter.TO_DATA.convert(normRev),
292292
pathPattern.patternString(), callback);
@@ -488,13 +488,13 @@ public CompletableFuture<Revision> watchRepository(String projectName, String re
488488
public <T> CompletableFuture<Entry<T>> watchFile(String projectName, String repositoryName,
489489
Revision lastKnownRevision, Query<T> query,
490490
long timeoutMillis, boolean errorOnEntryNotFound,
491-
boolean viewRaw) {
491+
boolean viewRaw, boolean applyTemplate) {
492492
checkArgument(!errorOnEntryNotFound, "errorOnEntryNotFound is not supported in LegacyCentralDogma.");
493493
validateProjectAndRepositoryName(projectName, repositoryName);
494494
requireNonNull(lastKnownRevision, "lastKnownRevision");
495495
requireNonNull(query, "query");
496496
final CompletableFuture<WatchFileResult> future = run(callback -> {
497-
// viewRaw is not supported in LegacyCentralDogma.
497+
// viewRaw and applyTemplate are not supported in LegacyCentralDogma.
498498
client.watchFile(projectName, repositoryName,
499499
RevisionConverter.TO_DATA.convert(lastKnownRevision),
500500
QueryConverter.TO_DATA.convert(query),

client/java-armeria/src/main/java/com/linecorp/centraldogma/internal/client/armeria/ArmeriaCentralDogma.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ private static Map<String, EntryType> listFiles(AggregatedHttpResponse res) {
467467

468468
@Override
469469
public <T> CompletableFuture<Entry<T>> getFile(String projectName, String repositoryName, Revision revision,
470-
Query<T> query, boolean viewRaw) {
470+
Query<T> query, boolean viewRaw, boolean applyTemplate) {
471471
validateProjectAndRepositoryName(projectName, repositoryName);
472472
requireNonNull(revision, "revision");
473473
requireNonNull(query, "query");
@@ -480,6 +480,9 @@ public <T> CompletableFuture<Entry<T>> getFile(String projectName, String reposi
480480
if (viewRaw) {
481481
path.append("&viewRaw=true");
482482
}
483+
if (applyTemplate) {
484+
path.append("&applyTemplate=true");
485+
}
483486
appendJsonPaths(path, query.type(), query.expressions());
484487

485488
return client.execute(headers(HttpMethod.GET, path.toString()))
@@ -504,7 +507,7 @@ private static <T> Entry<T> getFile(Revision normRev, AggregatedHttpResponse res
504507
@Override
505508
public CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, String repositoryName,
506509
Revision revision, PathPattern pathPattern,
507-
boolean viewRaw) {
510+
boolean viewRaw, boolean applyTemplate) {
508511
validateProjectAndRepositoryName(projectName, repositoryName);
509512
requireNonNull(revision, "revision");
510513
requireNonNull(pathPattern, "pathPattern");
@@ -519,6 +522,9 @@ public CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, Str
519522
if (viewRaw) {
520523
path.append("&viewRaw=true");
521524
}
525+
if (applyTemplate) {
526+
path.append("&applyTemplate=true");
527+
}
522528

523529
return client.execute(headers(HttpMethod.GET, path.toString()))
524530
.aggregate()
@@ -867,7 +873,7 @@ private static Revision watchRepository(AggregatedHttpResponse res, QueryType un
867873
public <T> CompletableFuture<Entry<T>> watchFile(String projectName, String repositoryName,
868874
Revision lastKnownRevision, Query<T> query,
869875
long timeoutMillis, boolean errorOnEntryNotFound,
870-
boolean viewRaw) {
876+
boolean viewRaw, boolean applyTemplate) {
871877
validateProjectAndRepositoryName(projectName, repositoryName);
872878
requireNonNull(lastKnownRevision, "lastKnownRevision");
873879
requireNonNull(query, "query");
@@ -893,6 +899,13 @@ public <T> CompletableFuture<Entry<T>> watchFile(String projectName, String repo
893899
// The query type can't be JSON_PATH here as checked above.
894900
path.append("?viewRaw=true");
895901
}
902+
if (applyTemplate) {
903+
if (path.indexOf("?") >= 0) {
904+
path.append("&applyTemplate=true");
905+
} else {
906+
path.append("?applyTemplate=true");
907+
}
908+
}
896909

897910
return watch(lastKnownRevision, timeoutMillis, path.toString(), query.type(),
898911
(res, queryType) -> watchFile(res, queryType, viewRaw), errorOnEntryNotFound);

client/java/src/main/java/com/linecorp/centraldogma/client/AbstractFileRequest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
class AbstractFileRequest<SELF extends AbstractFileRequest<SELF>> {
2222

2323
private boolean viewRaw;
24+
private boolean applyTemplate;
2425

2526
/**
2627
* Sets whether to view the raw content of the file.
@@ -40,6 +41,23 @@ boolean viewRaw() {
4041
return viewRaw;
4142
}
4243

44+
/**
45+
* Sets whether to apply template processing to the file using the variables defined in
46+
* the same repository and its parent project.
47+
*
48+
* <p>If {@link #viewRaw(boolean)} is set to true, the template processing will be applied to the raw
49+
* content. If {@link #viewRaw(boolean)} is set to false, the template processing will be applied to the
50+
* normalized content.
51+
*/
52+
public SELF applyTemplate(boolean applyTemplate) {
53+
this.applyTemplate = applyTemplate;
54+
return self();
55+
}
56+
57+
boolean applyTemplate() {
58+
return applyTemplate;
59+
}
60+
4361
@SuppressWarnings("unchecked")
4462
private SELF self() {
4563
return (SELF) this;

client/java/src/main/java/com/linecorp/centraldogma/client/CentralDogma.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ default CompletableFuture<Entry<?>> getFile(String projectName, String repositor
197197
*/
198198
default <T> CompletableFuture<Entry<T>> getFile(String projectName, String repositoryName,
199199
Revision revision, Query<T> query) {
200-
return getFile(projectName, repositoryName, revision, query, false);
200+
return getFile(projectName, repositoryName, revision, query, false, false);
201201
}
202202

203203
/**
@@ -215,7 +215,8 @@ default <T> CompletableFuture<Entry<T>> getFile(String projectName, String repos
215215
* @return the {@link Entry} that is matched by the given {@link Query}
216216
*/
217217
<T> CompletableFuture<Entry<T>> getFile(String projectName, String repositoryName,
218-
Revision revision, Query<T> query, boolean viewRaw);
218+
Revision revision, Query<T> query, boolean viewRaw,
219+
boolean applyTemplate);
219220

220221
/**
221222
* Retrieves the files matched by the path pattern.
@@ -244,7 +245,7 @@ default CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, St
244245
*/
245246
default CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, String repositoryName,
246247
Revision revision, PathPattern pathPattern) {
247-
return getFiles(projectName, repositoryName, revision, pathPattern, false);
248+
return getFiles(projectName, repositoryName, revision, pathPattern, false, false);
248249
}
249250

250251
/**
@@ -263,7 +264,7 @@ default CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, St
263264
*/
264265
CompletableFuture<Map<String, Entry<?>>> getFiles(String projectName, String repositoryName,
265266
Revision revision, PathPattern pathPattern,
266-
boolean viewRaw);
267+
boolean viewRaw, boolean applyTemplate);
267268

268269
/**
269270
* Retrieves the merged entry of the specified {@link MergeSource}s at the specified revision.
@@ -792,7 +793,7 @@ default <T> CompletableFuture<Entry<T>> watchFile(String projectName, String rep
792793
Revision lastKnownRevision, Query<T> query,
793794
long timeoutMillis, boolean errorOnEntryNotFound) {
794795
return watchFile(projectName, repositoryName, lastKnownRevision, query,
795-
timeoutMillis, errorOnEntryNotFound, false);
796+
timeoutMillis, errorOnEntryNotFound, false, false);
796797
}
797798

798799
/**
@@ -821,7 +822,7 @@ default <T> CompletableFuture<Entry<T>> watchFile(String projectName, String rep
821822
<T> CompletableFuture<Entry<T>> watchFile(String projectName, String repositoryName,
822823
Revision lastKnownRevision, Query<T> query,
823824
long timeoutMillis, boolean errorOnEntryNotFound,
824-
boolean viewRaw);
825+
boolean viewRaw, boolean applyTemplate);
825826

826827
/**
827828
* Returns a {@link Watcher} which notifies its listeners when the result of the

client/java/src/main/java/com/linecorp/centraldogma/client/FileRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public CompletableFuture<Entry<T>> get(Revision revision) {
5959
}
6060
return centralDogmaRepo.centralDogma().getFile(centralDogmaRepo.projectName(),
6161
centralDogmaRepo.repositoryName(),
62-
revision, query, viewRaw());
62+
revision, query, viewRaw(), applyTemplate());
6363
}
6464
}

client/java/src/main/java/com/linecorp/centraldogma/client/FileWatcher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ final class FileWatcher<T> extends AbstractWatcher<T> {
4040
private final Function<Object, ? extends T> mapper;
4141
@Nullable
4242
private final Executor mapperExecutor;
43+
private final boolean applyTemplate;
4344

4445
FileWatcher(CentralDogma centralDogma, ScheduledExecutorService watchScheduler, String projectName,
4546
String repositoryName, Query<T> query, long timeoutMillis, boolean errorOnEntryNotFound,
4647
@Nullable Function<Object, ? extends T> mapper, Executor mapperExecutor,
4748
long delayOnSuccessMillis, long initialDelayMillis, long maxDelayMillis, double multiplier,
48-
double jitterRate, @Nullable MeterRegistry meterRegistry) {
49+
double jitterRate, @Nullable MeterRegistry meterRegistry, boolean applyTemplate) {
4950
super(watchScheduler, projectName, repositoryName, query.path(), errorOnEntryNotFound,
5051
delayOnSuccessMillis, initialDelayMillis, maxDelayMillis, multiplier, jitterRate, meterRegistry);
5152
this.centralDogma = centralDogma;
@@ -56,13 +57,15 @@ final class FileWatcher<T> extends AbstractWatcher<T> {
5657
this.errorOnEntryNotFound = errorOnEntryNotFound;
5758
this.mapper = mapper;
5859
this.mapperExecutor = mapperExecutor;
60+
this.applyTemplate = applyTemplate;
5961
}
6062

6163
@Override
6264
CompletableFuture<Latest<T>> doWatch(Revision lastKnownRevision) {
6365
final CompletableFuture<Entry<T>> future = centralDogma.watchFile(projectName, repositoryName,
6466
lastKnownRevision, query,
65-
timeoutMillis, errorOnEntryNotFound);
67+
timeoutMillis, errorOnEntryNotFound,
68+
false, applyTemplate);
6669
if (mapper == null) {
6770
return future.thenApply(entry -> {
6871
if (entry == null) {

client/java/src/main/java/com/linecorp/centraldogma/client/FilesRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public CompletableFuture<Map<String, Entry<?>>> get(Revision revision) {
7979
requireNonNull(revision, "revision");
8080
return centralDogmaRepo.centralDogma().getFiles(centralDogmaRepo.projectName(),
8181
centralDogmaRepo.repositoryName(),
82-
revision, pathPattern, viewRaw());
82+
revision, pathPattern, viewRaw(), applyTemplate());
8383
}
8484
}

client/java/src/main/java/com/linecorp/centraldogma/client/WatchRequest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class WatchRequest<T> extends WatchOptions {
3535
private final CentralDogmaRepository centralDogmaRepo;
3636
private final Query<T> query;
3737
private boolean viewRaw;
38+
private boolean applyTemplate;
3839

3940
WatchRequest(CentralDogmaRepository centralDogmaRepo, Query<T> query) {
4041
this.centralDogmaRepo = centralDogmaRepo;
@@ -74,6 +75,19 @@ public WatchRequest<T> viewRaw(boolean viewRaw) {
7475
return this;
7576
}
7677

78+
/**
79+
* Sets whether to apply template processing to the file using the variables defined in
80+
* the same repository and its parent project.
81+
*
82+
* <p>If {@link #viewRaw(boolean)} is set to true, the template processing will be applied to the raw
83+
* content. If {@link #viewRaw(boolean)} is set to false, the template processing will be applied to the
84+
* normalized content.
85+
*/
86+
public WatchRequest<T> applyTemplate(boolean applyTemplate) {
87+
this.applyTemplate = applyTemplate;
88+
return this;
89+
}
90+
7791
/**
7892
* Waits for the file matched by the {@link Query} to be changed since the {@link Revision#HEAD}.
7993
* If no changes were made within the {@link #timeoutMillis(long)}, the
@@ -103,6 +117,7 @@ public CompletableFuture<Entry<T>> start(Revision lastKnownRevision) {
103117
return centralDogmaRepo.centralDogma().watchFile(centralDogmaRepo.projectName(),
104118
centralDogmaRepo.repositoryName(),
105119
lastKnownRevision, query,
106-
timeoutMillis(), errorOnEntryNotFound(), viewRaw);
120+
timeoutMillis(), errorOnEntryNotFound(), viewRaw,
121+
applyTemplate);
107122
}
108123
}

client/java/src/main/java/com/linecorp/centraldogma/client/WatcherRequest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.linecorp.centraldogma.client;
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
19+
import static com.google.common.base.Preconditions.checkState;
1920
import static java.util.Objects.requireNonNull;
2021

2122
import java.time.Duration;
@@ -60,7 +61,7 @@ public final class WatcherRequest<T> extends WatchOptions {
6061
private long maxDelayMillis = DEFAULT_MAX_DELAY_MILLIS;
6162
private double multiplier = DEFAULT_MULTIPLIER;
6263
private double jitterRate = DEFAULT_JITTER_RATE;
63-
private boolean viewRaw;
64+
private boolean applyTemplate;
6465

6566
WatcherRequest(CentralDogmaRepository centralDogmaRepo, Query<T> query,
6667
ScheduledExecutorService blockingTaskExecutor, @Nullable MeterRegistry meterRegistry) {
@@ -151,6 +152,15 @@ public WatcherRequest<T> jitterRate(double jitterRate) {
151152
return this;
152153
}
153154

155+
/**
156+
* Sets whether to apply template processing to the file using the variables defined in
157+
* the same repository and its parent project.
158+
*/
159+
public WatcherRequest<T> applyTemplate(boolean applyTemplate) {
160+
this.applyTemplate = applyTemplate;
161+
return this;
162+
}
163+
154164
@Override
155165
public WatcherRequest<T> timeout(Duration timeout) {
156166
//noinspection unchecked
@@ -181,8 +191,9 @@ public Watcher<T> start() {
181191
watcher = new FileWatcher<>(
182192
centralDogmaRepo.centralDogma(), blockingTaskExecutor, proName, repoName, query,
183193
timeoutMillis(), errorOnEntryNotFound(), mapper, executor, delayOnSuccessMillis,
184-
initialDelayMillis, maxDelayMillis, multiplier, jitterRate, meterRegistry);
194+
initialDelayMillis, maxDelayMillis, multiplier, jitterRate, meterRegistry, applyTemplate);
185195
} else {
196+
checkState(!applyTemplate, "applyTemplate can't be set when watching multiple files.");
186197
assert pathPattern != null;
187198
watcher = new FilesWatcher<>(
188199
centralDogmaRepo.centralDogma(), blockingTaskExecutor, proName, repoName, pathPattern,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 LY Corporation
3+
*
4+
* LY 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+
/**
20+
* An exception raised when a template processing fails.
21+
*/
22+
public final class TemplateProcessingException extends CentralDogmaException {
23+
private static final long serialVersionUID = -6137331564803016863L;
24+
25+
/**
26+
* Creates a new instance.
27+
*/
28+
public TemplateProcessingException(String message) {
29+
super(message);
30+
}
31+
32+
/**
33+
* Creates a new instance.
34+
*/
35+
public TemplateProcessingException(String message, Throwable cause) {
36+
super(message, cause);
37+
}
38+
}

0 commit comments

Comments
 (0)