1 |
############################################## |
## --------------------------------------------------------------------------- |
2 |
# |
## $Id$ |
3 |
# $Id$ |
## --------------------------------------------------------------------------- |
4 |
# |
## $Log$ |
5 |
# $Log$ |
## Revision 1.9 2003/02/18 19:35:56 joko |
6 |
# Revision 1.4 2002/12/05 13:56:49 joko |
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
7 |
# - var_deref |
## |
8 |
# + expand - more sophisticated dereferencing with callbacks using Iterate |
## Revision 1.8 2003/02/09 05:10:56 joko |
9 |
# |
## + minor update |
10 |
# Revision 1.3 2002/12/03 05:34:55 joko |
## |
11 |
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
## Revision 1.7 2003/01/19 03:26:59 joko |
12 |
# |
## + added 'sub deep_copy' - refactored from libp |
13 |
# Revision 1.2 2002/12/01 04:44:07 joko |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
14 |
# + code from Data::Transform::OO |
## |
15 |
# |
## Revision 1.6 2002/12/23 11:27:53 jonen |
16 |
# Revision 1.1 2002/11/29 04:49:20 joko |
## + changed behavior WATCH! |
17 |
# + initial check-in |
## |
18 |
# |
## Revision 1.5 2002/12/16 19:57:54 joko |
19 |
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
## + option 'init' |
20 |
# + new |
## |
21 |
# |
## Revision 1.4 2002/12/05 13:56:49 joko |
22 |
############################################## |
## - var_deref |
23 |
|
## + expand - more sophisticated dereferencing with callbacks using Iterate |
24 |
|
## |
25 |
|
## Revision 1.3 2002/12/03 05:34:55 joko |
26 |
|
## + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
27 |
|
## |
28 |
|
## Revision 1.2 2002/12/01 04:44:07 joko |
29 |
|
## + code from Data::Transform::OO |
30 |
|
## |
31 |
|
## Revision 1.1 2002/11/29 04:49:20 joko |
32 |
|
## + initial check-in |
33 |
|
## |
34 |
|
## Revision 1.1 2002/10/10 03:26:00 cvsjoko |
35 |
|
## + new |
36 |
|
## --------------------------------------------------------------------------- |
37 |
|
|
38 |
|
|
39 |
package Data::Transform::Deep; |
package Data::Transform::Deep; |
50 |
&hash2object |
&hash2object |
51 |
&refexpr2perlref |
&refexpr2perlref |
52 |
&expand |
&expand |
53 |
|
&deep_copy |
54 |
|
&merge |
55 |
); |
); |
56 |
# &var_deref |
# &var_deref |
57 |
# &getStructSlotByStringyAddress |
# &getStructSlotByStringyAddress |
59 |
use attributes; |
use attributes; |
60 |
use Data::Dumper; |
use Data::Dumper; |
61 |
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
62 |
use libp qw( deep_copy ); |
|
63 |
|
use Hash::Merge qw( merge ); |
64 |
use Iterate; |
use Iterate; |
65 |
|
|
66 |
sub var_NumericalHashToArray { |
sub var_NumericalHashToArray { |
89 |
} |
} |
90 |
|
|
91 |
|
|
92 |
|
# FIXME: could this be refactored using expand? |
93 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
94 |
|
# there is a perl make with perl |
95 |
sub var_deref { |
sub var_deref { |
96 |
my $obj = shift; |
my $obj = shift; |
97 |
my $options = shift; |
my $options = shift; |
276 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
277 |
|
|
278 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
279 |
hash2object_traverse_mixin($object, $data); |
hash2object_traverse_mixin($object, $data, 0, $options); |
280 |
|
|
281 |
# done in core mixin function? |
# done in core mixin function? |
282 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
303 |
my $object = shift; |
my $object = shift; |
304 |
my $data = shift; |
my $data = shift; |
305 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
306 |
|
my $options = shift; |
307 |
|
|
308 |
# clear our key - stack if we are called from user-code (non-recursively) |
# clear our key - stack if we are called from user-code (non-recursively) |
309 |
@indexstack = () if (!$bool_recursion); |
@indexstack = () if (!$bool_recursion); |
321 |
|
|
322 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
323 |
|
|
324 |
|
my @fields; |
325 |
# loop through fields of object (Tangram-object) |
# loop through fields of object (Tangram-object) |
326 |
foreach (keys %{$object}) { |
@fields = keys %{$object}; |
327 |
push @indexstack, $_; |
|
328 |
|
# loop through fields of to.be.injected-data (arbitrary Perl-data-structure) |
329 |
|
@fields = keys %{$data} if $options->{init}; |
330 |
|
# @fields = keys %{$data} if $options->{mixin}; |
331 |
|
|
332 |
|
foreach (@fields) { |
333 |
|
my $field = $_; |
334 |
|
push @indexstack, $field; |
335 |
|
|
336 |
# determine type of object |
# determine type of object |
337 |
my $ref = ref $object->{$_}; |
my $ref = ref $object->{$field}; |
338 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
339 |
if ($ref) { |
if ($ref) { |
340 |
hash2object_traverse_mixin($object->{$_}, $data, 1); |
hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
341 |
} else { |
} else { |
342 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
343 |
$object->{$_} = $val; |
|
344 |
|
#print Dumper($options); |
345 |
|
my $field_target = $field; |
346 |
|
if (my $pattern = $options->{pattern_strip_key}) { |
347 |
|
print "pattern: $pattern", "\n"; |
348 |
|
$field_target =~ s/$pattern//; |
349 |
|
print "field: $field_target", "\n"; |
350 |
|
} |
351 |
|
|
352 |
|
$object->{$field_target} = $val if defined $val; |
353 |
} |
} |
354 |
pop @indexstack; |
pop @indexstack; |
355 |
} |
} |
380 |
push @indexstack, $i; |
push @indexstack, $i; |
381 |
my $ref = ref $_; |
my $ref = ref $_; |
382 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
383 |
if ($ref && $_) { |
# if ($ref && $_) { |
384 |
hash2object_traverse_mixin($_, $data, 1); |
if ($ref) { |
385 |
|
hash2object_traverse_mixin($_, $data, 1, $options); |
386 |
} else { |
} else { |
387 |
$object->[$i] = $_; |
$object->[$i] = $_ if defined $_; |
388 |
} |
} |
389 |
pop @indexstack; |
pop @indexstack; |
390 |
$i++; |
$i++; |
395 |
|
|
396 |
|
|
397 |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
398 |
|
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
399 |
sub getStructSlotByStringyAddress { |
sub getStructSlotByStringyAddress { |
400 |
my $var = shift; |
my $var = shift; |
401 |
my $indexstack_ref = shift; |
my $indexstack_ref = shift; |
419 |
|
|
420 |
my $obj = shift; |
my $obj = shift; |
421 |
my $expr = shift; |
my $expr = shift; |
422 |
my $callbackMap = shift; |
my $values = shift; |
423 |
|
my $delimiter = shift; |
424 |
|
|
425 |
|
$delimiter ||= '->'; |
426 |
|
|
427 |
my $value; |
my $value; |
428 |
# detect for callback (code-reference) |
# detect for callback (code-reference) |
429 |
if (ref($expr) eq 'CODE') { |
if (ref($expr) eq 'CODE') { |
430 |
$value = &$expr($obj); |
$value = &$expr($obj); |
431 |
|
|
432 |
} elsif ($expr =~ m/->/) { |
} elsif ($expr =~ m/$delimiter/) { |
433 |
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
434 |
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr); |
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr, $delimiter); |
435 |
#print "\n", "expr: $expr"; |
|
436 |
#print "\n", "objPerlRefString: $objPerlRefString"; |
# trace |
437 |
$value = eval('$obj' . '->' . $objPerlRefString); |
#print "expr: $expr", "\n"; |
438 |
|
#print "objPerlRefString: $objPerlRefString", "\n"; |
439 |
|
|
440 |
# if value isn't set, try to "fallback" to callbackMap |
my $evstr = '$obj' . '->' . $objPerlRefString; |
441 |
|
|
442 |
|
# trace |
443 |
|
#print "eval: $evstr", "\n"; |
444 |
|
|
445 |
|
$value = eval($evstr); |
446 |
|
die ($@) if $@; |
447 |
|
|
448 |
|
# if value isn't set, try to set it |
449 |
|
my $callbackMap; |
450 |
|
if ($values) { |
451 |
|
#if (ref $values eq 'HASH') { |
452 |
|
if (ref $values eq 'Data::Map') { |
453 |
|
$callbackMap = $values->getAttributes(); |
454 |
|
} else { |
455 |
|
# apply $values as single value to referenced slot |
456 |
|
#print "APPLY!", "\n"; |
457 |
|
eval('$obj' . '->' . $objPerlRefString . ' = $values;'); |
458 |
|
die ($@) if $@; |
459 |
|
} |
460 |
|
} |
461 |
|
|
462 |
|
# determine $values - if it's a hashref, use it as a "valueMap" |
463 |
|
# "fallback" to callbackMap |
464 |
# callbacks are applied this way: |
# callbacks are applied this way: |
465 |
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
466 |
if (!$value && $callbackMap) { |
if (!$value && $callbackMap) { |
491 |
|
|
492 |
sub refexpr2perlref_parts { |
sub refexpr2perlref_parts { |
493 |
my $expr = shift; |
my $expr = shift; |
494 |
|
my $delimiter = shift; |
495 |
|
|
496 |
|
$delimiter = quotemeta($delimiter); |
497 |
|
|
498 |
# split expression by dereference operators first |
# split expression by dereference operators first |
499 |
my @parts_pure = split(/->/, $expr); |
my @parts_pure = split(/$delimiter/, $expr); |
500 |
|
|
501 |
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
502 |
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
509 |
return (join('->', @parts_capsule), \@parts_pure); |
return (join('->', @parts_capsule), \@parts_pure); |
510 |
} |
} |
511 |
|
|
512 |
|
# ACK's go to ... |
513 |
|
sub deep_copy { |
514 |
|
my $this = shift; |
515 |
|
if (not ref $this) { |
516 |
|
$this; |
517 |
|
} elsif (ref $this eq "ARRAY") { |
518 |
|
[map deep_copy($_), @$this]; |
519 |
|
} elsif (ref $this eq "HASH") { |
520 |
|
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
521 |
|
} elsif (ref $this eq "CODE") { |
522 |
|
$this; |
523 |
|
#} else { die "deep_copy asks: what type is $this?" } |
524 |
|
} else { print "deep_copy asks: what type is $this?", "\n"; } |
525 |
|
} |
526 |
|
|
527 |
1; |
1; |