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

Feat/string bytes #496

Merged
merged 7 commits into from
Jan 10, 2024
Merged

Feat/string bytes #496

merged 7 commits into from
Jan 10, 2024

Conversation

ccamel
Copy link
Member

@ccamel ccamel commented Jan 6, 2024

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 and base58 encodings.

Summary by CodeRabbit

  • New Features

    • Introduced new search and match capabilities with Prolog integration.
    • Enhanced JSON handling and conversion within Prolog terms.
    • Added support for advanced string and byte conversions.
    • Improved hexadecimal encoding and decoding functions.
  • Improvements

    • Updated error messaging for clarity and specificity in test functions.
    • Refined balance fetching logic with Prolog package utilization.
  • Bug Fixes

    • Fixed issues with incorrect package references, ensuring consistency across the codebase.
  • Refactor

    • Replaced deprecated utility functions with streamlined Prolog package methods.
    • Removed redundant code for a cleaner and more efficient codebase.
  • Tests

    • Expanded test coverage for new and existing functionalities, including encoding and JSON term extraction.
  • Documentation

    • Updated references to reflect the shift from utility to Prolog package use across various modules.

Copy link
Contributor

coderabbitai bot commented Jan 6, 2024

Walkthrough

The overarching change introduces a new prolog package, replacing util for various string, byte, and encoding operations in Prolog. It refines error messages, unifies hexadecimal byte conversion, and adds a string_bytes function. The update enhances the Prolog predicate handling, aligning with a more structured and unified approach for string and byte conversions across different encodings.

Changes

File Path Change Summary
x/logic/keeper/interpreter.go, x/logic/predicate/address.go, x/logic/predicate/bank.go, x/logic/predicate/crypto.go, x/logic/predicate/did.go, x/logic/predicate/json.go, x/logic/predicate/uri.go, x/logic/predicate/util.go Replaced util package usage with prolog package, updated function calls and type checks.
x/logic/predicate/address_test.go, x/logic/predicate/did_test.go Modified error messages for detailed failure contexts.
x/logic/predicate/crypto.go, x/logic/predicate/crypto_test.go Integrated HexBytes functionality into CryptoDataHash and removed specific test cases.
x/logic/predicate/encoding.go, x/logic/predicate/string.go, x/logic/prolog/encoding.go, x/logic/prolog/json.go, x/logic/prolog/list.go, x/logic/prolog/option.go, x/logic/prolog/term.go, x/logic/prolog/unify.go Added new functions for encoding, JSON handling, list retrieval, option handling, term conversion, and unification.
x/logic/predicate/encoding_test.go, x/logic/predicate/string_test.go, x/logic/prolog/assert_test.go, x/logic/prolog/json_test.go, x/logic/prolog/term_test.go Introduced new test cases for encoding, string and byte conversions, and JSON term extraction.
x/logic/prolog/assert.go, x/logic/prolog/atom.go, x/logic/prolog/error.go, x/logic/prolog/option_test.go Introduced functions for type checking, term assertions, error handling, and test case updates.

Related issues

  • okp4/okp4d#497: This issue could be linked to the PR as it discusses the introduction of a string_bytes/3 predicate, which is addressed by the new StringBytes function and related test cases in the changes.

Poem

In the realm of code, where logic's king, 🐇
A Prolog package, new functions bring. 📦
Bytes dance with strings, in a structured play,
As CodeRabbit hops, crafting code ballet. 🎭

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@ccamel ccamel self-assigned this Jan 7, 2024
@ccamel ccamel force-pushed the feat/string_bytes branch from 7322669 to 9767430 Compare January 7, 2024 15:45
@ccamel ccamel marked this pull request as ready for review January 7, 2024 15:52
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.

Review Status

Actionable comments generated: 7

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 77c7467 and 9767430.
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 of engine.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, and ValidCharacterCode 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 of engine.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 of engine.ListIterator to iterate over the list of pairs is appropriate.
x/logic/prolog/encoding.go (1)
  • 11-73: The Decode and Encode functions handle the conversion of byte slices to and from specified encodings. The use of charset.Lookup to find the encoding and the error handling with engine.DomainError are correctly implemented. The functions are modular and maintainable.
x/logic/prolog/option.go (1)
  • 9-88: The GetOption, GetOptionWithDefault, and GetOptionAsAtomWithDefault functions are well-implemented, providing a way to retrieve options from a Prolog term. The error handling is consistent, and the use of IsEmptyList and IsList 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 of Convey 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 the prolog package instead of the util 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, and AssertList 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 functions processSegment and didToTerms are correctly updated to use the prolog 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 of checkSolutions 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 replace util package references with prolog 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 the prolog package for JSON term extraction.

  • 127-133: The use of prolog.JSONBool, prolog.JSONEmptyArray, and prolog.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 and prolog.JSONNull is correct.

  • 170-172: The construction of a JSON object as a Prolog term using prolog.AtomPair.Apply and prolog.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 to prolog is correct and aligns with the PR objectives.
x/logic/prolog/assert_test.go (3)
  • 1-1: The package name change from util to prolog is correct and aligns with the PR objectives.

  • 18-18: The update from util.URLMatches to prolog.URLMatches is correct and aligns with the PR objectives to use the prolog package.

  • 173-173: The use of util.Indexed and util.WhitelistBlacklistMatches is correct, but it should be noted that the util package is still being used here. This is acceptable if the util package still contains relevant utility functions not moved to prolog.

x/logic/predicate/uri.go (3)
  • 10-10: The import of the prolog package is correct and aligns with the PR objectives to replace util package references with prolog 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 replace util package references with prolog 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 replace util package references with prolog 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 the toPredicate 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.

x/logic/prolog/term_test.go Show resolved Hide resolved
x/logic/prolog/term_test.go Show resolved Hide resolved
x/logic/predicate/crypto.go Show resolved Hide resolved
x/logic/predicate/crypto.go Show resolved Hide resolved
x/logic/predicate/crypto.go Show resolved Hide resolved
x/logic/predicate/string_test.go Show resolved Hide resolved
x/logic/prolog/term.go Show resolved Hide resolved
@ccamel ccamel requested a review from amimart January 7, 2024 16:08
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.

That was huge! Seems clear to me, hopefully tests are here to help me gain confidence 😄

@ccamel
Copy link
Member Author

ccamel commented Jan 10, 2024

Thanks so much for your review @amimart. Indeed, it gets tricky and tests are definitely essential.

@ccamel ccamel merged commit bf29e51 into main Jan 10, 2024
20 checks passed
@ccamel ccamel deleted the feat/string_bytes branch January 10, 2024 16:33
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