--- nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2002/12/01 04:44:07 1.2 +++ nfo/perl/libs/Data/Mungle/Transform/Deep.pm 2003/01/19 03:26:59 1.7 @@ -1,18 +1,33 @@ -############################################## -# -# $Id: Deep.pm,v 1.2 2002/12/01 04:44:07 joko Exp $ -# -# $Log: Deep.pm,v $ -# Revision 1.2 2002/12/01 04:44:07 joko -# + code from Data::Transform::OO -# -# Revision 1.1 2002/11/29 04:49:20 joko -# + initial check-in -# -# Revision 1.1 2002/10/10 03:26:00 cvsjoko -# + new -# -############################################## +## --------------------------------------------------------------------------- +## $Id: Deep.pm,v 1.7 2003/01/19 03:26:59 joko Exp $ +## --------------------------------------------------------------------------- +## $Log: Deep.pm,v $ +## Revision 1.7 2003/01/19 03:26:59 joko +## + added 'sub deep_copy' - refactored from libp +## + add 'sub merge' - exported from CPAN's 'Hash::Merge' +## +## Revision 1.6 2002/12/23 11:27:53 jonen +## + changed behavior WATCH! +## +## Revision 1.5 2002/12/16 19:57:54 joko +## + option 'init' +## +## Revision 1.4 2002/12/05 13:56:49 joko +## - var_deref +## + expand - more sophisticated dereferencing with callbacks using Iterate +## +## Revision 1.3 2002/12/03 05:34:55 joko +## + bugfix: now utilizing var_utf2iso from Data::Transform::Encode +## +## Revision 1.2 2002/12/01 04:44:07 joko +## + code from Data::Transform::OO +## +## Revision 1.1 2002/11/29 04:49:20 joko +## + initial check-in +## +## Revision 1.1 2002/10/10 03:26:00 cvsjoko +## + new +## --------------------------------------------------------------------------- package Data::Transform::Deep; @@ -24,17 +39,24 @@ our @ISA = qw( Exporter ); our @EXPORT_OK = qw( &var_NumericalHashToArray - &var_deref &var_mixin &object2hash &hash2object &refexpr2perlref + &expand + &deep_copy + &merge ); +# &var_deref # &getStructSlotByStringyAddress use attributes; use Data::Dumper; +use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); +use Hash::Merge qw( merge ); +#use libp qw( deep_copy ); +use Iterate; sub var_NumericalHashToArray { my $vref = shift; @@ -64,15 +86,20 @@ sub var_deref { my $obj = shift; + my $options = shift; my $result; if ((ref $obj) eq 'ARRAY') { foreach (@{$obj}) { my $ref = ref $_; if ($ref) { - push(@{$result}, var_deref($_)); + push(@{$result}, var_deref($_, $options)); + #undef $_; } else { - push(@{$result}, $_); + #push(@{$result}, $_); + push(@{$result}, deep_copy($_)); } + #undef $_ if $options->{destroy}; + #$options->{destroy}->($_) if $options->{destroy}; } # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? } else { @@ -80,16 +107,89 @@ my $key = $_; my $ref = ref $obj->{$_}; if ($ref) { - $result->{$_} = var_deref($obj->{$_}); + $result->{$_} = var_deref($obj->{$_}, $options); + #undef $obj->{$_}; } else { - $result->{$_} = $obj->{$_}; + #$result->{$_} = $obj->{$_}; + $result->{$_} = deep_copy($obj->{$_}); } + #undef $obj->{$_} if $options->{destroy}; + #$options->{destroy}->($obj->{$_}) if $options->{destroy}; } } + #undef $obj if $options->{destroy}; + $options->{cb_destroy}->($obj) if $options->{cb_destroy}; return $result; } +sub expand { + + my $obj = shift; + my $options = shift; + my $result; + + if ((ref $obj) eq 'ARRAY') { + + IterArray @$obj, sub { + my $item; + # if current item is a reference ... + if (ref $_[0]) { + # ... expand structure recursively + $item = expand($_[0], $options); + # destroy item via seperate callback method (a POST) if requested + #$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; + + # ... assume plain scalar + } else { + #$item = deep_copy($_[0]); + $item = $_[0]; + # conversions/encodings + $item = scalar2utf8($item) if ($item && $options->{utf8}); + } + #push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) + push(@{$result}, $item); # use item in any case + + } + + # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? + } else { + + IterHash %$obj, sub { + my $item; + + # if current item is a reference ... + if (ref $_[1]) { + # ... expand structure recursively + $item = expand($_[1], $options); + # destroy item via seperate callback method (a POST) if requested + #$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; + + # ... assume plain scalar + } else { + #$item = deep_copy($_[1]); + $item = $_[1]; + # conversions/encodings + $item = scalar2utf8($item) if ($item && $options->{utf8}); + } + #$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) + $result->{$_[0]} = $item; # use item in any case + } + + } + + # convert all values to utf8 (inside complex struct) + # now done in core-item-callbacks via Greg London's "Iterate" from CPAN + # var2utf8($result) if ($options->{utf8}); + + # destroy persistent object from memory to be sure to get a fresh one next time + #undef $obj if $options->{destroy}; + #$options->{cb_destroy}->($obj) if $options->{cb_destroy}; + #$options->{cb}->{destroy}->($obj) if $options->{destroy}; + + return $result; +} + my @indexstack; @@ -140,8 +240,9 @@ sub object2hash { my $object = shift; my $options = shift; - my $deref = var_deref($object); - var2utf8($deref) if ($options->{utf8}); + #my $deref = var_deref($object); + my $deref = expand($object, $options); + #var2utf8($deref) if ($options->{utf8}); return $deref; } @@ -151,6 +252,8 @@ my $data = shift; my $options = shift; + #print "hash2object", "\n"; + # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, # this method corrects this var_NumericalHashToArray($data) if ($options->{php}); @@ -165,7 +268,7 @@ #my $obj = $self->getObject($oid); # mix changes into fresh object and save it back - hash2object_traverse_mixin($object, $data); + hash2object_traverse_mixin($object, $data, 0, $options); # done in core mixin function? #$self->{storage}->update($obj); @@ -192,6 +295,7 @@ my $object = shift; my $data = shift; my $bool_recursion = shift; + my $options = shift; # clear our key - stack if we are called from user-code (non-recursively) @indexstack = () if (!$bool_recursion); @@ -209,18 +313,35 @@ # print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; + my @fields; # loop through fields of object (Tangram-object) - foreach (keys %{$object}) { - push @indexstack, $_; + @fields = keys %{$object}; + + # loop through fields of to.be.injected-data (arbitrary Perl-data-structure) + @fields = keys %{$data} if $options->{init}; +# @fields = keys %{$data} if $options->{mixin}; + + foreach (@fields) { + my $field = $_; + push @indexstack, $field; # determine type of object - my $ref = ref $object->{$_}; + my $ref = ref $object->{$field}; # print STDERR "attrname: $_ ATTRref: $ref", "\n"; if ($ref) { - hash2object_traverse_mixin($object->{$_}, $data, 1); + hash2object_traverse_mixin($object->{$field}, $data, 1, $options); } else { my $val = getStructSlotByStringyAddress($data, \@indexstack); - $object->{$_} = $val; + +print Dumper($options); + my $field_target = $field; + if (my $pattern = $options->{pattern_strip_key}) { + print "pattern: $pattern", "\n"; + $field_target =~ s/$pattern//; + print "field: $field_target", "\n"; + } + + $object->{$field_target} = $val if defined $val; } pop @indexstack; } @@ -251,10 +372,11 @@ push @indexstack, $i; my $ref = ref $_; # print STDERR "attrname: $_ ATTRref: $ref", "\n"; - if ($ref && $_) { - hash2object_traverse_mixin($_, $data, 1); +# if ($ref && $_) { + if ($ref) { + hash2object_traverse_mixin($_, $data, 1, $options); } else { - $object->[$i] = $_; + $object->[$i] = $_ if defined $_; } pop @indexstack; $i++; @@ -265,6 +387,7 @@ # 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; @@ -348,4 +471,19 @@ return (join('->', @parts_capsule), \@parts_pure); } +# ACK's go to ... +sub deep_copy { + my $this = shift; + if (not ref $this) { + $this; + } elsif (ref $this eq "ARRAY") { + [map deep_copy($_), @$this]; + } elsif (ref $this eq "HASH") { + +{map { $_ => deep_copy($this->{$_}) } keys %$this}; + } elsif (ref $this eq "CODE") { + $this; + #} else { die "deep_copy asks: what type is $this?" } + } else { print "deep_copy asks: what type is $this?", "\n"; } +} + 1;