| 1 | #!/usr/bin/perl -w | ||||||||||||||||||
| 2 | # | ||||||||||||||||||
| 3 | # Data::Toolkit::Map | ||||||||||||||||||
| 4 | # | ||||||||||||||||||
| 5 | # Andrew Findlay | ||||||||||||||||||
| 6 | # Nov 2006 | ||||||||||||||||||
| 7 | # andrew.findlay@skills-1st.co.uk | ||||||||||||||||||
| 8 | # | ||||||||||||||||||
| 9 | # $Id: Map.pm 388 2013-08-30 15:19:23Z remotesvn $ | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | package Data::Toolkit::Map; | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 5 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 14 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | 5 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 5 | ▶ | use Clone qw(clone); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | Data::Toolkit::Map | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | Data::Toolkit::Map objects implement mapping functions for attribute names | ||||||||||||||||||
| 25 | and values in Data::Toolkit::Entry objects. This is useful when converting between | ||||||||||||||||||
| 26 | different data representations in directory-synchronisation projects. | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | my $map = Data::Toolkit::Map->new(); | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | $map->set("surname", "sn" ); | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | $map->set("objectclass", [ "inetOrgPerson", "organizationalPerson", "person" ] ); | ||||||||||||||||||
| 35 | $map->set("phone","+44 1234 567890"); | ||||||||||||||||||
| 36 | $map->set("address", \&buildAddress); | ||||||||||||||||||
| 37 | $map->set("fn", sub { return firstValue("fullname", @_) }); | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | $arrayRef = $map->outputs(); | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | $values = $map->generate('attributeName', $entry [, $entry...]); | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | $newEntry = $map->newEntry($source1, $source2 ...); | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | $result = $map->delete('thisAttribute'); | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | my $currentDebugLevel = Data::Toolkit::Map->debug(); | ||||||||||||||||||
| 48 | my $newDebugLevel = Data::Toolkit::Map->debug(1); | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | my $string = $map->dump(); | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | =head1 DEPENDENCIES | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | Carp | ||||||||||||||||||
| 55 | Clone | ||||||||||||||||||
| 56 | Data::Dumper | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =cut | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | ######################################################################## | ||||||||||||||||||
| 61 | # Package globals | ||||||||||||||||||
| 62 | ######################################################################## | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 5 | ▶ | use vars qw($VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 65 | 5 | $VERSION = '1.0'; | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | # Set this non-zero for debug logging | ||||||||||||||||||
| 68 | # | ||||||||||||||||||
| 69 | 5 | my $debug = 0; | |||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | ######################################################################## | ||||||||||||||||||
| 72 | # Constructors and destructors | ||||||||||||||||||
| 73 | ######################################################################## | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | =head1 Constructor | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | =head2 new | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | my $map = Data::Toolkit::Map->new(); | ||||||||||||||||||
| 80 | my $map = Data::Toolkit::Map->new( {configAttrib => value, ....} ); | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | Creates an object of type Data::Toolkit::Map | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | Optionally accepts a hash of configuration items chosen from this list: | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | =over | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =item caseSensitiveNames | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | If this is defined with a true value then attribute names are case-sensitive. | ||||||||||||||||||
| 91 | By default they are not, so "Surname", "surname", and "SurName" are all the same attribute. | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | =item defaultMissingValueBehaviour | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | This is a hash defining what to do when mapping attributes that do not have values. | ||||||||||||||||||
| 96 | The keys are: | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | =over | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =item missing | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | Defines the behaviour when an input attribute is entirely missing. | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | =item noValues | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | Defines the behaviour when an input attribute exists but it has no values. | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | =item nullValue | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | Defines the behaviour when an input attribute exists and its first value is undef. | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | =item emptyString | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | Defines the behaviour when an input attribute has an empty string as its first value. | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | =back | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | The possible values are: | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | =over | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | =item delete | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | Delete the attribute entirely from the output of the map. | ||||||||||||||||||
| 125 | If the generate method is used on such an attribute, it will return undef. | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | =item noValues | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | The attribute will appear in the output of the map but no values will be defined. | ||||||||||||||||||
| 130 | If the generate method is used on such an attribute, it will return an empty array. | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | =item nullValue | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | The attribute will appear in the output of the map and its single value will be undef. | ||||||||||||||||||
| 135 | If the generate method is used on such an attribute, it will return an array containing one undef element. | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | =item emptyString | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | The attribute will appear in the output of the map with a single empty string value. | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | =item A subroutine reference or closure | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | If a pointer to an executable procedure is used as the value, that procedure | ||||||||||||||||||
| 144 | will be called and its return value used as the value of the attribute. | ||||||||||||||||||
| 145 | The return value can be undef, scalar, vector, or hash. | ||||||||||||||||||
| 146 | There is no way for the subroutine to request deletion of the entire attribute. | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | The subroutine is called with the name of the attribute being generated as its first parameter, | ||||||||||||||||||
| 149 | an indication of whether an array result is wanted as its second parameter, | ||||||||||||||||||
| 150 | and a reference to the input entry as its third parameter: | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | subroutine( $attributename, $wantarray, $entry); | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | =back | ||||||||||||||||||
| 155 | |||||||||||||||||||
| 156 | The default missing value behaviour is: | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | { | ||||||||||||||||||
| 159 | missing => 'delete', | ||||||||||||||||||
| 160 | noValues => 'noValues', | ||||||||||||||||||
| 161 | nullValue => 'nullValue', | ||||||||||||||||||
| 162 | emptyString => 'emptyString', | ||||||||||||||||||
| 163 | }; | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | =back | ||||||||||||||||||
| 166 | |||||||||||||||||||
| 167 | =cut | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | 6 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||
| 170 | 6 | my $class = shift; | |||||||||||||||||
| 171 | 6 | my $configParam = shift; | |||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | 6 | my $self = {}; | |||||||||||||||||
| 174 | 6 | $self->{mapping} = {}; | |||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | # Take a copy of the config hash | ||||||||||||||||||
| 177 | # - we don't want to store a ref to the one we were given | ||||||||||||||||||
| 178 | # in case it is part of another object | ||||||||||||||||||
| 179 | # | ||||||||||||||||||
| 180 | 6 | ▶ | if (defined($configParam)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 181 | 2 | ▶ | if ((ref $configParam) ne 'HASH') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 182 | 0 | croak "Data::Toolkit::Map->new expects a hash ref but was given something else" | |||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | |||||||||||||||||||
| 185 | 2 | $self->{config} = clone($configParam); | |||||||||||||||||
| 186 | } | ||||||||||||||||||
| 187 | else { | ||||||||||||||||||
| 188 | # Start with empty config | ||||||||||||||||||
| 189 | 4 | $self->{config} = {}; | |||||||||||||||||
| 190 | # Add the default missing value behaviour | ||||||||||||||||||
| 191 | $self->{config}->{defaultMissingValueBehaviour} = { | ||||||||||||||||||
| 192 | 4 | missing => 'delete', | |||||||||||||||||
| 193 | noValues => 'noValues', | ||||||||||||||||||
| 194 | nullValue => 'nullValue', | ||||||||||||||||||
| 195 | emptyString => 'emptyString', | ||||||||||||||||||
| 196 | }; | ||||||||||||||||||
| 197 | } | ||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | 6 | bless ($self, $class); | |||||||||||||||||
| 200 | |||||||||||||||||||
| 201 | 6 | ▶ | carp "Data::Toolkit::Map->new $self" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 202 | 6 | return $self; | |||||||||||||||||
| 203 | } | ||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | 6 | ▶ | sub DESTROY { | ||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||||||||||
| 206 | 6 | my $self = shift; | |||||||||||||||||
| 207 | 6 | ▶ | carp "Data::Toolkit::Map Destroying $self" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 208 | } | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | ######################################################################## | ||||||||||||||||||
| 211 | # Methods | ||||||||||||||||||
| 212 | ######################################################################## | ||||||||||||||||||
| 213 | |||||||||||||||||||
| 214 | =head1 Methods | ||||||||||||||||||
| 215 | |||||||||||||||||||
| 216 | =cut | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | ######################################## | ||||||||||||||||||
| 219 | |||||||||||||||||||
| 220 | =head2 set | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | Set or replace a mapping. | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | $map->set( outputAttribute, generator ) | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | outputAttribute must be a text string. Generator can be of several types: | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | =over | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | =item SCALAR - the value is the name of an attribute in the source entry, which is copied | ||||||||||||||||||
| 231 | to the outputAttribute | ||||||||||||||||||
| 232 | |||||||||||||||||||
| 233 | =item ARRAY - the value is a fixed array of strings which will be used as the value of | ||||||||||||||||||
| 234 | the outputAttribute | ||||||||||||||||||
| 235 | |||||||||||||||||||
| 236 | =item CODE - the value is a procedure or closure that is run to generate the value of | ||||||||||||||||||
| 237 | the outputAttribute. The procedure must return undef or an array reference. | ||||||||||||||||||
| 238 | |||||||||||||||||||
| 239 | =back | ||||||||||||||||||
| 240 | |||||||||||||||||||
| 241 | This is a simple mapping that generates a "surname" attribute by copying | ||||||||||||||||||
| 242 | the value of the input entry's "sn" attribute: | ||||||||||||||||||
| 243 | |||||||||||||||||||
| 244 | $map->set("surname", "sn" ); | ||||||||||||||||||
| 245 | |||||||||||||||||||
| 246 | This is a fixed mapping generating an LDAP objectClass attribute with | ||||||||||||||||||
| 247 | several values: | ||||||||||||||||||
| 248 | |||||||||||||||||||
| 249 | $map->set("objectclass", [ "inetOrgPerson", "organizationalPerson", "person" ] ); | ||||||||||||||||||
| 250 | |||||||||||||||||||
| 251 | This is a fixed mapping generating a single value (note the use of a list | ||||||||||||||||||
| 252 | to distinguish this from the first case above): | ||||||||||||||||||
| 253 | |||||||||||||||||||
| 254 | $map->set("phone", ["+44 1234 567890"]); | ||||||||||||||||||
| 255 | |||||||||||||||||||
| 256 | This is a dynamic mapping where the attribute is generated by a procedure: | ||||||||||||||||||
| 257 | |||||||||||||||||||
| 258 | $map->set("address", \&buildAddress); | ||||||||||||||||||
| 259 | |||||||||||||||||||
| 260 | When a dynamic mapping is evaluated, it is given the name of the attribute being generated | ||||||||||||||||||
| 261 | followed by all the parameters that were passed to the "generate" call, | ||||||||||||||||||
| 262 | so it can refer to entries and other objects. | ||||||||||||||||||
| 263 | |||||||||||||||||||
| 264 | Similarly, closures can be used: | ||||||||||||||||||
| 265 | |||||||||||||||||||
| 266 | $map->set("fn", sub { return firstValue("xyzzy", @_) }); | ||||||||||||||||||
| 267 | |||||||||||||||||||
| 268 | In this example, when the firstValue() procedure is called by "generate", | ||||||||||||||||||
| 269 | it gets one fixed parameter plus anything else that was passed to the "generate" call. | ||||||||||||||||||
| 270 | Thus the call: | ||||||||||||||||||
| 271 | |||||||||||||||||||
| 272 | $map->generate("fn",$entry) | ||||||||||||||||||
| 273 | |||||||||||||||||||
| 274 | would result in a call like this: | ||||||||||||||||||
| 275 | |||||||||||||||||||
| 276 | firstValue("fn","xyzzy",$entry) | ||||||||||||||||||
| 277 | |||||||||||||||||||
| 278 | |||||||||||||||||||
| 279 | =cut | ||||||||||||||||||
| 280 | |||||||||||||||||||
| 281 | 35 | ▶ | sub set { | ||||||||||||||||
|
Subroutine
set: called CC=5 SCAR=16.8
Pod
documented
| |||||||||||||||||||
| 282 | 35 | my $self = shift; | |||||||||||||||||
| 283 | 35 | my $attrib = shift; | |||||||||||||||||
| 284 | 35 | my $values = shift; | |||||||||||||||||
| 285 | |||||||||||||||||||
| 286 | 35 | ▶ | croak "set requires an attribute name" if (!$attrib); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 287 | 35 | ▶ | croak "set requires a value" if (!$values); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 288 | |||||||||||||||||||
| 289 | # Lower-case the attribute name if necessary | ||||||||||||||||||
| 290 | 35 | ▶ | $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 291 | |||||||||||||||||||
| 292 | 35 | ▶ | carp "Data::Toolkit::Map->set attribute '$attrib'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 293 | |||||||||||||||||||
| 294 | 35 | return $self->{mapping}->{$attrib} = $values; | |||||||||||||||||
| 295 | } | ||||||||||||||||||
| 296 | |||||||||||||||||||
| 297 | |||||||||||||||||||
| 298 | ######################################## | ||||||||||||||||||
| 299 | |||||||||||||||||||
| 300 | =head2 unset | ||||||||||||||||||
| 301 | |||||||||||||||||||
| 302 | Removes an attribute from a map. | ||||||||||||||||||
| 303 | Returns a reference to the deleted value. | ||||||||||||||||||
| 304 | |||||||||||||||||||
| 305 | =cut | ||||||||||||||||||
| 306 | |||||||||||||||||||
| 307 | 1 | ▶ | sub unset { | ||||||||||||||||
|
Subroutine
unset: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||
| 308 | 1 | my $self = shift; | |||||||||||||||||
| 309 | 1 | my $attrib = shift; | |||||||||||||||||
| 310 | |||||||||||||||||||
| 311 | 1 | ▶ | croak "unset requires an attribute name" if (!$attrib); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 312 | |||||||||||||||||||
| 313 | # Lower-case the attribute name if necessary | ||||||||||||||||||
| 314 | 1 | ▶ | $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 315 | |||||||||||||||||||
| 316 | 1 | ▶ | carp "Data::Toolkit::Map->unset attribute '$attrib'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 317 | |||||||||||||||||||
| 318 | 1 | return delete $self->{mapping}->{$attrib}; | |||||||||||||||||
| 319 | } | ||||||||||||||||||
| 320 | |||||||||||||||||||
| 321 | |||||||||||||||||||
| 322 | ######################################## | ||||||||||||||||||
| 323 | |||||||||||||||||||
| 324 | =head2 outputs | ||||||||||||||||||
| 325 | |||||||||||||||||||
| 326 | Return the list of attributes that the map generates. | ||||||||||||||||||
| 327 | |||||||||||||||||||
| 328 | Returns an empty list if there are no attributes. | ||||||||||||||||||
| 329 | |||||||||||||||||||
| 330 | $arrayRef = $map->outputs(); | ||||||||||||||||||
| 331 | |||||||||||||||||||
| 332 | =cut | ||||||||||||||||||
| 333 | |||||||||||||||||||
| 334 | 10 | ▶ | sub outputs { | ||||||||||||||||
|
Subroutine
outputs: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 335 | 10 | my $self = shift; | |||||||||||||||||
| 336 | |||||||||||||||||||
| 337 | 10 | my @keys_list = sort(CORE::keys %{$self->{mapping}}); | |||||||||||||||||
| 338 | 10 | ▶ | carp "Data::Toolkit::Map->outputs are: " . (join ',', @keys_list) if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 339 | 10 | ▶ | return( wantarray ? @keys_list : \@keys_list ); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 340 | |||||||||||||||||||
| 341 | } | ||||||||||||||||||
| 342 | |||||||||||||||||||
| 343 | ######################################## | ||||||||||||||||||
| 344 | # | ||||||||||||||||||
| 345 | # generateMissingValue( $attrib, $wantarray, $requiredBehaviour, $entry ); | ||||||||||||||||||
| 346 | # | ||||||||||||||||||
| 347 | # Internal procedure for handling missing values | ||||||||||||||||||
| 348 | |||||||||||||||||||
| 349 | 15 | ▶ | sub generateMissingValue { | ||||||||||||||||
|
Subroutine
generateMissingValue: called CC=11 SCAR=24.7
Pod
undocumented
| |||||||||||||||||||
| 350 | 15 | my ($attrib, $wantarray, $requiredBehaviour, $entry) = @_; | |||||||||||||||||
| 351 | |||||||||||||||||||
| 352 | 15 | ▶ | croak "generateMissingValue needs a requiredBehaviour parameter" if (!$requiredBehaviour); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 353 | |||||||||||||||||||
| 354 | 15 | ▶ | carp "generateMissingValue '$attrib', $wantarray, '$requiredBehaviour'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 355 | 15 | ▶ | if ( $requiredBehaviour eq 'delete' ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 356 | 7 | return undef; | |||||||||||||||||
| 357 | } | ||||||||||||||||||
| 358 | |||||||||||||||||||
| 359 | 8 | ▶ | if ($requiredBehaviour eq 'noValues') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 360 | # Return an empty array | ||||||||||||||||||
| 361 | 1 | my $res = []; | |||||||||||||||||
| 362 | 1 | ▶ | return ($wantarray ? @$res : $res); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 363 | } | ||||||||||||||||||
| 364 | |||||||||||||||||||
| 365 | 7 | ▶ | if ($requiredBehaviour eq 'nullValue') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 366 | # Return an array containing an undef value | ||||||||||||||||||
| 367 | 1 | my $res = [ undef ]; | |||||||||||||||||
| 368 | 1 | ▶ | return ($wantarray ? @$res : $res); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 369 | } | ||||||||||||||||||
| 370 | |||||||||||||||||||
| 371 | 6 | ▶ | if ($requiredBehaviour eq 'emptyString') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 372 | 1 | my $res = ['']; | |||||||||||||||||
| 373 | 1 | ▶ | return ($wantarray ? @$res : $res); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 374 | } | ||||||||||||||||||
| 375 | |||||||||||||||||||
| 376 | 5 | ▶ | if ((ref $requiredBehaviour) eq 'CODE') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 377 | # We have been given some code to run | ||||||||||||||||||
| 378 | 5 | return &$requiredBehaviour($attrib, $wantarray, $entry); | |||||||||||||||||
| 379 | } | ||||||||||||||||||
| 380 | |||||||||||||||||||
| 381 | 0 | croak "generateMissingValue was given an invalid requiredBehaviour parameter ($requiredBehaviour)"; | |||||||||||||||||
| 382 | } | ||||||||||||||||||
| 383 | |||||||||||||||||||
| 384 | |||||||||||||||||||
| 385 | ######################################## | ||||||||||||||||||
| 386 | |||||||||||||||||||
| 387 | =head2 generate | ||||||||||||||||||
| 388 | |||||||||||||||||||
| 389 | Generate a list of values for a given attribute. | ||||||||||||||||||
| 390 | |||||||||||||||||||
| 391 | $values = $map->generate('attributeName', $entry ); | ||||||||||||||||||
| 392 | |||||||||||||||||||
| 393 | =cut | ||||||||||||||||||
| 394 | |||||||||||||||||||
| 395 | 37 | ▶ | sub generate { | ||||||||||||||||
|
Subroutine
generate: called CC=25 SCAR=34.3
Pod
documented
| |||||||||||||||||||
| 396 | 37 | my $self = shift; | |||||||||||||||||
| 397 | 37 | my $attrib = shift; | |||||||||||||||||
| 398 | |||||||||||||||||||
| 399 | 37 | ▶ | croak "generate requires an attribute name" if (!$attrib); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 400 | |||||||||||||||||||
| 401 | # Lower-case the attribute name if necessary | ||||||||||||||||||
| 402 | 37 | ▶ | $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 403 | |||||||||||||||||||
| 404 | 37 | ▶ | carp "Data::Toolkit::Map->generate for attribute '$attrib'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 405 | |||||||||||||||||||
| 406 | 37 | my $mapping = $self->{mapping}->{$attrib}; | |||||||||||||||||
| 407 | # If that is undef or empty, return it immediately | ||||||||||||||||||
| 408 | 37 | ▶ | return $mapping if !$mapping; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 409 | |||||||||||||||||||
| 410 | 36 | my $refMap = ref $mapping; | |||||||||||||||||
| 411 | 36 | ▶ | if (!$refMap) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 412 | # We have a mapping but it is not a reference | ||||||||||||||||||
| 413 | 24 | ▶ | carp "Data::Toolkit::Map->generate attribute '$attrib' from source attribute '$mapping'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 414 | |||||||||||||||||||
| 415 | # Must be a simple attribute map so get the source entry | ||||||||||||||||||
| 416 | 24 | my $entry = shift; | |||||||||||||||||
| 417 | 24 | my @values = $entry->get($mapping); | |||||||||||||||||
| 418 | |||||||||||||||||||
| 419 | 24 | ▶ | if (not $values[0]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 420 | # We may have a missing value or attribute in the source | ||||||||||||||||||
| 421 | # (or it may just be zero...) so do some checks | ||||||||||||||||||
| 422 | 15 | my $valref = $entry->get($mapping); | |||||||||||||||||
| 423 | 15 | ▶ | if (not defined($valref)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 424 | # The attribute is entirely missing in the source | ||||||||||||||||||
| 425 | 6 | ▶ | carp "generate attribute '$attrib' from missing source attr" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 426 | return generateMissingValue( $attrib, wantarray, | ||||||||||||||||||
| 427 | 6 | $self->{config}->{defaultMissingValueBehaviour}->{missing}, $entry ); | |||||||||||||||||
| 428 | } | ||||||||||||||||||
| 429 | 9 | ▶ | if ((scalar @$valref) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 430 | # The attribute is present but has no values | ||||||||||||||||||
| 431 | 3 | ▶ | carp "generate attribute '$attrib' from source attr with no values" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 432 | return generateMissingValue( $attrib, wantarray, | ||||||||||||||||||
| 433 | 3 | $self->{config}->{defaultMissingValueBehaviour}->{noValues}, $entry ); | |||||||||||||||||
| 434 | } | ||||||||||||||||||
| 435 | 6 | ▶ | if (not defined($valref->[0])) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 436 | # The attribute is present but has a null value | ||||||||||||||||||
| 437 | 3 | ▶ | carp "generate attribute '$attrib' from null valued source attr" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 438 | return generateMissingValue( $attrib, wantarray, | ||||||||||||||||||
| 439 | 3 | $self->{config}->{defaultMissingValueBehaviour}->{nullValue}, $entry ); | |||||||||||||||||
| 440 | } | ||||||||||||||||||
| 441 | 3 | ▶ | if ($valref->[0] eq '') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 442 | # The attribute is present and the value is an empty string | ||||||||||||||||||
| 443 | 3 | ▶ | carp "generate attribute '$attrib' from empty string source attr" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 444 | return generateMissingValue( $attrib, wantarray, | ||||||||||||||||||
| 445 | 3 | $self->{config}->{defaultMissingValueBehaviour}->{emptyString}, $entry ); | |||||||||||||||||
| 446 | } | ||||||||||||||||||
| 447 | # In all other cases, just return what was there in the source entry | ||||||||||||||||||
| 448 | } | ||||||||||||||||||
| 449 | |||||||||||||||||||
| 450 | 9 | ▶ | return wantarray ? @values : \@values; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 451 | } | ||||||||||||||||||
| 452 | elsif ($refMap eq 'ARRAY') { | ||||||||||||||||||
| 453 | # Arrays represent constant data so just return it as-is | ||||||||||||||||||
| 454 | 3 | ▶ | carp "Data::Toolkit::Map->generate attribute '$attrib' from fixed array" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 455 | |||||||||||||||||||
| 456 | 3 | ▶ | return wantarray ? @$mapping : $mapping; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 457 | } | ||||||||||||||||||
| 458 | elsif ($refMap eq 'CODE') { | ||||||||||||||||||
| 459 | # We have been given some code to run | ||||||||||||||||||
| 460 | 9 | ▶ | carp "Data::Toolkit::Map->generate attribute '$attrib' from supplied code" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 461 | |||||||||||||||||||
| 462 | 9 | my $result = &$mapping($attrib, @_); | |||||||||||||||||
| 463 | # Do some sanity checking on the result | ||||||||||||||||||
| 464 | 9 | ▶ | return undef if !defined($result); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 465 | 7 | my $resType = ref $result; | |||||||||||||||||
| 466 | 7 | ▶ | $resType = 'SCALAR' if !$resType; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 467 | 7 | ▶ | if ($resType ne 'ARRAY') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 468 | 0 | croak "mapping procedure returned $resType while mapping for '$attrib' - it should have returned an ARRAY"; | |||||||||||||||||
| 469 | } | ||||||||||||||||||
| 470 | |||||||||||||||||||
| 471 | 7 | return $result; | |||||||||||||||||
| 472 | } | ||||||||||||||||||
| 473 | else { | ||||||||||||||||||
| 474 | # Don't know what to do with this! | ||||||||||||||||||
| 475 | 0 | croak "generate does not know how to handle a $refMap mapping"; | |||||||||||||||||
| 476 | } | ||||||||||||||||||
| 477 | |||||||||||||||||||
| 478 | } | ||||||||||||||||||
| 479 | |||||||||||||||||||
| 480 | ######################################## | ||||||||||||||||||
| 481 | |||||||||||||||||||
| 482 | =head2 newEntry | ||||||||||||||||||
| 483 | |||||||||||||||||||
| 484 | Create a new entry object by applying a map to one or more existing entries | ||||||||||||||||||
| 485 | |||||||||||||||||||
| 486 | $newEntry = $map->newEntry($source1, $source2 ...); | ||||||||||||||||||
| 487 | |||||||||||||||||||
| 488 | The source objects are Data::Toolkit::Entry objects | ||||||||||||||||||
| 489 | |||||||||||||||||||
| 490 | =cut | ||||||||||||||||||
| 491 | |||||||||||||||||||
| 492 | 3 | ▶ | sub newEntry { | ||||||||||||||||
|
Subroutine
newEntry: called CC=4 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 493 | 3 | my $self = shift; | |||||||||||||||||
| 494 | |||||||||||||||||||
| 495 | # If we have been passed any source entries, use the first one as a template | ||||||||||||||||||
| 496 | # to create the new entry | ||||||||||||||||||
| 497 | 3 | ▶ | if ($_[0]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 498 | 3 | ▶ | carp "Data::Toolkit::Map->newEntry from template entry" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 499 | 3 | my $template = shift; | |||||||||||||||||
| 500 | 3 | return $template->map( $self, @_ ); | |||||||||||||||||
| 501 | } | ||||||||||||||||||
| 502 | |||||||||||||||||||
| 503 | # Hmm - we seem to be mapping from nothing to create something! | ||||||||||||||||||
| 504 | 0 | ▶ | carp "Data::Toolkit::Map->newEntry from nothing" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 505 | |||||||||||||||||||
| 506 | # Create a new entry with default config to act as source | ||||||||||||||||||
| 507 | 0 | my $newEntry = Data::Template::Entry->new(); | |||||||||||||||||
| 508 | |||||||||||||||||||
| 509 | 0 | return $newEntry->map( $self ); | |||||||||||||||||
| 510 | } | ||||||||||||||||||
| 511 | |||||||||||||||||||
| 512 | |||||||||||||||||||
| 513 | ######################################## | ||||||||||||||||||
| 514 | |||||||||||||||||||
| 515 | =head2 delete | ||||||||||||||||||
| 516 | |||||||||||||||||||
| 517 | Delete an output from a map. | ||||||||||||||||||
| 518 | |||||||||||||||||||
| 519 | $result = $map->delete('thisAttribute'); | ||||||||||||||||||
| 520 | |||||||||||||||||||
| 521 | =cut | ||||||||||||||||||
| 522 | |||||||||||||||||||
| 523 | 0 | ▶ | sub delete { | ||||||||||||||||
|
Subroutine
delete: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||
| 524 | 0 | my $self = shift; | |||||||||||||||||
| 525 | 0 | my $attrib = shift; | |||||||||||||||||
| 526 | |||||||||||||||||||
| 527 | 0 | ▶ | croak "delete requires an attribute name" if (!$attrib); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 528 | |||||||||||||||||||
| 529 | # Lower-case the attribute name if necessary | ||||||||||||||||||
| 530 | 0 | ▶ | $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames}); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 531 | |||||||||||||||||||
| 532 | 0 | ▶ | carp "Data::Toolkit::Map->delete '$attrib'" if $debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 533 | |||||||||||||||||||
| 534 | 0 | return delete $self->{$attrib}; | |||||||||||||||||
| 535 | } | ||||||||||||||||||
| 536 | |||||||||||||||||||
| 537 | |||||||||||||||||||
| 538 | ######################################################################## | ||||||||||||||||||
| 539 | # Debugging methods | ||||||||||||||||||
| 540 | ######################################################################## | ||||||||||||||||||
| 541 | |||||||||||||||||||
| 542 | =head1 Debugging methods | ||||||||||||||||||
| 543 | |||||||||||||||||||
| 544 | =head2 debug | ||||||||||||||||||
| 545 | |||||||||||||||||||
| 546 | Set and/or get the debug level for Data::Toolkit::Map | ||||||||||||||||||
| 547 | |||||||||||||||||||
| 548 | my $currentDebugLevel = Data::Toolkit::Map->debug(); | ||||||||||||||||||
| 549 | my $newDebugLevel = Data::Toolkit::Map->debug(1); | ||||||||||||||||||
| 550 | |||||||||||||||||||
| 551 | Any non-zero debug level causes the module to print copious debugging information. | ||||||||||||||||||
| 552 | |||||||||||||||||||
| 553 | Note that this is a package method, not an object method. It should always be | ||||||||||||||||||
| 554 | called exactly as shown above. | ||||||||||||||||||
| 555 | |||||||||||||||||||
| 556 | All debug information is reported using "carp" from the Carp module, so if | ||||||||||||||||||
| 557 | you want a full stack backtrace included you can run your program like this: | ||||||||||||||||||
| 558 | |||||||||||||||||||
| 559 | perl -MCarp=verbose myProg | ||||||||||||||||||
| 560 | |||||||||||||||||||
| 561 | =cut | ||||||||||||||||||
| 562 | |||||||||||||||||||
| 563 | # Class method to set and/or get debug level | ||||||||||||||||||
| 564 | # | ||||||||||||||||||
| 565 | 4 | ▶ | sub debug { | ||||||||||||||||
|
Subroutine
debug: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||
| 566 | 4 | my $class = shift; | |||||||||||||||||
| 567 | 0 | ▶ | if (ref $class) { croak "Class method 'debug' called as object method" } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 568 | # print "DEBUG: ", (join '/', @_), "\n"; | ||||||||||||||||||
| 569 | 4 | ▶ | $debug = shift if (@_ == 1); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 570 | 4 | return $debug | |||||||||||||||||
| 571 | } | ||||||||||||||||||
| 572 | |||||||||||||||||||
| 573 | ######################################## | ||||||||||||||||||
| 574 | |||||||||||||||||||
| 575 | =head2 dump | ||||||||||||||||||
| 576 | |||||||||||||||||||
| 577 | Returns a text representation of the map. | ||||||||||||||||||
| 578 | |||||||||||||||||||
| 579 | my $string = $map->dump(); | ||||||||||||||||||
| 580 | |||||||||||||||||||
| 581 | =cut | ||||||||||||||||||
| 582 | |||||||||||||||||||
| 583 | |||||||||||||||||||
| 584 | 0 | ▶ | sub dump { | ||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 585 | 0 | my $self = shift; | |||||||||||||||||
| 586 | |||||||||||||||||||
| 587 | 0 | my %hash = $self->{mapping}; | |||||||||||||||||
| 588 | 0 | return Dumper(\%hash); | |||||||||||||||||
| 589 | } | ||||||||||||||||||
| 590 | |||||||||||||||||||
| 591 | |||||||||||||||||||
| 592 | ######################################################################## | ||||||||||||||||||
| 593 | ######################################################################## | ||||||||||||||||||
| 594 | |||||||||||||||||||
| 595 | =head1 Error handling | ||||||||||||||||||
| 596 | |||||||||||||||||||
| 597 | If you miss out an essential parameter, the module will throw an exception | ||||||||||||||||||
| 598 | using "croak" from the Carp module. These exceptions represent programming | ||||||||||||||||||
| 599 | errors in most cases so there is little point in trapping them with "eval". | ||||||||||||||||||
| 600 | |||||||||||||||||||
| 601 | =head1 Author | ||||||||||||||||||
| 602 | |||||||||||||||||||
| 603 | Andrew Findlay | ||||||||||||||||||
| 604 | |||||||||||||||||||
| 605 | Skills 1st Ltd | ||||||||||||||||||
| 606 | |||||||||||||||||||
| 607 | andrew.findlay@skills-1st.co.uk | ||||||||||||||||||
| 608 | |||||||||||||||||||
| 609 | http://www.skills-1st.co.uk/ | ||||||||||||||||||
| 610 | |||||||||||||||||||
| 611 | =cut | ||||||||||||||||||
| 612 | |||||||||||||||||||
| 613 | ######################################################################## | ||||||||||||||||||
| 614 | ######################################################################## | ||||||||||||||||||
| 615 | 5 | 1; | |||||||||||||||||