Skip to content

Commit f09327c

Browse files
Soy Authorscopybara-github
authored andcommitted
Pass LoggableElementMetadata from VE to Logger's enter function in the Server Side Logger
PiperOrigin-RevId: 749102931
1 parent 7eafc4c commit f09327c

File tree

7 files changed

+89
-30
lines changed

7 files changed

+89
-30
lines changed

java/src/com/google/template/soy/data/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ java_library(
113113
"//java/src/com/google/template/soy/internal/proto",
114114
"//java/src/com/google/template/soy/jbcsrc/api:helpers",
115115
"//java/src/com/google/template/soy/jbcsrc/shared:names",
116+
"//src/main/protobuf:ve_metadata_java_proto",
116117
"@com_google_auto_value_auto_value",
117118
"@com_google_protobuf//:protobuf_java",
118119
"@maven//:com_google_code_findbugs_jsr305",

java/src/com/google/template/soy/data/LogStatement.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020
import com.google.common.base.MoreObjects;
2121
import com.google.protobuf.Message;
2222
import com.google.protobuf.TextFormat;
23+
import com.google.template.soy.logging.LoggableElementMetadata;
2324
import javax.annotation.Nullable;
2425

2526
/** The value of a {@code velog} statement. */
2627
@AutoValue
2728
public abstract class LogStatement {
2829
public static LogStatement create(long id, @Nullable Message data, boolean logOnly) {
29-
return new AutoValue_LogStatement(id, data, logOnly);
30+
return new AutoValue_LogStatement(id, data, logOnly, null);
31+
}
32+
33+
public static LogStatement create(
34+
long id,
35+
@Nullable Message data,
36+
boolean logOnly,
37+
@Nullable LoggableElementMetadata loggableElementMetadata) {
38+
return new AutoValue_LogStatement(id, data, logOnly, loggableElementMetadata);
3039
}
3140

3241
LogStatement() {} // prevent subclasses outside the package
@@ -46,6 +55,9 @@ public static LogStatement create(long id, @Nullable Message data, boolean logOn
4655
*/
4756
public abstract boolean logOnly();
4857

58+
@Nullable
59+
public abstract LoggableElementMetadata loggableElementMetadata();
60+
4961
@Override
5062
public String toString() {
5163
return MoreObjects.toStringHelper("velog")
@@ -60,6 +72,7 @@ public String toString() {
6072
+ TextFormat.shortDebugString(data())
6173
+ "}")
6274
.addValue(logOnly() ? "logonly" : null)
75+
.add("loggableElementMetadata", loggableElementMetadata())
6376
.toString();
6477
}
6578
}

java/src/com/google/template/soy/jbcsrc/runtime/JbcSrcRuntime.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ public static boolean stringEqualsAsNumber(String expr, double number) {
132132
}
133133
}
134134

135-
136135
@Keep
137136
@Nonnull
138137
public static SoyValue getField(SoyValue record, RecordProperty field) {
@@ -752,16 +751,15 @@ public StackFrame render(
752751
@Keep
753752
public static LogStatement createLogStatement(boolean logOnly, SoyValue value) {
754753
SoyVisualElementData veData = (SoyVisualElementData) value;
755-
return LogStatement.create(veData.ve().id(), veData.data(), logOnly);
754+
return LogStatement.create(veData.ve().id(), veData.data(), logOnly, veData.ve().metadata());
756755
}
757756

758757
@Keep
759758
public static LogStatement createLogStatement(SoyValue value) {
760759
SoyVisualElementData veData = (SoyVisualElementData) value;
761-
return LogStatement.create(veData.ve().id(), veData.data(), false);
760+
return LogStatement.create(veData.ve().id(), veData.data(), false, veData.ve().metadata());
762761
}
763762

764-
765763
/** Asserts that all members of the list are resolved. */
766764
@Keep
767765
@Nonnull

java/tests/com/google/template/soy/jbcsrc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ java_library(
9191
"//java/tests/com/google/template/soy/data:soy_value_converter_utility",
9292
"//java/tests/com/google/template/soy/jbcsrc/restricted/testing",
9393
"//src/main/protobuf:logging_config_java_proto",
94+
"//src/main/protobuf:ve_metadata_java_proto",
9495
"//src/test/protobuf:test_protos_java_proto",
9596
"@com_google_protobuf//:protobuf_java",
9697
"@maven//:com_google_code_findbugs_jsr305",

java/tests/com/google/template/soy/jbcsrc/VeLoggingTest.java

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.template.soy.jbcsrc;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.template.soy.testing.VeMetadataExample.veMetadataExampleExtension;
2021
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.fail;
2223

@@ -33,6 +34,7 @@
3334
import com.google.template.soy.jbcsrc.shared.CompiledTemplates;
3435
import com.google.template.soy.jbcsrc.shared.RenderContext;
3536
import com.google.template.soy.jbcsrc.shared.StackFrame;
37+
import com.google.template.soy.logging.LoggableElementMetadata;
3638
import com.google.template.soy.logging.LoggingFunction;
3739
import com.google.template.soy.logging.SoyLogger;
3840
import com.google.template.soy.logging.SoyLogger.LoggingAttrs;
@@ -107,7 +109,10 @@ public void testBasicLogging_treeStructure() throws Exception {
107109
assertThat(sb.toString())
108110
.isEqualTo("<div data-id=1><div data-id=2></div><div data-id=3></div></div>");
109111
assertThat(testLogger.builder.toString())
110-
.isEqualTo("velog{id=1}\n" + " velog{id=2}\n" + " velog{id=3}");
112+
.isEqualTo(
113+
"velog{id=1, loggableElementMetadata=}\n"
114+
+ " velog{id=2, loggableElementMetadata=}\n"
115+
+ " velog{id=3, loggableElementMetadata=}");
111116
}
112117

113118
@Test
@@ -119,7 +124,7 @@ public void testBasicLogging_withData() throws Exception {
119124
"{velog ve_data(FooVe, Foo(intField: 123))}<div data-id=1></div>{/velog}");
120125
assertThat(sb.toString()).isEqualTo("<div data-id=1></div>");
121126
assertThat(testLogger.builder.toString())
122-
.isEqualTo("velog{id=1, data=soy.test.Foo{int_field: 123}}");
127+
.isEqualTo("velog{id=1, data=soy.test.Foo{int_field: 123}, loggableElementMetadata=}");
123128
}
124129

125130
@Test
@@ -131,7 +136,8 @@ public void testBasicLogging_logonly() throws Exception {
131136
"{velog FooVe logonly=\"true\"}<div data-id=1></div>{/velog}");
132137
// logonly ve's disable content generation
133138
assertThat(sb.toString()).isEmpty();
134-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, logonly}");
139+
assertThat(testLogger.builder.toString())
140+
.isEqualTo("velog{id=1, logonly, loggableElementMetadata=}");
135141
}
136142

137143
@Test
@@ -152,7 +158,10 @@ public void testBasicLogging_logonly_dynamic() throws Exception {
152158
"{velog Bar logonly=\"$f\"}<div data-id=2></div>{/velog}");
153159
// logonly ve's disable content generation
154160
assertThat(sb.toString()).isEqualTo("<div data-id=2></div>");
155-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, logonly}\nvelog{id=2}");
161+
assertThat(testLogger.builder.toString())
162+
.isEqualTo(
163+
"velog{id=1, logonly, loggableElementMetadata=}\n"
164+
+ "velog{id=2, loggableElementMetadata=}");
156165
}
157166

