Skip to content

Commit

Permalink
Move ItemBatcher validations to method
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Nov 6, 2024
1 parent 20acf94 commit 4af7a1d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/floe/workflow/item_batcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ def initialize(payload, name)
@max_items_per_batch_path = ReferencePath.new(payload["MaxItemsPerBatchPath"]) if payload["MaxItemsPerBatchPath"]
@max_input_bytes_per_batch_path = ReferencePath.new(payload["MaxInputBytesPerBatchPath"]) if payload["MaxInputBytesPerBatchPath"]

if [max_items_per_batch, max_input_bytes_per_batch, max_items_per_batch_path, max_input_bytes_per_batch_path].all?(&:nil?)
parser_error!("must have one of \"MaxItemsPerBatch\", \"MaxItemsPerBatchPath\", \"MaxInputBytesPerBatch\", \"MaxInputBytesPerBatchPath\"")
end

parser_error!("must not specify both \"MaxItemsPerBatch\" and \"MaxItemsPerBatchPath\"") if max_items_per_batch && max_items_per_batch_path
parser_error!("must not specify both \"MaxInputBytesPerBatch\" and \"MaxInputBytesPerBatchPath\"") if max_input_bytes_per_batch && max_input_bytes_per_batch_path
invalid_field_error!("MaxItemsPerBatch", max_items_per_batch, "must be a positive integer") if max_items_per_batch && max_items_per_batch <= 0
invalid_field_error!("MaxInputBytesPerBatch", max_input_bytes_per_batch, "must be a positive integer") if max_input_bytes_per_batch && max_input_bytes_per_batch <= 0
validate!
end

def value(context, input, state_input = nil)
Expand All @@ -37,6 +30,8 @@ def value(context, input, state_input = nil)
end
end

private

def max_items(context, state_input)
return max_items_per_batch if max_items_per_batch
return if max_items_per_batch_path.nil?
Expand All @@ -54,6 +49,17 @@ def max_input_bytes(context, state_input)

result
end

def validate!
if [max_items_per_batch, max_input_bytes_per_batch, max_items_per_batch_path, max_input_bytes_per_batch_path].all?(&:nil?)
parser_error!("must have one of \"MaxItemsPerBatch\", \"MaxItemsPerBatchPath\", \"MaxInputBytesPerBatch\", \"MaxInputBytesPerBatchPath\"")
end

parser_error!("must not specify both \"MaxItemsPerBatch\" and \"MaxItemsPerBatchPath\"") if max_items_per_batch && max_items_per_batch_path
parser_error!("must not specify both \"MaxInputBytesPerBatch\" and \"MaxInputBytesPerBatchPath\"") if max_input_bytes_per_batch && max_input_bytes_per_batch_path
invalid_field_error!("MaxItemsPerBatch", max_items_per_batch, "must be a positive integer") if max_items_per_batch && max_items_per_batch <= 0
invalid_field_error!("MaxInputBytesPerBatch", max_input_bytes_per_batch, "must be a positive integer") if max_input_bytes_per_batch && max_input_bytes_per_batch <= 0
end
end
end
end

0 comments on commit 4af7a1d

Please sign in to comment.