-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tapioca compiler for JobIteration
It takes the job's `build_enumerator` method and generates the signatures for the job's corresponding `perform_later` and `perform_now` methods.
- Loading branch information
Showing
4 changed files
with
371 additions
and
0 deletions.
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
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,96 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(JobIteration::Iteration) | ||
|
||
require "prism" | ||
|
||
module Tapioca | ||
module Dsl | ||
module Compilers | ||
class JobIteration < Compiler | ||
extend T::Sig | ||
|
||
ConstantType = type_member { { fixed: T.class_of(::JobIteration::Iteration) } } | ||
|
||
sig { override.void } | ||
def decorate | ||
return unless constant.instance_methods(false).include?(:build_enumerator) | ||
|
||
root.create_path(constant) do |job| | ||
method = constant.instance_method(:build_enumerator) | ||
constant_name = name_of(constant) | ||
parameters = compile_method_parameters_to_rbi(method).reject do |typed_param| | ||
typed_param.param.name == "cursor" | ||
end | ||
|
||
expanded_parameters = parameters.flat_map do |typed_param| | ||
hash_param = typed_param.type.match(/\A\{.*\}\z/) | ||
if hash_param | ||
key_value_pairs = parse_hash_parameter(typed_param) | ||
key_value_pairs.map do |key, value| | ||
create_kw_param(key, type: value) | ||
end | ||
else | ||
typed_param | ||
end | ||
end | ||
|
||
return_type = compile_method_return_type_to_rbi(method) | ||
|
||
job.create_method( | ||
"perform_later", | ||
parameters: expanded_parameters, | ||
return_type: "T.any(#{constant_name}, FalseClass)", | ||
class_method: true, | ||
) | ||
|
||
job.create_method( | ||
"perform_now", | ||
parameters: expanded_parameters, | ||
return_type: return_type, | ||
class_method: true, | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def parse_hash_parameter(typed_param) | ||
parse_result = Prism.parse(typed_param.type) | ||
return "T.untyped" if parse_result.failure? | ||
|
||
visitor = HashParamVisitor.new | ||
parse_result.value.accept(visitor) | ||
visitor.key_value_pairs | ||
end | ||
|
||
class HashParamVisitor < Prism::Visitor | ||
attr_reader :key_value_pairs | ||
|
||
def initialize | ||
super | ||
@key_value_pairs = [] | ||
end | ||
|
||
def visit_hash_node(node) | ||
node.elements.each do |element| | ||
key = element.key.unescaped | ||
value = element.value.slice | ||
@key_value_pairs << [key, value] | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { override.returns(T::Enumerable[Module]) } | ||
def gather_constants | ||
all_classes.select { |c| c < ::JobIteration::Iteration } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.