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

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

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