Skip to content

refactor: modularize dict_to_protobuf and simplify field mapping to reduce complexity - #191

Open
bhagathkrishnacdac wants to merge 1 commit into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-prototodict
Open

refactor: modularize dict_to_protobuf and simplify field mapping to reduce complexity#191
bhagathkrishnacdac wants to merge 1 commit into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-prototodict

Conversation

@bhagathkrishnacdac

Copy link
Copy Markdown

Description
This PR refactors _get_field_mapping and _dict_to_protobuf in pybess/protobuf_to_dict.py to address code complexity and improve readability. The monolithic, highly nested structures have been decomposed into smaller, single-responsibility helper functions.

Key Changes

  • Decomposed Field Mapping (_get_field_mapping):
    • Extracted _process_regular_fields to isolate standard key-value conversions.
    • Extracted _process_extension_fields to isolate protobuf extensions mapping.
    • Fixed an exception-formatting bug from the original codebase where the incorrect variable (key instead of ext_num) was referenced during a strict KeyError.
  • Modularized Deserialization (_dict_to_protobuf):
    • Replaced deeply nested conditional trees with structured execution handlers (_handle_repeated_field, _handle_message_field, _handle_extension_field) that return boolean flags to coordinate loop progression.
    • Extracted dedicated handlers for map-fields (_handle_map_field) and array-fields (_handle_regular_repeated_field).
    • Preserved the exact original evaluation order: running type-callable mapping before checking extension assignments, and enum conversions after.
    • Cleaned up and removed the unused _convert_field_value helper to prevent dead code.

…educe complexity

Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
@bhagathkrishnacdac
bhagathkrishnacdac requested a review from a team August 2, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant