Skip to content

Conversation

@alexgarzao
Copy link
Collaborator

Closes #40.

This PR introduces a significant refactor to the code generation and validator writing process. The core logic for generating validator functions is now separated from the logic for writing validator files, resulting in a cleaner architecture and improved testability. Validator functions are generated per struct and then assembled into a single file per package. Additionally, tests and Makefile logic have been updated to reflect these changes.

Key changes include:

Refactor and Separation of Concerns:

  • The function for generating validator code for structs (BuildFuncValidatorCode) is now separated from file writing, which is handled by a new pkgwriter package. (internal/codegenerator/build_validator.go [1] internal/pkgwriter/file_validator.go [2]
  • A new package-level data structure (Pkg) is introduced to group structs and their generated validator code by package. (internal/codegenerator/types.go internal/codegenerator/types.goR1-R18)

Validator File Generation and Test Updates:

  • Validator files are now generated per package, containing all validator functions for that package, instead of one file per struct. (internal/pkgwriter/file_validator.go [1] main.go [2]
  • New tests are added for the file validator writer, and existing tests are updated to match the new architecture. (internal/pkgwriter/file_validator_test.go [1] internal/codegenerator/build_func_validator_test.go [2]

Project Structure and Clean-up:

  • Validator function files are now named validator__.go and Makefile/test logic is updated accordingly. (Makefile MakefileL28-R28)
  • Old per-struct validator files are removed and replaced with consolidated files in generated test outputs. (tests/cmpbenchtests/generated_tests/validator__.go [1] tests/cmpbenchtests/generated_tests/validgenstringemailstruct_validator.go [2] tests/cmpbenchtests/generated_tests/validgenstringeqicstruct_validator.go [3]

API and Function Signature Changes:

  • The GenerateCode function now returns a map of packages to be written, rather than writing files directly. (internal/codegenerator/codegen.go internal/codegenerator/codegen.goL3-R22)
  • Obsolete code for direct file writing and import path generation is removed from the code generator. (internal/codegenerator/build_validator.go [1] [2]

File Renames and Minor Cleanups:

  • Several files are renamed to match the new naming convention for validator files (e.g., validator__.go). (_examples/email_test/validator__.go [1] _examples/test01/validator__.go [2] _examples/test02/structsinpkg/validator__.go [3] _examples/test03/validator__.go [4] tests/bench/validator__.go [5]

This refactor lays the groundwork for better modularity and future extensibility in how validators are generated and written to files.

References:
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]

@alexgarzao alexgarzao requested a review from Copilot August 20, 2025 21:23
@alexgarzao alexgarzao self-assigned this Aug 20, 2025

This comment was marked as outdated.

@alexgarzao alexgarzao requested a review from Copilot August 20, 2025 21:29

This comment was marked as outdated.

@alexgarzao alexgarzao changed the title 40 one validator file per peckage 40 one validator file per package Aug 20, 2025
@alexgarzao alexgarzao requested a review from Copilot August 20, 2025 21:31

This comment was marked as outdated.

@alexgarzao alexgarzao requested a review from Copilot August 21, 2025 23:17
Copy link
Contributor

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 implements a significant architectural refactor that consolidates validator generation from per-struct files to per-package files. The refactor introduces cleaner separation of concerns by separating code generation logic from file writing operations and groups all validator functions for structs within the same package into a single validator__.go file.

Key changes include:

  • Introduction of a new pkgwriter package to handle file writing operations separately from code generation
  • Consolidation of validator functions into package-level files named validator__.go instead of individual {struct}_validator.go files
  • Updated data structures to group structs by package for consolidated file generation

Reviewed Changes

Copilot reviewed 30 out of 35 changed files in this pull request and generated no comments.

Show a summary per file
File Description
main.go Updated to use new two-step process: code generation followed by file writing
internal/codegenerator/codegen.go Refactored to return package map instead of writing files directly
internal/codegenerator/build_validator.go Separated function code generation from file operations
internal/codegenerator/types.go Added new data structures for package-level organization
internal/pkgwriter/file_validator.go New package handling consolidated validator file generation
internal/pkgwriter/file_validator_test.go Tests for the new file writer functionality
tests/endtoend/validator__.go New consolidated validator file replacing individual struct files
Multiple *_validator.go files Removed individual struct validator files
Makefile Updated cleanup pattern to match new file naming convention

@alexgarzao alexgarzao merged commit 360615b into main Aug 22, 2025
1 check passed
@alexgarzao alexgarzao deleted the 40-one-validator-file-per-peckage branch August 22, 2025 23:19
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.

Generate one validator file per package

5 participants