-
Notifications
You must be signed in to change notification settings - Fork 128
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/string bytes #496
Feat/string bytes #496
Conversation
WalkthroughThe overarching change introduces a new Changes
Related issues
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 as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
7322669
to
9767430
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.
Review Status
Actionable comments generated: 7
Configuration used: CodeRabbit UI
Files selected for processing (29)
- docs/predicate/predicates.md (1 hunks)
- x/logic/keeper/interpreter.go (3 hunks)
- x/logic/predicate/address.go (4 hunks)
- x/logic/predicate/address_test.go (2 hunks)
- x/logic/predicate/bank.go (2 hunks)
- x/logic/predicate/crypto.go (7 hunks)
- x/logic/predicate/crypto_test.go (1 hunks)
- x/logic/predicate/did.go (3 hunks)
- x/logic/predicate/did_test.go (1 hunks)
- x/logic/predicate/encoding.go (1 hunks)
- x/logic/predicate/encoding_test.go (1 hunks)
- x/logic/predicate/json.go (7 hunks)
- x/logic/predicate/string.go (2 hunks)
- x/logic/predicate/string_test.go (1 hunks)
- x/logic/predicate/uri.go (2 hunks)
- x/logic/predicate/util.go (2 hunks)
- x/logic/prolog/assert.go (1 hunks)
- x/logic/prolog/assert_test.go (3 hunks)
- x/logic/prolog/atom.go (1 hunks)
- x/logic/prolog/encoding.go (1 hunks)
- x/logic/prolog/error.go (1 hunks)
- x/logic/prolog/json.go (1 hunks)
- x/logic/prolog/json_test.go (1 hunks)
- x/logic/prolog/list.go (1 hunks)
- x/logic/prolog/option.go (1 hunks)
- x/logic/prolog/option_test.go (1 hunks)
- x/logic/prolog/term.go (1 hunks)
- x/logic/prolog/term_test.go (1 hunks)
- x/logic/prolog/unify.go (1 hunks)
Files skipped from review due to trivial changes (3)
- x/logic/predicate/crypto_test.go
- x/logic/predicate/did_test.go
- x/logic/predicate/util.go
Additional comments: 38
x/logic/prolog/list.go (1)
- 5-12: The function
ListHead
correctly retrieves the first element of a Prolog list. The use ofengine.ListIterator
is appropriate for this task, and the logic to check if the iterator has a next element and to return the current element is sound.x/logic/prolog/error.go (1)
- 5-32: The error handling functions such as
ValidCharset
,ValidEncoding
,ValidByte
, andValidCharacterCode
are well-defined and return Prolog terms that represent different types of domain errors. These functions will be useful for standardized error handling across the Prolog engine.x/logic/predicate/encoding.go (1)
- 13-61: The
HexBytes
predicate function is well-structured and handles both directions of conversion between hexadecimal strings and byte lists. The error handling is appropriate, and the use ofengine.Error
to return Prolog errors is consistent with the rest of the codebase.x/logic/prolog/json.go (1)
- 9-74: The
ExtractJSONTerm
function correctly processes a Prolog compound term representing a JSON object. The error messages are descriptive and the checks for the correct functor and arity are in place. The use ofengine.ListIterator
to iterate over the list of pairs is appropriate.x/logic/prolog/encoding.go (1)
- 11-73: The
Decode
andEncode
functions handle the conversion of byte slices to and from specified encodings. The use ofcharset.Lookup
to find the encoding and the error handling withengine.DomainError
are correctly implemented. The functions are modular and maintainable.x/logic/prolog/option.go (1)
- 9-88: The
GetOption
,GetOptionWithDefault
, andGetOptionAsAtomWithDefault
functions are well-implemented, providing a way to retrieve options from a Prolog term. The error handling is consistent, and the use ofIsEmptyList
andIsList
utility functions improves readability.x/logic/prolog/json_test.go (1)
- 12-81: The test cases for
ExtractJSONTerm
are comprehensive and cover various scenarios, including invalid functors, incorrect arity, and incorrect list structures. The use ofConvey
for nested test descriptions provides clarity on the test structure.x/logic/prolog/atom.go (1)
- 5-55: The file defines a comprehensive list of atoms used throughout the Prolog engine. The naming is consistent and follows the conventions of Prolog terms. This centralization of atom definitions will make it easier to manage and use these atoms across the codebase.
x/logic/prolog/unify.go (1)
- 5-71: The
UnifyFunctional
function provides a generic mechanism for unifying terms based on conversion functions. The logic is sound, and the function is well-documented, explaining the behavior and the conditions under which it operates.x/logic/predicate/address.go (1)
- 57-77: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [11-89]
The
Bech32Address
predicate function has been updated to use theprolog
package instead of theutil
package. The logic for converting between Bech32 and address pairs is correct, and the error handling is consistent with the rest of the codebase.x/logic/prolog/assert.go (1)
- 10-127: Utility functions like
IsList
,IsEmptyList
,IsVariable
,IsAtom
,IsCompound
,IsFullyInstantiated
,AreFullyInstantiated
,AssertAtom
,AssertCharacterCode
,AssertCharacter
, andAssertList
are correctly implemented and provide essential checks and assertions for Prolog terms.x/logic/predicate/did.go (1)
- 118-124: > Note: This review was outside the patches, so it was mapped to the patch with the greatest overlap. Original lines [11-143]
The
DIDComponents
predicate function and the helper functionsprocessSegment
anddidToTerms
are correctly updated to use theprolog
package. The logic for decomposing and reconstructing DIDs is sound, and the error handling is appropriate.x/logic/predicate/string.go (1)
- 83-151: The
StringBytes
predicate function is a new addition that handles the conversion between strings and byte lists with encoding considerations. The function is well-documented, and the implementation is consistent with the Prolog engine's standards.x/logic/predicate/encoding_test.go (1)
- 23-134: The test cases for the
HexBytes
predicate are comprehensive, covering various scenarios including successful conversions, mismatches, and error conditions. The use ofcheckSolutions
to validate the results is appropriate, and the error handling in the tests is consistent with expected outcomes.x/logic/predicate/json.go (8)
17-17: The import of the
prolog
package is correct and aligns with the PR objectives to replaceutil
package references withprolog
package references.68-68: The use of
prolog.StringToTerm
to convert a JSON byte slice to a Prolog term is consistent with the PR objectives.109-110: The call to
prolog.ExtractJSONTerm
is correct and aligns with the PR objectives to use theprolog
package for JSON term extraction.127-133: The use of
prolog.JSONBool
,prolog.JSONEmptyArray
, andprolog.JSONNull
for converting JSON types to Prolog terms is correct and aligns with the PR objectives.146-146: The conversion of a JSON number to a Prolog integer term is correct, and the error handling for non-integer and overflow cases is appropriate.
157-159: The conversion of JSON boolean and null values to Prolog terms using
prolog.JSONBool
andprolog.JSONNull
is correct.170-172: The construction of a JSON object as a Prolog term using
prolog.AtomPair.Apply
andprolog.AtomJSON.Apply
is correct and aligns with the PR objectives.176-176: The handling of an empty JSON array by returning
prolog.JSONEmptyArray()
is correct.x/logic/prolog/option_test.go (1)
- 1-1: The package name change from
util
toprolog
is correct and aligns with the PR objectives.x/logic/prolog/assert_test.go (3)
1-1: The package name change from
util
toprolog
is correct and aligns with the PR objectives.18-18: The update from
util.URLMatches
toprolog.URLMatches
is correct and aligns with the PR objectives to use theprolog
package.173-173: The use of
util.Indexed
andutil.WhitelistBlacklistMatches
is correct, but it should be noted that theutil
package is still being used here. This is acceptable if theutil
package still contains relevant utility functions not moved toprolog
.x/logic/predicate/uri.go (3)
10-10: The import of the
prolog
package is correct and aligns with the PR objectives to replaceutil
package references withprolog
package references.182-182: The use of
prolog.StringToTerm
to convert a decoded URI component to a Prolog term is consistent with the PR objectives.188-188: The use of
prolog.StringToTerm
to convert an encoded URI component to a Prolog term is consistent with the PR objectives.x/logic/predicate/bank.go (2)
11-11: The import of the
prolog
package is correct and aligns with the PR objectives to replaceutil
package references withprolog
package references.166-167: The use of
prolog.Tuple
to create a Prolog term from the bank balances is consistent with the PR objectives.x/logic/keeper/interpreter.go (3)
19-19: The import of the
prolog2
package is correct and aligns with the PR objectives to replaceutil
package references withprolog
package references.130-130: The use of
prolog2.PredicateMatches
to filter predicates is correct and aligns with the PR objectives.169-169: The use of
prolog2.PredicateMatches
within thetoPredicate
function is correct and aligns with the PR objectives.x/logic/predicate/address_test.go (3)
92-92: The updated error message in the test case provides more detailed information about the failure, which is helpful for debugging.
102-102: The updated error message in the test case provides more detailed information about the failure, which is helpful for debugging.
107-107: The updated error message in the test case provides more detailed information about the failure, which is helpful for debugging.
docs/predicate/predicates.md (1)
- 455-484: The documentation for the
string_bytes/3
predicate has been added and is well-written, providing clear examples and explanations. The signature and examples are consistent with the Prolog predicate conventions.No issues found in this documentation segment.
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.
That was huge! Seems clear to me, hopefully tests are here to help me gain confidence 😄
Thanks so much for your review @amimart. Indeed, it gets tricky and tests are definitely essential. |
Scope
This PR introduces implements the standard predicate
string_bytes/3
as specified by #497 and functionally aligned with the SWI-Prolog implementation (except for errors).Implementation details
The PR comes with many changes, my apologies for that. But the implementation required a refactoring of how prolog terms are processed during conversion to and from byte list representations. This involved introducing support for various encodings (e.g., UTF-8).
Additionally, a first step towards standardization of error messages has been undertaken. This primarily involves transitioning from text-described errors to errors represented by Prolog terms. These terms are highly compatible with the conventions outlined in SWI-Prolog's documentation. This foundation for the approach exists in Itchiban Prolog.
Finally, this PR lays the groundwork for implementing additional conversion predicates, particularly those related to
base64
andbase58
encodings.Summary by CodeRabbit
New Features
Improvements
Bug Fixes
Refactor
Tests
Documentation