Skip to content

feat: uint#171

Merged
Oudwins merged 6 commits intomasterfrom
feat/uint
Jul 3, 2025
Merged

feat: uint#171
Oudwins merged 6 commits intomasterfrom
feat/uint

Conversation

@Oudwins
Copy link
Owner

@Oudwins Oudwins commented Jul 2, 2025

closes #169

Summary by CodeRabbit

  • New Features
    • Added support for coercing a wider range of integer, unsigned integer, floating-point, boolean, and string types to boolean, int, uint, and float64 values with overflow and sign checks.
    • Introduced a new function for creating unsigned integer schemas.
  • Bug Fixes
    • Improved type handling and error checking when converting between numeric types.
  • Tests
    • Expanded test coverage for integer coercion to include additional integer and unsigned integer types.
    • Added comprehensive tests for unsigned integer validation, parsing, transformations, and custom rules.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 2, 2025

Caution

Review failed

The pull request is closed.

"""

Walkthrough

A new unsigned integer (Uint) coercer was introduced, and existing coercers for Bool, Int, and Float64 were expanded to handle a wider range of integer, unsigned integer, and floating-point types. Corresponding tests were added, and a Uint number schema constructor was implemented.

Changes

File(s) Change Summary
conf/Coercers.go Added Uint coercer to DefaultCoercers; expanded Bool, Int, and Float64 coercers to support more types.
conf/Coercers_test.go Added test cases for Int coercer with additional integer and unsigned integer types; added new tests for Uint coercer.
numbers.go Added Uint function to create a NumberSchema for uint with the new coercer.
numbers_uint_test.go Added comprehensive unit tests for the Uint validator covering parsing, coercion, validation, and error handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Schema
    participant Coercers

    User->>Schema: Call Uint(opts...)
    Schema->>Coercers: Use Coercers.Uint to coerce input
    Coercers-->>Schema: Return coerced uint value or error
    Schema-->>User: Return NumberSchema[uint] instance
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement Uint validator and schema constructor (#169)
Add coercer for unsigned integers (Uint) (#169)
Expand coercers to support all integer and unsigned types (#169)

Poem

In the warren where numbers hop and run,
Now unsigned integers join the fun!
Coercers leap, new types in tow,
Testing proves they're good to go.
🐇 With every change, the code grows bright—
Uint, Int, and Float64 in the light!
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 05ae1cd and 0ab59d3.

📒 Files selected for processing (2)
  • conf/Coercers.go (5 hunks)
  • conf/Coercers_test.go (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment
  • Commit Unit Tests in branch feat/uint

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🔭 Outside diff range comments (1)
conf/Coercers_test.go (1)

89-89: Add test coverage for the new Uint coercer

The PR introduces a new Uint coercer but doesn't include any tests for it. Please add comprehensive test cases similar to TestIntCoercer.

Would you like me to generate the test cases for the Uint coercer?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6b75bad and 1d57ffa.

📒 Files selected for processing (3)
  • conf/Coercers.go (4 hunks)
  • conf/Coercers_test.go (1 hunks)
  • numbers.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
numbers.go (2)
utilsOptions.go (1)
  • SchemaOption (64-64)
conf/Coercers.go (1)
  • Coercers (219-219)
conf/Coercers.go (1)
numbers.go (2)
  • Uint (127-135)
  • Float64 (48-56)
🔇 Additional comments (4)
conf/Coercers.go (2)

62-72: Verify handling of integer values other than 0 or 1

The Bool coercer only handles integer values 0 (false) and 1 (true), but will return an error for other values like 2, -1, etc. Please confirm this is the intended behavior, as some systems treat any non-zero value as true.


179-198: LGTM!

The Float64 coercer correctly handles all integer and unsigned integer types with appropriate conversions.

numbers.go (1)

127-135: LGTM!

The Uint function correctly implements the NumberSchema constructor following the established pattern used by other numeric types.

conf/Coercers_test.go (1)

71-78: Test coverage looks good for Int coercer

The added test cases properly validate the expanded integer type support.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1d57ffa and a40a430.

📒 Files selected for processing (1)
  • numbers_uint_test.go (1 hunks)
🔇 Additional comments (17)
numbers_uint_test.go (17)

1-8: LGTM - Clean package structure and imports.

The package declaration and imports are well-structured and include the necessary dependencies for comprehensive testing.


10-18: LGTM - Basic parsing functionality test is correct.

The test properly verifies successful uint parsing and destination assignment.


20-31: LGTM - Formatter functionality is properly tested.

The test correctly verifies both custom message behavior and formatter override functionality.


33-42: LGTM - Custom coercer option test is correct.

The test properly verifies that custom coercer functions override default coercion behavior.


44-70: Verify the required field test logic.

The test logic appears correct for most scenarios, but verify that the expected behavior matches the implementation:

  1. Lines 53-57: Empty string should trigger coerce error ✓
  2. Lines 59-63: Whitespace string should trigger coerce error ✓
  3. Lines 65-69: nil should trigger required error with custom message ✓

The test covers the required validation scenarios appropriately.


86-94: LGTM - Default value test is correct.

The test properly verifies that default values are applied when parsing nil inputs.


96-104: LGTM - Catch functionality test is correct.

The test properly verifies that catch values are used when coercion fails.


106-119: LGTM - Post-transform test is correct.

The test properly verifies that transform functions are applied after successful parsing.


121-135: LGTM - Multiple transforms test is correct.

The test properly verifies that multiple chained transforms are applied in sequence.


137-150: LGTM - OneOf validation test is correct.

The test properly verifies both successful validation with allowed values and failure with disallowed values. The behavior of setting the destination even on validation failure appears to be intentional (coercion succeeds, validation fails).


152-165: LGTM - Equality validation test is correct.

The test properly verifies both successful and failed equality validation scenarios.


167-185: LGTM - Greater than validation test is correct.

The test comprehensively verifies GT validation with both boundary and non-boundary cases.


187-204: LGTM - Greater than or equal validation test is correct.

The test properly verifies GTE validation including the boundary condition (equal case).


206-224: LGTM - Less than validation test is correct.

The test comprehensively verifies LT validation with both boundary and non-boundary cases.


226-243: LGTM - Less than or equal validation test is correct.

The test properly verifies LTE validation including the boundary condition (equal case).


245-257: LGTM - Custom test function validation is correct.

The test properly verifies custom validation logic through the TestFunc mechanism.


259-262: LGTM - Type identification test is correct.

The test properly verifies that the Uint validator reports the correct type.

Comment on lines +72 to +84
func TestUintOptional(t *testing.T) {
validator := Uint().Optional()
dest := uint(0)
errs := validator.Parse(uint(5), &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(nil, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, uint(5), dest)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix the assertion logic in optional field test.

The assertion on line 83 may be incorrect. After parsing nil for an optional field, the destination should remain at its zero value, not the value from the previous parsing operation.

func TestUintOptional(t *testing.T) {
	validator := Uint().Optional()
	dest := uint(0)
	errs := validator.Parse(uint(5), &dest)
	if len(errs) > 0 {
		t.Errorf("Expected no errors, got %v", errs)
	}
+	assert.Equal(t, uint(5), dest)
+	
+	dest = uint(0) // Reset for next test
	errs = validator.Parse(nil, &dest)
	if len(errs) > 0 {
		t.Errorf("Expected no errors, got %v", errs)
	}
-	assert.Equal(t, uint(5), dest)
+	assert.Equal(t, uint(0), dest) // Should remain unchanged for optional nil
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func TestUintOptional(t *testing.T) {
validator := Uint().Optional()
dest := uint(0)
errs := validator.Parse(uint(5), &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
errs = validator.Parse(nil, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, uint(5), dest)
}
func TestUintOptional(t *testing.T) {
validator := Uint().Optional()
dest := uint(0)
errs := validator.Parse(uint(5), &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, uint(5), dest)
dest = uint(0) // Reset for next test
errs = validator.Parse(nil, &dest)
if len(errs) > 0 {
t.Errorf("Expected no errors, got %v", errs)
}
assert.Equal(t, uint(0), dest) // Should remain unchanged for optional nil
}
🤖 Prompt for AI Agents
In numbers_uint_test.go around lines 72 to 84, the test incorrectly asserts that
the destination value remains 5 after parsing nil for an optional field. Update
the assertion after parsing nil to check that the destination is reset to its
zero value (0) instead of retaining the previous value.

@Oudwins Oudwins merged commit 97ce447 into master Jul 3, 2025
8 of 9 checks passed
@Oudwins Oudwins deleted the feat/uint branch July 3, 2025 05:23
Oudwins added a commit that referenced this pull request Oct 3, 2025
* feat: improve type coercion and uint type coercer

* feat: uint schema

* chore: tests for uint

* fix: coercers

* fix: handle negative values on uints

* fix: overflows
Oudwins added a commit that referenced this pull request Dec 14, 2025
* feat: improve type coercion and uint type coercer

* feat: uint schema

* chore: tests for uint

* fix: coercers

* fix: handle negative values on uints

* fix: overflows
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.

Uint Validators

1 participant