feat: handle multiple occurrences of the same data fields#400
feat: handle multiple occurrences of the same data fields#400EngMahmut wants to merge 1 commit intomoov-io:masterfrom
Conversation
|
@EngMahmut thank you for the PR! I'll take a look soon. |
|
@EngMahmut how do you see packing composite having repeated data? Could you please share the code to work with repeated fields? Is the plan to |
|
With the updated implementation, repeated subfields inside composite fields are handled automatically by the library. Consumers can retrieve all subfields via getSubfields() and iterate over them. There is no need to manually define indexed fields in the data structs. When packing, the suffixes are removed and the base tag is used repeatedly in the correct order, ensuring the final ISO message remains compliant with the specification. |
Bug Fix Summary
Problem
When a composite field contained the same tag (e.g., "1a") appearing multiple times, only the last occurrence was kept - previous values were overwritten and data was lost.
Example:
Input: Tag "1a" with value "ABC"
Tag "1a" with value "DEF"
Tag "1a" with value "GHI"
Before Fix:
subfields["1a"] = "GHI" (only last value, data loss!)
After Fix:
subfields["1a"] = "ABC" (first occurrence)
subfields["1a_1"] = "DEF" (second occurrence with suffix)
subfields["1a_2"] = "GHI" (third occurrence with suffix)
Solution
Added two helper functions and updated the unpacking/packing logic in composite.go:
getUniqueFieldKey(tag) - When unpacking duplicate tags, generates unique keys with numeric suffixes:
First occurrence: "1a"
Second occurrence: "1a_1"
Third occurrence: "1a_2", etc.
extractBaseTag(fieldKey) - When packing, removes the suffix to restore the original tag:
"1a" → "1a"
"1a_1" → "1a"
"1a_2" → "1a", etc.
Updated unpackSubfieldsByTag() - Uses getUniqueFieldKey() to preserve all duplicate occurrences
Updated packByTag() - Uses extractBaseTag() to restore original tag names when packing
Test Results
✅ All tests PASS:
single_occurrence - Basic functionality
multiple_tags_no_data_loss - Tag-based composites
pack_unpack_consistency - Round-trip encoding/decoding
same_tag_multiple_occurrences_bug_repro - BUG FIXED! 🎉
All 3 occurrences of tag "1a" are now correctly preserved with their distinct values!