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
Basic DSL to define operations that can fail #6
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
waiting-for-dev
force-pushed
the
waiting-for-dev/operations
branch
from
October 2, 2023 14:48
d646a10
to
db90f8f
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
waiting-for-dev
force-pushed
the
waiting-for-dev/operations
branch
from
October 3, 2023 04:51
db90f8f
to
f43ef25
Compare
timriley
approved these changes
Oct 4, 2023
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.
Looking great, thanks @waiting-for-dev! I've one little request about flatting the file structure for our unit specs, but otherwise, let's get this in :)
We introduce a thin DSL on top of dry-monads' result type [1] to define operations that can fail. `Dry::Operation#steps` accepts a block where individual operations can be called with `#step`. When they return a `Success`, the inner value is automatically unwrapped, ready to be consumed by subsequen steps. When a `Failure` is returned along the way, the remaining steps are skipped and the failure is returned. Example: ```ruby require "dry/operation" class MyOperation < Dry::Operation def call(input) steps do attrs = step validate(input) user = step persist(attrs) step notify(user) user end end def validate(input) # Dry::Monads::Result::Success or Dry::Monads::Result::Failure end def persist(attrs) # Dry::Monads::Result::Success or Dry::Monads::Result::Failure end def notify(user) # Dry::Monads::Result::Success or Dry::Monads::Result::Failure end end include Dry::Monads[:result] case MyOperation.new.call(input) in Success(user) puts "User #{user.name} created" in Failure[:invalid_input, validation_errors] puts "Invalid input: #{validation_errors}" in Failure(:database_error) puts "Database error" in Failure(:email_error) puts "Email error" end ``` The approach is similar to the so-called "do notation" in Haskell [1], but done in an idiomatic Ruby way. There's no magic happening between every line within the block (i.e., "programmable semicolons"). Besides not being something possible in Ruby, it'd be very confusing for people to require all the lines to return a `Result` type (e.g., we want to allow debugging). Instead, it's required to unwrap intermediate results through the `step` method. Notice that not having logic to magically unwrap results is also intentional to allow flexibility to transform results in between steps (e.g., `validate(input).value_or({})`) [1] https://dry-rb.org/gems/dry-monads/1.6/result/ [2] https://en.wikibooks.org/wiki/Haskell/do_notation
waiting-for-dev
force-pushed
the
waiting-for-dev/operations
branch
from
October 4, 2023 12:41
f43ef25
to
a727bbf
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
We introduce a thin DSL on top of dry-monads' result type to define operations that can fail.
Dry::Operation#steps
accepts a block where individual operations can be called with#step
. When they return aSuccess
, the inner value is automatically unwrapped, ready to be consumed by subsequen steps. When aFailure
is returned along the way, the remaining steps are skipped and the failure is returned.Example:
The approach is similar to the so-called "do notation" in Haskell, but done in an idiomatic Ruby way. There's no magic happening between every line within the block (i.e., "programmable semicolons"). Besides not being something possible in Ruby, it'd be very confusing for people to require all the lines to return a
Result
type (e.g., we want to allow debugging). Instead, it's required to unwrap intermediate results through thestep
method. Notice that not having logic to magically unwrap results is also intentional to allow flexibility to transform results in between steps (e.g.,validate(input).value_or({})
)Please, check also the first two commits that perform some extra setup for development.