Skip to content

feat: handle multiple occurrences of the same data fields#400

Open
EngMahmut wants to merge 1 commit intomoov-io:masterfrom
EngMahmut:master
Open

feat: handle multiple occurrences of the same data fields#400
EngMahmut wants to merge 1 commit intomoov-io:masterfrom
EngMahmut:master

Conversation

@EngMahmut
Copy link

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!

@EngMahmut EngMahmut requested a review from alovak as a code owner January 15, 2026 14:56
@alovak
Copy link
Contributor

alovak commented Jan 16, 2026

@EngMahmut thank you for the PR! I'll take a look soon.

@alovak
Copy link
Contributor

alovak commented Jan 16, 2026

@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 getSubfields() and then check if there are "repeats" for some fields or add fields with indexes like 1a_1 to data structs?

@EngMahmut
Copy link
Author

With the updated implementation, repeated subfields inside composite fields are handled automatically by the library.
When unpacking, if the same tag appears multiple times, it is stored using a unique internal key format like 1a, 1a_1, 1a_2, etc., so no values are overwritten.

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.

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.

2 participants