Skip to content

Commit a5361cb

Browse files
Add validation to cog configs
1 parent 3047d8f commit a5361cb

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/roast/dsl/cog/config.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ module Roast
55
module DSL
66
class Cog
77
class Config
8+
class ConfigError < Roast::Error; end
9+
10+
class InvalidConfigError < ConfigError; end
11+
12+
# Validate that the config instance has all required parameters set in an acceptable manner
13+
#
14+
# Inheriting cog should implement this for its config class if validation is desired.
15+
#
16+
#: () -> void
17+
def validate!; end
18+
819
#: Hash[Symbol, untyped]
920
attr_reader :values
1021

lib/roast/dsl/config_manager.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def config_for(cog_class, name = nil)
5353
end.values.each { |cfg| config = config.merge(cfg) }
5454
name_scoped_config = fetch_name_scoped_config(cog_class, name) unless name.nil?
5555
config = config.merge(name_scoped_config) if name_scoped_config
56+
config.validate!
5657
config
5758
end
5859

lib/roast/dsl/system_cogs/map.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ class Map < SystemCog
88
class Config < Cog::Config
99
#: (Integer) -> void
1010
def parallel(value)
11-
raise ArgumentError, "value must be >= 0" if value < 0
12-
1311
# treat 0 as unlimited parallelism
1412
@values[:parallel] = value > 0 ? value : nil
1513
end
@@ -24,9 +22,17 @@ def no_parallel!
2422
@values[:parallel] = 1
2523
end
2624

25+
def validate!
26+
valid_parallel!
27+
end
28+
2729
#: () -> Integer?
28-
def max_parallel_tasks
29-
@values.fetch(:parallel, 1)
30+
def valid_parallel!
31+
parallel = @values.fetch(:parallel, 1)
32+
return if parallel.nil?
33+
raise InvalidConfigError, "'parallel' must be >= 0 if specified" if parallel < 0
34+
35+
parallel
3036
end
3137
end
3238

@@ -103,7 +109,8 @@ def create_map_system_cog(params, input_proc)
103109
em
104110
end
105111

106-
max_parallel_semaphore = Async::Semaphore.new(config.max_parallel_tasks) if config.max_parallel_tasks.present?
112+
max_parallel_tasks = config.valid_parallel!
113+
max_parallel_semaphore = Async::Semaphore.new(max_parallel_tasks) if max_parallel_tasks.present?
107114
tasks = input.items.map.with_index do |item, index|
108115
if max_parallel_semaphore
109116
max_parallel_semaphore.async { create_and_run_execution_manager.call(item, index) }

0 commit comments

Comments
 (0)