Package ai.pipestream.common.util
Class TypeConverter
java.lang.Object
ai.pipestream.common.util.TypeConverter
Utility class for converting between different Protocol Buffer types.
Handles conversions between messages, primitives, Struct, Value, and common Java types.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconvertToFieldType(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.StructmessageToStruct(com.google.protobuf.Message message) Converts a protobuf Message to a Struct.com.google.protobuf.DynamicMessagestructToMessage(com.google.protobuf.Struct struct, com.google.protobuf.Descriptors.Descriptor descriptor) Converts a Struct to a DynamicMessage of the specified type.com.google.protobuf.ValueConverts a Java object to a protobuf Value.
-
Constructor Details
-
TypeConverter
public TypeConverter()
-
-
Method Details
-
toValue
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
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 convertdescriptor- 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 convertfield- The target field descriptor- Returns:
- The converted value, or the original if no conversion is needed
- Throws:
IllegalArgumentException- if conversion is not possible
-