Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/json float support #778

Merged
merged 5 commits into from
Oct 9, 2024
Merged

Fix/json float support #778

merged 5 commits into from
Oct 9, 2024

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Oct 8, 2024

This PR introduces two main features:

  • Support for floating-point numbers: Floating-point numbers were previously missing from the implementation. Floats are now treated as the canonical representation of all numbers in JSON, using the Prolog engine.Float type. As the underlying type relies on apd -introduced in PR #1- it provides a robust mechanism for handling numbers.
  • Support for the Text type in JSON terms: This allows the use of a list of character codes or a list of characters, in addition to atoms.

Summary by CodeRabbit

  • New Features

    • Enhanced JSON to Prolog conversion capabilities with new functions for decoding and encoding.
    • Introduced a new function to convert byte slices to Prolog atoms.
  • Bug Fixes

    • Improved error handling and clarity in conversion processes for various Prolog term types.
  • Tests

    • Updated and added test cases to ensure accurate validation of JSON to Prolog conversions, including handling of complex structures and numeric types.

@ccamel ccamel self-assigned this Oct 8, 2024
Copy link

codecov bot commented Oct 8, 2024

Codecov Report

Attention: Patch coverage is 95.23810% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
x/logic/predicate/json.go 95.08% 2 Missing and 1 partial ⚠️

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #778      +/-   ##
==========================================
+ Coverage   55.42%   55.55%   +0.12%     
==========================================
  Files          73       73              
  Lines        2966     2970       +4     
==========================================
+ Hits         1644     1650       +6     
+ Misses       1228     1227       -1     
+ Partials       94       93       -1     
Files with missing lines Coverage Δ
x/logic/prolog/byte.go 11.11% <100.00%> (+11.11%) ⬆️
x/logic/predicate/json.go 87.20% <95.08%> (+1.83%) ⬆️

@ccamel ccamel force-pushed the fix/json-float-support branch from 4ce231c to ddf1cea Compare October 8, 2024 17:11
@ccamel ccamel marked this pull request as ready for review October 8, 2024 17:11
Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The changes involve significant updates to the JSON and Prolog integration within the predicate/json.go and json_test.go files. Key modifications include renaming and altering method signatures to enhance functionality and clarity, particularly in handling Prolog terms and JSON conversions. A new function, BytesToAtom, has been introduced in byte.go to convert byte slices into Prolog atoms. The test cases have also been refined for better accuracy and clarity, ensuring they align with the updated functionality.

Changes

File Change Summary
x/logic/predicate/json.go - Renamed jsonStringToTerms to decodeJSONToTerm, updated its signature to accept engine.Term.
- Renamed termsToJSON to encodeTermToJSON, updated its signature.
- Renamed jsonToTerms to jsonToTerm, adjusted internal logic.
- Added termToJSON for specific Prolog term to JSON conversion.
- Refined compoundToJSON for better handling of Prolog compound terms.
x/logic/predicate/json_test.go - Updated test case descriptions for clarity.
- Added new test cases for JSON object strings.
- Refined handling of numeric values and updated expected results.
- Adjusted error messages and handling for boolean and null values.
x/logic/prolog/byte.go - Added BytesToAtom function to convert []byte to engine.Atom.

Suggested reviewers

  • amimart
  • bdeneux

🐰 In the garden where logic blooms,
JSON dances, dispelling glooms.
Prolog terms in harmony play,
With bytes transformed in a delightful way!
Clarity shines in tests anew,
Hopping forward, we bid adieu! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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
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

🧹 Outside diff range and nitpick comments (1)
x/logic/predicate/json.go (1)

134-134: Add tests to cover error handling for invalid JSON types

The error handling at lines 134 and 142 returns errors when encountering invalid JSON types or when list iteration fails. According to the static analysis hints, these lines are not covered by tests.

Consider adding unit tests that specifically trigger these error cases to ensure that the error handling works as expected. Testing these paths will improve code robustness. Would you like assistance in writing these tests?

