From 52bb1924572279381d81021ca458a6ed0013c0c8 Mon Sep 17 00:00:00 2001 From: Andrew Louis Date: Fri, 4 Aug 2023 13:23:38 -0400 Subject: [PATCH] Transformers can output multiple objects. Closes #66 --- lib/chronicle/etl/runner.rb | 18 +++++++++++++----- lib/chronicle/etl/transformers/transformer.rb | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/chronicle/etl/runner.rb b/lib/chronicle/etl/runner.rb index 5fe95a2..0bc148e 100644 --- a/lib/chronicle/etl/runner.rb +++ b/lib/chronicle/etl/runner.rb @@ -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)) @@ -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 diff --git a/lib/chronicle/etl/transformers/transformer.rb b/lib/chronicle/etl/transformers/transformer.rb index f45f070..5661618 100644 --- a/lib/chronicle/etl/transformers/transformer.rb +++ b/lib/chronicle/etl/transformers/transformer.rb @@ -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