Skip to content

Conversation

umputun
Copy link
Member

@umputun umputun commented Aug 11, 2025

Summary

This PR introduces significant performance improvements, new features, and comprehensive test coverage enhancements to the enum generator.

Key Changes

Performance Improvements

  • Replaced switch-based parsing with O(1) map lookups for 10x faster parsing
  • Pre-computed Values and Names as package variables instead of functions
  • Single shared enum instances to reduce memory allocation

New Features

  • SQL Support: Added database/sql driver.Valuer and sql.Scanner interfaces
  • Go 1.23 Iterator: Added All() method with backward compatibility
  • Index Method: Access underlying integer values via Index()
  • Getter Generation: New -getter flag for GetTypeByID functions
  • Declaration Order: Preserves source order instead of alphabetical
  • Lowercase Flag: -lower flag for lowercase string representation

Type System Enhancements

  • Preserves underlying type information (uint8, int32, etc.)
  • Proper character literal support ('A', '\n', '\x00')
  • Improved binary expression handling with iota
  • Fixed iota increment to match Go compiler behavior
  • Correct handling of underscore placeholders in const blocks

Testing

  • Test coverage improved from 90.5% to 99.6%
  • Added comprehensive tests for all new features
  • Edge case handling (division by zero, UTF-8, etc.)

Documentation

  • Updated README with comprehensive examples
  • Added SQL integration documentation
  • Documented performance characteristics
  • Go 1.23 iterator usage examples

Breaking Changes

None - all changes are backward compatible.

Testing

All tests pass with 99.6% coverage:

ok  	github.com/go-pkgz/enum/internal/generator	0.202s	coverage: 99.6% of statements

Performance Improvements:
- Replace switch-based parsing with O(1) map lookups for 10x faster parsing
- Pre-compute Values and Names as package variables instead of functions
- Use single shared enum instances to reduce memory allocation

New Features:
- Add SQL support with database/sql driver.Valuer and sql.Scanner interfaces
- Add Go 1.23 iterator support with All() and backward compatibility
- Add Index() method to access underlying integer values
- Add -getter flag for generating GetTypeByID functions
- Preserve source declaration order instead of alphabetical sorting
- Add -lower flag for lowercase string representation in marshaling

Type System Enhancements:
- Preserve underlying type information (uint8, int32, etc.) in generated code
- Add proper character literal support ('A', '\n', '\x00', etc.)
- Improve binary expression handling with iota operations
- Fix iota increment behavior to match Go compiler (increment per ValueSpec)
- Handle underscore placeholders in const blocks correctly

SQL Integration:
- Implement Scan method for unmarshaling from database
- Smart NULL handling - use zero value when available, error otherwise
- Add Value method for database marshaling

Code Generation Improvements:
- Enhanced error messages with specific invalid value reporting
- Better handling of edge cases (division by zero, empty blocks)
- Improved template with cleaner generated code structure
- Add validation for getter flag (requires unique values)

Testing:
- Add comprehensive test coverage (improved from 90.5% to 99.6%)
- Add tests for binary expressions with iota
- Add tests for character literals and UTF-8 handling
- Add tests for SQL NULL handling
- Add tests for declaration order preservation
- Add tests for underscore placeholders
- Add tests for various underlying types
- Add tests for getter functionality

Documentation:
- Update README with comprehensive examples
- Add SQL integration examples
- Document performance characteristics
- Add Go 1.23 iterator usage examples
- Document -getter and -lower flags

Development:
- Add coverage files to .gitignore
- Fix go.mod dependencies
- Update test data files for new features
@umputun umputun requested a review from Copilot August 11, 2025 05:45
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces significant performance improvements, new features, and comprehensive test coverage enhancements to the enum generator, achieving 99.6% test coverage.

  • Major refactoring of parsing logic from switch-based to O(1) map lookups for 10x faster parsing
  • Added SQL support, Go 1.23 iterator support, and type system enhancements
  • Preserved declaration order instead of alphabetical sorting and improved underlying type handling

Reviewed Changes

Copilot reviewed 12 out of 15 changed files in this pull request and generated no comments.

Show a summary per file
File Description
main.go Updated flag descriptions and usage help format
main_test.go Added comprehensive error handling tests for parsing and generation failures
internal/generator/generator.go Major refactor of parsing logic with new data structures and binary expression handling
internal/generator/generator_test.go Extensive new test coverage for edge cases, type preservation, and SQL handling
internal/generator/enum.go.tmpl Updated template to use variables instead of functions and added SQL/type support
internal/generator/testdata/*.go New test data files for various edge cases and type scenarios
go.mod Updated golang.org/x/text dependency version
README.md Updated documentation with new features and performance characteristics

@umputun umputun merged commit 1834756 into master Aug 11, 2025
4 checks passed
@umputun umputun deleted the major-refactor-and-enhancements branch August 11, 2025 05:46
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