Skip to content

Conversation

@alexgarzao
Copy link
Collaborator

@alexgarzao alexgarzao commented Oct 1, 2025

Closes #59

This PR introduces significant improvements to how validation operations are defined and handled across the codebase. The changes standardize type representations, expand support for numeric types, and enhance parsing and testing of validation tags. These updates make the validator more robust, consistent, and easier to extend for future types and operations.

Type System and Operation Support

  • Standardized type representation by replacing concrete types (e.g., string, uint8, bool) with generic placeholders (<STRING>, <INT>, <BOOL>) throughout the validation logic and code generation tables. This enables broader and more consistent support for all integer types and simplifies future expansions. [1] [2] [3]
  • Added support for additional numeric operations (gt, lt) and extended existing operations (eq, neq, gte, lte, etc.) to all integer types, not just uint8. This is reflected in both the operation definitions and code generation logic. [1] [2]

Validation and Parsing Enhancements

  • Improved parsing of multi-value validation tags to support both comma and space as separators, making the validator more flexible and user-friendly.
  • Updated the README to clarify type support and implementation status for each validation, reflecting the expanded support for integer types and the introduction of partial implementation markers.

Testing and Internal Logic

  • Expanded and refactored tests to cover new type representations and operations, ensuring correctness and consistency for all supported types and validation scenarios. [1] [2] [3] [4]
  • Improved the internal analyzer logic to correctly handle custom structs and validation applicability, preventing invalid operations and improving error reporting. [1] [2]

These changes collectively make the validation system more extensible, reliable, and easier to maintain.

@alexgarzao alexgarzao requested a review from Copilot October 1, 2025 21:36
@alexgarzao alexgarzao self-assigned this Oct 1, 2025
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

Adds normalized type identifiers (, , , ) to unify validation logic, expands numeric (all integer) support, introduces gt/lt operations, enhances multi-value parsing (space or comma), updates code generation, tests, and README.

  • Normalizes type handling across analyzer and code generator (operations & condition tables now keyed by normalized types).
  • Expands integer coverage in validators and tests; adds gt/lt and multi-separator parsing for multi-value tags.
  • Introduces helper utilities for normalized/base type conversions and updates README with new status indicators.

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/endtoend/validator__.go Generated validators for all integer types exercising new operations.
tests/endtoend/numeric.go Generated numeric E2E test cases covering all integer variants and operations.
tests/endtoend/main.go Invokes new numeric test suite.
tests/endtoend/generate_tests/numeric.tpl Template for generating numeric struct tests.
tests/endtoend/generate_tests/generate_numeric_tests_main.go Generates numeric test file using normalized type expansion.
tests/cmpbenchtests/generate_cmp_benchtests_main.go Switches from html/template to text/template (appropriate for code gen).
internal/parser/parser_test.go Adds float32 parsing coverage.
internal/common/types_test.go Tests new normalization and expansion helpers.
internal/common/types.go Adds normalization utilities and reverse expansion; updates IsGoType set.
internal/common/normalized_base_type_test.go Tests NormalizedBaseType String method.
internal/common/normalized_base_type.go Defines NormalizedBaseType enum.
internal/codegenerator/test_elements.go Looks up conditions via normalized type string.
internal/codegenerator/get_test_elements_numeric_test.go Adds broad numeric operation tests using normalized expansion.
internal/codegenerator/get_test_elements_errors_test.go Adjusts invalid operation test input.
internal/codegenerator/condition_table.go Re-keys condition mappings to normalized type tokens; adds gt/lt and numeric coverage.
internal/analyzer/parser_validation_test.go Tests multi-value parsing with commas.
internal/analyzer/parser_validation.go Supports comma or space separated multi-values.
internal/analyzer/operations_test.go Refactors tests to normalized types and iterates over expansions.
internal/analyzer/operations.go Normalized operation/type matrix; adds gt/lt and broader integer support.
internal/analyzer/analyzer_test.go Adds int variants for field-to-field operations.
internal/analyzer/analyzer.go Uses normalized type string for operation validation.
README.md Updates validation capability matrix (integer scope, partial statuses).
Makefile Generates numeric tests prior to E2E run.
Comments suppressed due to low confidence (3)

internal/analyzer/operations.go:1

  • Tests (operations_test.go) expect support for slices and arrays of integers (types "[]" and "[N]") for required, in, and nin, but these normalized types are absent from the ValidTypes maps here. This mismatch will cause those tests to fail with invalid type errors. Add "[]" (and for in/nin also "[N]") to the corresponding ValidTypes maps (and ensure matching condition table entries) or remove the test cases if not intended to be supported yet.
package analyzer

