Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Java Protobuf Selector and Mapper?

45 views
Skip to first unread message

Kristian Rickert

unread,
Apr 23, 2025, 7:56:14 AMApr 23
to Protocol Buffers

Hi everyone,

I've created a Java utility for mapping between Protobuf messages using simple rules and selecting nested data using string paths (like "a.b.c", map["key"], struct.key).

What it does:

  • Selector: Retrieve data using a path string (e.g., valueHandler.getValue(msg, "profile.address[\"home\"].zip_code")). Handles nested fields, maps, structs, and unwraps google.protobuf.Value.
  • Mapper: Map between different proto messages using rules like:
    Java
    List<String> rules = Arrays.asList(
        "target_field = source.field",
        "target_list += source_item", // Append
        "target_map[\"key\"] = source_value", // Map put
        "-field_to_delete" // Delete
    );
    Message target = protoMapper.map(sourceMsg, Target.getDescriptor(), rules);

Why? I initially built this for a pipeline mapping step to reduce boilerplate code. It ended up handling quite a few edge cases around type conversion, path resolution, literals, and dynamic message handling.

Question: Would a utility like this be useful to others? Are there established Java libraries that provide this specific rule-based mapping and deep path selection beyond standard reflection or FieldMask projections?

It started as a quick selector but grew more complex. I'm considering making it a standalone library if there's interest.

The current code (WIP) is here: https://github.com/krickert/micronaut-multiproject-example/tree/main/protobuf-models/src/main/java/com/krickert/search/model/mapper

Thanks for any feedback!

Gary Peck

unread,
Apr 24, 2025, 11:15:49 AMApr 24
to Protocol Buffers
It reminds me of CEL (https://cel.dev/), which is used by the protovalidate project (https://github.com/bufbuild/protovalidate) but can also be used standalone to accomplish some of the things you're doing.

Gary

Reply all
Reply to author
Forward
0 new messages