From 02844348159b438d75eb7376ddd8aadb7183846e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 16 Sep 2022 19:48:35 -0400 Subject: [PATCH] fix(tools): allow subroutine execution before reading $anvil->data --- tools/anvil-access-module | 59 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/tools/anvil-access-module b/tools/anvil-access-module index d14ac8ba..0be4860e 100755 --- a/tools/anvil-access-module +++ b/tools/anvil-access-module @@ -17,6 +17,11 @@ if (($running_directory =~ /^\./) && ($ENV{PWD})) my $anvil = Anvil::Tools->new(); +sub is_array +{ + return ref($_[0]) eq "ARRAY"; +} + sub is_hash { return ref($_[0]) eq "HASH"; @@ -36,6 +41,43 @@ sub db_access : $anvil->Database->query($access_parameters); } +sub call_pre_data_fns +{ + my $parameters = shift; + my $fns = $parameters->{fns}; + + if (is_array($fns)) + { + foreach my $fn_wrapper ( @{$fns} ) + { + if (is_array($fn_wrapper)) + { + my $pre_chain = @{$fn_wrapper}[0] // ""; + my @fn_params = @{$fn_wrapper}[1..$#{$fn_wrapper}] // (); + my @chain = split(/->|,/, $pre_chain); + my $intermediate = $anvil; + my $key_index = 0; + + foreach my $key ( @chain ) + { + last if not defined $intermediate->${key}; + + if ($key_index == $#chain && $intermediate->can($key)) + { + eval { $intermediate->${key}(@fn_params); }; + } + else + { + $intermediate = $intermediate->${key}; + } + + $key_index += 1; + } + } + } + } +} + sub get_anvil_data { my $parameters = shift; @@ -69,7 +111,7 @@ sub get_anvil_data } } -sub run_fn +sub call_fn { my $parameters = shift; my $chain = $parameters->{chain}; @@ -109,7 +151,7 @@ sub foreach_nested push(@{$chain}, $key); - $is_continue_chain = run_fn({ chain => $chain, fallback => $is_continue_chain, fn => $on_key }); + $is_continue_chain = call_fn({ chain => $chain, fallback => $is_continue_chain, fn => $on_key }); if ( ($is_continue_chain) && is_hash($value) ) { @@ -123,7 +165,7 @@ sub foreach_nested } else { - run_fn({ chain => $chain, fn => $on_chain_end }); + call_fn({ chain => $chain, fn => $on_chain_end }); } pop(@{$chain}); @@ -171,6 +213,17 @@ elsif ($anvil->${sub_module_name}->can($sub_name)) } elsif ($data_hash) { + if ($pre_data) + { + my $decoded_pre_data; + my $is_decode_pre_data_success = eval { $decoded_pre_data = decode_json($pre_data); }; + + if ($is_decode_pre_data_success && is_array($decoded_pre_data)) + { + call_pre_data_fns({ fns => $decoded_pre_data }); + } + } + my $decoded_data_hash; my $is_decode_data_hash_success = eval { $decoded_data_hash = decode_json($data_hash); };