| 1 | package Data::Record::Serialize::Util; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # ABSTRACT: Useful things | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 23 | ▶ | use v5.12; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 23 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 23 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 23 | our $VERSION = '2.05'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 23 | ▶ | use parent 'Exporter::Tiny'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 23 | ▶ | use Hash::Ordered; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 23 | my @TYPE_CATEGORY_NAMES; | |||||||||||||
| 15 | my %TYPES; | ||||||||||||||
| 16 | 23 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 23 | @TYPE_CATEGORY_NAMES = qw( | |||||||||||||
| 18 | ANY | ||||||||||||||
| 19 | INTEGER | ||||||||||||||
| 20 | FLOAT | ||||||||||||||
| 21 | NUMBER | ||||||||||||||
| 22 | STRING | ||||||||||||||
| 23 | NOT_STRING | ||||||||||||||
| 24 | BOOLEAN | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 23 | %TYPES = ( | |||||||||||||
| 28 | T_INTEGER => 'I', | ||||||||||||||
| 29 | T_NUMBER => 'N', | ||||||||||||||
| 30 | T_STRING => 'S', | ||||||||||||||
| 31 | T_BOOLEAN => 'B', | ||||||||||||||
| 32 | ); | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 23 | ▶ | use enum @TYPE_CATEGORY_NAMES; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 23 | ▶ | use constant \%TYPES; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | ## no critic(BuiltinFunctions::ProhibitComplexMappings) | ||||||||||||||
| 39 | our @TYPE_CATEGORIES = map { | ||||||||||||||
| 40 | 23 | ; # add a ; to help 5.10 | |||||||||||||
| 41 | 23 | ▶ | no strict 'refs'; ## no critic(ProhibitNoStrict) | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 161 | $_->(); | |||||||||||||
| 43 | } @TYPE_CATEGORY_NAMES; | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 23 | our %EXPORT_TAGS = ( | |||||||||||||
| 46 | types => [ keys %TYPES ], | ||||||||||||||
| 47 | categories => \@TYPE_CATEGORY_NAMES, | ||||||||||||||
| 48 | subs => [qw( is_type index_types populate_set )], | ||||||||||||||
| 49 | ); | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 23 | our @EXPORT_OK = map { @{$_} } values %EXPORT_TAGS; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 23 | my @TypeRE; | |||||||||||||
| 54 | $TypeRE[ $_->[0] ] = $_->[1] | ||||||||||||||
| 55 | 23 | for [ +( ANY ) => qr/.*/ ], | |||||||||||||
| 56 | [ +( STRING ) => qr/^S/i ], | ||||||||||||||
| 57 | [ +( FLOAT ) => qr/^N/i ], | ||||||||||||||
| 58 | [ +( INTEGER ) => qr/^I/i ], | ||||||||||||||
| 59 | [ +( BOOLEAN ) => qr/^B/i ], | ||||||||||||||
| 60 | [ +( NUMBER ) => qr/^[NI]/i ], | ||||||||||||||
| 61 | [ +( NOT_STRING ) => qr/^[^S]+/ ]; | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 96 | ▶ | sub is_type { | ||||||||||||
|
Subroutine
is_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 64 | 96 | my ( $type, $type_enum ) = @_; | |||||||||||||
| 65 | 96 | $type =~ $TypeRE[$type_enum]; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 89 | ▶ | sub index_types { | ||||||||||||
|
Subroutine
index_types: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 69 | 89 | my ( $types ) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 89 | my @fields = keys %$types; | |||||||||||||
| 72 | 89 | my @type_index; | |||||||||||||
| 73 | |||||||||||||||
| 74 | 89 | for my $category ( @TYPE_CATEGORIES ) { | |||||||||||||
| 75 | 623 | my $re = $TypeRE[$category]; | |||||||||||||
| 76 | 623 | $type_index[$category] = [ grep { $types->{$_} =~ $re } @fields ]; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 89 | return \@type_index; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | |||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | |||||||||||||||
| 86 | |||||||||||||||
| 87 | |||||||||||||||
| 88 | |||||||||||||||
| 89 | |||||||||||||||
| 90 | |||||||||||||||
| 91 | |||||||||||||||
| 92 | |||||||||||||||
| 93 | |||||||||||||||
| 94 | |||||||||||||||
| 95 | |||||||||||||||
| 96 | |||||||||||||||
| 97 | |||||||||||||||
| 98 | |||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | |||||||||||||||
| 105 | |||||||||||||||
| 106 | |||||||||||||||
| 107 | |||||||||||||||
| 108 | |||||||||||||||
| 109 | |||||||||||||||
| 110 | |||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | |||||||||||||||
| 114 | |||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | |||||||||||||||
| 118 | |||||||||||||||
| 119 | |||||||||||||||
| 120 | |||||||||||||||
| 121 | |||||||||||||||
| 122 | |||||||||||||||
| 123 | |||||||||||||||
| 124 | |||||||||||||||
| 125 | |||||||||||||||
| 126 | |||||||||||||||
| 127 | |||||||||||||||
| 128 | |||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | |||||||||||||||
| 132 | |||||||||||||||
| 133 | |||||||||||||||
| 134 | |||||||||||||||
| 135 | |||||||||||||||
| 136 | |||||||||||||||
| 137 | |||||||||||||||
| 138 | |||||||||||||||
| 139 | |||||||||||||||
| 140 | 24 | ▶ | sub populate_set { | ||||||||||||
|
Subroutine
populate_set: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||
| 141 | 24 | my ( $values, $input ) = @_; | |||||||||||||
| 142 | |||||||||||||||
| 143 | 24 | ▶ | return [] unless @{$input}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | |||||||||||||||
| 145 | 23 | my $output = Hash::Ordered->new; | |||||||||||||
| 146 | |||||||||||||||
| 147 | # if first element is a deletion, preload all possible values | ||||||||||||||
| 148 | 23 | ▶ | if ( substr( $input->[0], 0, 1 ) eq q{-} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | 7 | $output->push( $_ ) for @{$values}; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 23 | for my $elem ( @{$input} ) { | |||||||||||||
| 153 | 44 | ▶ | if ( $elem eq q{-} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | 3 | $output->clear; | |||||||||||||
| 155 | 3 | next; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | 41 | ▶ | if ( $elem eq q{+} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 3 | $output->clear; | |||||||||||||
| 160 | 3 | $output->push( $_ ) for @{$values}; | |||||||||||||
| 161 | 3 | next; | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 38 | my $op = substr( $elem, 0, 1 ); | |||||||||||||
| 165 | 38 | ▶ | $op eq q{-} ? $output->delete( substr( $elem, 1 ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 166 | : $op eq q{+} ? $output->push( substr( $elem, 1 ) ) | ||||||||||||||
| 167 | : $output->push( $elem ); | ||||||||||||||
| 168 | } | ||||||||||||||
| 169 | 23 | return [ $output->keys ]; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | |||||||||||||||
| 173 | |||||||||||||||
| 174 | |||||||||||||||
| 175 | 23 | 1; | |||||||||||||
| 176 | |||||||||||||||
| 177 | # | ||||||||||||||
| 178 | # This file is part of Data-Record-Serialize | ||||||||||||||
| 179 | # | ||||||||||||||
| 180 | # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory. | ||||||||||||||
| 181 | # | ||||||||||||||
| 182 | # This is free software, licensed under: | ||||||||||||||
| 183 | # | ||||||||||||||
| 184 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||
| 185 | # | ||||||||||||||
| 186 | |||||||||||||||
| 187 | __END__ | ||||||||||||||