Commit fb2385f7 authored by Stefan Tauner's avatar Stefan Tauner
Browse files

fiji_scripts: allow value validation to include old value

parent 7ec8fd69
......@@ -17,7 +17,10 @@ use warnings;
# natural: values must be oct, hex, binary strings looking like a real number.
# hexadecimal: values must be hexadecimal numbers.
# boolean: will be convert to a truth value by Perl semantics
# - values = (optional) an array reference listing all valid values (emulates an enum)
# - values = (optional) used to restrict valid values. Can be...
# - an array reference listing all valid values (emulates an enum)
# - a code reference to a function that gets the new and old values and
# returns true if the new value is allowed, or false otherwise.
# - phases_opt = (optional) list of phases (subset of "setup", "instrument", "download")
# where no value must be present in input (e.g. ID is only necessary while downloading).
# - default = (optional) default value if not given in file and not determinable otherwise
......
......@@ -292,10 +292,18 @@ sub validate_fiu_value {
return validate_value(FIUMAP, $k, $v_ref);
}
# Do validation (and conversation from external->internal representation)
sub validate_value ($$$;$) {
## @function validate_value (%$map_ref, $k, $$v_ref, $old, $log_func)
# Do validation (and conversation from external->internal representation)
#
# \param map_ref reference to FIJI Settings mappings
# \param k key identifying the respective setting
# \param v_ref scalar reference to the proposed value (that may be modified)
# \param old (optional) previously valid value
# \param log_func (optional) the (log4perl) log function to use
# (defaul is \&Log::Log4perl::Logger::trace)
sub validate_value ($$$;$$) {
my $logger = get_logger();
my ($map_ref, $k, $v_ref, $log_func) = @_;
my ($map_ref, $k, $v_ref, $old, $log_func) = @_;
$log_func = \&Log::Log4perl::Logger::trace if !defined($log_func);
if (defined($map_ref->{$k}->{'type'})) {
my $orig = ${$v_ref};
......@@ -357,7 +365,7 @@ sub validate_value ($$$;$) {
return 0;
}
} elsif (ref($values_ref) eq 'CODE') {
if (!$values_ref->(${$v_ref})) {
if (!$values_ref->(${$v_ref}, $old)) {
$log_func->($logger, "$k: ${$v_ref} is not allowed.");
return 0;
}
......
......@@ -504,9 +504,10 @@ sub _validate_fiu_entry {
sub _validate_entry {
my ($map, $widget, $name, $new, $diff, $old, $char_idx, $type) = @_;
my ($map, $widget, $name, $new, $diff, $old_str, $char_idx, $type) = @_;
my $new_bak = $new;
my $ok = FIJI::Settings::validate_value($map, $name, \$new);
my $old_val = ${$widget->{'fiji_backend_ref'}};
my $ok = FIJI::Settings::validate_value($map, $name, \$new, $old_val);
_highlight_widget($widget, (!$ok));
if ($ok) {
${$widget->{'fiji_backend_ref'}} = $new;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment