Skip to content

Conversation

@luabagg
Copy link
Owner

@luabagg luabagg commented Nov 11, 2025

No description provided.

- Identified issues with type assertion-based strategy pattern
- Proposed 4 alternative design approaches to eliminate type assertions
- Recommended functional options pattern as the most idiomatic solution
- Included migration path and comparison matrix
- Addressed input type dispatch, error handling, and extensibility concerns
Implemented functional options pattern to provide compile-time type safety
and eliminate runtime type assertions throughout the package.

Major Changes:
- Added BaseHandler interface for non-generic handler operations
- Created HandlerBuilder with fluent API for handler construction
- Added PDF() and Screenshot() factory functions
- Implemented GenerateHTML() and GenerateURL() functions
- Deprecated Generate() and NewHandler() functions

Benefits:
- No runtime type assertions required
- Compile-time type safety
- Clearer, more explicit API
- Better IDE support and documentation
- Easier testing and mocking
- Follows idiomatic Go patterns

Backward Compatibility:
- All existing functions remain functional
- Deprecated functions marked with clear migration guidance
- No breaking changes for existing users

Documentation:
- Updated README with new API examples
- Added comprehensive migration guide
- Added new example functions demonstrating best practices
- Maintained existing examples for reference

Closes the architectural concerns outlined in ARCHITECTURE_ANALYSIS.md
This is a breaking change that establishes a clean, modern codebase
without any backward compatibility layer or deprecated functions.

Changes:
- Removed all deprecated functions (Generate, NewHandler)
- Removed MIGRATION_GUIDE.md (no backward compatibility)
- Updated ConvertHTML/ConvertWebpage to use HandlerBuilder
- Added ConvertURL as a cleaner alternative
- Completely rewrote all unit tests for new API
- Updated all examples to showcase only the new API
- Cleaned up README to show only new, recommended patterns

New API Surface:
- PDF(config) - Factory for PDF handler builders
- Screenshot(config) - Factory for screenshot handler builders
- GenerateHTML(html, builder, output) - Generate from HTML bytes
- GenerateURL(url, builder, output) - Generate from URL
- ConvertHTML(builder, html) - Convert HTML, return Fileinfo
- ConvertURL(builder, url) - Convert URL, return Fileinfo
- HandlerBuilder.FullPage(bool) - Method chaining for configuration

Benefits:
✓ Zero runtime type assertions
✓ Complete compile-time type safety
✓ Fluent, chainable API
✓ Clear, explicit function names
✓ Comprehensive unit test coverage
✓ Clean, maintainable codebase
✓ No deprecated cruft

All tests compile successfully. Integration tests require browser
which is not available in this environment but unit tests verify
the API structure and builder pattern work correctly.
This commit significantly improves the underlying Rod browser automation
implementation with better error handling, resource management, and API design.

Changes:

WebDriver Improvements:
- Replace all panic-prone Must* methods with proper error handling
- Add UrlToPage() error return for better error propagation
- Add WaitLoad() error return with context-based timeout
- Fix resource leak in HTMLToPage() - properly close temp file
- Add ConnectWithConfig() for customizable browser launch
- Add browser configuration options (Headless, NoSandbox)
- Add context support for cancellable operations
- Improved Close() with nil-safety check

Handler Improvements:
- Fix PDF config mutation bug - handlers now reusable
- Fix Screenshot Quality default (was pointer to 0, now nil)
- Proper config copying to avoid side effects

Test Updates:
- Update all tests to handle new error-returning APIs
- Add graceful test skipping when browser unavailable
- Fix all webdriver, PDF, and screenshot handler tests
- Update examples to show proper error handling

Error Handling:
- No more panics in production code
- All errors properly propagated to callers
- Backward compatible panic in FromDefault() for convenience
- Graceful error handling throughout

Benefits:
✓ No unexpected panics
✓ Proper resource cleanup
✓ Context-based cancellation support
✓ Better error messages and debugging
✓ Handlers can be safely reused
✓ Configuration options for different environments
✓ Production-ready error handling

Removed:
- ARCHITECTURE_ANALYSIS.md (no longer needed)

All code compiles successfully and follows Go best practices
for error handling and resource management.
Changed go directive from 1.24 to 1.22 to prevent Go from trying
to download go1.24.0 when go1.24.4 is already installed via toolchain.
The toolchain go1.24.4 directive ensures the correct version is used.
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.

3 participants