Class MoreStructs

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

@CheckReturnValue @RequiresProtobuf public final class MoreStructs extends Object
Additional utilities to help create Struct messages.

The struct(name, value) helpers can be used to create Struct conveniently, for example: struct("age", 10).

To build Struct with more than one fields, use StructBuilder; or use toStruct() to collect from a BiStream.

If you have complex nested data structures such as Multimap<String, Optional<Integer>>, consider to use Structor, which translates POJO to Struct for common primitive types and collection types.

Since:
5.8
  • Method Summary

    Modifier and Type
    Method
    Description
    static Map<String,Object>
    asMap(com.google.protobuf.Struct struct)
    Returns a Map<String, Object> view over struct.
    static Collector<com.google.protobuf.Struct,?,com.google.protobuf.Struct>
    Returns a Collector that flattens all fields from the input Structs and collects them into the final Struct.
    static com.google.protobuf.Struct
    struct(String name, boolean value)
    Returns a Struct with name and value.
    static com.google.protobuf.Struct
    struct(String name, double value)
    Returns a Struct with name and value.
    static com.google.protobuf.Struct
    struct(String name, com.google.protobuf.ListValue value)
    Returns a Struct with name and value.
    static com.google.protobuf.Struct
    struct(String name, com.google.protobuf.Struct value)
    Returns a Struct with name and value.
    static com.google.protobuf.Struct
    struct(String name, com.google.protobuf.Value value)
    Returns a Struct with name and value.
    static com.google.protobuf.Struct
    struct(String name, String value)
    Returns a Struct with name and value.
    static BiCollector<CharSequence,com.google.protobuf.Value,com.google.protobuf.Struct>
    Returns a BiCollector that collects the input key-value pairs into Struct.
    static <T> Collector<T,?,com.google.protobuf.Struct>
    toStruct(Function<? super T,? extends CharSequence> keyFunction, Function<? super T,com.google.protobuf.Value> valueFunction)
    Returns a Collector that collects input key-value pairs into Struct.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • struct

      public static com.google.protobuf.Struct struct(String name, boolean value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name is null
    • struct

      public static com.google.protobuf.Struct struct(String name, double value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name is null
    • struct

      public static com.google.protobuf.Struct struct(String name, String value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name or value is null
    • struct

      public static com.google.protobuf.Struct struct(String name, com.google.protobuf.Struct value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name or value is null
    • struct

      public static com.google.protobuf.Struct struct(String name, com.google.protobuf.Value value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name or value is null
    • struct

      public static com.google.protobuf.Struct struct(String name, com.google.protobuf.ListValue value)
      Returns a Struct with name and value.
      Throws:
      NullPointerException - if name or value is null
    • toStruct

      public static <T> Collector<T,?,com.google.protobuf.Struct> toStruct(Function<? super T,? extends CharSequence> keyFunction, Function<? super T,com.google.protobuf.Value> valueFunction)
      Returns a Collector that collects input key-value pairs into Struct.

      Duplicate keys (according to CharSequence.toString()) are not allowed.

    • toStruct

      public static BiCollector<CharSequence,com.google.protobuf.Value,com.google.protobuf.Struct> toStruct()
      Returns a BiCollector that collects the input key-value pairs into Struct.

      Duplicate keys (according to CharSequence.toString()) are not allowed.

    • flatteningToStruct

      public static Collector<com.google.protobuf.Struct,?,com.google.protobuf.Struct> flatteningToStruct()
      Returns a Collector that flattens all fields from the input Structs and collects them into the final Struct.

      Duplicate field keys are not allowed.

    • asMap

      public static Map<String,Object> asMap(com.google.protobuf.Struct struct)
      Returns a Map<String, Object> view over struct.

      Value wrappers are unwrapped using MoreValues.fromValue(com.google.protobuf.ValueOrBuilder), such that Values.of(1) is unwrapped to 1L, ListValue is unwrapped as List<Object>, and NullValue is unwrapped as null, etc.

      Field encounter order is preserved in the result Map.

      Since:
      5.9