Class StructBuilder

java.lang.Object
com.google.mu.protobuf.util.StructBuilder

@RequiresProtobuf public final class StructBuilder extends Object
A builder that supports building heterogeneous Struct more conveniently, while eliding most of the intermediary and verbose Value creation.

Unlike Struct.Builder, the add() methods will throw upon duplicate keys.

Since:
5.8
  • Constructor Details

    • StructBuilder

      public StructBuilder()
  • Method Details

    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, boolean value)
      Adds a (name, value) field.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, double value)
      Adds a (name, value) field.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, String value)
      Adds a (name, value) field.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, com.google.protobuf.ListValue value)
      Adds a (name, value) field.

      See MoreValues for helpers that create common ListValue conveniently.

      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, Iterable<com.google.protobuf.Value> values)
      Adds a (name, values) field, with values wrapped in ListValue.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, Map<String,com.google.protobuf.Value> map)
      Adds a (name, map) field. map is converted to a nested Struct.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, com.google.protobuf.Struct struct)
      Adds a (name, struct) field.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, StructBuilder struct)
      Adds a (name, struct) field.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • add

      @CanIgnoreReturnValue public StructBuilder add(String name, com.google.protobuf.Value value)
      Adds a (name, value) field.

      To add a null value, use MoreValues.NULL as in add("name", NULL).

      Returns:
      this builder
      Throws:
      IllegalArgumentException - if name is duplicate
    • addAll

      @CanIgnoreReturnValue public StructBuilder addAll(Map<String,com.google.protobuf.Value> map)
      Adds all key-value pairs from map into this builder.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if any key is duplicate
    • addAll

      @CanIgnoreReturnValue public StructBuilder addAll(com.google.common.collect.Multimap<String,com.google.protobuf.Value> multimap)
      Adds all distinct keys from multimap into this builder. Values mapping to the same key are grouped together in ListValue.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if any key is duplicate
    • addAll

      @CanIgnoreReturnValue public StructBuilder addAll(com.google.common.collect.Table<String,String,com.google.protobuf.Value> table)
      Adds all rows from table into this builder. Columns of each row are grouped together in Struct, keyed by column name.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if any row key is duplicate
    • addAllFields

      @CanIgnoreReturnValue public StructBuilder addAllFields(com.google.protobuf.Struct that)
      Adds all fields from that into this builder.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if duplicate field name is encountered
    • addAllFields

      @CanIgnoreReturnValue public StructBuilder addAllFields(StructBuilder that)
      Adds all fields from that into this builder.
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if duplicate field name is encountered
    • build

      public com.google.protobuf.Struct build()
      Returns a new Struct instance with all added fields.
    • toString

      public String toString()
      Overrides:
      toString in class Object