Class TypeConverter

java.lang.Object
ai.pipestream.common.util.TypeConverter

public class TypeConverter extends Object
Utility class for converting between different Protocol Buffer types. Handles conversions between messages, primitives, Struct, Value, and common Java types.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    convertToFieldType(Object value, com.google.protobuf.Descriptors.FieldDescriptor field)
    Attempts to convert a value to match the expected type of a field.
    fromValue(com.google.protobuf.Value value)
    Converts a protobuf Value to a Java object.
    com.google.protobuf.Struct
    messageToStruct(com.google.protobuf.Message message)
    Converts a protobuf Message to a Struct.
    com.google.protobuf.DynamicMessage
    structToMessage(com.google.protobuf.Struct struct, com.google.protobuf.Descriptors.Descriptor descriptor)
    Converts a Struct to a DynamicMessage of the specified type.
    com.google.protobuf.Value
    toValue(Object value)
    Converts a Java object to a protobuf Value.

    Methods inherited from class java.lang.Object

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

    • TypeConverter

      public TypeConverter()
  • Method Details

    • toValue

      public com.google.protobuf.Value toValue(Object value)
      Converts a Java object to a protobuf Value. Supports null, String, Number, Boolean, Struct, List, and Message types.
      Parameters:
      value - The value to convert
      Returns:
      A protobuf Value representation
      Throws:
      IllegalArgumentException - if the type cannot be converted
    • fromValue

      public Object fromValue(com.google.protobuf.Value value)
      Converts a protobuf Value to a Java object.
      Parameters:
      value - The Value to convert
      Returns:
      The unwrapped Java object (String, Double, Boolean, Struct, List, or null)
    • messageToStruct

      public com.google.protobuf.Struct messageToStruct(com.google.protobuf.Message message)
      Converts a protobuf Message to a Struct. Recursively converts all fields to Value representations.
      Parameters:
      message - The message to convert
      Returns:
      A Struct representation of the message
    • structToMessage

      public com.google.protobuf.DynamicMessage structToMessage(com.google.protobuf.Struct struct, com.google.protobuf.Descriptors.Descriptor descriptor)
      Converts a Struct to a DynamicMessage of the specified type.
      Parameters:
      struct - The Struct to convert
      descriptor - The descriptor of the target message type
      Returns:
      A DynamicMessage built from the Struct
      Throws:
      IllegalArgumentException - if conversion fails
    • convertToFieldType

      public Object convertToFieldType(Object value, com.google.protobuf.Descriptors.FieldDescriptor field)
      Attempts to convert a value to match the expected type of a field. Provides intelligent type coercion where possible.
      Parameters:
      value - The value to convert
      field - The target field descriptor
      Returns:
      The converted value, or the original if no conversion is needed
      Throws:
      IllegalArgumentException - if conversion is not possible