Skip to content

Commit

Permalink
Transformers can output multiple objects. Closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
hyfen committed Aug 4, 2023
1 parent f96d111 commit 52bb192
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/chronicle/etl/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,24 @@ def run_extraction
end

def process_extraction(extraction)
# For each extraction from our extractor, we create a new tarnsformer
# For each extraction from our extractor, we create a new transformer
transformer = @job.instantiate_transformer(extraction)

# And then transform that record, logging it if we're in debug log level
record = transformer.transform
# And then transform the record, capturing the new object(s)
new_objects = [transformer.transform].flatten

# raise an error unless all new_objects are a Base
unless new_objects.all? { |r| r.is_a?(Chronicle::ETL::Models::Base) || r.is_a?(Chronicle::ETL::Models::Raw) }
raise(Chronicle::ETL::RunnerError, "Expected transformer to output a Chronicle ETL Model")
end

Chronicle::ETL::Logger.debug(tty_log_transformation(transformer))
@job_logger.log_transformation(transformer)

# Then send the results to the loader
@loader.load(record) unless @job.dry_run?
new_objects.each do |object|
@loader.load(object) unless @job.dry_run?
end
rescue Chronicle::ETL::TransformationError => e
# TODO: have an option to cancel job if we encounter an error
Chronicle::ETL::Logger.error(tty_log_transformation_failure(e, transformer))
Expand All @@ -116,7 +124,7 @@ def tty_log_transformation(transformer)

def tty_log_transformation_failure(exception, transformer)
output = " ✖".red
output += " Failed to build #{transformer}. #{exception.message}"
output += " Failed to transform #{transformer}. #{exception.message}"
end

def tty_log_completion
Expand Down
2 changes: 1 addition & 1 deletion lib/chronicle/etl/transformers/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def to_s

identifier = begin
unknown = self.class.to_s
friendly_identifier || self.class.to_s
friendly_identifier || "instance of #{self.class.to_s}"
rescue TransformationError, NotImplementedError
unknown
end
Expand Down

0 comments on commit 52bb192

Please sign in to comment.