Also applies to: 142-142

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 134-134: x/logic/predicate/json.go#L134
Added line #L134 was not covered by tests

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 193f7c2 and ddf1cea.

⛔ Files ignored due to path filters (1)
  • docs/predicate/json_prolog_2.md is excluded by !docs/predicate/**/*.md
📒 Files selected for processing (3)
  • x/logic/predicate/json.go (6 hunks)
  • x/logic/predicate/json_test.go (6 hunks)
  • x/logic/prolog/byte.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
x/logic/predicate/json.go

[warning] 134-134: x/logic/predicate/json.go#L134
Added line #L134 was not covered by tests


[warning] 142-142: x/logic/predicate/json.go#L142
Added line #L142 was not covered by tests

x/logic/prolog/byte.go

[warning] 35-36: x/logic/prolog/byte.go#L35-L36
Added lines #L35 - L36 were not covered by tests

🔇 Additional comments (13)
x/logic/prolog/byte.go (1)

34-37: LGTM: New BytesToAtom function looks good

The implementation of BytesToAtom is concise and correctly converts a byte slice to a Prolog atom. This addition aligns well with the PR objectives of enhancing JSON handling capabilities, particularly for text representation.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 35-36: x/logic/prolog/byte.go#L35-L36
Added lines #L35 - L36 were not covered by tests

x/logic/predicate/json_test.go (12)

41-44: LGTM: Improved test case description and error message

The update to the test case description and error message accurately reflects the changes in JSON handling, particularly the support for the Text type. This change enhances the clarity and precision of the test case.


111-117: LGTM: New test case for JSON 0 conversion

This new test case correctly implements the conversion of JSON 0 to Prolog floating-point representation. It aligns with the PR objective of using floating-point numbers as the canonical representation for all JSON numbers.


119-125: LGTM: Updated test case for JSON 10 conversion

The modification of this test case to expect "10.0" instead of "10" is consistent with the new approach of using floating-point representation for all JSON numbers. This change maintains consistency across the test suite.


126-140: LGTM: New test cases for negative decimal and large number conversion

These additional test cases for converting -10.9 and 100000000000000000000 to Prolog representations are valuable. They ensure that the new floating-point representation works correctly for negative decimals and very large numbers, enhancing the robustness of the JSON-Prolog conversion.


143-146: LGTM: New test case for handling excessively large exponents

This test case is a valuable addition as it verifies the system's ability to properly handle and reject numbers with excessively large exponents. This enhances the robustness of the JSON parsing by ensuring it can gracefully handle edge cases that could potentially cause overflow or other issues.


307-313: LGTM: New test case for Prolog 0 to JSON conversion

This new test case correctly verifies the conversion of Prolog 0 to its JSON representation. The expected result of '0' as a string is consistent with standard JSON number representation.


314-329: LGTM: Updated test cases for Prolog integer and float to JSON conversion

These test case updates correctly verify the conversion of Prolog integers and floating-point numbers to their JSON representations. The expected results as string representations ('10' and '10.4') are consistent with standard JSON number formatting, ensuring accurate bidirectional conversion between Prolog and JSON.


330-353: LGTM: New test cases for diverse numeric conversions

These additional test cases significantly enhance the coverage of numeric conversions from Prolog to JSON. They verify correct handling of:

  1. Negative decimal numbers
  2. Very large numbers
  3. Numbers in scientific notation

This comprehensive set of tests ensures robust and accurate conversion across a wide range of numeric representations, which is crucial for maintaining data integrity in JSON-Prolog conversions.


354-358: LGTM: New test case for handling extremely large exponents

This test case is a valuable addition as it verifies the system's ability to properly handle and reject numbers with extremely large exponents. This enhances the robustness of the JSON-Prolog conversion by ensuring it can gracefully handle edge cases that could potentially cause overflow or loss of precision.


511-511: LGTM: Updated term representation for consistent floating-point usage

The modification of the expected term representation to use a floating-point number (30.0) instead of an integer (30) is consistent with the new approach of using floating-point representation for all JSON numbers. This change ensures consistency in the bidirectional conversion between JSON and Prolog.


