Class AnyHandler

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

public class AnyHandler extends Object
Utility class for handling google.protobuf.Any messages. Provides packing, unpacking, and type URL management for Any fields.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new AnyHandler with an empty descriptor registry.
    AnyHandler(DescriptorRegistry descriptorRegistry)
    Creates a new AnyHandler with the specified descriptor registry.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    boolean
    is(com.google.protobuf.Any any, com.google.protobuf.Descriptors.Descriptor descriptor)
    Checks if an Any message contains a specific type.
    boolean
    is(com.google.protobuf.Any any, String fullTypeName)
    Checks if an Any message contains a specific type by full name.
    com.google.protobuf.Any
    pack(com.google.protobuf.Message message)
    Packs a message into a google.protobuf.Any.
    com.google.protobuf.Any
    packStruct(com.google.protobuf.Struct struct, String typeName)
    Packs a Struct into an Any with a specified type name.
    com.google.protobuf.Message
    unpack(com.google.protobuf.Any any)
    Unpacks an Any message to a concrete message type.
    com.google.protobuf.Message
    unpackSafe(com.google.protobuf.Any any)
    Safely unpacks an Any, returning null if unpacking fails.
    com.google.protobuf.DynamicMessage
    unpackToDynamic(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.Struct
    unpackToStruct(com.google.protobuf.Any any)
    Unpacks an Any to a Struct representation.

    Methods inherited from class java.lang.Object

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

    • AnyHandler

      public AnyHandler(DescriptorRegistry descriptorRegistry)
      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 unpack
      descriptor - 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 check
      descriptor - The descriptor of the expected type
      Returns:
      true if the Any contains the specified type
    • is

      public boolean is(com.google.protobuf.Any any, String fullTypeName)
      Checks if an Any message contains a specific type by full name.
      Parameters:
      any - The Any message to check
      fullTypeName - The full type name (e.g., "ai.pipestream.data.v1.SearchMetadata")
      Returns:
      true if the Any contains the specified type
    • getTypeName

      public String getTypeName(com.google.protobuf.Any any)
      Gets the type name from an Any message.
      Parameters:
      any - The Any message
      Returns:
      The full type name
    • createTypeUrl

      public String createTypeUrl(String typeName)
      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

      public com.google.protobuf.Any packStruct(com.google.protobuf.Struct struct, String typeName)
      Packs a Struct into an Any with a specified type name. The Struct is first converted to a DynamicMessage.
      Parameters:
      struct - The Struct to pack
      typeName - 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

      public DescriptorRegistry getDescriptorRegistry()
      Gets the descriptor registry used by this handler.
      Returns:
      The descriptor registry