/[cvs]/nfo/perl/libs/Data/Mungle/Transform/Deep.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Mungle/Transform/Deep.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by joko, Thu Dec 5 13:56:49 2002 UTC revision 1.20 by joko, Fri Apr 4 17:31:23 2003 UTC
# Line 1  Line 1 
1  ##############################################  ## ---------------------------------------------------------------------------
2  #  ##  $Id$
3  #  $Id$  ## ---------------------------------------------------------------------------
4  #  ##  $Log$
5  #  $Log$  ##  Revision 1.20  2003/04/04 17:31:23  joko
6  #  Revision 1.4  2002/12/05 13:56:49  joko  ##  minor update to 'childObj2string'
7  #  - var_deref  ##
8  #  + expand - more sophisticated dereferencing with callbacks using Iterate  ##  Revision 1.19  2003/03/28 03:11:25  jonen
9  #  ##  + bugfix
10  #  Revision 1.3  2002/12/03 05:34:55  joko  ##
11  #  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode  ##  Revision 1.18  2003/03/28 03:07:26  jonen
12  #  ##  + minor changes
13  #  Revision 1.2  2002/12/01 04:44:07  joko  ##
14  #  + code from Data::Transform::OO  ##  Revision 1.17  2003/03/27 15:17:07  joko
15  #  ##  namespace fixes for Data::Mungle::*
16  #  Revision 1.1  2002/11/29 04:49:20  joko  ##
17  #  + initial check-in  ##  Revision 1.16  2003/03/27 15:04:52  joko
18  #  ##  minor update: comment
19  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##
20  #  + new  ##  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
47    ##  + added 'sub deep_copy'  -  refactored from libp
48    ##  + add 'sub merge'  -  exported from CPAN's 'Hash::Merge'
49    ##
50    ##  Revision 1.6  2002/12/23 11:27:53  jonen
51    ##  + changed behavior WATCH!
52    ##
53    ##  Revision 1.5  2002/12/16 19:57:54  joko
54    ##  + option 'init'
55    ##
56    ##  Revision 1.4  2002/12/05 13:56:49  joko
57    ##  - var_deref
58    ##  + expand - more sophisticated dereferencing with callbacks using Iterate
59    ##
60    ##  Revision 1.3  2002/12/03 05:34:55  joko
61    ##  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode
62    ##
63    ##  Revision 1.2  2002/12/01 04:44:07  joko
64    ##  + code from Data::Transform::OO
65    ##
66    ##  Revision 1.1  2002/11/29 04:49:20  joko
67    ##  + initial check-in
68    ##
69    ##  Revision 1.1  2002/10/10 03:26:00  cvsjoko
70    ##  + new
71    ## ---------------------------------------------------------------------------
72    
73    
74  package Data::Transform::Deep;  package Data::Mungle::Transform::Deep;
75    
76  use strict;  use strict;
77  use warnings;  use warnings;
# Line 30  use warnings; Line 79  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
84      &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 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') {
# Line 62  sub var_NumericalHashToArray { Line 109  sub var_NumericalHashToArray {
109                }                }
110                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
111              }              }
112              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
113            }            }
114  #      } else {  #      } else {
115  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 72  sub var_NumericalHashToArray { Line 119  sub var_NumericalHashToArray {
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;
# Line 111  sub var_deref { Line 161  sub var_deref {
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    
# Line 133  sub expand { Line 194  sub expand {
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                
# Line 158  sub expand { Line 231  sub expand {
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};
# Line 181  sub expand { Line 259  sub expand {
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;
# Line 225  sub var_traverse_mixin_update { Line 303  sub var_traverse_mixin_update {
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;
# Line 244  sub hash2object { Line 320  sub hash2object {
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:
# Line 256  sub hash2object { Line 332  sub hash2object {
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);    _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);
# Line 279  sub hash2object { Line 355  sub hash2object {
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;
362      my $options = shift;
363    
364    # 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)
365    @indexstack = () if (!$bool_recursion);    @indexstack = () if (!$bool_recursion);
# Line 296  sub hash2object_traverse_mixin { Line 373  sub hash2object_traverse_mixin {
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                    
388        my @fields;
389      # loop through fields of object (Tangram-object)      # loop through fields of object (Tangram-object)
390      foreach (keys %{$object}) {      @fields = keys %{$object};
391        push @indexstack, $_;  
392        # loop through fields of to.be.injected-data (arbitrary Perl-data-structure)
393        @fields = keys %{$data} if $options->{init};
394    #    @fields = keys %{$data} if $options->{mixin};
395        
396        foreach (@fields) {
397          my $field = $_;
398          push @indexstack, $field;
399                            
400        # determine type of object        # determine type of object
401        my $ref = ref $object->{$_};        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->{$_}, $data, 1);          _hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
405        } else {        } else {
406          my $val = getStructSlotByStringyAddress($data, \@indexstack);          my $val = ref_slot($data, \@indexstack);
407          $object->{$_} = $val;                  
408    #print Dumper($options);
409            my $field_target = $field;
410            if (my $pattern = $options->{pattern_strip_key}) {
411              print "pattern: $pattern", "\n";
412              $field_target =~ s/$pattern//;
413              print "field: $field_target", "\n";
414            }
415    
416            $object->{$field_target} = $val if defined $val;
417        }        }
418        pop @indexstack;        pop @indexstack;
419      }      }
# Line 342  sub hash2object_traverse_mixin { Line 444  sub hash2object_traverse_mixin {
444        push @indexstack, $i;        push @indexstack, $i;
445        my $ref = ref $_;        my $ref = ref $_;
446  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
447        if ($ref && $_) {  #      if ($ref && $_) {
448          hash2object_traverse_mixin($_, $data, 1);        if ($ref) {
449            _hash2object_traverse_mixin($_, $data, 1, $options);
450        } else {        } else {
451          $object->[$i] = $_;          $object->[$i] = $_ if defined $_;
452        }        }
453        pop @indexstack;        pop @indexstack;
454        $i++;        $i++;
455      }      }
456    
457    =pod
458      # object?
459      } else {
460        print "reference: ", ref $object, "\n";
461        print "reftype: ", $reftype, "\n";
462        die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!");
463    =cut
464      
465    }    }
466    
467  }  }
468    
469    
470  # this function seems to do similar stuff like these below (refexpr2perlref & co.)  # ACK's go to Randal L. Schwartz <merlyn@stonehenge.com>
471  sub getStructSlotByStringyAddress {  # on the website:
472    my $var = shift;  # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
473    my $indexstack_ref = shift;  # Further distribution or use is not permitted.
474    my @indexstack = @{$indexstack_ref};  # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
475    my $joiner = '->';  sub deep_copy1 {
476    my @evlist;    my $this = shift;
477    foreach (@indexstack) {    if (not ref $this) {
478      my $elem;      $this;
479      if ($_ =~ m/\d+/) { $elem = "[$_]"; }    } elsif (ref $this eq "ARRAY") {
480      if ($_ =~ m/\D+/) { $elem = "{$_}"; }      [map deep_copy1($_), @$this];
481      push @evlist, $elem;    } elsif (ref $this eq "HASH") {
482    }      +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
483    my $evstring = join($joiner, @evlist);    } elsif (ref $this eq "CODE") {
484    $evstring = 'return $var->' . $evstring . ';';      $this;
485    #print "ev: $evstring\n";    } else { die "deep_copy1 asks: what type is $this?" }
486    return eval($evstring);    #} 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    
 sub refexpr2perlref {  
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;    my $obj = shift;
503    my $expr = shift;    my $option = shift;
504    my $callbackMap = shift;    my $string = 'n/a';
505      
506    my $value;    if ($option == 1) {
507    # detect for callback (code-reference)      if ((my $classname = ref $obj) && (my $guid = $obj->{guid})) {
508    if (ref($expr) eq 'CODE') {        $string = join('_', 'o', $guid, $classname);
509      $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";  
       }  
     }  
   
   } else {  
     # use expr as simple scalar key (attributename)  
     $value = $obj->{$expr};  
510    }    }
             
   return $value;  
511        
512      return $string;
513  }  }
514    
515    
 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;  
   
   # join parts with dereference operators together again and return built string  
   return (join('->', @parts_capsule), \@parts_pure);  
 }  
   
516  1;  1;
517    __END__

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.20

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed