Skip to content

Commit 37cc4d8

Browse files
committed
Handle RenderDirective in annotation converters
1 parent e5742bc commit 37cc4d8

32 files changed

+405
-56
lines changed

pkl-core/src/main/java/org/pkl/core/runtime/VmPklBinaryEncoder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
258258
visit(value);
259259
}
260260

261+
@Override
262+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
263+
visitRenderDirective(value);
264+
}
265+
261266
@Override
262267
public void visitClass(VmClass value) {
263268
try {

pkl-core/src/main/java/org/pkl/core/runtime/VmValueConverter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public String toString() {
8585

8686
T convertFunction(VmFunction value, Iterable<Object> path);
8787

88+
/** Returns with an empty identifier if the second value is a RenderDirective */
8889
Pair<Identifier, T> convertProperty(ClassProperty property, Object value, Iterable<Object> path);
8990

9091
default T convert(Object value, Iterable<Object> path) {

pkl-core/src/main/java/org/pkl/core/stdlib/AbstractRenderer.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public void visit(Object value) {
163163
/** Visits a property of a {@link VmDynamic} or {@link VmTyped}. */
164164
protected abstract void visitProperty(Identifier name, Object value, boolean isFirst);
165165

166+
/** Perform logic for rendering a render directive in place of a property */
167+
protected abstract void visitPropertyRenderDirective(VmTyped value, boolean isFirst);
168+
166169
protected abstract void endDynamic(VmDynamic value, boolean isEmpty);
167170

168171
protected abstract void endTyped(VmTyped value, boolean isEmpty);
@@ -346,9 +349,14 @@ private void doVisitProperty(
346349
name = propVal.getFirst();
347350
value = propVal.getSecond();
348351
}
349-
var convertedValue = converter.convert(value, currPath);
350-
if (!(skipNullProperties && convertedValue instanceof VmNull)) {
351-
visitProperty(name, convertedValue, isFirst.getAndSetFalse());
352+
if (name.toString().isEmpty()) {
353+
assert isRenderDirective(value);
354+
visitPropertyRenderDirective((VmTyped) value, isFirst.getAndSetFalse());
355+
} else {
356+
var convertedValue = converter.convert(value, currPath);
357+
if (!(skipNullProperties && convertedValue instanceof VmNull)) {
358+
visitProperty(name, convertedValue, isFirst.getAndSetFalse());
359+
}
352360
}
353361
currPath.pop();
354362
currSourceSection = prevSourceSection;

pkl-core/src/main/java/org/pkl/core/stdlib/PklConverter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
public final class PklConverter implements VmValueConverter<Object> {
2525
private interface AnnotationConverter {
26-
VmPair convert(String property, VmTyped annotation, Object value);
26+
Object convert(String property, VmTyped annotation, Object value);
2727
}
2828

2929
private final Map<VmClass, VmFunction> typeConverters;
@@ -208,9 +208,10 @@ public Pair<Identifier, Object> convertProperty(
208208
if (converter == null) {
209209
continue;
210210
}
211-
var nameVal = converter.convert(name.toString(), annotation, value);
212-
name = Identifier.get((String) nameVal.getFirst());
213-
value = nameVal.getSecond();
211+
var converted = converter.convert(name.toString(), annotation, value);
212+
return converted instanceof VmPair vmPair
213+
? Pair.of(Identifier.get((String) vmPair.getFirst()), vmPair.getSecond())
214+
: Pair.of(Identifier.get(""), converted); // RenderDirective
214215
}
215216

216217
return Pair.of(name, value);
@@ -237,8 +238,7 @@ private Map<VmClass, AnnotationConverter> createAnnotationConverters(
237238
assert value != null; // forced in ctor
238239
result.put(
239240
(VmClass) key,
240-
(name, annotation, val) ->
241-
(VmPair) ((VmFunction) value).apply(annotation, name, val));
241+
(name, annotation, val) -> ((VmFunction) value).apply(annotation, name, val));
242242
return true;
243243
});
244244

pkl-core/src/main/java/org/pkl/core/stdlib/base/JsonRendererNodes.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
233233
visit(value);
234234
}
235235

236+
@Override
237+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
238+
if (!isFirst) builder.append(',');
239+
startNewLine();
240+
visitRenderDirective(value);
241+
}
242+
236243
@Override
237244
protected void endDynamic(VmDynamic value, boolean isEmpty) {
238245
if (value.hasElements()) {

pkl-core/src/main/java/org/pkl/core/stdlib/base/PListRendererNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
279279
builder.append(LINE_BREAK);
280280
}
281281

282+
@Override
283+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
284+
if (isFirst) {
285+
builder.append("<dict>").append(LINE_BREAK);
286+
}
287+
builder.append(currIndent);
288+
visitRenderDirective(value);
289+
builder.append(LINE_BREAK);
290+
}
291+
282292
@Override
283293
protected void endDynamic(VmDynamic value, boolean isEmpty) {
284294
if (value.hasElements()) {

pkl-core/src/main/java/org/pkl/core/stdlib/base/PcfRenderer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
2+
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -279,6 +279,15 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
279279
visit(value);
280280
}
281281

282+
@Override
283+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
284+
if (!builder.isEmpty()) {
285+
builder.append('\n');
286+
builder.append(currIndent);
287+
}
288+
visitRenderDirective(value);
289+
}
290+
282291
@Override
283292
protected void endDynamic(VmDynamic value, boolean isEmpty) {
284293
endObject(value, isEmpty);

pkl-core/src/main/java/org/pkl/core/stdlib/base/PropertiesRendererNodes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
231231
visitKeyedValue(value);
232232
}
233233

234+
@Override
235+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
236+
builder.append(VmUtils.readTextProperty(value));
237+
if (isDocument) {
238+
writeLineBreak();
239+
}
240+
}
241+
234242
@Override
235243
protected void endDynamic(VmDynamic value, boolean isEmpty) {}
236244

pkl-core/src/main/java/org/pkl/core/stdlib/base/YamlRendererNodes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
334334
visit(value);
335335
}
336336

337+
@Override
338+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
339+
if (!isFirst) {
340+
startNewLine();
341+
}
342+
visitRenderDirective(value);
343+
}
344+
337345
@Override
338346
protected void endDynamic(VmDynamic value, boolean isEmpty) {
339347
if (value.hasElements()) {

pkl-core/src/main/java/org/pkl/core/stdlib/jsonnet/RendererNodes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,15 @@ protected void visitProperty(Identifier name, Object value, boolean isFirst) {
309309
visit(value);
310310
}
311311

312+
@Override
313+
protected void visitPropertyRenderDirective(VmTyped value, boolean isFirst) {
314+
if (!isFirst) {
315+
builder.append(',');
316+
}
317+
builder.append(memberSeparator).append(currIndent);
318+
visitRenderDirective(value);
319+
}
320+
312321
@Override
313322
protected void visitElement(long index, Object value, boolean isFirst) {
314323
if (!isFirst) {

0 commit comments

Comments
 (0)