-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
Introduce AST error compiler #216
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/schema/error_compiler' | ||
require 'dry/schema/ast_error_set' | ||
|
||
module Dry | ||
module Schema | ||
# Compiles rule results AST into machine-readable format | ||
# | ||
# @api private | ||
class AstErrorCompiler < ErrorCompiler | ||
# @api private | ||
def call(ast) | ||
AstErrorSet[ast] | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/schema/error_set' | ||
|
||
module Dry | ||
module Schema | ||
# A set of AST errors used to generate machine-readable errors | ||
# | ||
# @see Result#message_set | ||
# | ||
# @api public | ||
class AstErrorSet < ErrorSet | ||
private | ||
|
||
# @api private | ||
def errors_map(errors = self.errors) | ||
errors | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dry | ||
module Schema | ||
# Compiles rule results AST into some type of errors | ||
# | ||
# @api private | ||
class ErrorCompiler | ||
# @api private | ||
def call(_ast) | ||
raise NotImplementedError | ||
end | ||
|
||
# @api private | ||
def with(*args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
self | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/equalizer' | ||
|
||
module Dry | ||
module Schema | ||
# A set of generic errors | ||
# | ||
# @see Result#message_set | ||
# | ||
# @api public | ||
class ErrorSet | ||
include Enumerable | ||
include Dry::Equalizer(:errors, :options) | ||
|
||
# A list of compiled errors | ||
# | ||
# @return [Array<Any>] | ||
attr_reader :errors | ||
|
||
# Options hash | ||
# | ||
# @return [Hash] | ||
attr_reader :options | ||
|
||
# @api private | ||
def self.[](errors, options = EMPTY_HASH) | ||
new(errors, options) | ||
end | ||
|
||
# @api private | ||
def initialize(errors, options = EMPTY_HASH) | ||
@errors = errors | ||
@options = options | ||
end | ||
|
||
# Iterate over errors | ||
# | ||
# @example | ||
# result.errors.each do |message| | ||
# puts message.text | ||
# end | ||
# | ||
# @return [Array] | ||
# | ||
# @api public | ||
def each(&block) | ||
return self if empty? | ||
return to_enum unless block | ||
|
||
errors.each(&block) | ||
end | ||
|
||
# Dump message set to a hash | ||
# | ||
# @return [Hash<Symbol=>Array<String>>] | ||
# | ||
# @api public | ||
def to_h | ||
@to_h ||= errors_map | ||
end | ||
alias_method :to_hash, :to_h | ||
|
||
# Get a list of errors for the given key | ||
# | ||
# @param [Symbol] key | ||
# | ||
# @return [Array<String>] | ||
# | ||
# @api public | ||
def [](key) | ||
to_h[key] | ||
end | ||
|
||
# Get a list of errors for the given key | ||
# | ||
# @param [Symbol] key | ||
# | ||
# @return [Array<String>] | ||
# | ||
# @raise KeyError | ||
# | ||
# @api public | ||
def fetch(key) | ||
self[key] || raise(KeyError, "+#{key}+ error was not found") | ||
end | ||
|
||
# Check if an error set is empty | ||
# | ||
# @return [Boolean] | ||
# | ||
# @api public | ||
def empty? | ||
@empty ||= errors.empty? | ||
end | ||
|
||
# @api private | ||
def freeze | ||
to_h | ||
empty? | ||
super | ||
end | ||
|
||
# @api private | ||
def errors_map(_errors) | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
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
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
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
Oops, something went wrong.
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.
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.
Issue found: Extra blank line detected.