Skip to content

Add Hook Interface for Customizable XSD Parsing and Code Generation #112

Closed
eleenmohd-wk wants to merge 21 commits intoxuri:masterfrom
raduionita-wk:hook-feature
Closed

Add Hook Interface for Customizable XSD Parsing and Code Generation #112
eleenmohd-wk wants to merge 21 commits intoxuri:masterfrom
raduionita-wk:hook-feature

Conversation

@eleenmohd-wk
Copy link
Contributor

@eleenmohd-wk eleenmohd-wk commented Oct 28, 2025

PR Details

This PR introduces am optional plugin system via the Hook interface that allows users to customize the XSD parsing and code generation process without modifying xgen's core logic.

There are 5 interception points:

  • OnStartElement: Intercept XML start elements during parsing
  • OnEndElement: Intercept XML end elements during parsing
  • OnCharData: Intercept character data/text content during parsing
  • OnGenerate: Intercept code generation before each type is processed
  • OnAddContent: Post-process generated code content

During XML parsing (parsing.go), the hook intercepts the XML token stream before xgen processes it. Each hook method returns a next boolean - if false, xgen skips that element, allowing custom filtering or preprocessing.

The hook intercepts code generation (genGo.go) at two points:

  1. Before generating code for each type (OnGenerate): Can skip generating specific types by returning false
  2. After generating each code block (OnAddContent): Receives a pointer to the generated code string. Can modify, enhance, or replace the generated code

Description

Adds Parser.Hooks map[string]Hook for handling custom logic on specific elements

Related Issue

Motivation and Context

expands functionality w/o major changes to the core code. the hook is optional and all existing code continues to work without modification so there's no changes in the existing functionality

How Has This Been Tested

unit tests

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codecov
Copy link

codecov bot commented Oct 28, 2025

Codecov Report

❌ Patch coverage is 89.23077% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.29%. Comparing base (2cb22a3) to head (4cf8ad1).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
parser.go 78.26% 3 Missing and 2 partials ⚠️
genGo.go 95.23% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #112      +/-   ##
==========================================
+ Coverage   81.92%   84.29%   +2.36%     
==========================================
  Files          37       37              
  Lines        1870     1636     -234     
==========================================
- Hits         1532     1379     -153     
+ Misses        246      163      -83     
- Partials       92       94       +2     
Flag Coverage Δ
unittests 84.29% <89.23%> (+2.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@xuri xuri added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 29, 2025
- Add TestOnAddContentCoverage to test OnAddContent calls across different type generators
- Add TestOnAddContentForAllTypes to verify hook integration with existing fixtures
- Create hook-coverage.xsd fixture to exercise SimpleType (list/union/basic), ComplexType, Group, and AttributeGroup generators
- These tests cover lines 162, 190, 203, 282, 323, and 351 in genGo.go
- Ensures all OnAddContent hook calls are properly tested and can modify generated code
@eleenmohd-wk eleenmohd-wk marked this pull request as draft October 31, 2025 19:31
@eleenmohd-wk eleenmohd-wk marked this pull request as ready for review October 31, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants