Skip to content

Commit

Permalink
Add static builder method to thrift structs
Browse files Browse the repository at this point in the history
Summary:
Creates a static builder pattern as an alternative to writing `new Builder()` for concisenss. For example it can be used in the two following ways.

```
MyType.builder().setField(...).build();

// or copy from existing struct

MyType.builder(other).setField(...).build()
```

Reviewed By: fanzeyi

Differential Revision: D64505414

fbshipit-source-id: 1b87aa89f930e4994263c4c3ee15b8b6eb4bfa63
  • Loading branch information
j-bahr authored and facebook-github-bot committed Oct 17, 2024
1 parent f9e84d9 commit b82d7d7
Show file tree
Hide file tree
Showing 129 changed files with 1,290 additions and 258 deletions.
12 changes: 10 additions & 2 deletions thrift/compiler/generate/templates/java/Struct.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ import static com.google.common.base.MoreObjects.ToStringHelper;
public final class {{struct:javaCapitalName}} implements com.facebook.thrift.payload.ThriftSerializable {
{{> struct/ThriftConstructor}}

public static Builder builder() {
return new Builder();
}

public static Builder builder({{struct:javaCapitalName}} other) {
return new Builder(other);
}

{{> struct/StructBuilder}}
{{> struct/AdapterDefinition}}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("{{struct:name}}");
{{#struct:fields}}{{!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public A(



public static Builder builder() {
return new Builder();
}

public static Builder builder(A other) {
return new Builder(other);
}

public static class Builder {

public Builder() { }
Expand All @@ -43,8 +51,8 @@ public A build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("A");
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected AdaptTemplatedNestedTestStruct() {
this.adaptedStruct = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(AdaptTemplatedNestedTestStruct other) {
return new Builder(other);
}

public static class Builder {
private test.fixtures.adapter.AdaptTemplatedTestStruct adaptedStruct = null;

Expand All @@ -59,8 +67,8 @@ public AdaptTemplatedNestedTestStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("AdaptTemplatedNestedTestStruct");
private final test.fixtures.adapter.AdaptTemplatedTestStruct adaptedStruct;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ protected AdaptTemplatedTestStruct() {
this.doubleTypedefBool = false;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(AdaptTemplatedTestStruct other) {
return new Builder(other);
}

public static class Builder {
private boolean adaptedBool = false;
private byte adaptedByte = 0;
Expand Down Expand Up @@ -365,8 +373,8 @@ public AdaptTemplatedTestStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("AdaptTemplatedTestStruct");
private final boolean adaptedBool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ protected AdaptTestStruct() {
this.binaryData = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(AdaptTestStruct other) {
return new Builder(other);
}

public static class Builder {
private long delay = 0L;
private byte[] custom = null;
Expand Down Expand Up @@ -185,8 +193,8 @@ public AdaptTestStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("AdaptTestStruct");
private final long delay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected AdaptedStruct() {
this.data = 0L;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(AdaptedStruct other) {
return new Builder(other);
}

public static class Builder {
private long data = 0L;

Expand All @@ -59,8 +67,8 @@ public AdaptedStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("AdaptedStruct");
private final long data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected AlsoMoveOnly() {
this.ptr = 0L;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(AlsoMoveOnly other) {
return new Builder(other);
}

public static class Builder {
private long ptr = 0L;

Expand All @@ -59,8 +67,8 @@ public AlsoMoveOnly build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("AlsoMoveOnly");
private final long ptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public ApplyAdapter(



public static Builder builder() {
return new Builder();
}

public static Builder builder(ApplyAdapter other) {
return new Builder(other);
}

public static class Builder {

public Builder() { }
Expand All @@ -43,8 +51,8 @@ public ApplyAdapter build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("ApplyAdapter");
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected B() {
this.a = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(B other) {
return new Builder(other);
}

public static class Builder {
private test.fixtures.adapter.A a = null;

Expand All @@ -59,8 +67,8 @@ public B build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("B");
private final test.fixtures.adapter.A a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ protected Bar() {
this.adaptedStructField = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(Bar other) {
return new Builder(other);
}

public static class Builder {
private test.fixtures.adapter.Foo structField = null;
private test.fixtures.adapter.Foo optionalStructField = null;
Expand Down Expand Up @@ -143,8 +151,8 @@ public Bar build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("Bar");
private final test.fixtures.adapter.Foo structField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected CircularAdaptee() {
this.field = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(CircularAdaptee other) {
return new Builder(other);
}

public static class Builder {
private test.fixtures.adapter.CircularStruct field = null;

Expand All @@ -59,8 +67,8 @@ public CircularAdaptee build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("CircularAdaptee");
private final test.fixtures.adapter.CircularStruct field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected CircularStruct() {
this.field = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(CircularStruct other) {
return new Builder(other);
}

public static class Builder {
private test.fixtures.adapter.CircularAdaptee field = null;

Expand All @@ -59,8 +67,8 @@ public CircularStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("CircularStruct");
private final test.fixtures.adapter.CircularAdaptee field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ protected Config() {
this.path = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(Config other) {
return new Builder(other);
}

public static class Builder {
private String path = null;

Expand All @@ -59,8 +67,8 @@ public Config build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("Config");
private final String path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ protected CountingStruct() {
this.regularString = null;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(CountingStruct other) {
return new Builder(other);
}

public static class Builder {
private Long regularInt = null;
private Long countingInt = null;
Expand Down Expand Up @@ -87,8 +95,8 @@ public CountingStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("CountingStruct");
private final Long regularInt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public DeclaredAfterStruct(



public static Builder builder() {
return new Builder();
}

public static Builder builder(DeclaredAfterStruct other) {
return new Builder(other);
}

public static class Builder {

public Builder() { }
Expand All @@ -43,8 +51,8 @@ public DeclaredAfterStruct build() {
}
}

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<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<>();
private static final TStruct STRUCT_DESC = new TStruct("DeclaredAfterStruct");
static {
Expand Down
Loading

0 comments on commit b82d7d7

Please sign in to comment.