2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.20 2003/04/04 17:31:23 joko |
6 |
|
## minor update to 'childObj2string' |
7 |
|
## |
8 |
|
## Revision 1.19 2003/03/28 03:11:25 jonen |
9 |
|
## + bugfix |
10 |
|
## |
11 |
|
## Revision 1.18 2003/03/28 03:07:26 jonen |
12 |
|
## + minor changes |
13 |
|
## |
14 |
|
## Revision 1.17 2003/03/27 15:17:07 joko |
15 |
|
## namespace fixes for Data::Mungle::* |
16 |
|
## |
17 |
|
## Revision 1.16 2003/03/27 15:04:52 joko |
18 |
|
## minor update: comment |
19 |
|
## |
20 |
|
## Revision 1.15 2003/02/27 14:39:48 jonen |
21 |
|
## + fixed bug at _hash2object() |
22 |
|
## |
23 |
|
## Revision 1.14 2003/02/22 17:13:55 jonen |
24 |
|
## + added function 'childObject2string()' to encode 'child'-references to option related string |
25 |
|
## + use new option at 'expand()' for replacing 'childObject2string' |
26 |
|
## |
27 |
|
## Revision 1.13 2003/02/21 01:48:50 joko |
28 |
|
## renamed core function |
29 |
|
## |
30 |
|
## Revision 1.12 2003/02/20 22:45:19 joko |
31 |
|
## fix regarding new deep_copy |
32 |
|
## |
33 |
|
## Revision 1.11 2003/02/20 21:13:54 joko |
34 |
|
## - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository) |
35 |
|
## |
36 |
|
## Revision 1.10 2003/02/20 20:48:00 joko |
37 |
|
## - refactored lots of code to Data::Code::Ref |
38 |
|
## + alternative 'deep_copy' implementation |
39 |
|
## |
40 |
|
## Revision 1.9 2003/02/18 19:35:56 joko |
41 |
|
## +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too |
42 |
|
## |
43 |
|
## Revision 1.8 2003/02/09 05:10:56 joko |
44 |
|
## + minor update |
45 |
|
## |
46 |
## Revision 1.7 2003/01/19 03:26:59 joko |
## Revision 1.7 2003/01/19 03:26:59 joko |
47 |
## + added 'sub deep_copy' - refactored from libp |
## + added 'sub deep_copy' - refactored from libp |
48 |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
## + add 'sub merge' - exported from CPAN's 'Hash::Merge' |
71 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
72 |
|
|
73 |
|
|
74 |
package Data::Transform::Deep; |
package Data::Mungle::Transform::Deep; |
75 |
|
|
76 |
use strict; |
use strict; |
77 |
use warnings; |
use warnings; |
79 |
require Exporter; |
require Exporter; |
80 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
81 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
|
&var_NumericalHashToArray |
|
|
&var_mixin |
|
|
&object2hash |
|
|
&hash2object |
|
|
&refexpr2perlref |
|
82 |
&expand |
&expand |
83 |
&deep_copy |
&deep_copy |
84 |
&merge |
&merge_to |
85 |
); |
); |
86 |
# &var_deref |
|
|
# &getStructSlotByStringyAddress |
|
87 |
|
|
88 |
use attributes; |
use attributes; |
89 |
use Data::Dumper; |
use Data::Dumper; |
|
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
|
|
|
|
|
use Hash::Merge qw( merge ); |
|
|
#use libp qw( deep_copy ); |
|
90 |
use Iterate; |
use Iterate; |
91 |
|
|
92 |
sub var_NumericalHashToArray { |
use Pitonyak::DeepCopy; |
93 |
|
use Data::Mungle::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar ); |
94 |
|
use Data::Mungle::Code::Ref qw( ref_slot ); |
95 |
|
|
96 |
|
sub numhash2list { |
97 |
my $vref = shift; |
my $vref = shift; |
98 |
#my $context = shift; |
#my $context = shift; |
99 |
if (ref $vref eq 'HASH') { |
if (ref $vref eq 'HASH') { |
109 |
} |
} |
110 |
$vref->{$_} = \@arr; |
$vref->{$_} = \@arr; |
111 |
} |
} |
112 |
var_NumericalHashToArray($vref->{$_}); |
numhash2list($vref->{$_}); |
113 |
} |
} |
114 |
# } else { |
# } else { |
115 |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
# #$vref->{$_} = scalar2iso($vref->{$_}); |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
sub var_deref { |
# FIXME: could this be refactored using expand? |
123 |
|
# btw: "expand": look at scripts@CPAN (System Administration): |
124 |
|
# there is a perl make with perl |
125 |
|
sub _var_deref_test { |
126 |
my $obj = shift; |
my $obj = shift; |
127 |
my $options = shift; |
my $options = shift; |
128 |
my $result; |
my $result; |
161 |
} |
} |
162 |
|
|
163 |
|
|
164 |
|
# convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML |
165 |
|
# but still using the known latin locale stuff |
166 |
sub expand { |
sub expand { |
167 |
|
|
168 |
my $obj = shift; |
my $obj = shift; |
169 |
my $options = shift; |
my $options = shift; |
170 |
my $result; |
my $result; |
171 |
|
|
172 |
if ((ref $obj) eq 'ARRAY') { |
#print "ref: ", ref $obj, "\n"; |
173 |
|
|
174 |
|
if (ref $obj eq 'ARRAY') { |
175 |
|
|
176 |
IterArray @$obj, sub { |
IterArray @$obj, sub { |
177 |
my $item; |
my $item; |
178 |
# if current item is a reference ... |
# if current item is a reference ... |
179 |
if (ref $_[0]) { |
if (ref $_[0]) { |
180 |
# ... expand structure recursively |
$item = $_[0]; |
181 |
$item = expand($_[0], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
182 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
183 |
|
if ($item && $options->{childObj2string}) { |
184 |
|
$item = childObj2string($item, $options->{childObj2string}); |
185 |
|
} else { |
186 |
|
# ... expand structure recursively |
187 |
|
$item = expand($_[0], $options); |
188 |
|
} |
189 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
190 |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
191 |
|
|
194 |
#$item = deep_copy($_[0]); |
#$item = deep_copy($_[0]); |
195 |
$item = $_[0]; |
$item = $_[0]; |
196 |
# conversions/encodings |
# conversions/encodings |
197 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
198 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
199 |
} |
} |
200 |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
201 |
push(@{$result}, $item); # use item in any case |
push(@{$result}, $item); # use item in any case |
202 |
|
|
203 |
} |
} |
204 |
|
|
205 |
|
} elsif (ref $obj eq 'CODE') { |
206 |
|
#print Dumper($obj); |
207 |
|
#exit; |
208 |
|
|
209 |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
210 |
} else { |
} elsif (ref $obj) { |
211 |
|
|
212 |
IterHash %$obj, sub { |
IterHash %$obj, sub { |
213 |
my $item; |
my $item; |
214 |
|
|
215 |
# if current item is a reference ... |
# if current item is a reference ... |
216 |
if (ref $_[1]) { |
if (ref $_[1]) { |
217 |
# ... expand structure recursively |
$item = $_[1]; |
218 |
$item = expand($_[1], $options); |
# if $options->{childObj2string} is TRUE or STRING don't expand referenced object, |
219 |
|
# instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> ) |
220 |
|
if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) { |
221 |
|
$item = childObj2string($item, $options->{childObj2string}); |
222 |
|
} else { |
223 |
|
# ... expand structure recursively |
224 |
|
$item = expand($_[1], $options); |
225 |
|
} |
226 |
# destroy item via seperate callback method (a POST) if requested |
# destroy item via seperate callback method (a POST) if requested |
227 |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
228 |
|
|
231 |
#$item = deep_copy($_[1]); |
#$item = deep_copy($_[1]); |
232 |
$item = $_[1]; |
$item = $_[1]; |
233 |
# conversions/encodings |
# conversions/encodings |
234 |
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
$item = latin_to_utf8_scalar($item) if ($item && $options->{utf8}); |
235 |
|
$item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin}); |
236 |
} |
} |
237 |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
238 |
$result->{$_[0]} = $item; # use item in any case |
$result->{$_[0]} = $item; # use item in any case |
239 |
} |
} |
240 |
|
|
241 |
|
} else { |
242 |
|
#die ("not a reference!"); |
243 |
|
$result = $obj; |
244 |
|
|
245 |
} |
} |
246 |
|
|
247 |
# convert all values to utf8 (inside complex struct) |
# convert all values to utf8 (inside complex struct) |
248 |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
249 |
# var2utf8($result) if ($options->{utf8}); |
# latin_to_utf8($result) if ($options->{utf8}); |
250 |
|
|
251 |
# destroy persistent object from memory to be sure to get a fresh one next time |
# destroy persistent object from memory to be sure to get a fresh one next time |
252 |
#undef $obj if $options->{destroy}; |
#undef $obj if $options->{destroy}; |
259 |
|
|
260 |
|
|
261 |
my @indexstack; |
my @indexstack; |
262 |
sub var_traverse_mixin_update { |
sub _var_traverse_mixin_update_old { |
263 |
my $obj = shift; |
my $obj = shift; |
264 |
my $data = shift; |
my $data = shift; |
265 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
303 |
#return $result; |
#return $result; |
304 |
} |
} |
305 |
|
|
306 |
sub object2hash { |
sub merge_to { |
307 |
my $object = shift; |
_hash2object(@_); |
308 |
my $options = shift; |
# TODO: |
309 |
#my $deref = var_deref($object); |
# re-implement using CPAN's "Iterate" and/or a modified Hash::Merge. |
|
my $deref = expand($object, $options); |
|
|
#var2utf8($deref) if ($options->{utf8}); |
|
|
return $deref; |
|
310 |
} |
} |
311 |
|
|
312 |
|
|
313 |
# todo: maybe do diff(s) here sometimes!? |
# todo: maybe do diff(s) here sometimes!? |
314 |
sub hash2object { |
sub _hash2object { |
315 |
my $object = shift; |
my $object = shift; |
316 |
my $data = shift; |
my $data = shift; |
317 |
my $options = shift; |
my $options = shift; |
320 |
|
|
321 |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
322 |
# this method corrects this |
# this method corrects this |
323 |
var_NumericalHashToArray($data) if ($options->{php}); |
numhash2list($data) if ($options->{php}); |
324 |
|
|
325 |
# utf8-conversion/-encoding (essential for I18N) |
# utf8-conversion/-encoding (essential for I18N) |
326 |
var_utf2iso($data) if ($options->{utf8}); |
utf8_to_latin($data) if ($options->{utf8}); |
327 |
|
|
328 |
# get fresh object from database |
# get fresh object from database |
329 |
# todo: |
# todo: |
332 |
#my $obj = $self->getObject($oid); |
#my $obj = $self->getObject($oid); |
333 |
|
|
334 |
# mix changes into fresh object and save it back |
# mix changes into fresh object and save it back |
335 |
hash2object_traverse_mixin($object, $data, 0, $options); |
_hash2object_traverse_mixin($object, $data, 0, $options); |
336 |
|
|
337 |
# done in core mixin function? |
# done in core mixin function? |
338 |
#$self->{storage}->update($obj); |
#$self->{storage}->update($obj); |
355 |
#my @indexstack; |
#my @indexstack; |
356 |
|
|
357 |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
358 |
sub hash2object_traverse_mixin { |
sub _hash2object_traverse_mixin { |
359 |
my $object = shift; |
my $object = shift; |
360 |
my $data = shift; |
my $data = shift; |
361 |
my $bool_recursion = shift; |
my $bool_recursion = shift; |
373 |
# what's exactly done here? hmmm.... it works ;) |
# what's exactly done here? hmmm.... it works ;) |
374 |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
375 |
# extend! check! |
# extend! check! |
376 |
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
|
377 |
|
#print attributes::reftype($object), "\n"; |
378 |
|
|
379 |
|
# V1 |
380 |
|
#if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
381 |
|
# V2 |
382 |
|
my $reftype = attributes::reftype($object); |
383 |
|
#print "reftype: '$reftype'", "\n"; |
384 |
|
if ($reftype eq 'HASH') { |
385 |
|
|
386 |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
387 |
|
|
401 |
my $ref = ref $object->{$field}; |
my $ref = ref $object->{$field}; |
402 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
403 |
if ($ref) { |
if ($ref) { |
404 |
hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
_hash2object_traverse_mixin($object->{$field}, $data, 1, $options); |
405 |
} else { |
} else { |
406 |
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
my $val = ref_slot($data, \@indexstack); |
407 |
|
|
408 |
print Dumper($options); |
#print Dumper($options); |
409 |
my $field_target = $field; |
my $field_target = $field; |
410 |
if (my $pattern = $options->{pattern_strip_key}) { |
if (my $pattern = $options->{pattern_strip_key}) { |
411 |
print "pattern: $pattern", "\n"; |
print "pattern: $pattern", "\n"; |
446 |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
447 |
# if ($ref && $_) { |
# if ($ref && $_) { |
448 |
if ($ref) { |
if ($ref) { |
449 |
hash2object_traverse_mixin($_, $data, 1, $options); |
_hash2object_traverse_mixin($_, $data, 1, $options); |
450 |
} else { |
} else { |
451 |
$object->[$i] = $_ if defined $_; |
$object->[$i] = $_ if defined $_; |
452 |
} |
} |
453 |
pop @indexstack; |
pop @indexstack; |
454 |
$i++; |
$i++; |
455 |
} |
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
|
|
# TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ? ;-) |
|
|
sub getStructSlotByStringyAddress { |
|
|
my $var = shift; |
|
|
my $indexstack_ref = shift; |
|
|
my @indexstack = @{$indexstack_ref}; |
|
|
my $joiner = '->'; |
|
|
my @evlist; |
|
|
foreach (@indexstack) { |
|
|
my $elem; |
|
|
if ($_ =~ m/\d+/) { $elem = "[$_]"; } |
|
|
if ($_ =~ m/\D+/) { $elem = "{$_}"; } |
|
|
push @evlist, $elem; |
|
|
} |
|
|
my $evstring = join($joiner, @evlist); |
|
|
$evstring = 'return $var->' . $evstring . ';'; |
|
|
#print "ev: $evstring\n"; |
|
|
return eval($evstring); |
|
|
} |
|
|
|
|
|
|
|
|
sub refexpr2perlref { |
|
|
|
|
|
my $obj = shift; |
|
|
my $expr = shift; |
|
|
my $callbackMap = shift; |
|
|
|
|
|
my $value; |
|
|
# detect for callback (code-reference) |
|
|
if (ref($expr) eq 'CODE') { |
|
|
$value = &$expr($obj); |
|
|
|
|
|
} elsif ($expr =~ m/->/) { |
|
|
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
|
|
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr); |
|
|
#print "\n", "expr: $expr"; |
|
|
#print "\n", "objPerlRefString: $objPerlRefString"; |
|
|
$value = eval('$obj' . '->' . $objPerlRefString); |
|
|
|
|
|
# if value isn't set, try to "fallback" to callbackMap |
|
|
# callbacks are applied this way: |
|
|
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
|
|
if (!$value && $callbackMap) { |
|
|
#print Dumper($callbackMap); |
|
|
|
|
|
# prepare needle |
|
|
my @parts = @$parts; |
|
|
my $count = $#parts; |
|
|
my $needle = $parts[$count]; |
|
|
|
|
|
# prepare haystack |
|
|
my @haystack = keys %$callbackMap; |
|
|
if (grep($needle, @haystack)) { |
|
|
$value = $callbackMap->{$needle}->(); |
|
|
#print "value: ", $value, "\n"; |
|
|
} |
|
|
} |
|
456 |
|
|
457 |
|
=pod |
458 |
|
# object? |
459 |
} else { |
} else { |
460 |
# use expr as simple scalar key (attributename) |
print "reference: ", ref $object, "\n"; |
461 |
$value = $obj->{$expr}; |
print "reftype: ", $reftype, "\n"; |
462 |
} |
die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!"); |
463 |
|
=cut |
|
return $value; |
|
464 |
|
|
465 |
} |
} |
|
|
|
|
|
|
|
sub refexpr2perlref_parts { |
|
|
my $expr = shift; |
|
|
|
|
|
# split expression by dereference operators first |
|
|
my @parts_pure = split(/->/, $expr); |
|
|
|
|
|
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
|
|
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
|
|
my @parts_capsule = @parts_pure; |
|
|
map { |
|
|
m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}"); |
|
|
} @parts_capsule; |
|
466 |
|
|
|
# join parts with dereference operators together again and return built string |
|
|
return (join('->', @parts_capsule), \@parts_pure); |
|
467 |
} |
} |
468 |
|
|
469 |
# ACK's go to ... |
|
470 |
sub deep_copy { |
# ACK's go to Randal L. Schwartz <merlyn@stonehenge.com> |
471 |
|
# on the website: |
472 |
|
# This text is copyright by Miller-Freeman, Inc., and is used with their permission. |
473 |
|
# Further distribution or use is not permitted. |
474 |
|
# please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html |
475 |
|
sub deep_copy1 { |
476 |
my $this = shift; |
my $this = shift; |
477 |
if (not ref $this) { |
if (not ref $this) { |
478 |
$this; |
$this; |
479 |
} elsif (ref $this eq "ARRAY") { |
} elsif (ref $this eq "ARRAY") { |
480 |
[map deep_copy($_), @$this]; |
[map deep_copy1($_), @$this]; |
481 |
} elsif (ref $this eq "HASH") { |
} elsif (ref $this eq "HASH") { |
482 |
+{map { $_ => deep_copy($this->{$_}) } keys %$this}; |
+{map { $_ => deep_copy1($this->{$_}) } keys %$this}; |
483 |
} elsif (ref $this eq "CODE") { |
} elsif (ref $this eq "CODE") { |
484 |
$this; |
$this; |
485 |
#} else { die "deep_copy asks: what type is $this?" } |
} else { die "deep_copy1 asks: what type is $this?" } |
486 |
} else { print "deep_copy asks: what type is $this?", "\n"; } |
#} else { print "deep_copy asks: what type is $this?", "\n"; } |
487 |
|
} |
488 |
|
|
489 |
|
# ACK's go to Andrew Pitonyak |
490 |
|
# Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org) |
491 |
|
# please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html |
492 |
|
sub deep_copy { |
493 |
|
Pitonyak::DeepCopy::deep_copy(@_); |
494 |
} |
} |
495 |
|
|
496 |
|
|
497 |
|
# encodes object-references to serialized string representations |
498 |
|
# made up of: |
499 |
|
# - 'o_<classname>_<ref type>_<guid>'??? |
500 |
|
# - 'o_{guid}_{classname}'!!! |
501 |
|
sub childObj2string { |
502 |
|
my $obj = shift; |
503 |
|
my $option = shift; |
504 |
|
my $string = 'n/a'; |
505 |
|
|
506 |
|
if ($option == 1) { |
507 |
|
if ((my $classname = ref $obj) && (my $guid = $obj->{guid})) { |
508 |
|
$string = join('_', 'o', $guid, $classname); |
509 |
|
} |
510 |
|
} |
511 |
|
|
512 |
|
return $string; |
513 |
|
} |
514 |
|
|
515 |
|
|
516 |
1; |
1; |
517 |
|
__END__ |