Skip to content

Constructing Builders with nullable structs #244

@jparise

Description

@jparise

As I convert more and more (Java) code from Apache Thrift to Thrifty, I've started to run into a pattern where I start with an existing struct object that might be null and then "extends" that struct by setting some existing values. The most direct way to do this is by conditionally calling the correct Builder constructor:

public Struct updateStruct(Struct struct) {
    Struct.Builder builder = (struct == null)
        ? new Struct.Builder()
        : new Struct.Builder(struct);

    builder.foo(True);

    return builder.build();
}

It would be much more convenient if Struct.Builder accepted a nullable argument and added a if (struct != null) guard to the generated code so the above could just become:

public Struct updateStruct(Struct struct) {
    return new Struct.Builder(struct)
        .foo(True)
        .build();
}

That would detract a bit from the purity of the existing Builder interface, but this could be a case where caller convenience could take precedence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions