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

ffi: Add Value class to represent all supported primitive values for the key-value pair IR format. #502

Merged
merged 13 commits into from
Aug 8, 2024

Conversation

LinZhihao-723
Copy link
Member

@LinZhihao-723 LinZhihao-723 commented Jul 31, 2024

Description

This PR adds a class named Value, which wraps std::variant to implement a supertype for all the supported value types in the key-value pair IR format. The core idea of the Value class is to provide type safety, using templates to guard the supported types, as well as the methods to:

  • Construct the value
  • Query the type of the underlying value
  • Get an immutable view of the underlying value

The current implementation's support types include int64_t, double, bool, std::string, and EncodedTextAst (both four-byte encoding and eight-byte encoding). The value can also be null if it is constructed without any input (implemented using std::monostate).
With the current design, it should be easy to extend the supported types in future development.

Validation performed

  1. Ensure all workflow passed.
  2. Add new unit tests to cover all the supported types.

gibber9809
gibber9809 previously approved these changes Jul 31, 2024
Copy link
Contributor

@gibber9809 gibber9809 left a comment

Choose a reason for hiding this comment

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

LGTM. PR title is also fine by me.

Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

Part 1

components/core/src/clp/ffi/Value.hpp Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
components/core/src/clp/ffi/Value.hpp Outdated Show resolved Hide resolved
Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

Part 2

Comment on lines 78 to 85
REQUIRE(int_value.is<value_int_t>());
REQUIRE((int_value.get_immutable_view<value_int_t>() == cIntVal));
REQUIRE_FALSE(int_value.is_null());
REQUIRE_FALSE(int_value.is<value_float_t>());
REQUIRE_FALSE(int_value.is<value_bool_t>());
REQUIRE_FALSE(int_value.is<string>());
REQUIRE_FALSE(int_value.is<EightByteEncodedTextAst>());
REQUIRE_FALSE(int_value.is<FourByteEncodedTextAst>());
Copy link
Member

Choose a reason for hiding this comment

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

We can extract this into a method that basically tests

REQUIRE((std::is_same_v<value_int_t, Type> == value.is<value_int_t>()));

for each type, and then just passes in the values defined here, right?

Comment on lines 86 to 90
REQUIRE_THROWS(int_value.get_immutable_view<value_float_t>());
REQUIRE_THROWS(int_value.get_immutable_view<value_bool_t>());
REQUIRE_THROWS(int_value.get_immutable_view<string>());
REQUIRE_THROWS(int_value.get_immutable_view<EightByteEncodedTextAst>());
REQUIRE_THROWS(int_value.get_immutable_view<FourByteEncodedTextAst>());
Copy link
Member

Choose a reason for hiding this comment

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

We can extract this into a method that does something like:

if constexpr (std::is_same_v<value_int_t, Type>) {
    REQUIRE(value.get_immutable_view<Type>() == typed_value);
} else {
    REQUIRE_THROWS(value.get_immutable_view<value_int_t>());
}

for each type, right?

components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
@LinZhihao-723
Copy link
Member Author

To be consistent within the same file, and also not be confusing with using Type = xxx, I switch Type to T in all templates

components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
components/core/tests/test-ffi_Value.cpp Outdated Show resolved Hide resolved
Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

For the PR title, how about:

ffi: Add Value class to represent all supported primitive values for the key-value pair IR format.

@LinZhihao-723 LinZhihao-723 changed the title ffi: Add Value class to represent all supported primitive values in the key-value pair IR format. ffi: Add Value class to represent all supported primitive values for the key-value pair IR format. Aug 8, 2024
@LinZhihao-723 LinZhihao-723 merged commit c3cdf66 into y-scope:main Aug 8, 2024
13 checks passed
jackluo923 pushed a commit to jackluo923/clp that referenced this pull request Dec 4, 2024
…r the key-value pair IR format. (y-scope#502)

Co-authored-by: kirkrodrigues <[email protected]>
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.

3 participants