Class ProtoFieldMapperImpl

java.lang.Object
ai.pipestream.common.util.ProtoFieldMapperImpl
All Implemented Interfaces:
ProtoFieldMapper

@ApplicationScoped public class ProtoFieldMapperImpl extends Object implements ProtoFieldMapper
A streamlined utility for dynamically mapping fields between Protocol Buffer messages.

This class refactors the original concept of a multi-component mapping system (parser, resolver, executor) into a more consolidated design. The core logic is encapsulated within the `FieldAccessor`, which handles both reading and writing field values based on a dot-notation path, similar to a simplified object property selector.

Features:

  • Handles literal value assignments (strings, booleans, numbers, null).
  • Correctly reads from and writes to `google.protobuf.Struct` fields, handling type conversions from DynamicMessage.
  • Supports `google.protobuf.Any` field packing and unpacking.
  • Provides type conversion between different protobuf types.
  • Manages proto descriptors for dynamic message handling.
  • Provides precise error messages for invalid paths.
  • Constructor Details

    • ProtoFieldMapperImpl

      public ProtoFieldMapperImpl()
      Default constructor for ProtoFieldMapper. Creates a new instance with a RuleParser and FieldAccessor.
    • ProtoFieldMapperImpl

      public ProtoFieldMapperImpl(DescriptorRegistry descriptorRegistry)
      Constructor with custom descriptor registry. Allows pre-registering descriptors for Any field handling.
      Parameters:
      descriptorRegistry - The descriptor registry to use
  • Method Details

    • getDescriptorRegistry

      public DescriptorRegistry getDescriptorRegistry()
      Gets the descriptor registry for this mapper. Use this to register descriptors for Any field handling.
      Specified by:
      getDescriptorRegistry in interface ProtoFieldMapper
      Returns:
      The descriptor registry
    • getAnyHandler

      public AnyHandler getAnyHandler()
      Gets the AnyHandler for this mapper. Use this for manual Any field operations.
      Specified by:
      getAnyHandler in interface ProtoFieldMapper
      Returns:
      The AnyHandler
    • withAutoLoad

      public static ProtoFieldMapper withAutoLoad()
      Creates a ProtoFieldMapper with auto-loading enabled. This will automatically load descriptors from the grpc-google-descriptor module if available on the classpath.
      Returns:
      A new ProtoFieldMapper with auto-loaded descriptors
    • map

      public void map(com.google.protobuf.Message source, com.google.protobuf.Message.Builder targetBuilder, List<String> rules) throws ProtoFieldMapperImpl.MappingException
      Maps fields from a source message to a target message builder based on a list of rules.
      Specified by:
      map in interface ProtoFieldMapper
      Parameters:
      source - The source protobuf message to read values from
      targetBuilder - The target message builder to write values to
      rules - List of mapping rules in string format
      Throws:
      ProtoFieldMapperImpl.MappingException - If any rule cannot be applied due to invalid paths or type mismatches