158167
@Test
@@ -183,14 +192,30 @@ public void testBasicLogging_logonly_true_noLogger() throws Exception {
183192
}
184193
}
185194

195+
@Test
196+
public void testBasicLogging_loggableElementMetadata() throws Exception {
197+
StringBuilder sb = new StringBuilder();
198+
TestLogger testLogger = new TestLogger();
199+
renderTemplate(
200+
OutputAppendable.create(sb, testLogger),
201+
"{velog ve_data(VeWithLoggableElementMetadata)}<div data-id=1></div>{/velog}");
202+
assertThat(sb.toString()).isEqualTo("<div data-id=1></div>");
203+
assertThat(testLogger.builder.toString())
204+
.isEqualTo(
205+
"velog{id=5, loggableElementMetadata=[soy.test.ve_metadata_example_extension]:"
206+
+ " \"foo\"\n"
207+
+ "}");
208+
}
209+
186210
@Test
187211
public void testLogging_letVariables() throws Exception {
188212
StringBuilder sb = new StringBuilder();
189213
TestLogger testLogger = new TestLogger();
190214
renderTemplate(
191215
OutputAppendable.create(sb, testLogger),
192216
"{let $foo kind=\"html\"}{velog FooVe}<div data-id=1></div>{/velog}{/let}{$foo}{$foo}");
193-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}\nvelog{id=1}");
217+
assertThat(testLogger.builder.toString())
218+
.isEqualTo("velog{id=1, loggableElementMetadata=}\nvelog{id=1, loggableElementMetadata=}");
194219
assertThat(sb.toString()).isEqualTo("<div data-id=1></div><div data-id=1></div>");
195220
}
196221

