Skip to content

Fix JSON struct tag parsing to handle modifiers#7

Open
krismcfarlin wants to merge 2 commits intoalpkeskin:mainfrom
krismcfarlin:fix-json-tags
Open

Fix JSON struct tag parsing to handle modifiers#7
krismcfarlin wants to merge 2 commits intoalpkeskin:mainfrom
krismcfarlin:fix-json-tags

Conversation

@krismcfarlin
Copy link
Copy Markdown

Fixes parsing of JSON struct tags to properly extract field names before comma-separated modifiers like ,omitempty and ,inline.

This allows TOON to correctly handle Go structs with:

  • Inline embedding (json:\",inline\")
  • Optional fields (json:\"fieldname,omitempty\")

Changes:

  • Added strings import to normalize.go
  • Parse JSON tags to extract field name before comma
  • Handle inline embedding by merging nested struct fields into parent object

Problem:
The current implementation reads the entire JSON tag value including modifiers as the field name. For example:

  • json:\",inline\" was being used as field name \",inline\"
  • json:\"field,omitempty\" was being used as field name \"field,omitempty\"

Solution:
Split the tag on comma and use only the portion before the first comma as the field name. Special handling for inline fields (empty name) to merge nested struct contents into parent.

Testing:
Tested with structs containing various tag modifiers and confirmed proper field name extraction and inline embedding behavior.

- Skip fields starting with underscore during struct normalization
- Reduces output size by 75% (517K -> 127K chars in test case)
- Filters internal database fields (_id, _createdDate, _email, _updatedDate)
- Aligns with common convention of underscore prefix for private/internal fields
Copy link
Copy Markdown

@eakeur eakeur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Saving lives!

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