internal/codegenerator/condition_table.go:68

  • Support for integer slices/arrays ("[]", "[N]") is referenced in tests (operations_test.go) but there are no corresponding ConditionByType entries here for required, in, or nin. Even if added to operations.go, lookups will still fail without matching condition templates. Add entries for "[]" and "[N]" (using numeric slice containment helpers) and for "[]" in required with the same non-empty length check as string slices.
	"required": {
		Name: "required",
		ConditionByType: map[string]ConditionTable{
			"<STRING>": {
				operation:      `obj.{{.Name}} != ""`,
				concatOperator: "",
				errorMessage:   "{{.Name}} is required",
			},
			"<INT>": {
				operation:      `obj.{{.Name}} != 0`,
				concatOperator: "",
				errorMessage:   "{{.Name}} is required",
			},
			"[]<STRING>": {
				operation:      `len(obj.{{.Name}}) != 0`,
				concatOperator: "",
				errorMessage:   "{{.Name}} must not be empty",
			},
			"map[<STRING>]": {
				operation:      `len(obj.{{.Name}}) >= 1`,
				concatOperator: "",
				errorMessage:   "{{.Name}} must not be empty",
			},
			"map[<INT>]": {
				operation:      `len(obj.{{.Name}}) >= 1`,
				concatOperator: "",
				errorMessage:   "{{.Name}} must not be empty",
			},
		},
	},

internal/codegenerator/condition_table.go:292

  • Support for integer slices/arrays ("[]", "[N]") is referenced in tests (operations_test.go) but there are no corresponding ConditionByType entries here for required, in, or nin. Even if added to operations.go, lookups will still fail without matching condition templates. Add entries for "[]" and "[N]" (using numeric slice containment helpers) and for "[]" in required with the same non-empty length check as string slices.
	"in": {
		Name: "in",
		ConditionByType: map[string]ConditionTable{
			"<STRING>": {
				operation:      `obj.{{.Name}} == "{{.Target}}"`,
				concatOperator: "||",
				errorMessage:   "{{.Name}} must be one of {{.Targets}}",
			},
			"<INT>": {
				operation:      `obj.{{.Name}} == {{.Target}}`,
				concatOperator: "||",
				errorMessage:   "{{.Name}} must be one of {{.Targets}}",
			},
			"[]<STRING>": {
				operation:      `types.SliceOnlyContains(obj.{{.Name}}, {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must be one of {{.Targets}}",
			},
			"[N]<STRING>": {
				operation:      `types.SliceOnlyContains(obj.{{.Name}}[:], {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must be one of {{.Targets}}",
			},
			"map[<STRING>]": {
				operation:      `types.MapOnlyContains(obj.{{.Name}}, {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must be one of {{.Targets}}",
			},
			"map[<INT>]": {
				operation:      `types.MapOnlyContains(obj.{{.Name}}, {{.TargetsAsNumericSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must be one of {{.Targets}}",
			},
		},
	},
	"nin": {
		Name: "nin",
		ConditionByType: map[string]ConditionTable{
			"<STRING>": {
				operation:      `obj.{{.Name}} != "{{.Target}}"`,
				concatOperator: "&&",
				errorMessage:   "{{.Name}} must not be one of {{.Targets}}",
			},
			"<INT>": {
				operation:      `obj.{{.Name}} != {{.Target}}`,
				concatOperator: "&&",
				errorMessage:   "{{.Name}} must not be one of {{.Targets}}",
			},

			"[]<STRING>": {
				operation:      `types.SliceNotContains(obj.{{.Name}}, {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must not be one of {{.Targets}}",
			},
			"[N]<STRING>": {
				operation:      `types.SliceNotContains(obj.{{.Name}}[:], {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must not be one of {{.Targets}}",
			},
			"map[<STRING>]": {
				operation:      `types.MapNotContains(obj.{{.Name}}, {{.TargetsAsStringSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must not be one of {{.Targets}}",
			},
			"map[<INT>]": {
				operation:      `types.MapNotContains(obj.{{.Name}}, {{.TargetsAsNumericSlice}})`,
				concatOperator: "",
				errorMessage:   "{{.Name}} elements must not be one of {{.Targets}}",
			},

@alexgarzao alexgarzao requested a review from Copilot October 3, 2025 00:43
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

Copilot reviewed 27 out of 27 changed files in this pull request and generated 8 comments.

@alexgarzao alexgarzao requested a review from Copilot October 3, 2025 01:48
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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 6 comments.

@alexgarzao alexgarzao requested a review from Copilot October 3, 2025 12:34
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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 4 comments.

@alexgarzao alexgarzao requested a review from Copilot October 3, 2025 14:15
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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

internal/analyzer/analyzer.go:1

  • The map key is stored using common.KeyPath(package, structName) but later looked up with only fdType.BaseType; if KeyPath returns anything other than the bare struct name (e.g. adds package), the lookup will fail and custom structs with validations may be incorrectly treated as unsupported types. Store and look up using a consistent key (either just struct name in both places or always KeyPath) or add an additional direct structName key when populating the map.
package analyzer

@alexgarzao alexgarzao requested a review from Copilot October 3, 2025 17:09
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

Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.

@alexgarzao alexgarzao merged commit 2ae7db1 into main Oct 6, 2025
4 checks passed
@alexgarzao alexgarzao deleted the 59-support-integer-types branch October 6, 2025 17: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.

Implement support for integer types

3 participants