@@ -205,7 +230,7 @@ public void testLogging_msg() throws Exception {
205230
+ " Greetings, {velog FooVe}<a href='./wiki?human'>Human</a>{/velog}\n"
206231
+ "{/msg}");
207232
assertThat(sb.toString()).isEqualTo("Greetings, <a href='./wiki?human'>Human</a>");
208-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
233+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
209234
}
210235

211236
// Regression test for a bug where logging would get dropped if there was a velog, in a msg around
@@ -221,7 +246,7 @@ public void testLogging_msg_void_element() throws Exception {
221246
+ " Greetings, {velog FooVe}<input type=text>{/velog}\n"
222247
+ "{/msg}");
223248
assertThat(sb.toString()).isEqualTo("Greetings, <input type=text>");
224-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
249+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
225250
}
226251

227252
@Test
@@ -237,10 +262,10 @@ public void testLogging_nestedLogOnly() throws IOException {
237262
assertThat(sb.toString()).isEmpty();
238263
assertThat(testLogger.builder.toString())
239264
.isEqualTo(
240-
"velog{id=1, logonly}\n"
241-
+ " velog{id=1}\n"
242-
+ " velog{id=1, logonly}\n"
243-
+ " velog{id=1, logonly}");
265+
"velog{id=1, logonly, loggableElementMetadata=}\n"
266+
+ " velog{id=1, loggableElementMetadata=}\n"
267+
+ " velog{id=1, logonly, loggableElementMetadata=}\n"
268+
+ " velog{id=1, logonly, loggableElementMetadata=}");
244269
}
245270

246271
@Test
@@ -252,7 +277,7 @@ public void testLogging_loggingFunction_basic() throws Exception {
252277
"<div data-depth={depth()}></div>"
253278
+ "{velog FooVe}<div data-depth={depth()}></div>{/velog}"
254279
+ "<div data-depth={depth()}></div>");
255-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
280+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
256281
assertThat(sb.toString())
257282
.isEqualTo("<div data-depth=0></div><div data-depth=1></div><div data-depth=0></div>");
258283
}
@@ -283,7 +308,7 @@ public void testLogging_elvis() throws Exception {
283308
" {/velog}",
284309
"{/let}",
285310
"{$log ?? ''}");
286-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
311+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
287312
assertThat(sb.toString()).isEqualTo("<div>hello</div>");
288313
}
289314

@@ -294,7 +319,7 @@ public void testLoggingAttributes_basic() throws Exception {
294319
testLogger.attrsMap.put(1L, LoggingAttrs.builder().addDataAttribute("data-foo", "bar").build());
295320
renderTemplate(
296321
OutputAppendable.create(sb, testLogger), "{velog FooVe}<div>hello</div>{/velog}");
297-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
322+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
298323
assertThat(sb.toString()).isEqualTo("<div data-foo=\"bar\">hello</div>");
299324
}
300325

@@ -305,7 +330,7 @@ public void testLoggingAttributes_withCall() throws Exception {
305330
testLogger.attrsMap.put(1L, LoggingAttrs.builder().addDataAttribute("data-foo", "bar").build());
306331
renderTemplate(
307332
OutputAppendable.create(sb, testLogger), "{velog FooVe}{call another /}{/velog}");
308-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
333+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
309334
assertThat(sb.toString()).isEqualTo("<div data-foo=\"bar\">called</div>");
310335
}
311336

