Skip to content

Commit

Permalink
Use whisker for java
Browse files Browse the repository at this point in the history
Summary:
Whisker is a replacement for mustache and is largely backward compatible. It brings several quality of life improvements like... error checking.

You can read more about the details [in this post](https://fb.workplace.com/groups/498666380550247/permalink/2051389461944590/).

This diff moves `t_mstch_java_generator` to use whisker instead of mstch.

This requires some changes to the templates to work around the bugs in mstch that have been previously worked around.

#buildall

Reviewed By: iahs

Differential Revision: D67874968

fbshipit-source-id: 6267cd0ed3c04360241fb3f9b45e6f90b3278142
  • Loading branch information
praihan authored and facebook-github-bot committed Jan 7, 2025
1 parent f2b0092 commit 5b94e52
Show file tree
Hide file tree
Showing 184 changed files with 9,018 additions and 10,095 deletions.
11 changes: 11 additions & 0 deletions thrift/compiler/generate/t_mstch_java_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ class t_mstch_java_generator : public t_mstch_generator {
public:
using t_mstch_generator::t_mstch_generator;

std::optional<whisker_options> use_whisker() const override {
whisker_options opts;
opts.allowed_undefined_variables = {
"type:typedef_type", // in UnionWrite.mustache
"struct:isUnion?", // in WriteResponseType.mustache
"struct:asBean?", // in DefaultValue.mustache
"field:hasAdapter?", // in BoxedType.mustache
};
return opts;
}

std::string template_prefix() const override { return "java"; }

void generate_program() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{{> AutoGenerated}}

package {{service:javaPackage}};
{{#service:disableReactive?}}// Generation of this class is disabled using (java.swift.disable_reactive){{/service:disableReactive?}}{{^service:disableReactive?}}

import java.util.*;

public class {{service:javaCapitalName}}ReactiveAsyncWrapper {{#service:extends}} extends {{service:javaPackage}}.{{service:javaCapitalName}}ReactiveAsyncWrapper{{/service:extends}}
Expand Down Expand Up @@ -95,4 +95,3 @@ public class {{service:javaCapitalName}}ReactiveAsyncWrapper {{#service:extends}
{{/last?}}
{{/service:interactions}}
}
{{/service:disableReactive?}}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{{> AutoGenerated}}

package {{service:javaPackage}};
{{#service:disableReactive?}}// Generation of this class is disabled using (java.swift.disable_reactive){{/service:disableReactive?}}{{^service:disableReactive?}}

import java.util.*;

public class {{service:javaCapitalName}}ReactiveBlockingWrapper {{#service:extends}} extends {{service:javaPackage}}.{{service:javaCapitalName}}ReactiveBlockingWrapper{{/service:extends}}
Expand Down Expand Up @@ -94,4 +94,3 @@ public class {{service:javaCapitalName}}ReactiveBlockingWrapper {{#service:exten
{{/last?}}
{{/service:interactions}}
}
{{/service:disableReactive?}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class Builder {
{{#field:isSensitive?}}
@org.apache.thrift.annotations.Sensitive
{{/field:isSensitive?}}
{{> ThriftFieldAnnotation}}{{#field:javaAnnotation?}} @{{field:javaAnnotation}}{{/field:javaAnnotation?}}
{{> ThriftFieldAnnotation}}
{{#field:hasWrapper?}}
public Builder {{> SetterName}}({{field:wrapperType}} {{field:javaName}}) {
this.{{field:javaName}} = {{field:javaName}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public A(
}



public static Builder builder() {
return new Builder();
}
Expand All @@ -50,7 +50,7 @@ public A build() {
return result;
}
}

public static final Map<String, Integer> NAMES_TO_IDS = new HashMap<>();
public static final Map<String, Integer> THRIFT_NAMES_TO_IDS = new HashMap<>();
public static final Map<Integer, TField> FIELD_METADATA = new HashMap<>();
Expand All @@ -65,7 +65,7 @@ public String toString() {
ToStringHelper helper = toStringHelper(this);
return helper.toString();
}

@java.lang.Override
public boolean equals(java.lang.Object o) {
if (this == o) {
Expand All @@ -80,13 +80,13 @@ public boolean equals(java.lang.Object o) {
return
true;
}

@java.lang.Override
public int hashCode() {
return Arrays.deepHashCode(new java.lang.Object[] {
});
}


public static com.facebook.thrift.payload.Reader<A> asReader() {
return A::read0;
Expand All @@ -109,13 +109,13 @@ public static A read0(TProtocol oprot) throws TException {
oprot.readStructEnd();
return builder.build();
}

public void write0(TProtocol oprot) throws TException {
oprot.writeStructBegin(STRUCT_DESC);
oprot.writeFieldStop();
oprot.writeStructEnd();
}

private static class _ALazy {
private static final A _DEFAULT = new A.Builder().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AdaptTemplatedNestedTestStruct(
protected AdaptTemplatedNestedTestStruct() {
this.adaptedStruct = null;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -45,8 +45,7 @@ public static Builder builder(AdaptTemplatedNestedTestStruct other) {
public static class Builder {
private test.fixtures.adapter.AdaptTemplatedTestStruct adaptedStruct = null;

@com.facebook.swift.codec.ThriftField(value=1, name="adaptedStruct", requiredness=Requiredness.NONE)
public Builder setAdaptedStruct(test.fixtures.adapter.AdaptTemplatedTestStruct adaptedStruct) {
@com.facebook.swift.codec.ThriftField(value=1, name="adaptedStruct", requiredness=Requiredness.NONE) public Builder setAdaptedStruct(test.fixtures.adapter.AdaptTemplatedTestStruct adaptedStruct) {
this.adaptedStruct = adaptedStruct;
return this;
}
Expand All @@ -66,7 +65,7 @@ public AdaptTemplatedNestedTestStruct build() {
return result;
}
}

public static final Map<String, Integer> NAMES_TO_IDS = new HashMap<>();
public static final Map<String, Integer> THRIFT_NAMES_TO_IDS = new HashMap<>();
public static final Map<Integer, TField> FIELD_METADATA = new HashMap<>();
Expand All @@ -86,14 +85,14 @@ public AdaptTemplatedNestedTestStruct build() {
@Nullable
@com.facebook.swift.codec.ThriftField(value=1, name="adaptedStruct", requiredness=Requiredness.NONE)
public test.fixtures.adapter.AdaptTemplatedTestStruct getAdaptedStruct() { return adaptedStruct; }

@java.lang.Override
public String toString() {
ToStringHelper helper = toStringHelper(this);
helper.add("adaptedStruct", adaptedStruct);
return helper.toString();
}

@java.lang.Override
public boolean equals(java.lang.Object o) {
if (this == o) {
Expand All @@ -109,14 +108,14 @@ public boolean equals(java.lang.Object o) {
Objects.equals(adaptedStruct, other.adaptedStruct) &&
true;
}

@java.lang.Override
public int hashCode() {
return Arrays.deepHashCode(new java.lang.Object[] {
adaptedStruct
});
}


public static com.facebook.thrift.payload.Reader<AdaptTemplatedNestedTestStruct> asReader() {
return AdaptTemplatedNestedTestStruct::read0;
Expand Down Expand Up @@ -147,7 +146,7 @@ public static AdaptTemplatedNestedTestStruct read0(TProtocol oprot) throws TExce
oprot.readStructEnd();
return builder.build();
}

public void write0(TProtocol oprot) throws TException {
oprot.writeStructBegin(STRUCT_DESC);
if (adaptedStruct != null) {
Expand All @@ -158,7 +157,7 @@ public void write0(TProtocol oprot) throws TException {
oprot.writeFieldStop();
oprot.writeStructEnd();
}

private static class _AdaptTemplatedNestedTestStructLazy {
private static final AdaptTemplatedNestedTestStruct _DEFAULT = new AdaptTemplatedNestedTestStruct.Builder().build();
}
Expand Down
Loading

0 comments on commit 5b94e52

Please sign in to comment.