/[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.3 by joko, Tue Dec 3 05:34:55 2002 UTC revision 1.14 by jonen, Sat Feb 22 17:13:55 2003 UTC
# Line 1  Line 1 
1  ##############################################  ## ---------------------------------------------------------------------------
2  #  ##  $Id$
3  #  $Id$  ## ---------------------------------------------------------------------------
4  #  ##  $Log$
5  #  $Log$  ##  Revision 1.14  2003/02/22 17:13:55  jonen
6  #  Revision 1.3  2002/12/03 05:34:55  joko  ##  + added function 'childObject2string()' to encode 'child'-references to option related string
7  #  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode  ##  + use new option at 'expand()' for replacing 'childObject2string'
8  #  ##
9  #  Revision 1.2  2002/12/01 04:44:07  joko  ##  Revision 1.13  2003/02/21 01:48:50  joko
10  #  + code from Data::Transform::OO  ##  renamed core function
11  #  ##
12  #  Revision 1.1  2002/11/29 04:49:20  joko  ##  Revision 1.12  2003/02/20 22:45:19  joko
13  #  + initial check-in  ##  fix regarding new deep_copy
14  #  ##
15  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##  Revision 1.11  2003/02/20 21:13:54  joko
16  #  + new  ##  - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository)
17  #  ##
18  ##############################################  ##  Revision 1.10  2003/02/20 20:48:00  joko
19    ##  - refactored lots of code to Data::Code::Ref
20    ##  + alternative 'deep_copy' implementation
21    ##
22    ##  Revision 1.9  2003/02/18 19:35:56  joko
23    ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
24    ##
25    ##  Revision 1.8  2003/02/09 05:10:56  joko
26    ##  + minor update
27    ##
28    ##  Revision 1.7  2003/01/19 03:26:59  joko
29    ##  + added 'sub deep_copy'  -  refactored from libp
30    ##  + add 'sub merge'  -  exported from CPAN's 'Hash::Merge'
31    ##
32    ##  Revision 1.6  2002/12/23 11:27:53  jonen
33    ##  + changed behavior WATCH!
34    ##
35    ##  Revision 1.5  2002/12/16 19:57:54  joko
36    ##  + option 'init'
37    ##
38    ##  Revision 1.4  2002/12/05 13:56:49  joko
39    ##  - var_deref
40    ##  + expand - more sophisticated dereferencing with callbacks using Iterate
41    ##
42    ##  Revision 1.3  2002/12/03 05:34:55  joko
43    ##  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode
44    ##
45    ##  Revision 1.2  2002/12/01 04:44:07  joko
46    ##  + code from Data::Transform::OO
47    ##
48    ##  Revision 1.1  2002/11/29 04:49:20  joko
49    ##  + initial check-in
50    ##
51    ##  Revision 1.1  2002/10/10 03:26:00  cvsjoko
52    ##  + new
53    ## ---------------------------------------------------------------------------
54    
55    
56  package Data::Transform::Deep;  package Data::Transform::Deep;
# Line 26  use warnings; Line 61  use warnings;
61  require Exporter;  require Exporter;
62  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
63  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
64    &var_NumericalHashToArray    &expand
65    &var_deref    &deep_copy
66    &var_mixin    &merge_to
   &object2hash  
   &hash2object  
   &refexpr2perlref  
67  );  );
68  #  &getStructSlotByStringyAddress  
69    
70  use attributes;  use attributes;
71  use Data::Dumper;  use Data::Dumper;
72  use Data::Transform::Encode qw( var_utf2iso );  use Iterate;
73    
74    use Pitonyak::DeepCopy;
75    use Data::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar );
76    use Data::Code::Ref qw( ref_slot );
77    
78  sub var_NumericalHashToArray {  sub numhash2list {
79    my $vref = shift;    my $vref = shift;
80    #my $context = shift;    #my $context = shift;
81    if (ref $vref eq 'HASH') {    if (ref $vref eq 'HASH') {
# Line 55  sub var_NumericalHashToArray { Line 91  sub var_NumericalHashToArray {
91                }                }
92                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
93              }              }
94              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
95            }            }
96  #      } else {  #      } else {
97  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 65  sub var_NumericalHashToArray { Line 101  sub var_NumericalHashToArray {
101  }  }
102    
103    
104  sub var_deref {  # FIXME: could this be refactored using expand?
105    # btw: "expand": look at scripts@CPAN (System Administration):
106    # there is a perl make with perl
107    sub _var_deref_test {
108    my $obj = shift;    my $obj = shift;
109      my $options = shift;
110    my $result;    my $result;
111    if ((ref $obj) eq 'ARRAY') {    if ((ref $obj) eq 'ARRAY') {
112      foreach (@{$obj}) {      foreach (@{$obj}) {
113        my $ref = ref $_;        my $ref = ref $_;
114        if ($ref) {        if ($ref) {
115          push(@{$result}, var_deref($_));          push(@{$result}, var_deref($_, $options));
116            #undef $_;
117        } else {        } else {
118          push(@{$result}, $_);          #push(@{$result}, $_);
119            push(@{$result}, deep_copy($_));
120        }        }
121          #undef $_ if $options->{destroy};
122          #$options->{destroy}->($_) if $options->{destroy};
123      }      }
124    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
125    } else {    } else {
# Line 83  sub var_deref { Line 127  sub var_deref {
127        my $key = $_;        my $key = $_;
128        my $ref = ref $obj->{$_};        my $ref = ref $obj->{$_};
129        if ($ref) {        if ($ref) {
130          $result->{$_} = var_deref($obj->{$_});          $result->{$_} = var_deref($obj->{$_}, $options);
131            #undef $obj->{$_};
132        } else {        } else {
133          $result->{$_} = $obj->{$_};          #$result->{$_} = $obj->{$_};
134            $result->{$_} = deep_copy($obj->{$_});
135        }        }
136          #undef $obj->{$_} if $options->{destroy};
137          #$options->{destroy}->($obj->{$_}) if $options->{destroy};
138      }      }
139    }    }
140      #undef $obj if $options->{destroy};
141      $options->{cb_destroy}->($obj) if $options->{cb_destroy};
142    return $result;    return $result;
143  }  }
144    
145    
146    # convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML
147    # but still using the known latin locale stuff
148    sub expand {
149    
150      my $obj = shift;
151      my $options = shift;
152      my $result;
153    
154      #print "ref: ", ref $obj, "\n";
155    
156      if (ref $obj eq 'ARRAY') {
157    
158        IterArray @$obj, sub {
159          my $item;
160          # if current item is a reference ...
161          if (ref $_[0]) {
162            $item = $_[0];
163            # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
164            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
165            if ($item && $options->{childObj2string}) {
166              $item = childObj2string($item, $options->{childObj2string});
167            } else {
168              # ... expand structure recursively
169              $item = expand($_[0], $options);
170            }
171            # destroy item via seperate callback method (a POST) if requested
172            #$options->{cb}->{destroy}->($_[0]) if $options->{destroy};
173    
174          # ... assume plain scalar
175          } else {
176            #$item = deep_copy($_[0]);
177            $item = $_[0];
178            # conversions/encodings
179            $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
180            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
181          }
182          #push(@{$result}, $item) if $item;   # use item only if not undef  (TODO: make configurable via $options)
183          push(@{$result}, $item);                 # use item in any case
184    
185        }
186    
187      } elsif (ref $obj eq 'CODE') {
188        #print Dumper($obj);
189        #exit;
190    
191      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
192      } elsif (ref $obj) {
193    
194        IterHash %$obj, sub {
195          my $item;
196          
197          # if current item is a reference ...
198          if (ref $_[1]) {
199            $item = $_[1];
200            # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
201            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
202            if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) {
203              $item = childObj2string($item, $options->{childObj2string});
204            } else {
205              # ... expand structure recursively
206              $item = expand($_[1], $options);
207            }
208            # destroy item via seperate callback method (a POST) if requested
209            #$options->{cb}->{destroy}->($_[1]) if $options->{destroy};
210          
211          # ... assume plain scalar
212          } else {
213            #$item = deep_copy($_[1]);
214            $item = $_[1];
215            # conversions/encodings
216            $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
217            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
218          }
219          #$result->{$_[0]} = $item if $item;   # use item only if not undef  (TODO: make configurable via $options)
220          $result->{$_[0]} = $item;               # use item in any case
221        }
222    
223      } else {
224        #die ("not a reference!");
225        $result = $obj;
226    
227      }
228    
229      # convert all values to utf8 (inside complex struct)
230        # now done in core-item-callbacks via Greg London's "Iterate" from CPAN
231        # latin_to_utf8($result) if ($options->{utf8});
232    
233      # destroy persistent object from memory to be sure to get a fresh one next time
234      #undef $obj if $options->{destroy};
235      #$options->{cb_destroy}->($obj) if $options->{cb_destroy};
236      #$options->{cb}->{destroy}->($obj) if $options->{destroy};
237      
238      return $result;
239    }
240    
241    
242    
243  my @indexstack;  my @indexstack;
244  sub var_traverse_mixin_update {  sub _var_traverse_mixin_update_old {
245    my $obj = shift;    my $obj = shift;
246    my $data = shift;    my $data = shift;
247    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 140  sub var_traverse_mixin_update { Line 285  sub var_traverse_mixin_update {
285    #return $result;    #return $result;
286  }  }
287    
288  sub object2hash {  sub merge_to {
289    my $object = shift;    _hash2object(@_);
290    my $options = shift;    # TODO:
291    my $deref = var_deref($object);    # re-implement using CPAN's "Iterate".
   var2utf8($deref) if ($options->{utf8});  
   return $deref;  
292  }  }
293    
294    
295  # todo: maybe do diff(s) here sometimes!?  # todo: maybe do diff(s) here sometimes!?
296  sub hash2object {  sub _hash2object {
297    my $object = shift;    my $object = shift;
298    my $data = shift;    my $data = shift;
299    my $options = shift;    my $options = shift;
# Line 158  sub hash2object { Line 302  sub hash2object {
302        
303    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
304    # this method corrects this    # this method corrects this
305    var_NumericalHashToArray($data) if ($options->{php});    numhash2list($data) if ($options->{php});
306    
307    # utf8-conversion/-encoding (essential for I18N)    # utf8-conversion/-encoding (essential for I18N)
308    var_utf2iso($data) if ($options->{utf8});    var_utf2iso($data) if ($options->{utf8});
# Line 170  sub hash2object { Line 314  sub hash2object {
314    #my $obj = $self->getObject($oid);    #my $obj = $self->getObject($oid);
315    
316    # mix changes into fresh object and save it back    # mix changes into fresh object and save it back
317    hash2object_traverse_mixin($object, $data);    _hash2object_traverse_mixin($object, $data, 0, $options);
318    
319    # done in core mixin function?    # done in core mixin function?
320    #$self->{storage}->update($obj);    #$self->{storage}->update($obj);
# Line 193  sub hash2object { Line 337  sub hash2object {
337  #my @indexstack;  #my @indexstack;
338    
339  # 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
340  sub hash2object_traverse_mixin {  sub _hash2object_traverse_mixin {
341    my $object = shift;    my $object = shift;
342    my $data = shift;    my $data = shift;
343    my $bool_recursion = shift;    my $bool_recursion = shift;
344      my $options = shift;
345    
346    # 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)
347    @indexstack = () if (!$bool_recursion);    @indexstack = () if (!$bool_recursion);
# Line 210  sub hash2object_traverse_mixin { Line 355  sub hash2object_traverse_mixin {
355    # what's exactly done here? hmmm.... it works ;)    # what's exactly done here? hmmm.... it works ;)
356    # 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!!!
357    # extend! check!    # extend! check!
358    if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {    
359      #print attributes::reftype($object), "\n";
360      
361      # V1
362      #if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
363      # V2
364      my $reftype = attributes::reftype($object);
365      #print "reftype: '$reftype'", "\n";
366      if ($reftype eq 'HASH') {
367    
368  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
369                    
370        my @fields;
371      # loop through fields of object (Tangram-object)      # loop through fields of object (Tangram-object)
372      foreach (keys %{$object}) {      @fields = keys %{$object};
373        push @indexstack, $_;  
374        # loop through fields of to.be.injected-data (arbitrary Perl-data-structure)
375        @fields = keys %{$data} if $options->{init};
376    #    @fields = keys %{$data} if $options->{mixin};
377        
378        foreach (@fields) {
379          my $field = $_;
380          push @indexstack, $field;
381                            
382        # determine type of object        # determine type of object
383        my $ref = ref $object->{$_};        my $ref = ref $object->{$field};
384  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
385        if ($ref) {        if ($ref) {
386          hash2object_traverse_mixin($object->{$_}, $data, 1);          _hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
387        } else {        } else {
388          my $val = getStructSlotByStringyAddress($data, \@indexstack);          my $val = ref_slot($data, \@indexstack);
389          $object->{$_} = $val;                  
390    #print Dumper($options);
391            my $field_target = $field;
392            if (my $pattern = $options->{pattern_strip_key}) {
393              print "pattern: $pattern", "\n";
394              $field_target =~ s/$pattern//;
395              print "field: $field_target", "\n";
396            }
397    
398            $object->{$field_target} = $val if defined $val;
399        }        }
400        pop @indexstack;        pop @indexstack;
401      }      }
# Line 256  sub hash2object_traverse_mixin { Line 426  sub hash2object_traverse_mixin {
426        push @indexstack, $i;        push @indexstack, $i;
427        my $ref = ref $_;        my $ref = ref $_;
428  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
429        if ($ref && $_) {  #      if ($ref && $_) {
430          hash2object_traverse_mixin($_, $data, 1);        if ($ref) {
431            _hash2object_traverse_mixin($_, $data, 1, $options);
432        } else {        } else {
433          $object->[$i] = $_;          $object->[$i] = $_ if defined $_;
434        }        }
435        pop @indexstack;        pop @indexstack;
436        $i++;        $i++;
437      }      }
438    
439    =pod
440      # object?
441      } else {
442        print "reference: ", ref $object, "\n";
443        print "reftype: ", $reftype, "\n";
444        die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!");
445    =cut
446      
447    }    }
448    
449  }  }
450    
451    
452  # this function seems to do similar stuff like these below (refexpr2perlref & co.)  # ACK's go to Randal L. Schwartz <merlyn@stonehenge.com>
453  sub getStructSlotByStringyAddress {  # on the website:
454    my $var = shift;  # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
455    my $indexstack_ref = shift;  # Further distribution or use is not permitted.
456    my @indexstack = @{$indexstack_ref};  # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
457    my $joiner = '->';  sub deep_copy1 {
458    my @evlist;    my $this = shift;
459    foreach (@indexstack) {    if (not ref $this) {
460      my $elem;      $this;
461      if ($_ =~ m/\d+/) { $elem = "[$_]"; }    } elsif (ref $this eq "ARRAY") {
462      if ($_ =~ m/\D+/) { $elem = "{$_}"; }      [map deep_copy1($_), @$this];
463      push @evlist, $elem;    } elsif (ref $this eq "HASH") {
464    }      +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
465    my $evstring = join($joiner, @evlist);    } elsif (ref $this eq "CODE") {
466    $evstring = 'return $var->' . $evstring . ';';      $this;
467    #print "ev: $evstring\n";    } else { die "deep_copy1 asks: what type is $this?" }
468    return eval($evstring);    #} else { print "deep_copy asks: what type is $this?", "\n"; }
469  }  }
470    
471    # ACK's go to Andrew Pitonyak
472    # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)
473    # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html
474    sub deep_copy {
475      Pitonyak::DeepCopy::deep_copy(@_);
476    }
477    
 sub refexpr2perlref {  
478    
479    sub childObj2string {
480    my $obj = shift;    my $obj = shift;
481    my $expr = shift;    my $option = shift;
482    my $callbackMap = shift;    my $classname = ref $obj;
   
   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";  
       }  
     }  
   
   } else {  
     # use expr as simple scalar key (attributename)  
     $value = $obj->{$expr};  
   }  
             
   return $value;  
483        
484      if($option == 1) {
485        my $string = "o_" . $classname . "_" .$obj->{guid};
486        return $string;
487      }  
488  }  }
489    
490    
 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);  
 }  
   
491  1;  1;
492    __END__

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.14

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