/[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.1 by joko, Fri Nov 29 04:49:20 2002 UTC revision 1.21 by joko, Wed Apr 9 07:21:56 2003 UTC
# Line 1  Line 1 
1  ##############################################  ## ---------------------------------------------------------------------------
2  #  ##  $Id$
3  #  $Id$  ## ---------------------------------------------------------------------------
4  #  ##  $Log$
5  #  $Log$  ##  Revision 1.21  2003/04/09 07:21:56  joko
6  #  Revision 1.1  2002/11/29 04:49:20  joko  ##  childObj2string now inside Encode.pm, renamed to 'twingle_reference'
7  #  + initial check-in  ##
8  #  ##  Revision 1.20  2003/04/04 17:31:23  joko
9  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##  minor update to 'childObj2string'
10  #  + new  ##
11  #  ##  Revision 1.19  2003/03/28 03:11:25  jonen
12  ##############################################  ##  + bugfix
13    ##
14    ##  Revision 1.18  2003/03/28 03:07:26  jonen
15    ##  + minor changes
16    ##
17    ##  Revision 1.17  2003/03/27 15:17:07  joko
18    ##  namespace fixes for Data::Mungle::*
19    ##
20    ##  Revision 1.16  2003/03/27 15:04:52  joko
21    ##  minor update: comment
22    ##
23    ##  Revision 1.15  2003/02/27 14:39:48  jonen
24    ##  + fixed bug at _hash2object()
25    ##
26    ##  Revision 1.14  2003/02/22 17:13:55  jonen
27    ##  + added function 'childObject2string()' to encode 'child'-references to option related string
28    ##  + use new option at 'expand()' for replacing 'childObject2string'
29    ##
30    ##  Revision 1.13  2003/02/21 01:48:50  joko
31    ##  renamed core function
32    ##
33    ##  Revision 1.12  2003/02/20 22:45:19  joko
34    ##  fix regarding new deep_copy
35    ##
36    ##  Revision 1.11  2003/02/20 21:13:54  joko
37    ##  - removed implementation of deep_copy2 - get this from the Pitonyak namespace (now cloned to repository)
38    ##
39    ##  Revision 1.10  2003/02/20 20:48:00  joko
40    ##  - refactored lots of code to Data::Code::Ref
41    ##  + alternative 'deep_copy' implementation
42    ##
43    ##  Revision 1.9  2003/02/18 19:35:56  joko
44    ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
45    ##
46    ##  Revision 1.8  2003/02/09 05:10:56  joko
47    ##  + minor update
48    ##
49    ##  Revision 1.7  2003/01/19 03:26:59  joko
50    ##  + added 'sub deep_copy'  -  refactored from libp
51    ##  + add 'sub merge'  -  exported from CPAN's 'Hash::Merge'
52    ##
53    ##  Revision 1.6  2002/12/23 11:27:53  jonen
54    ##  + changed behavior WATCH!
55    ##
56    ##  Revision 1.5  2002/12/16 19:57:54  joko
57    ##  + option 'init'
58    ##
59    ##  Revision 1.4  2002/12/05 13:56:49  joko
60    ##  - var_deref
61    ##  + expand - more sophisticated dereferencing with callbacks using Iterate
62    ##
63    ##  Revision 1.3  2002/12/03 05:34:55  joko
64    ##  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode
65    ##
66    ##  Revision 1.2  2002/12/01 04:44:07  joko
67    ##  + code from Data::Transform::OO
68    ##
69    ##  Revision 1.1  2002/11/29 04:49:20  joko
70    ##  + initial check-in
71    ##
72    ##  Revision 1.1  2002/10/10 03:26:00  cvsjoko
73    ##  + new
74    ## ---------------------------------------------------------------------------
75    
76    
77  package Data::Transform::Deep;  package Data::Mungle::Transform::Deep;
78    
79  use strict;  use strict;
80  use warnings;  use warnings;
81    
82  require Exporter;  require Exporter;
83  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
84  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
85    &var_NumericalHashToArray    &expand
86    &var_deref    &deep_copy
87    &var_mixin    &merge_to
   &getStructSlotByStringyAddress  
88  );  );
89    
90    
91  use attributes;  use attributes;
92    use Data::Dumper;
93    use Iterate;
94    
95    use Pitonyak::DeepCopy;
96    use Data::Mungle::Transform::Encode qw( latin_to_utf8 latin_to_utf8_scalar utf8_to_latin utf8_to_latin_scalar twingle_reference );
97    use Data::Mungle::Code::Ref qw( ref_slot );
98    
99  sub var_NumericalHashToArray {  sub numhash2list {
100    my $vref = shift;    my $vref = shift;
101    #my $context = shift;    #my $context = shift;
102    if (ref $vref eq 'HASH') {    if (ref $vref eq 'HASH') {
# Line 45  sub var_NumericalHashToArray { Line 112  sub var_NumericalHashToArray {
112                }                }
113                $vref->{$_} = \@arr;                $vref->{$_} = \@arr;
114              }              }
115              var_NumericalHashToArray($vref->{$_});              numhash2list($vref->{$_});
116            }            }
117  #      } else {  #      } else {
118  #        #$vref->{$_} = scalar2iso($vref->{$_});  #        #$vref->{$_} = scalar2iso($vref->{$_});
# Line 55  sub var_NumericalHashToArray { Line 122  sub var_NumericalHashToArray {
122  }  }
123    
124    
125  sub var_deref {  # FIXME: could this be refactored using expand?
126    # btw: "expand": look at scripts@CPAN (System Administration):
127    # there is a perl make with perl
128    sub _var_deref_test {
129    my $obj = shift;    my $obj = shift;
130      my $options = shift;
131    my $result;    my $result;
132    if ((ref $obj) eq 'ARRAY') {    if ((ref $obj) eq 'ARRAY') {
133      foreach (@{$obj}) {      foreach (@{$obj}) {
134        my $ref = ref $_;        my $ref = ref $_;
135        if ($ref) {        if ($ref) {
136          push(@{$result}, var_deref($_));          push(@{$result}, var_deref($_, $options));
137            #undef $_;
138        } else {        } else {
139          push(@{$result}, $_);          #push(@{$result}, $_);
140            push(@{$result}, deep_copy($_));
141        }        }
142          #undef $_ if $options->{destroy};
143          #$options->{destroy}->($_) if $options->{destroy};
144      }      }
145      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
146    } else {    } else {
147      foreach (keys %{$obj}) {      foreach (keys %{$obj}) {
148        my $key = $_;        my $key = $_;
149        my $ref = ref $obj->{$_};        my $ref = ref $obj->{$_};
150        if ($ref) {        if ($ref) {
151          $result->{$_} = var_deref($obj->{$_});          $result->{$_} = var_deref($obj->{$_}, $options);
152            #undef $obj->{$_};
153        } else {        } else {
154          $result->{$_} = $obj->{$_};          #$result->{$_} = $obj->{$_};
155            $result->{$_} = deep_copy($obj->{$_});
156        }        }
157          #undef $obj->{$_} if $options->{destroy};
158          #$options->{destroy}->($obj->{$_}) if $options->{destroy};
159      }      }
160    }    }
161      #undef $obj if $options->{destroy};
162      $options->{cb_destroy}->($obj) if $options->{cb_destroy};
163    return $result;    return $result;
164  }  }
165    
166    
167    # convert values in hash to utf8 (and back) to be ready for (e.g.) encapsulation in XML
168    # but still using the known latin locale stuff
169    sub expand {
170    
171      my $obj = shift;
172      my $options = shift;
173      my $result;
174    
175      #print "ref: ", ref $obj, "\n";
176    
177      if (ref $obj eq 'ARRAY') {
178    
179        IterArray @$obj, sub {
180          my $item;
181          # if current item is a reference ...
182          if (ref $_[0]) {
183            $item = $_[0];
184            # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
185            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
186            if ($item && $options->{childObj2string}) {
187              $item = twingle_reference($item);
188            } else {
189              # ... expand structure recursively
190              $item = expand($_[0], $options);
191            }
192            # destroy item via seperate callback method (a POST) if requested
193            #$options->{cb}->{destroy}->($_[0]) if $options->{destroy};
194    
195          # ... assume plain scalar
196          } else {
197            #$item = deep_copy($_[0]);
198            $item = $_[0];
199            # conversions/encodings
200            $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
201            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
202          }
203          #push(@{$result}, $item) if $item;   # use item only if not undef  (TODO: make configurable via $options)
204          push(@{$result}, $item);                 # use item in any case
205    
206        }
207    
208      } elsif (ref $obj eq 'CODE') {
209        #print Dumper($obj);
210        #exit;
211    
212      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
213      } elsif (ref $obj) {
214    
215        IterHash %$obj, sub {
216          my $item;
217          
218          # if current item is a reference ...
219          if (ref $_[1]) {
220            $item = $_[1];
221            # if $options->{childObj2string} is TRUE or STRING don't expand referenced object,
222            # instead replace it by per option choosed string (default: o_<classname>_<ref type>_<guid> )
223            if ($item && $options->{childObj2string} && !(ref $_[1] eq "ARRAY") && !(ref $_[1] eq "HASH")) {
224              $item = twingle_reference($item);
225            } else {
226              # ... expand structure recursively
227              $item = expand($_[1], $options);
228            }
229            # destroy item via seperate callback method (a POST) if requested
230            #$options->{cb}->{destroy}->($_[1]) if $options->{destroy};
231          
232          # ... assume plain scalar
233          } else {
234            #$item = deep_copy($_[1]);
235            $item = $_[1];
236            # conversions/encodings
237            $item = latin_to_utf8_scalar($item) if ($item && $options->{utf8});
238            $item = utf8_to_latin_scalar($item) if ($item && $options->{to_latin});
239          }
240          #$result->{$_[0]} = $item if $item;   # use item only if not undef  (TODO: make configurable via $options)
241          $result->{$_[0]} = $item;               # use item in any case
242        }
243    
244      } else {
245        #die ("not a reference!");
246        $result = $obj;
247    
248      }
249    
250      # convert all values to utf8 (inside complex struct)
251        # now done in core-item-callbacks via Greg London's "Iterate" from CPAN
252        # latin_to_utf8($result) if ($options->{utf8});
253    
254      # destroy persistent object from memory to be sure to get a fresh one next time
255      #undef $obj if $options->{destroy};
256      #$options->{cb_destroy}->($obj) if $options->{cb_destroy};
257      #$options->{cb}->{destroy}->($obj) if $options->{destroy};
258      
259      return $result;
260    }
261    
262    
263    
264  my @indexstack;  my @indexstack;
265  sub var_traverse_mixin_update {  sub _var_traverse_mixin_update_old {
266    my $obj = shift;    my $obj = shift;
267    my $data = shift;    my $data = shift;
268    my $bool_recursion = shift;    my $bool_recursion = shift;
# Line 129  sub var_traverse_mixin_update { Line 306  sub var_traverse_mixin_update {
306    #return $result;    #return $result;
307  }  }
308    
309  sub getStructSlotByStringyAddress {  sub merge_to {
310    my $var = shift;    _hash2object(@_);
311    my $indexstack_ref = shift;    # TODO:
312    my @indexstack = @{$indexstack_ref};    # re-implement using CPAN's "Iterate" and/or a modified Hash::Merge.
313    my $joiner = '->';  }
314    my @evlist;  
315    foreach (@indexstack) {  
316      my $elem;  # todo: maybe do diff(s) here sometimes!?
317      if ($_ =~ m/\d+/) { $elem = "[$_]"; }  sub _hash2object {
318      if ($_ =~ m/\D+/) { $elem = "{$_}"; }    my $object = shift;
319      push @evlist, $elem;    my $data = shift;
320    }    my $options = shift;
321    my $evstring = join($joiner, @evlist);  
322    $evstring = 'return $var->' . $evstring . ';';    #print "hash2object", "\n";
323    #print "ev: $evstring\n";    
324    return eval($evstring);    # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
325      # this method corrects this
326      numhash2list($data) if ($options->{php});
327    
328      # utf8-conversion/-encoding (essential for I18N)
329      utf8_to_latin($data) if ($options->{utf8});
330    
331      # get fresh object from database
332      # todo:
333      #   - handle "locked" objects !!!
334      #   - check if already another update occoured (object-revisioning needed!)
335      #my $obj = $self->getObject($oid);
336    
337      # mix changes into fresh object and save it back
338      _hash2object_traverse_mixin($object, $data, 0, $options);
339    
340      # done in core mixin function?
341      #$self->{storage}->update($obj);
342    
343      # we should "undef" some objects here to destroy them in memory and enforce reloading when used next time
344      # simulate:
345      #$self->{storage}->disconnect();
346    
347    }
348    
349    # ------------------------------------
350    #   core mixin function
351    # TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data
352    
353    # remember keys of structures we are traversing
354    # this is a HACK:
355    #  - don't (!!!) "packageglobal" @indexstack
356    #  - it will lead to problems with parallelism!
357    
358    #my @indexstack;
359    
360    # traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object
361    sub _hash2object_traverse_mixin {
362      my $object = shift;
363      my $data = shift;
364      my $bool_recursion = shift;
365      my $options = shift;
366    
367      # clear our key - stack if we are called from user-code (non-recursively)
368      @indexstack = () if (!$bool_recursion);
369    
370      my $classname = ref $object;
371    #  if ($classname) {
372    #    print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n";
373    #  }
374    
375      # parser: detected OBJECT (a Tangram one?)   (reftype == HASH) (assume)
376      # what's exactly done here? hmmm.... it works ;)
377      # maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!!
378      # extend! check!
379      
380      #print attributes::reftype($object), "\n";
381      
382      # V1
383      #if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
384      # V2
385      my $reftype = attributes::reftype($object);
386      #print "reftype: '$reftype'", "\n";
387      if ($reftype eq 'HASH') {
388    
389    #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
390            
391        my @fields;
392        # loop through fields of object (Tangram-object)
393        @fields = keys %{$object};
394    
395        # loop through fields of to.be.injected-data (arbitrary Perl-data-structure)
396        @fields = keys %{$data} if $options->{init};
397    #    @fields = keys %{$data} if $options->{mixin};
398        
399        foreach (@fields) {
400          my $field = $_;
401          push @indexstack, $field;
402                
403          # determine type of object
404          my $ref = ref $object->{$field};
405    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
406          if ($ref) {
407            _hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
408          } else {
409            my $val = ref_slot($data, \@indexstack);
410            
411    #print Dumper($options);
412            my $field_target = $field;
413            if (my $pattern = $options->{pattern_strip_key}) {
414              print "pattern: $pattern", "\n";
415              $field_target =~ s/$pattern//;
416              print "field: $field_target", "\n";
417            }
418    
419            $object->{$field_target} = $val if defined $val;
420          }
421          pop @indexstack;
422        }
423    
424        # save object to database ...
425        # ... do an update if it already exists, do an insert if it doesn't
426    #    my $objectId = $self->{storage}->id($obj);
427    #    $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" );
428    #    if ($objectId) {
429    #      $self->{storage}->update($obj);
430    
431    #print __PACKAGE__ . ":", "\n";
432    #print Dumper($object);
433    
434    #    } else {
435    #      $self->{storage}->insert($obj);
436    #    }
437        
438      }
439    
440    #&& ( ref($obj) ne 'HASH' )
441    
442        # loop through entries of array (IntrArray, isn't it?)
443      if ((ref $object) eq 'ARRAY') {
444    #    print STDERR "===", "refttype ", attributes::reftype($obj), "\n";
445        my $i = 0;
446        foreach (@{$object}) {
447          push @indexstack, $i;
448          my $ref = ref $_;
449    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
450    #      if ($ref && $_) {
451          if ($ref) {
452            _hash2object_traverse_mixin($_, $data, 1, $options);
453          } else {
454            $object->[$i] = $_ if defined $_;
455          }
456          pop @indexstack;
457          $i++;
458        }
459    
460    =pod
461      # object?
462      } else {
463        print "reference: ", ref $object, "\n";
464        print "reftype: ", $reftype, "\n";
465        die(__PACKAGE__ . "->_hash2object_traverse_mixin: can not handle this!");
466    =cut
467      
468      }
469    
470    }
471    
472    
473    # ACK's go to Randal L. Schwartz <merlyn@stonehenge.com>
474    # on the website:
475    # This text is copyright by Miller-Freeman, Inc., and is used with their permission.
476    # Further distribution or use is not permitted.
477    # please visit http://www.stonehenge.com/merlyn/UnixReview/col30.html
478    sub deep_copy1 {
479      my $this = shift;
480      if (not ref $this) {
481        $this;
482      } elsif (ref $this eq "ARRAY") {
483        [map deep_copy1($_), @$this];
484      } elsif (ref $this eq "HASH") {
485        +{map { $_ => deep_copy1($this->{$_}) } keys %$this};
486      } elsif (ref $this eq "CODE") {
487        $this;
488      } else { die "deep_copy1 asks: what type is $this?" }
489      #} else { print "deep_copy asks: what type is $this?", "\n"; }
490    }
491    
492    # ACK's go to Andrew Pitonyak
493    # Copyright 2002, Andrew Pitonyak (perlboy@pitonyak.org)
494    # please visit: http://www.pitonyak.org/code/perl/Pitonyak/DeepCopy.pm.html
495    sub deep_copy {
496      Pitonyak::DeepCopy::deep_copy(@_);
497  }  }
498    
499    
500  1;  1;
501    __END__

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.21

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