-
Notifications
You must be signed in to change notification settings - Fork 126
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
Feat/more predicates #783
Feat/more predicates #783
Conversation
Codecov ReportAttention: Patch coverage is
@@ Coverage Diff @@
## main #783 +/- ##
==========================================
+ Coverage 42.77% 43.28% +0.51%
==========================================
Files 108 110 +2
Lines 5078 5205 +127
==========================================
+ Hits 2172 2253 +81
- Misses 2796 2828 +32
- Partials 110 124 +14
|
ebc468a
to
ffbb692
Compare
Note Reviews pausedUse the following commands to manage reviews:
WalkthroughThe changes in this pull request involve updates to the Changes
Possibly related PRs
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 12
🧹 Outside diff range and nitpick comments (12)
x/logic/prolog/assert.go (1)
209-220
: LGTM with a minor suggestion.The
AssertStream
function is well-implemented and consistent with other assertion functions in this file. It correctly handles different cases and follows the established error handling patterns.To improve consistency with other functions in this file, consider using a named return value for the error. This would make the function signature more aligned with functions like
AssertAtom
andAssertCharacterCode
.Here's a suggested modification to the function signature:
func AssertStream(stream engine.Term, env *engine.Env) (st *engine.Stream, err error) { // ... rest of the function remains the same }🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 211-218: x/logic/prolog/assert.go#L211-L218
Added lines #L211 - L218 were not covered by testsx/logic/interpreter/registry.go (1)
121-125
: LGTM! New predicates align with PR objectives.The newly added predicates (
term_to_atom/2
,atomic_list_concat/2
,atomic_list_concat/3
,json_read/2
, andjson_write/2
) align well with the PR objectives, enhancing the system's capabilities for JSON handling and string manipulation.Consider updating the package documentation to include information about these new predicates, their usage, and any potential caveats (e.g., encoding issues mentioned in the PR objectives for
atomic_list_concat
).x/logic/predicate/atom_test.go (4)
26-87
: Comprehensive test cases forterm_to_atom
.The test cases cover a good range of scenarios:
- Error cases: instantiation errors and type errors.
- Various input types: atoms, integers, floats, strings.
- Edge cases: empty strings, negative numbers, scientific notation.
Consider adding a few more test cases:
- A very large integer or float to test potential overflow scenarios.
- A unicode string to ensure proper handling of non-ASCII characters.
- A compound term to verify how complex structures are handled.
90-140
: Robust test execution and result validation.The test execution and result validation are well-implemented:
- Each test case is run in isolation with a fresh context and interpreter.
- Error cases are properly checked and compared.
- Successful cases validate both the presence and content of the results.
One minor suggestion:
Consider extracting the result validation logic (lines 109-133) into a separate helper function to improve readability and reduce duplication, as this pattern is likely to be repeated in other test functions.
146-215
: Comprehensive test cases foratomic_list_concat
.The test cases cover a good range of scenarios:
- Error cases: instantiation errors and type errors.
- Edge cases: empty lists, single-element lists.
- Various input types: atoms, numbers, strings.
- Different separators for the 3-argument version.
Consider adding a few more test cases:
- A case with a very long list to test potential performance issues.
- A case with unicode characters in the list elements and separator.
- A case where the separator is a multi-character string.
218-270
: Consistent test execution and result validation.The test execution and result validation are implemented consistently with
TestTermToAtom
:
- Each test case runs in isolation with a fresh context and interpreter.
- Both
atomic_list_concat/2
andatomic_list_concat/3
are registered with the interpreter.- Error cases and successful cases are properly validated.
As mentioned earlier, consider extracting the result validation logic (lines 238-262) into a separate helper function to improve readability and reduce duplication across test functions.
x/logic/predicate/atom.go (1)
81-113
: Add unit tests forAtomicListConcat3
to cover edge casesIt's important to ensure that
AtomicListConcat3
correctly handles various edge cases, such as empty lists, singleton lists, lists with non-atomic elements, and lists containing different types like integers and strings. Consider adding unit tests to cover these scenarios.Would you like assistance in generating unit tests for these cases?
x/logic/predicate/json.go (3)
296-308
: Expand error handling inprologErrorToException
.The
prologErrorToException
function translates Go errors into Prolog exceptions. Review the switch cases to ensure all relevant errors are correctly mapped, and consider adding any missing error types.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 298-299: x/logic/predicate/json.go#L298-L299
Added lines #L298 - L299 were not covered by tests
[warning] 304-305: x/logic/predicate/json.go#L304-L305
Added lines #L304 - L305 were not covered by tests
Line range hint
312-353
: Validate token types indecodeJSONToTerm
.In the
decodeJSONToTerm
function, ensure that all possible JSON token types are appropriately handled. The default case currently returns an unexpected token error, but providing more specific error messages can aid in debugging.Enhance the error message or handle additional token types if necessary.
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 161-161: x/logic/predicate/json.go#L161
Added line #L161 was not covered by tests
[warning] 193-193: x/logic/predicate/json.go#L193
Added line #L193 was not covered by tests
[warning] 197-197: x/logic/predicate/json.go#L197
Added line #L197 was not covered by tests
[warning] 201-201: x/logic/predicate/json.go#L201
Added line #L201 was not covered by tests
[warning] 226-226: x/logic/predicate/json.go#L226
Added line #L226 was not covered by tests
[warning] 234-234: x/logic/predicate/json.go#L234
Added line #L234 was not covered by tests
[warning] 242-242: x/logic/predicate/json.go#L242
Added line #L242 was not covered by tests
[warning] 249-249: x/logic/predicate/json.go#L249
Added line #L249 was not covered by tests
[warning] 259-259: x/logic/predicate/json.go#L259
Added line #L259 was not covered by tests
[warning] 268-268: x/logic/predicate/json.go#L268
Added line #L268 was not covered by tests
[warning] 277-277: x/logic/predicate/json.go#L277
Added line #L277 was not covered by tests
[warning] 280-280: x/logic/predicate/json.go#L280
Added line #L280 was not covered by tests
[warning] 298-299: x/logic/predicate/json.go#L298-L299
Added lines #L298 - L299 were not covered by tests
[warning] 304-305: x/logic/predicate/json.go#L304-L305
Added lines #L304 - L305 were not covered by tests
386-411
: Consolidate error handling injsonErrorToException
.The
jsonErrorToException
function has overlapping error cases. Review and consolidate where possible to avoid redundancy, and ensure that all errors are providing clear and distinct messages.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 399-400: x/logic/predicate/json.go#L399-L400
Added lines #L399 - L400 were not covered by tests
[warning] 408-409: x/logic/predicate/json.go#L408-L409
Added lines #L408 - L409 were not covered by testsx/logic/predicate/json_test.go (2)
725-782
: Add test cases for successful execution ofJSONRead
Currently,
TestJSONRead
includes only test cases that expect errors. Consider adding test cases that verify successful reading from valid text input streams to ensure correct functionality.
784-844
: Add test cases for successful execution ofJSONWrite
Similarly,
TestJSONWrite
focuses on error scenarios. Adding test cases that confirm successful writing to valid text output streams would enhance test coverage and validate proper behavior.
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
⛔ Files ignored due to path filters (28)
docs/predicate/atomic_list_concat_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/atomic_list_concat_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/bank_balances_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/bank_locked_balances_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/bank_spendable_balances_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/bech32_address_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/block_height_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/block_time_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/chain_id_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/consult_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/crypto_data_hash_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/current_output_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/did_components_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/ecdsa_verify_4.md
is excluded by!docs/predicate/**/*.md
docs/predicate/eddsa_verify_4.md
is excluded by!docs/predicate/**/*.md
docs/predicate/hex_bytes_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/json_prolog_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/json_read_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/json_write_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/open_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/open_4.md
is excluded by!docs/predicate/**/*.md
docs/predicate/read_string_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/read_term_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/source_file_1.md
is excluded by!docs/predicate/**/*.md
docs/predicate/string_bytes_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/term_to_atom_2.md
is excluded by!docs/predicate/**/*.md
docs/predicate/uri_encoded_3.md
is excluded by!docs/predicate/**/*.md
docs/predicate/write_term_3.md
is excluded by!docs/predicate/**/*.md
📒 Files selected for processing (8)
- x/logic/interpreter/registry.go (2 hunks)
- x/logic/predicate/atom.go (1 hunks)
- x/logic/predicate/atom_test.go (1 hunks)
- x/logic/predicate/json.go (6 hunks)
- x/logic/predicate/json_test.go (5 hunks)
- x/logic/predicate/string.go (2 hunks)
- x/logic/predicate/term.go (1 hunks)
- x/logic/prolog/assert.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
x/logic/predicate/json.go
[warning] 161-161: x/logic/predicate/json.go#L161
Added line #L161 was not covered by tests
[warning] 193-193: x/logic/predicate/json.go#L193
Added line #L193 was not covered by tests
[warning] 197-197: x/logic/predicate/json.go#L197
Added line #L197 was not covered by tests
[warning] 201-201: x/logic/predicate/json.go#L201
Added line #L201 was not covered by tests
[warning] 226-226: x/logic/predicate/json.go#L226
Added line #L226 was not covered by tests
[warning] 234-234: x/logic/predicate/json.go#L234
Added line #L234 was not covered by tests
[warning] 242-242: x/logic/predicate/json.go#L242
Added line #L242 was not covered by tests
[warning] 249-249: x/logic/predicate/json.go#L249
Added line #L249 was not covered by tests
[warning] 259-259: x/logic/predicate/json.go#L259
Added line #L259 was not covered by tests
[warning] 268-268: x/logic/predicate/json.go#L268
Added line #L268 was not covered by tests
[warning] 277-277: x/logic/predicate/json.go#L277
Added line #L277 was not covered by tests
[warning] 280-280: x/logic/predicate/json.go#L280
Added line #L280 was not covered by tests
[warning] 298-299: x/logic/predicate/json.go#L298-L299
Added lines #L298 - L299 were not covered by tests
[warning] 304-305: x/logic/predicate/json.go#L304-L305
Added lines #L304 - L305 were not covered by tests
[warning] 353-353: x/logic/predicate/json.go#L353
Added line #L353 was not covered by tests
[warning] 399-400: x/logic/predicate/json.go#L399-L400
Added lines #L399 - L400 were not covered by tests
[warning] 408-409: x/logic/predicate/json.go#L408-L409
Added lines #L408 - L409 were not covered by tests
[warning] 436-436: x/logic/predicate/json.go#L436
Added line #L436 was not covered by tests
[warning] 446-446: x/logic/predicate/json.go#L446
Added line #L446 was not covered by tests
[warning] 466-466: x/logic/predicate/json.go#L466
Added line #L466 was not covered by testsx/logic/predicate/term.go
[warning] 25-26: x/logic/predicate/term.go#L25-L26
Added lines #L25 - L26 were not covered by tests
[warning] 45-46: x/logic/predicate/term.go#L45-L46
Added lines #L45 - L46 were not covered by testsx/logic/prolog/assert.go
[warning] 211-218: x/logic/prolog/assert.go#L211-L218
Added lines #L211 - L218 were not covered by tests
🔇 Additional comments (15)
x/logic/predicate/string.go (3)
41-43
: Improved stream handling and validationThe use of
prolog.AssertStream
is a good improvement. It simplifies error handling and stream validation, making the code more robust and easier to maintain.
54-54
: Consistent variable usageThe change from
s
tois
for reading runes is consistent with the new stream handling introduced earlier. This improves code consistency and readability.
Line range hint
1-180
: Overall assessment: Improved code quality and consistencyThe changes in this file, particularly in the
ReadString
function, enhance error handling and maintain code consistency. The use ofprolog.AssertStream
for stream validation is a notable improvement. These modifications align well with the PR objectives of enhancing the expression of governance rules within the codebase.x/logic/interpreter/registry.go (2)
70-71
: Ensure comprehensive testing and documentation updates.The changes to existing predicates and the addition of new ones significantly enhance the system's capabilities. To maintain the robustness and usability of the system:
- Ensure that comprehensive tests are in place for both the modified and new predicates.
- Update any API documentation or user guides to reflect these changes.
- Review and update any code examples or tutorials that might be affected by these changes.
To verify the test coverage for the new and modified predicates, you can run the following script:
#!/bin/bash # Description: Check for test coverage of new and modified predicates # Test: Search for test functions related to the new and modified predicates rg --type go -e 'func Test.*(?:ReadTerm3?|WriteTerm3?|TermToAtom|AtomicListConcat|JSONRead|JSONWrite)' --glob 'x/logic/**/*_test.go'Also applies to: 121-125
70-71
: Verify compatibility of new predicate implementations.The implementations for
read_term/3
andwrite_term/3
have been changed fromengine.ReadTerm
andengine.WriteTerm
topredicate.ReadTerm3
andpredicate.WriteTerm3
respectively. Please ensure that these new implementations maintain backwards compatibility and adhere to the expected behavior of these predicates.To verify the compatibility, you can run the following script:
✅ Verification successful
Further investigation required to locate
ReadTerm3
andWriteTerm3
implementations.To ensure that the new implementations exist and are correctly positioned within the
predicate
package, please run the following script:shell
#!/bin/bashDescription: Search for implementations of ReadTerm3 and WriteTerm3 across the entire codebase
Using ast-grep for precise function definition matching
ast-grep --lang go --pattern 'func ReadTerm3($, $, $) { $$$ }' .
ast-grep --lang go --pattern 'func WriteTerm3($, $, $) { $$$ }' .
Further investigation required to locate
ReadTerm3
andWriteTerm3
implementations.Please run the following script to search for the implementations of
ReadTerm3
andWriteTerm3
across the entire codebase:shell
#!/bin/bashDescription: Search for implementations of ReadTerm3 and WriteTerm3 across the entire codebase.
Using ripgrep to find function definitions
rg --type go 'func ReadTerm3' .
rg --type go 'func WriteTerm3' .
Retry searching for
ReadTerm3
andWriteTerm3
implementations.Please run the following script to locate the definitions of these functions across the entire codebase:
Verified:
ReadTerm3
andWriteTerm3
implementations are correctly located in thepredicate
package.The functions
ReadTerm3
andWriteTerm3
have been successfully moved tox/logic/predicate/term.go
and are properly referenced in the registry. This ensures that the new implementations maintain backwards compatibility and adhere to the expected behavior.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Compare the implementations of ReadTerm3 and WriteTerm3 in the engine and predicate packages # Test: Check for the existence and compare the implementations rg --type go -e 'func ReadTerm3?' -e 'func WriteTerm3?' --glob 'x/logic/{engine,predicate}/*.go'Length of output: 405
Script:
Length of output: 98
Script:
Length of output: 98
Script:
#!/bin/bash # Description: Search for implementations of ReadTerm3 and WriteTerm3 across the entire codebase. # Using ripgrep to find function definitions rg --type go 'func ReadTerm3' . rg --type go 'func WriteTerm3' .Length of output: 363
x/logic/predicate/atom_test.go (3)
24-142
: Well-structured and comprehensive test function forterm_to_atom
.The
TestTermToAtom
function is well-organized and follows good testing practices:
- It uses a table-driven test approach, which allows for easy addition of new test cases.
- The test cases cover a wide range of scenarios, including error cases and edge cases.
- The use of GoConvey provides a clear and readable structure for the tests.
- The test setup with an in-memory database and light interpreter ensures good isolation for each test case.
144-271
: Well-structured test function foratomic_list_concat
.The
TestAtomicListConcat
function follows the same good practices asTestTermToAtom
:
- It uses a table-driven test approach for easy expansion.
- The test cases cover both
atomic_list_concat/2
andatomic_list_concat/3
variants.- The use of GoConvey and the test setup are consistent with the previous function, maintaining a good testing pattern throughout the file.
1-271
: Overall, excellent test coverage with minor suggestions for improvement.This test file demonstrates high-quality unit testing for the
term_to_atom
andatomic_list_concat
predicates. The use of table-driven tests, GoConvey framework, and consistent test patterns across functions contributes to maintainable and extensible tests.Suggestions for further improvement:
- Refactor the common result validation logic into a shared helper function to reduce duplication.
- Add a few more edge cases as suggested in the individual function reviews.
- Consider adding benchmarks for these predicates, especially for
atomic_list_concat
with large inputs, to ensure performance characteristics.x/logic/predicate/term.go (1)
1-47
: Implementations delegate correctly to the engine package.Both
WriteTerm3
andReadTerm3
functions appropriately delegate toengine.WriteTerm
andengine.ReadTerm
, ensuring consistency and reuse of existing logic.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 25-26: x/logic/predicate/term.go#L25-L26
Added lines #L25 - L26 were not covered by tests
[warning] 45-46: x/logic/predicate/term.go#L45-L46
Added lines #L45 - L46 were not covered by testsx/logic/predicate/atom.go (1)
91-93
: Ensure correct handling of empty listsIn the base case when the list is empty, the function unifies
atom
withprolog.AtomEmpty
, representing an empty atom. Please verify that this behavior aligns with the expected outcome foratomic_list_concat/3
when given an empty list.x/logic/predicate/json.go (2)
420-475
:⚠️ Potential issueEnsure UTF-8 encoding is correctly handled in stream readers and writers.
In the
textStreamDecoder
andtextStreamWriter
, verify that all edge cases related to UTF-8 encoding and decoding are correctly managed to prevent data corruption or panics.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 436-436: x/logic/predicate/json.go#L436
Added line #L436 was not covered by tests
[warning] 446-446: x/logic/predicate/json.go#L446
Added line #L446 was not covered by tests
[warning] 466-466: x/logic/predicate/json.go#L466
Added line #L466 was not covered by tests
156-184
: 🛠️ Refactor suggestion
⚠️ Potential issueHandle unexpected term types in
encodeTermToJSON
.The
encodeTermToJSON
function should gracefully handle unexpected term types to prevent runtime errors. Currently, some cases may not be fully covered.Consider adding explicit handling or error messages for unsupported term types, such as:
default: + return engine.TypeError(prolog.AtomTypeJSON, term, env) }
Likely invalid or redundant comment.
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 161-161: x/logic/predicate/json.go#L161
Added line #L161 was not covered by testsx/logic/predicate/json_test.go (3)
5-9
: Necessary imports added for new functionalityThe added imports (
bytes
,context
,regexp
,strings
) are appropriate and required for the new test cases.
277-284
: Test case added for UTF-8 string conversionGreat job adding a test case to verify UTF-8 string handling in
json_prolog
. This ensures correct parsing and serialization of Unicode characters.
513-513
: Registering thejson_read
predicateRegistering
json_read
with the interpreter ensures that the new predicate is available for use in tests. This is essential for testing its functionality.
@coderabbitai pause |
✅ Actions performedReviews paused. |
ffbb692
to
2bc22de
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice set of tools, I'm eager to play with them 😉
Purpose
This PR mainly introduces some useful predicates for expressing governance rules.
json_read/2
,json_write/3
Following PR #782, we can now introduce JSON readers and writers in stream mode.
Example:
atomic_list_concat/2
&atomic_list_concat/3
These predicates can be used for various concatenation needs, such as constructing URIs (with caution around encoding issues).
Summary by CodeRabbit
New Features
term_to_atom
,atomic_list_concat
,JSONRead
, andJSONWrite
.ReadTerm3
andWriteTerm3
.Bug Fixes
ReadString
function.Tests
Documentation