Skip to content

Conversation

@eleenmohd-wk
Copy link
Contributor

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.

raduionita-wk and others added 21 commits October 9, 2025 16:18
Add Hook Interface for Customizable XSD Parsing and Code Generation
- 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
Copy link
Contributor Author

@xuri Im not sure why you're not getting assigned as a reviewer to my PR. I tried to close it and reopen it, but it's still not working. Do you think you can take a look at these changes?

@joshprzybyszewski-wf
Copy link

@xuri friendly bump: would you be able to take a look at this PR?

@xuri xuri added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 15, 2025
Copy link
Owner

@xuri xuri left a comment

Choose a reason for hiding this comment

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

Great work! Thanks for your contribution.

@xuri xuri merged commit 663f25f into xuri:master Nov 17, 2025
11 checks passed
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.

4 participants