Skip to content

Conversation

@bkeepers
Copy link
Collaborator

This is a fairly significant refactor of Expressions (#692).

  1. Adds Constant (which quacks like an Expression) to represent explicit string, number, and boolean values. Note that there are still String, Number, and Boolean expressions, which will perform casting of values passed to them, but those expressions are no longer used by default to represent constant values when serialized:
    {
      "Equal" => [
        {"Property" => ["plan"]},
    -     {"String" => ["basic"]},
    +     "basic",
      ]
    }    ```
  2. A new expression is now a simple object that responds to #call and receives any arguments instead of subclassing Expression. All evaluating of arguments is done internally in Expression before being passed to the #call method. If the context: key is part of the method signature, it will be passed in.
    - class Flipper::Expressions::Property < Expression
    -   def initialize(args)
    -     super Array(args)
    -   end
    - 
    -   def evaluate(context = {})
    -     key = evaluate_arg(0, context)
    -     context.dig(:properties, key.to_s)
    -     end
    -   end
    - end
    + class Flipper::Expressions::Property
    +   def self.call(key, context:)
    +     context.dig(:properties, key.to_s)
    +   end
    + end

This greatly simplifies the process of adding new expressions internally, and paves the way for making it easy for users to create expressions of their own.

@bkeepers bkeepers requested a review from jnunemaker March 27, 2023 19:13
bkeepers added 10 commits March 27, 2023 15:57
…s-experiment

* origin/learn-the-rules: (57 commits)
  Add threadsafe: option to memory adapter
  Ensure memory adapter unlocked on fork
  Simplify boolean enabled
  Make expressions work with nil or empty array of actors
  Fix expression gate for no actor
  Add some actor specs
  a bit of formatting
  Get rid of shotgun
  Release 0.28.0
  Update changelog
  Add a few more multiple actor specs
  Add example with multiple actors and in different order
  Another type spot
  A couple minor type tweaks
  Just add multiple actors on
  Pass flipper_ids so we can make that supported on the backend in cloud
  flipper_id should be string
  Less change to log subscriber
  Fix benchmark language
  Show example of deprecated payload argument
  ...
Copy link
Collaborator

@jnunemaker jnunemaker left a comment

Choose a reason for hiding this comment

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

Looks great. I left a few minor things.

My only concern is the time stuff. I think it's too liberal right now for multi-language. We'll probably want to lock it down and only accept strings in a particular format or formats.

module Expressions
class Time
def self.call(value)
::Time.parse(value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will assume local time, not utc, won't it? I stumbled on this stack overflow when googling.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only if the string passed in excludes the timezone. In the schemas I'm working on, I explicitly defined Time to take a date-time, which JSON Schema defines as an ISO8601 formatted string. Here are the tests:

    Time
      valid
        ✓ {"Time":"2021-01-01T00:00:00Z"}
        ✓ {"Time":["2021-01-01T00:00:00Z"]}
        ✓ {"Time":"2021-01-01T00:00:00-05:00"}
        ✓ {"Time":["2021-01-01T00:00:00-05:00"]}
        ✓ {"Time":{"Property":"created_at"}}
        ✓ {"Time":[{"Property":"created_at"}]}
      invalid
        ✓ {"Time":"2021-01-01"}
        ✓ {"Time":"January 1, 2021 10:00"}
        ✓ {"Time":null}
        ✓ {"Time":false}
        ✓ {"Time":[{"Property":"created_at"},{"Property":"updated_at"}]}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I should add I'm planning to pull those schemas in and validate the input in Expression.build.

jnunemaker and others added 3 commits March 29, 2023 10:29
Note some of the breaking changes in more detail.
Co-authored-by: John Nunemaker <[email protected]>
* origin/main:
  Fix typo in comment
  Update Changelog.md
@bkeepers bkeepers merged commit b237c8c into learn-the-rules Mar 30, 2023
@bkeepers bkeepers deleted the expressions-experiment branch March 30, 2023 22:34
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.

4 participants