Skip to content

Conversation

@bukzor
Copy link
Contributor

@bukzor bukzor commented Oct 22, 2025

PR #162 revealed that missing node type handlers in switch statements
cause silent data loss - CDATA content was being dropped because
CharDataNode wasn't handled. This PR adds defensive programming to
prevent this entire class of bug.

Changes:

  • Added exhaustive switch coverage for all xmlquery.NodeType values
  • Added exhaustive switch coverage for xml.Token and html.TokenType
  • Added exhaustive switch coverage for json.Token and json.Delim
  • Added default cases with panic() to all exhaustive switches
  • Added explicit documented defaults to intentionally non-exhaustive switches

This follows the Go stdlib pattern (runtime/panic.go, go/types) of using
panic("unreachable") for "should be impossible" states. If future xmlquery
versions add new node types, or if we overlook a handler, tests will fail
immediately rather than silently corrupting output.

All switches now explicitly handle defaults:

  • 8 exhaustive switches panic on unknown values
  • 7 non-exhaustive switches have documented intentional behavior

Added TestExhaustiveNodeTypeHandling to verify all node types are
processed without panicking. All existing tests pass.

This is a non-breaking change - no API modifications, only defensive
additions to switch statements.

Related: #162

PR sibprogrammer#162 revealed that missing node type handlers in switch statements
cause silent data loss - CDATA content was being dropped because
CharDataNode wasn't handled. This PR adds defensive programming to
prevent this entire class of bug.

Changes:
- Added exhaustive switch coverage for all xmlquery.NodeType values
- Added exhaustive switch coverage for xml.Token and html.TokenType
- Added exhaustive switch coverage for json.Token and json.Delim
- Added default cases with panic() to all exhaustive switches
- Added explicit documented defaults to intentionally non-exhaustive switches

This follows the Go stdlib pattern (runtime/panic.go, go/types) of using
panic("unreachable") for "should be impossible" states. If future xmlquery
versions add new node types, or if we overlook a handler, tests will fail
immediately rather than silently corrupting output.

All switches now explicitly handle defaults:
- 8 exhaustive switches panic on unknown values
- 7 non-exhaustive switches have documented intentional behavior

Added TestExhaustiveNodeTypeHandling to verify all node types are
processed without panicking. All existing tests pass.

This is a non-breaking change - no API modifications, only defensive
additions to switch statements.

Related: sibprogrammer#162
@bukzor bukzor marked this pull request as ready for review October 22, 2025 18:15
@bukzor bukzor force-pushed the fix-160-exhaustive-switches branch from 93ecda9 to a780997 Compare October 22, 2025 20:57
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.

1 participant