@@ -318,7 +343,7 @@ public void testLoggingAttributes_withLet() throws Exception {
318343
OutputAppendable.create(sb, testLogger),
319344
"{let $div kind='html'}<div>hello</div>{/let}",
320345
"{velog FooVe}{$div}{/velog}");
321-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
346+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
322347
assertThat(sb.toString()).isEqualTo("<div data-foo=\"bar\">hello</div>");
323348
}
324349

@@ -329,7 +354,7 @@ public void testLoggingAttributes_anchor() throws Exception {
329354
testLogger.attrsMap.put(
330355
1L, LoggingAttrs.builder().addAnchorHref(SafeUrls.fromConstant("./go")).build());
331356
renderTemplate(OutputAppendable.create(sb, testLogger), "{velog FooVe}<a>hello</a>{/velog}");
332-
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1}");
357+
assertThat(testLogger.builder.toString()).isEqualTo("velog{id=1, loggableElementMetadata=}");
333358
assertThat(sb.toString()).isEqualTo("<a href=\"./go\">hello</a>");
334359
}
335360

@@ -361,15 +386,24 @@ private void renderTemplate(
361386
throws IOException {
362387
SoyFileSetParserBuilder builder =
363388
SoyFileSetParserBuilder.forTemplateAndImports(
364-
"{const FooVe = ve_def('FooVe', 1, Foo) /}"
365-
+ "{const Bar = ve_def('Bar', 2, Foo) /}"
366-
+ "{const Baz = ve_def('Baz', 3, Foo) /}"
367-
+ "{const Quux = ve_def('Quux', 4, Foo) /}"
389+
// End of the line comments are to force the formatter to keep each line on its own
390+
// line.
391+
"{const FooVe = ve_def('FooVe', 1, Foo) /}" //
392+
+ "{const Bar = ve_def('Bar', 2, Foo) /}" //
393+
+ "{const Baz = ve_def('Baz', 3, Foo) /}" //
394+
+ "{const Quux = ve_def('Quux', 4, Foo) /}" //
395+
+ "{const VeWithLoggableElementMetadata = ve_def(" //
396+
+ " 'VeWithLoggableElementMetadata'," //
397+
+ " 5," //
398+
+ " Foo," //
399+
+ " LoggableElementMetadata(veMetadataExampleExtension: 'foo')) /}" //
368400
+ "{template foo}\n"
369401
+ Joiner.on("\n").join(templateBodyLines)
370-
+ "\n{/template}"
371-
+ "{template another}<div>called</div>{/template}\n",
372-
Foo.getDescriptor())
402+
+ "\n{/template}" //
403+
+ "{template another}<div>called</div>{/template}\n", //
404+
Foo.getDescriptor(),
405+
LoggableElementMetadata.getDescriptor(),
406+
veMetadataExampleExtension.getDescriptor())
373407
.addSoySourceFunction(new DepthFunction())
374408
.addHtmlAttributesForLogging(true)
375409
.runAutoescaper(true);

src/test/protobuf/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
# limitations under the License.
1515
##
1616

17-
load("@rules_proto//proto:defs.bzl", "proto_library")
1817
load("@rules_java//java:defs.bzl", "java_proto_library")
18+
load("@rules_proto//proto:defs.bzl", "proto_library")
1919

2020
package(
2121
default_testonly = 1,
@@ -38,6 +38,7 @@ proto_library(
3838
["*.proto"],
3939
exclude = ["collision.proto"],
4040
),
41+
deps = ["//src/main/protobuf:ve_metadata_java_proto"],
4142
)
4243

4344
proto_library(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto2";
2+
3+
package soy.test;
4+
5+
import "src/main/protobuf/ve_metadata.proto";
6+
7+
option java_package = "com.google.template.soy.testing";
8+
9+
extend LoggableElementMetadata {
10+
optional string ve_metadata_example_extension = 1010;
11+
}

0 commit comments

Comments
 (0)