Package ai.pipestream.common.util
Class AnyHandler
java.lang.Object
ai.pipestream.common.util.AnyHandler
Utility class for handling google.protobuf.Any messages.
Provides packing, unpacking, and type URL management for Any fields.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new AnyHandler with an empty descriptor registry.AnyHandler(DescriptorRegistry descriptorRegistry) Creates a new AnyHandler with the specified descriptor registry. -
Method Summary
Modifier and TypeMethodDescriptioncreateTypeUrl(String typeName) Creates a type URL from a type name.Gets the descriptor registry used by this handler.getTypeName(com.google.protobuf.Any any) Gets the type name from an Any message.booleanis(com.google.protobuf.Any any, com.google.protobuf.Descriptors.Descriptor descriptor) Checks if an Any message contains a specific type.booleanChecks if an Any message contains a specific type by full name.com.google.protobuf.Anypack(com.google.protobuf.Message message) Packs a message into a google.protobuf.Any.com.google.protobuf.AnypackStruct(com.google.protobuf.Struct struct, String typeName) Packs a Struct into an Any with a specified type name.com.google.protobuf.Messageunpack(com.google.protobuf.Any any) Unpacks an Any message to a concrete message type.com.google.protobuf.MessageunpackSafe(com.google.protobuf.Any any) Safely unpacks an Any, returning null if unpacking fails.com.google.protobuf.DynamicMessageunpackToDynamic(com.google.protobuf.Any any, com.google.protobuf.Descriptors.Descriptor descriptor) Unpacks an Any message to a DynamicMessage using the provided descriptor.com.google.protobuf.StructunpackToStruct(com.google.protobuf.Any any) Unpacks an Any to a Struct representation.
-
Constructor Details
-
AnyHandler
Creates a new AnyHandler with the specified descriptor registry.- Parameters:
descriptorRegistry- Registry for looking up message descriptors
-
AnyHandler
public AnyHandler()Creates a new AnyHandler with an empty descriptor registry.
-
-
Method Details
-
pack
public com.google.protobuf.Any pack(com.google.protobuf.Message message) Packs a message into a google.protobuf.Any.- Parameters:
message- The message to pack- Returns:
- An Any containing the packed message
-
unpack
public com.google.protobuf.Message unpack(com.google.protobuf.Any any) throws com.google.protobuf.InvalidProtocolBufferException Unpacks an Any message to a concrete message type. Uses the descriptor registry to resolve the type.- Parameters:
any- The Any message to unpack- Returns:
- The unpacked message
- Throws:
com.google.protobuf.InvalidProtocolBufferException- if unpacking fails
-
unpackToDynamic
public com.google.protobuf.DynamicMessage unpackToDynamic(com.google.protobuf.Any any, com.google.protobuf.Descriptors.Descriptor descriptor) throws com.google.protobuf.InvalidProtocolBufferException Unpacks an Any message to a DynamicMessage using the provided descriptor.- Parameters:
any- The Any message to unpackdescriptor- The descriptor to use for unpacking- Returns:
- The unpacked DynamicMessage
- Throws:
com.google.protobuf.InvalidProtocolBufferException- if unpacking fails
-
is
public boolean is(com.google.protobuf.Any any, com.google.protobuf.Descriptors.Descriptor descriptor) Checks if an Any message contains a specific type.- Parameters:
any- The Any message to checkdescriptor- The descriptor of the expected type- Returns:
- true if the Any contains the specified type
-
is
Checks if an Any message contains a specific type by full name.- Parameters:
any- The Any message to checkfullTypeName- The full type name (e.g., "ai.pipestream.data.v1.SearchMetadata")- Returns:
- true if the Any contains the specified type
-
getTypeName
Gets the type name from an Any message.- Parameters:
any- The Any message- Returns:
- The full type name
-
createTypeUrl
Creates a type URL from a type name.- Parameters:
typeName- The full type name- Returns:
- The type URL
-
unpackToStruct
public com.google.protobuf.Struct unpackToStruct(com.google.protobuf.Any any) throws com.google.protobuf.InvalidProtocolBufferException Unpacks an Any to a Struct representation. This is useful for accessing Any content in a generic way.- Parameters:
any- The Any message to unpack- Returns:
- A Struct representation of the message
- Throws:
com.google.protobuf.InvalidProtocolBufferException- if unpacking fails
-
packStruct
Packs a Struct into an Any with a specified type name. The Struct is first converted to a DynamicMessage.- Parameters:
struct- The Struct to packtypeName- The full type name for the Any- Returns:
- An Any containing the packed message
- Throws:
IllegalArgumentException- if the descriptor is not found
-
unpackSafe
public com.google.protobuf.Message unpackSafe(com.google.protobuf.Any any) Safely unpacks an Any, returning null if unpacking fails.- Parameters:
any- The Any message to unpack- Returns:
- The unpacked message, or null if unpacking fails
-
getDescriptorRegistry
Gets the descriptor registry used by this handler.- Returns:
- The descriptor registry
-