Package ai.pipestream.common.util
Class ProtoFieldMapperImpl
java.lang.Object
ai.pipestream.common.util.ProtoFieldMapperImpl
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException thrown when a mapping rule cannot be applied. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor for ProtoFieldMapper.ProtoFieldMapperImpl(DescriptorRegistry descriptorRegistry) Constructor with custom descriptor registry. -
Method Summary
Modifier and TypeMethodDescriptionGets the AnyHandler for this mapper.Gets the descriptor registry for this mapper.voidmap(com.google.protobuf.Message source, com.google.protobuf.Message.Builder targetBuilder, List<String> rules) Maps fields from a source message to a target message builder based on a list of rules.static ProtoFieldMapperCreates a ProtoFieldMapper with auto-loading enabled.
-
Constructor Details
-
ProtoFieldMapperImpl
public ProtoFieldMapperImpl()Default constructor for ProtoFieldMapper. Creates a new instance with a RuleParser and FieldAccessor. -
ProtoFieldMapperImpl
Constructor with custom descriptor registry. Allows pre-registering descriptors for Any field handling.- Parameters:
descriptorRegistry- The descriptor registry to use
-
-
Method Details
-
getDescriptorRegistry
Gets the descriptor registry for this mapper. Use this to register descriptors for Any field handling.- Specified by:
getDescriptorRegistryin interfaceProtoFieldMapper- Returns:
- The descriptor registry
-
getAnyHandler
Gets the AnyHandler for this mapper. Use this for manual Any field operations.- Specified by:
getAnyHandlerin interfaceProtoFieldMapper- Returns:
- The AnyHandler
-
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:
mapin interfaceProtoFieldMapper- Parameters:
source- The source protobuf message to read values fromtargetBuilder- The target message builder to write values torules- List of mapping rules in string format- Throws:
ProtoFieldMapperImpl.MappingException- If any rule cannot be applied due to invalid paths or type mismatches
-