521-521: LGTM: Updated array representation for consistent floating-point usage

The modification of the expected term representation to use floating-point numbers ([1.0,2.0,3.0]) instead of integers ([1,2,3]) in the array is consistent with the new approach of using floating-point representation for all JSON numbers. This change ensures consistency in the bidirectional conversion between JSON and Prolog, even for arrays of numbers.


Line range hint 1-646: Overall assessment: Comprehensive and consistent updates to JSON-Prolog conversion tests

The changes made to this test file are thorough and well-aligned with the PR objectives. Key improvements include:

  1. Consistent use of floating-point representation for all numbers in JSON-Prolog conversions.
  2. Addition of new test cases covering edge cases such as very large numbers, negative decimals, and scientific notation.
  3. Updates to existing test cases to reflect the new floating-point representation approach.
  4. Enhanced error handling for invalid numeric inputs.

These changes significantly improve the test coverage and ensure that the JSON-Prolog conversion functionality is robust and consistent with the new implementation.

x/logic/prolog/byte.go Show resolved Hide resolved
@ccamel
Copy link
Member Author

ccamel commented Oct 8, 2024

@coderabbitai pause

Copy link
Contributor

coderabbitai bot commented Oct 8, 2024

✅ Actions performed

Reviews paused.

@ccamel ccamel force-pushed the fix/json-float-support branch from ddf1cea to 9c67d8a Compare October 8, 2024 18:29
@ccamel ccamel force-pushed the fix/json-float-support branch from 9c67d8a to c4348de Compare October 8, 2024 18:31
@ccamel ccamel requested review from amimart and bdeneux October 8, 2024 18:38
Copy link
Member

@amimart amimart left a comment

Choose a reason for hiding this comment

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

It looks good to me, we now adheres to the JSON standard thanks 🙏

I was wondering about the handling of higher precisions and I think we'll need to provide string conversion predicates for such needs, what do you think?

@ccamel
Copy link
Member Author

ccamel commented Oct 9, 2024

I was wondering about the handling of higher precisions and I think we'll need to provide string conversion predicates for such needs, what do you think?

Yeah, that’s a great idea! I totally agree that adding conversion predicates to handle those cases makes sense.

@ccamel ccamel merged commit acf2e04 into main Oct 9, 2024
20 checks passed
@ccamel ccamel deleted the fix/json-float-support branch October 9, 2024 12:29
@ccamel
Copy link
Member Author

ccamel commented Oct 11, 2024

I was wondering about the handling of higher precisions and I think we'll need to provide string conversion predicates for such needs, what do you think?

@coderabbitai, could you please open a new issue to explore the implementation of string/float conversion predicates, as suggested by @amimart? Thanks!

Copy link
Contributor

coderabbitai bot commented Oct 11, 2024

Tip

For best results, initiate chat on the files or code changes.

@ccamel, I've opened a new issue to explore the implementation of string/float conversion predicates, as suggested by @amimart. You can find it here: #781.

🔗 Analysis chain
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Create a new GitHub issue as requested by @ccamel

# Variables
PR_NUMBER=778
PR_URL="https://github.com/axone-protocol/axoned/pull/${PR_NUMBER}"
USER="ccamel"

# Fetch the URL of the specific comment made by @ccamel on PR #778
COMMENTS=$(gh api repos/axone-protocol/axoned/issues/${PR_NUMBER}/comments)
COMMENT_URL=$(echo "$COMMENTS" | jq -r 'reverse | .[] | select(.user.login=="'"$USER"'") | .html_url' | head -n1)

# Create the new GitHub issue
gh issue create \
--title "Explore implementation of string/float conversion predicates" \
--body "As suggested by @amimart in [this comment](${COMMENT_URL}), we need to explore the implementation of string/float conversion predicates to handle higher precision numbers.

This issue originates from [PR #${PR_NUMBER}](${PR_URL}).

Assignee: @${USER}" \
--assignee ${USER}

Length of output: 71688

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.

2 participants