| 1 | package OIDC::Client::Utils; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 11 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 11 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 11 | ▶ | use Moose::Exporter; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 11 | ▶ | use MooseX::Params::Validate; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 11 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | =encoding utf8 | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 NAME | ||||||||||||||
| 12 | |||||||||||||||
| 13 | OIDC::Client::Utils - Utility functions | ||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||
| 16 | |||||||||||||||
| 17 | Exports utility functions. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =cut | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 11 | Moose::Exporter->setup_import_methods(as_is => [qw/get_values_from_space_delimited_string | |||||||||||||
| 22 | reach_data | ||||||||||||||
| 23 | affect_data | ||||||||||||||
| 24 | delete_data/]); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 FUNCTIONS | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =head2 get_values_from_space_delimited_string( $value ) | ||||||||||||||
| 30 | |||||||||||||||
| 31 | Returns the values (arrayref) from a space-delimited string value. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =cut | ||||||||||||||
| 34 | |||||||||||||||
| 35 | sub get_values_from_space_delimited_string { | ||||||||||||||
| 36 | 24 | ▶ | my ($str) = pos_validated_list(\@_, { isa => 'Str', optional => 0 }); | ||||||||||||
|
Subroutine
get_values_from_space_delimited_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 37 | 24 | return [ grep { $_ ne '' } split(/\s+/, $str) ]; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | =head2 reach_data( $data_tree, \@path, $optional ) | ||||||||||||||
| 42 | |||||||||||||||
| 43 | Simplified data diver. No support for arrayref nodes. | ||||||||||||||
| 44 | |||||||||||||||
| 45 | Tries to find a node under root $data_tree, walking down the tree and choosing subnodes | ||||||||||||||
| 46 | according to values given in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 47 | |||||||||||||||
| 48 | While walking down the tree, if a key doesn't exist, undef is returned if the $optional | ||||||||||||||
| 49 | parameter is true (default value), otherwise an exception is thrown if the $optional parameter | ||||||||||||||
| 50 | is false. | ||||||||||||||
| 51 | |||||||||||||||
| 52 | No autovivification is performed by this function. | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =cut | ||||||||||||||
| 55 | |||||||||||||||
| 56 | sub reach_data { | ||||||||||||||
| 57 | 115 | ▶ | my ($data_tree, $path, $optional) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | ||||||||||||
|
Subroutine
reach_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 58 | { isa => 'ArrayRef[Str]', optional => 0 }, | ||||||||||||||
| 59 | { isa => 'Bool', default => 1 }); | ||||||||||||||
| 60 | 115 | foreach my $key (@$path) { | |||||||||||||
| 61 | 469 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: not a hashref to reach the value of the '$key' key"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 468 | ▶ | if (exists $data_tree->{$key}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 450 | $data_tree = $data_tree->{$key}; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | elsif ($optional) { | ||||||||||||||
| 66 | 16 | return; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | else { | ||||||||||||||
| 69 | 2 | croak("OIDC: the '$key' key is not present"); | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 96 | return $data_tree; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | |||||||||||||||
| 77 | =head2 affect_data( $data_tree, \@path, $value ) | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Walks down the $data_tree and sets the $value to the subnode according to values given | ||||||||||||||
| 80 | in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 81 | |||||||||||||||
| 82 | Arrayref nodes are not supported. | ||||||||||||||
| 83 | |||||||||||||||
| 84 | Autovivification can be performed by this function. | ||||||||||||||
| 85 | |||||||||||||||
| 86 | =cut | ||||||||||||||
| 87 | |||||||||||||||
| 88 | sub affect_data { | ||||||||||||||
| 89 | 76 | ▶ | my ($data_tree, $path, $value) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | ||||||||||||
|
Subroutine
affect_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 90 | { isa => 'ArrayRef[Str]', optional => 0 }, | ||||||||||||||
| 91 | { optional => 0 }); | ||||||||||||||
| 92 | 76 | ▶ | @$path >= 1 or croak(q{OIDC: to affect data, at least one value must be provided in the 'path' arrayref}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | |||||||||||||||
| 94 | 75 | my @path_to_iterate = @$path; | |||||||||||||
| 95 | 75 | my $key_to_assign_value = pop @path_to_iterate; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 75 | foreach my $key (@path_to_iterate) { | |||||||||||||
| 98 | 332 | ▶ | $data_tree = ($data_tree->{$key} //= {}); | ||||||||||||
|
Condition100%2/2
$data_tree->{$key} //= {}
MC/DC100%1/1
$data_tree->{$key} //= {}
$data_tree->{$key}
Truth table100%2/2
A
A= $data_tree->{$key}
| |||||||||||||||
| 99 | 332 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | } | ||||||||||||||
| 101 | 74 | $data_tree->{$key_to_assign_value} = $value; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 74 | return; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 delete_data( $data_tree, \@path ) | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Walks down the $data_tree, deletes the key of the last subnode and returns its value | ||||||||||||||
| 110 | according to values given in $path (which should be an arrayref of scalar values). | ||||||||||||||
| 111 | |||||||||||||||
| 112 | Arrayref nodes are not supported. | ||||||||||||||
| 113 | |||||||||||||||
| 114 | While walking down the tree, if a key doesn't exist, undef is returned. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | No autovivification is performed by this function. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =cut | ||||||||||||||
| 119 | |||||||||||||||
| 120 | sub delete_data { | ||||||||||||||
| 121 | 17 | ▶ | my ($data_tree, $path) = pos_validated_list(\@_, { isa => 'HashRef', optional => 0 }, | ||||||||||||
|
Subroutine
delete_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 122 | { isa => 'ArrayRef[Str]', optional => 0 }); | ||||||||||||||
| 123 | 17 | ▶ | @$path >= 1 or croak(q{OIDC: to delete data, at least one value must be provided in the 'path' arrayref}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | |||||||||||||||
| 125 | 16 | my @path_to_iterate = @$path; | |||||||||||||
| 126 | 16 | my $key_to_delete = pop @path_to_iterate; | |||||||||||||
| 127 | |||||||||||||||
| 128 | 16 | foreach my $key (@path_to_iterate) { | |||||||||||||
| 129 | 17 | ▶ | return unless defined $data_tree->{$key}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 14 | $data_tree = $data_tree->{$key}; | |||||||||||||
| 131 | 14 | ▶ | ref $data_tree eq 'HASH' or croak("OIDC: the value of the '$key' key is not a hash reference"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 12 | return delete $data_tree->{$key_to_delete}; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | |||||||||||||||
| 138 | 11 | 1; | |||||||||||||