Skip to content

Fix wrong rounding with double/float values on x/y#130

Merged
rafaeltonholo merged 4 commits intomainfrom
bugfix/issue-129/rect-x-y-int-instead-of-float
Mar 5, 2025
Merged

Fix wrong rounding with double/float values on x/y#130
rafaeltonholo merged 4 commits intomainfrom
bugfix/issue-129/rect-x-y-int-instead-of-float

Conversation

@rafaeltonholo
Copy link
Copy Markdown
Owner

@rafaeltonholo rafaeltonholo commented Mar 5, 2025

Summary by CodeRabbit

  • New Features

    • Introduced automated quality checks during code commits and pushes for improved development integrity.
  • Chores

    • Upgraded the build pipeline to include extended automated static analysis.
    • Updated the application version to 2.1.1.
  • Refactor

    • Refined coordinate handling to enhance the precision of graphical rendering.
  • Tests

    • Adjusted validations to align with the improved coordinate precision in shape rendering.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 5, 2025

Walkthrough

This update introduces two new Git hook scripts—one for pre-commit and one for pre-push—to automate code quality checks and tests. The pre-commit hook runs Detekt static analysis for both the library and plugin portions of the project, while the pre-push hook executes a suite of tests. Additionally, the GitHub workflow for pull requests has been adjusted to run Detekt on two project components, the application version has been incremented in the properties file, and several functions and tests in the SVG-to-compose module have been updated to use floating-point coordinates instead of integers.

Changes

File(s) Change Summary
.githooks/pre-commit.sh
.githooks/pre-push.sh
New Git hook scripts added. The pre-commit hook runs Detekt analysis on both library and plugin components, and the pre-push hook runs tests via Gradle, aborting the push if tests fail.
.github/workflows/pull_request.yml Updated workflow steps for Detekt analysis. The existing step is renamed to run Detekt on svg-to-compose, and a new step is added to run Detekt on svg-to-compose-gradle-plugin.
app.properties Updated version number from 2.1.0 to 2.1.1.
svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/StrokeDashArray.kt Modified createDashedPathForRect function: parameters x and y changed from Int to Float.
svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNode.kt Updated SvgRectNode properties and functions: x and y types changed to nullable Float?, and related functions now accept Float instead of Int, with default values updated accordingly.
svg-to-compose/src/commonTest/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNodeTests.kt Adjusted test assertions: expected values for x and y updated from integers to floating-point numbers (e.g., 1010f, 2020f).

Sequence Diagram(s)

sequenceDiagram
  participant Dev as Developer
  participant Git as Git
  participant PreCommit as pre-commit.sh
  participant Detekt as Detekt Tool

  Dev->>Git: Initiate commit
  Git->>PreCommit: Trigger pre-commit hook
  PreCommit->>Detekt: Run Detekt for library analysis
  Detekt-->>PreCommit: Library analysis result
  PreCommit->>Detekt: Run Detekt for plugin analysis
  Detekt-->>PreCommit: Plugin analysis result
  PreCommit-->>Git: Return status (pass/fail)
Loading
sequenceDiagram
  participant Dev as Developer
  participant Git as Git
  participant PrePush as pre-push.sh
  participant Gradle as Gradle Test Runner

  Dev->>Git: Initiate push
  Git->>PrePush: Trigger pre-push hook
  PrePush->>Gradle: Run tests (“./gradlew cleanAllTests allTests”)
  Gradle-->>PrePush: Test results
  PrePush-->>Git: Allow push or abort based on results
Loading
sequenceDiagram
  participant PR as Pull Request
  participant Workflow as GitHub Workflow
  participant Gradle as Gradle Runner

  PR->>Workflow: Open pull request
  Workflow->>Gradle: Run "detektMetadataCommonMain" (svg-to-compose)
  Gradle-->>Workflow: Return analysis output
  Workflow->>Gradle: Run "detekt" (svg-to-compose-gradle-plugin)
  Gradle-->>Workflow: Return analysis output
  Workflow-->>PR: Report analysis results
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

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
Copy Markdown
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: 0

🧹 Nitpick comments (2)
.githooks/pre-commit.sh (2)

3-14: Output File Creation Function
The fn_create_output_file function correctly converts the input type to lowercase and constructs unique log file paths based on the current timestamp.

  • Suggestion: Before attempting to write to the log file, ensure the target directory exists to avoid any file creation issues.
-    BASE_DIR="./build/reports/detekt"
+    BASE_DIR="./build/reports/detekt"
+    mkdir -p "$BASE_DIR"

16-37: Detekt Check Execution Function
The fn_run_detekt function dynamically sets the Gradle task based on the type (using detektMetadataCommonMain for "library" and defaulting to detekt for "plugin"), captures the output in a log file, and handles failures appropriately.

  • Suggestion: To capture all error details from Gradle, consider redirecting both stdout and stderr to the log file.
-    ./gradlew "$GRADLE_TASK" > "$OUTPUT_FILE"
+    ./gradlew "$GRADLE_TASK" > "$OUTPUT_FILE" 2>&1
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c52deb and c0d7741.

📒 Files selected for processing (7)
  • .githooks/pre-commit.sh (1 hunks)
  • .githooks/pre-push.sh (1 hunks)
  • .github/workflows/pull_request.yml (1 hunks)
  • app.properties (1 hunks)
  • svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/StrokeDashArray.kt (1 hunks)
  • svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNode.kt (5 hunks)
  • svg-to-compose/src/commonTest/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNodeTests.kt (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • app.properties
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build-and-check (ubuntu-latest)
  • GitHub Check: build-and-check (windows-latest)
  • GitHub Check: build-and-check (macos-latest)
🔇 Additional comments (25)
.githooks/pre-push.sh (3)

1-2: Shebang and Function Declaration
The shebang (#!/bin/sh) and the start of the fn_main function definition are set up correctly for portability in a shell environment.


3-12: Pre-push Hook Test Execution Logic
The hook properly runs tests via ./gradlew cleanAllTests allTests and checks for a non-zero exit code to abort the push when tests fail. Consider, as an optional improvement, redirecting stderr to the output (e.g., using 2>&1) if you require more detailed diagnostic logs in the terminal.


14-15: Invocation of the Main Function
Calling fn_main at the end ensures the hook executes as expected.

.github/workflows/pull_request.yml (2)

56-60: Renaming and Running Detekt on svg-to-compose
The renamed step ("Running Detekt on svg-to-compose") now clearly reflects the command ./gradlew detektMetadataCommonMain intended for static analysis on the main project component.


61-65: Adding Detekt Check for the Gradle Plugin
Introducing a new step to run ./gradlew detekt for the svg-to-compose-gradle-plugin is a valuable addition. This ensures that both project components are statically analyzed.

.githooks/pre-commit.sh (2)

1-2: Initialization and Constant Declaration
The file starts with a correct shebang and the declaration of INVALID_OUTPUT_FILE_ERROR_CODE. This enhances clarity and sets up the basis for consistent error handling.


40-48: Main Function Orchestration
The fn_main function clearly orchestrates the execution of Detekt checks for both "library" and "plugin" types. Its sequential structure ensures that each check is performed before committing the changes.

svg-to-compose/src/commonTest/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNodeTests.kt (2)

19-20: Correctly updated test assertions to use floating-point values

The test assertions have been appropriately updated to expect floating-point values (10f and 20f) instead of integers, aligning with the type changes in the SvgRectNode class.


19-20: Good update to the test expectations for float values.

The test expectations have been correctly updated to use float literals (10f, 20f) instead of integers, consistent with the implementation changes in SvgRectNode.kt where x and y were changed from Int? to Float?.

svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/StrokeDashArray.kt (2)

30-31: Function signature updated to use float parameters

The parameter types of x and y have been appropriately changed from Int to Float to support more precise positioning and rounding. This change aligns with similar modifications in SvgRectNode.


29-32: Parameter type change improves precision.

Changing the x and y parameters from Int to Float allows for more precise positioning of dashed paths, which addresses the issue with wrong rounding for double/float values mentioned in the PR objectives.

svg-to-compose/src/commonMain/kotlin/dev/tonholo/s2c/domain/svg/SvgRectNode.kt (14)

28-32: Property type updated from Int to Float for x coordinate

The x property has been correctly changed from Int? to Float? and the conversion function from toIntOrNull to toFloatOrNull. This change enhances coordinate precision for SVG elements.


33-37: Property type updated from Int to Float for y coordinate

The y property has been correctly changed from Int? to Float? and the conversion function from toIntOrNull to toFloatOrNull. This change enhances coordinate precision for SVG elements.


71-72: Default values updated to match new Float type

The default values for x and y have been appropriately updated from 0 to 0f to match the new Float type.


90-95: Function parameter types updated to Float for createDashedRect

The parameter types for x and y in the createDashedRect function have been correctly updated from Int to Float, maintaining consistency with the property type changes.


110-114: Function parameter types updated to Float for createRegularRect

The parameter types for x and y in the createRegularRect function have been correctly updated from Int to Float, maintaining consistency with the property type changes.


137-143: Function parameter types updated to Float for createRoundedCornerRect

The parameter types for x and y in the createRoundedCornerRect function have been correctly updated from Int to Float, maintaining consistency with the property type changes.


28-193:

Details

✅ Verification successful

Verify all call sites using x/y coordinates

The change from Int to Float for x/y coordinates is well-implemented in this file, but we should verify that all call sites using these coordinates have been updated accordingly.


🏁 Script executed:

#!/bin/bash
# Description: Find all places in the codebase where SvgRectNode's x and y properties are accessed or where the modified functions are called

# Search for direct references to SvgRectNode.x and SvgRectNode.y
echo "### Searching for references to SvgRectNode.x and SvgRectNode.y properties:"
rg -A 3 "SvgRectNode.*\.(x|y)" --type kotlin

# Search for calls to the updated functions
echo -e "\n### Searching for calls to updated functions:"
rg -A 3 "(createDashedRect|createRegularRect|createRoundedCornerRect).*x,.*y" --type kotlin

# Look for potential type mismatches (Int vs Float) when working with coordinates
echo -e "\n### Searching for potential type mismatches with coordinates:"
rg -A 3 "(x|y)(\s*:\s*Int|\s*=\s*\d+[^f])" --type kotlin | grep -v "width" | grep -v "height"

Length of output: 16729


Action Verified: x/y Coordinate Call Sites are Consistent

The updated implementation in SvgRectNode.kt using Float? for both x and y has been verified against all related call sites. Our search confirms that:

  • All calls to createDashedRect, createRegularRect, and createRoundedCornerRect correctly pass x and y as Float values.
  • Test cases (e.g., in SvgPathLineToTests.kt, SvgPathHorizontalLineToTests.kt, etc.) consistently use floating point values (e.g., 80f).
  • No type mismatches were detected in the repository for the x and y coordinates in SvgRectNode.

These findings indicate that the refactoring from Int to Float for x and y is applied uniformly and without adverse effects.


28-32: Type change from Int to Float fixes rounding issues.

Changing x from Int? to Float? and updating the parsing method from toIntOrNull to toFloatOrNull preserves fractional position values, addressing the rounding issue mentioned in the PR objectives.


33-37: Type change from Int to Float fixes rounding issues.

Changing y from Int? to Float? and updating the parsing method from toIntOrNull to toFloatOrNull preserves fractional position values, addressing the rounding issue mentioned in the PR objectives.


71-72: Default values updated to match new type.

The default values for x and y have been correctly updated to use float literals (0f) to match the new Float type, maintaining type consistency.


90-95: Parameter type change maintains consistency across codebase.

The parameter types for x and y in createDashedRect have been updated to Float to align with the property type changes, ensuring type consistency.


110-113: Parameter type change maintains consistency across codebase.

The parameter types for x and y in createRegularRect have been updated to Float to align with the property type changes, ensuring type consistency.


137-142: Parameter type change maintains consistency across codebase.

The parameter types for x and y in createRoundedCornerRect have been updated to Float to align with the property type changes, ensuring type consistency.


200-201:

Details

✅ Verification successful

Verify remaining non-primitive operations with Float values.

The string interpolation for x and y values should work fine with Float values, but it's worth verifying that any other operations (formatting, transformations, etc.) throughout the codebase that might interact with these values handle Float values correctly.


🏁 Script executed:

#!/bin/bash
# Search for any code that uses x and y properties from SvgRectNode to ensure compatibility with Float values
echo "Searching for usages of SvgRectNode.x and SvgRectNode.y..."
rg -A 2 -B 2 "\.x\b|\.y\b" --type kotlin | grep -v "Float" | grep -v "SvgRectNode\.kt" || echo "No potential type incompatibilities found."

Length of output: 54527


Float Operations Verified for SvgRectNode Properties

After reviewing the test outputs and scanning through the codebase, all non-primitive operations that interact with the Float values for SvgRectNode.x and SvgRectNode.y are correctly handling these values:

  • String Interpolation: The code snippet using string interpolation for the x and y values is working as expected.
  • Conversions & Transformations: Various usages across the codebase (e.g., explicit conversions via toDouble() in arithmetic and transformation operations) correctly work with Float values.
  • Testing Confirmation: The tests in SvgRectNodeTests.kt confirm that both non-null and null cases for these properties behave as intended without type incompatibilities.

No further action is required regarding Float handling in these operations.

@rafaeltonholo rafaeltonholo merged commit c3c8cbc into main Mar 5, 2025
4 checks passed
@rafaeltonholo rafaeltonholo deleted the bugfix/issue-129/rect-x-y-int-instead-of-float branch March 5, 2025 23:13
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.

Wrong rounding with double/float values on x/y

1 participant