From f2812c477b230a1407381bfbbb393119c7280076 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Fri, 21 Sep 2018 14:18:31 +1200 Subject: Refactor config assertions --- IdaliusConfig.pm | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'IdaliusConfig.pm') diff --git a/IdaliusConfig.pm b/IdaliusConfig.pm index 4a8b42f..21c4efa 100644 --- a/IdaliusConfig.pm +++ b/IdaliusConfig.pm @@ -14,35 +14,29 @@ sub config_describe { return "$plugin -> $parm"; } -sub assert_scalar { - my ($config, $plugin, $parm) = @_; +sub assert_common { + my ($config, $plugin, $parm, $human_type, $ref_type) = @_; my $ref = $config->{$parm}; my $name = config_describe($plugin, $parm); - die "Error: Configuration \"$name\" must be scalar" unless + die "Error: Configuration \"$name\" must be of type $human_type" unless defined $ref - and ref($ref) eq ""; + and ref($ref) eq $ref_type; } -sub assert_list { +sub assert_scalar { my ($config, $plugin, $parm) = @_; - my $ref = $config->{$parm}; - my $name = config_describe($plugin, $parm); + assert_common($config, $plugin, $parm, "scalar", ""); +} - die "Error: Configuration \"$name\" must be list" unless - defined $ref - and ref($ref) eq "ARRAY"; +sub assert_list { + my ($config, $plugin, $parm) = @_; + assert_common($config, $plugin, $parm, "list", "ARRAY"); } sub assert_dict { my ($config, $plugin, $parm) = @_; - my $ref = $config->{$parm}; - my $name = config_describe($plugin, $parm); - - die "Error: Configuration \"$name\" must be dictionary" unless - defined $ref - and ref($ref) eq "HASH"; - + assert_common($config, $plugin, $parm, "dictionary", "HASH"); } # Check presence and/or sanity of config parameters for the bot's core -- cgit v1.1