Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

[ufc] fix string conversion#51

Merged
schmit merged 2 commits intomainfrom
sps/rules-string-conversion
Jun 7, 2024
Merged

[ufc] fix string conversion#51
schmit merged 2 commits intomainfrom
sps/rules-string-conversion

Conversation

@schmit
Copy link

@schmit schmit commented Jun 6, 2024


labels: mergeable

Fixes: #issue

Motivation and Context

Ensure consistent conversion of subject attribute values to strings across SDKs

Description

Custom conversion from integer floats and booleans to strings

Related PR to update docs:
Eppo-exp/eppo-docs#382

How has this been tested?

  • new tests
  • passes general SDK tests

Summary by CodeRabbit

  • New Features

    • Introduced a function to convert various attribute types to strings for better comparison.
  • Tests

    • Added tests for the new string conversion function with different data types.
    • Updated existing tests to handle boolean values as strings.

@schmit schmit requested review from aarsilv and greghuels June 6, 2024 23:01
@schmit schmit changed the title fix string conversion [ufc] fix string conversion Jun 6, 2024
return False


def to_string(value: AttributeType) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Handle an array of strings?

Copy link
Member

Choose a reason for hiding this comment

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

null?

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

You could normalize boolean, None and arrays by converting to JSON. Just about every language has some library that can convert to JSON, so this would work for every SDK:
Screenshot 2024-06-07 at 8 25 54 AM

So the logic might be:

if isinstance(value, str):
  return value
elif value.is_integer():
  return str(int(value)) 
return json.dumps(value)

Copy link
Author

Choose a reason for hiding this comment

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

AttributeType = Union[str, int, float, bool]

Attribute cannot be an array -- but good call that we do want to handle null.

Using json is a great suggestion, but I do want to make sure we aren't losing a lot of performance (I've seen json being very slow, but perhaps here it's sufficiently quickly given that the input is small)

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

Yeah, that's a valid concern. It still might be useful as an "else" case for any value that we don't expect (sort of a cross-language toString()), while handling the cases that we expect in a more performant way, but I'm good with whatever you decide so long as we handle None / null

Copy link
Author

Choose a reason for hiding this comment

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

Screenshot 2024-06-07 at 10 30 35 AM

Seems like using json is equally fast

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

it looks like they're equal because they both landed on if isinstance(value, str):. You should try it out with no conditionals -- just running the functions we want tested: (str vs json.dumps)

Copy link

@greghuels greghuels Jun 7, 2024

Choose a reason for hiding this comment

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

But in any case, the json.dumps only happens after all other conditionals fail, so we're still good here.

Copy link
Author

Choose a reason for hiding this comment

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

Oh good catch 😓

Copy link

@greghuels greghuels left a comment

Choose a reason for hiding this comment

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

suggestion to use json to handle edge cases

@schmit
Copy link
Author

schmit commented Jun 7, 2024

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jun 7, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link

coderabbitai bot commented Jun 7, 2024

Walkthrough

The recent changes in the eppo_client module involve renaming the SubjectAttributes type to AttributeType and introducing a new to_string function in rules.py. This function converts different attribute types to strings for comparison within the evaluate_condition function. Corresponding updates and new tests have been added to test/rules_test.py to handle these changes, including boolean value handling and testing the to_string function.

Changes

Files Change Summary
eppo_client/rules.py Renamed SubjectAttributes to AttributeType and added to_string function for type conversion.
test/rules_test.py Added to_string function, modified evaluate_condition calls for boolean strings, added tests.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant Rules
    participant TestSuite

    Client->>Rules: Call evaluate_condition()
    Rules->>Rules: Convert attributes using to_string()
    Rules-->>Client: Return evaluation result

    TestSuite->>Rules: Call to_string() with various data types
    Rules-->>TestSuite: Return string representations
    TestSuite->>Rules: Call evaluate_condition() with boolean values as strings
    Rules-->>TestSuite: Return evaluation results
Loading

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 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

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.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

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

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c6ce44d and 6294f06.

Files selected for processing (2)
  • eppo_client/rules.py (3 hunks)
  • test/rules_test.py (4 hunks)
Additional comments not posted (3)
eppo_client/rules.py (1)

52-52: Consider caching the string conversion to improve performance.
[PERFORMANCE]
In conditions like ONE_OF and NOT_ONE_OF, the to_string(subject_value) is called multiple times which can be inefficient. Consider caching the result in a variable at the start of the function to avoid repeated conversions.

Also applies to: 56-56, 59-59, 63-65

test/rules_test.py (2)

409-425: Consider adding edge case tests for to_string.
[TESTS]
The current tests cover basic functionality. Consider adding tests for edge cases such as empty strings, very large numbers, and special characters to ensure robustness.


141-156: Expand testing for boolean values in conditions.
[TESTS]
The tests for boolean values are a good start, but consider adding more scenarios to cover cases where boolean values are mixed with other data types or used in complex expressions to ensure comprehensive coverage.

Also applies to: 364-367

Comment on lines +124 to +131
def to_string(value: AttributeType) -> str:
if isinstance(value, str):
return value
elif isinstance(value, bool):
return "true" if value else "false"
elif isinstance(value, float):
return str(int(value)) if value.is_integer() else str(value)
return str(value)
Copy link

Choose a reason for hiding this comment

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

Optimize float conversion in to_string.

- return str(int(value)) if value.is_integer() else str(value)
+ return f"{value:.0f}" if value.is_integer() else str(value)

This change uses formatted strings to handle float to integer conversion more efficiently.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def to_string(value: AttributeType) -> str:
if isinstance(value, str):
return value
elif isinstance(value, bool):
return "true" if value else "false"
elif isinstance(value, float):
return str(int(value)) if value.is_integer() else str(value)
return str(value)
def to_string(value: AttributeType) -> str:
if isinstance(value, str):
return value
elif isinstance(value, bool):
return "true" if value else "false"
elif isinstance(value, float):
return f"{value:.0f}" if value.is_integer() else str(value)
return str(value)

@schmit schmit requested a review from greghuels June 7, 2024 17:31
@schmit schmit merged commit 7300f72 into main Jun 7, 2024
@schmit schmit deleted the sps/rules-string-conversion branch June 7, 2024 18:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants