| 1 | # | ||||||||||
| 2 | # This file is part of Config-Model | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is Copyright (c) 2005-2022 by Dominique Dumont. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software, licensed under: | ||||||||||
| 7 | # | ||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||
| 9 | # | ||||||||||
| 10 | package Config::Model::Value::UpdateFromFile 2.169; | ||||||||||
| 11 | |||||||||||
| 12 | 59 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 59 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 59 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 59 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 59 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 59 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 59 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 59 | ▶ | use Config::Model::Exception; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 59 | ▶ | use feature qw/postderef signatures/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 59 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 59 | my $logger = get_logger("Tree::Element::Value"); | |||||||||
| 26 | 59 | my $user_logger = get_logger("User"); | |||||||||
| 27 | |||||||||||
| 28 | my %update_dispatch = ( | ||||||||||
| 29 | 4 | ▶ | ini => sub ($self, $file, $subpath) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 4 | _lazy_load("INI", "Config::INI::Reader"); | |||||||||
| 31 | 4 | my $data = Config::INI::Reader->read_file($file); | |||||||||
| 32 | 4 | return $self->__data_from_path($data, $subpath, 2); | |||||||||
| 33 | }, | ||||||||||
| 34 | 1 | ▶ | json => sub ($self, $file, $subpath) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 1 | _lazy_load("JSON", "JSON"); | |||||||||
| 36 | 1 | JSON->import(); | |||||||||
| 37 | # utf8 decode is done by JSON module, so slurp_raw must be used | ||||||||||
| 38 | 1 | my $data = decode_json(path($file)->slurp_raw); | |||||||||
| 39 | 1 | return $self->__data_from_path($data, $subpath); | |||||||||
| 40 | }, | ||||||||||
| 41 | 1 | ▶ | yaml => sub ($self, $file, $subpath) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 1 | _lazy_load("YAML", "YAML::PP"); | |||||||||
| 43 | 1 | my $data = YAML::PP->new()-> load_file($file); | |||||||||
| 44 | 1 | return $self->__data_from_path($data, $subpath); | |||||||||
| 45 | }, | ||||||||||
| 46 | 1 | ▶ | toml => sub ($self, $file, $subpath) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | _lazy_load("TOML", "TOML::Tiny"); | |||||||||
| 48 | 1 | my $data = TOML::Tiny->new()-> decode(path($file)->slurp_utf8); | |||||||||
| 49 | 1 | return $self->__data_from_path($data, $subpath); | |||||||||
| 50 | }, | ||||||||||
| 51 | 59 | ); | |||||||||
| 52 | |||||||||||
| 53 | 59 | has [qw/file type subpath location/] => (is => 'rw', isa => 'Str'); | |||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | sub get_info ($self) { | ||||||||
|
Subroutine
get_info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 1 | return $self->file.':'.$self->subpath; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 7 | ▶ | sub __data_from_path ($self, $data, $path, $limit = 0) { | ||||||||
|
Subroutine
__data_from_path: called CC=5 SCAR=17.1
| |||||||||||
| 60 | # negative look behing assertion to skip \. in subpath | ||||||||||
| 61 | 7 | foreach my $step (split /(?<!\\)\./, $path, $limit) { | |||||||||
| 62 | 14 | $step =~ s/\\\././g; # convert \. to . | |||||||||
| 63 | 14 | ▶ | $data = (ref($data) eq 'HASH') ? $data->{$step} : $data->[$step]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 14 | ▶ | return $data if not ref $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | } | ||||||||||
| 66 | 0 | $user_logger->warn("Did not get scalar value with $path: got $data"); | |||||||||
| 67 | 0 | return; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 7 | ▶ | sub _lazy_load ($type, $module) { | ||||||||
|
Subroutine
_lazy_load: called CC=2 SCAR=7.2
| |||||||||||
| 71 | 7 | my $file = ($module =~ s!::!/!gr).".pm"; | |||||||||
| 72 | 7 | eval { require $file }; | |||||||||
| 73 | 7 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | $user_logger->logdie( | |||||||||
| 75 | "Error: Value update from $type file requires ". | ||||||||||
| 76 | "$module module. Please install this module." | ||||||||||
| 77 | ); | ||||||||||
| 78 | } | ||||||||||
| 79 | 7 | return; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # returns (value, undef) or (undef, error) | ||||||||||
| 83 | 8 | ▶ | sub get_update_value ($self) { | ||||||||
|
Subroutine
get_update_value: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 84 | 8 | my $type = $self->type; | |||||||||
| 85 | 8 | my $file = $self->file; | |||||||||
| 86 | 8 | my $subpath = $self->subpath; | |||||||||
| 87 | |||||||||||
| 88 | 8 | $logger->debug("update called on type $type, file $file, path $subpath"); | |||||||||
| 89 | 8 | ▶ | if ( !-e $file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 1 | return ( undef, "Cannot update value: $file does not exist" ); | |||||||||
| 91 | } | ||||||||||
| 92 | 7 | my $update_sub = $update_dispatch{$type}; | |||||||||
| 93 | 7 | ▶ | if ( not defined $update_sub ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | $logger->logdie( | |||||||||
| 95 | "Unexpected ", $self->location, | ||||||||||
| 96 | "Value update type $type. Allowed values are ", | ||||||||||
| 97 | join( ', ', keys %update_dispatch ) | ||||||||||
| 98 | ); | ||||||||||
| 99 | 0 | return; | |||||||||
| 100 | } | ||||||||||
| 101 | 7 | my $v = $update_sub->( $self, $file, $subpath ); | |||||||||
| 102 | 7 | ▶ | my $err = defined $v ? '' : "No value found in file $file, path $subpath"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 7 | return ( $v, $err ); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | |||||||||||
| 107 | 59 | 1; | |||||||||
| 108 | |||||||||||
| 109 | # ABSTRACT: Retrieve data from external file | ||||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||