/[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.9 by joko, Tue Feb 18 19:35:56 2003 UTC
# Line 1  Line 1 
1  ##############################################  ## ---------------------------------------------------------------------------
2  #  ##  $Id$
3  #  $Id$  ## ---------------------------------------------------------------------------
4  #  ##  $Log$
5  #  $Log$  ##  Revision 1.9  2003/02/18 19:35:56  joko
6  #  Revision 1.1  2002/11/29 04:49:20  joko  ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
7  #  + initial check-in  ##
8  #  ##  Revision 1.8  2003/02/09 05:10:56  joko
9  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##  + minor update
10  #  + new  ##
11  #  ##  Revision 1.7  2003/01/19 03:26:59  joko
12  ##############################################  ##  + added 'sub deep_copy'  -  refactored from libp
13    ##  + add 'sub merge'  -  exported from CPAN's 'Hash::Merge'
14    ##
15    ##  Revision 1.6  2002/12/23 11:27:53  jonen
16    ##  + changed behavior WATCH!
17    ##
18    ##  Revision 1.5  2002/12/16 19:57:54  joko
19    ##  + option 'init'
20    ##
21    ##  Revision 1.4  2002/12/05 13:56:49  joko
22    ##  - var_deref
23    ##  + expand - more sophisticated dereferencing with callbacks using Iterate
24    ##
25    ##  Revision 1.3  2002/12/03 05:34:55  joko
26    ##  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode
27    ##
28    ##  Revision 1.2  2002/12/01 04:44:07  joko
29    ##  + code from Data::Transform::OO
30    ##
31    ##  Revision 1.1  2002/11/29 04:49:20  joko
32    ##  + initial check-in
33    ##
34    ##  Revision 1.1  2002/10/10 03:26:00  cvsjoko
35    ##  + new
36    ## ---------------------------------------------------------------------------
37    
38    
39  package Data::Transform::Deep;  package Data::Transform::Deep;
# Line 19  use warnings; Line 43  use warnings;
43    
44  require Exporter;  require Exporter;
45  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
46  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
47    &var_NumericalHashToArray    &var_NumericalHashToArray
48    &var_deref    &var_mixin
49    &var_mixin    &object2hash
50    &getStructSlotByStringyAddress    &hash2object
51      &refexpr2perlref
52      &expand
53      &deep_copy
54      &merge
55  );  );
56    #  &var_deref
57    #  &getStructSlotByStringyAddress
58    
59  use attributes;  use attributes;
60    use Data::Dumper;
61    use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 );
62    
63    use Hash::Merge qw( merge );
64    use Iterate;
65    
66  sub var_NumericalHashToArray {  sub var_NumericalHashToArray {
67    my $vref = shift;    my $vref = shift;
# Line 55  sub var_NumericalHashToArray { Line 89  sub var_NumericalHashToArray {
89  }  }
90    
91    
92    # FIXME: could this be refactored using expand?
93    # btw: "expand": look at scripts@CPAN (System Administration):
94    # there is a perl make with perl
95  sub var_deref {  sub var_deref {
96    my $obj = shift;    my $obj = shift;
97      my $options = shift;
98    my $result;    my $result;
99    if ((ref $obj) eq 'ARRAY') {    if ((ref $obj) eq 'ARRAY') {
100      foreach (@{$obj}) {      foreach (@{$obj}) {
101        my $ref = ref $_;        my $ref = ref $_;
102        if ($ref) {        if ($ref) {
103          push(@{$result}, var_deref($_));          push(@{$result}, var_deref($_, $options));
104            #undef $_;
105        } else {        } else {
106          push(@{$result}, $_);          #push(@{$result}, $_);
107            push(@{$result}, deep_copy($_));
108        }        }
109          #undef $_ if $options->{destroy};
110          #$options->{destroy}->($_) if $options->{destroy};
111      }      }
112      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
113    } else {    } else {
114      foreach (keys %{$obj}) {      foreach (keys %{$obj}) {
115        my $key = $_;        my $key = $_;
116        my $ref = ref $obj->{$_};        my $ref = ref $obj->{$_};
117        if ($ref) {        if ($ref) {
118          $result->{$_} = var_deref($obj->{$_});          $result->{$_} = var_deref($obj->{$_}, $options);
119            #undef $obj->{$_};
120        } else {        } else {
121          $result->{$_} = $obj->{$_};          #$result->{$_} = $obj->{$_};
122            $result->{$_} = deep_copy($obj->{$_});
123        }        }
124          #undef $obj->{$_} if $options->{destroy};
125          #$options->{destroy}->($obj->{$_}) if $options->{destroy};
126      }      }
127    }    }
128      #undef $obj if $options->{destroy};
129      $options->{cb_destroy}->($obj) if $options->{cb_destroy};
130    return $result;    return $result;
131  }  }
132    
133    
134    sub expand {
135    
136      my $obj = shift;
137      my $options = shift;
138      my $result;
139    
140      if ((ref $obj) eq 'ARRAY') {
141    
142        IterArray @$obj, sub {
143          my $item;
144          # if current item is a reference ...
145          if (ref $_[0]) {
146            # ... expand structure recursively
147            $item = expand($_[0], $options);
148            # destroy item via seperate callback method (a POST) if requested
149            #$options->{cb}->{destroy}->($_[0]) if $options->{destroy};
150    
151          # ... assume plain scalar
152          } else {
153            #$item = deep_copy($_[0]);
154            $item = $_[0];
155            # conversions/encodings
156            $item = scalar2utf8($item) if ($item && $options->{utf8});
157          }
158          #push(@{$result}, $item) if $item;   # use item only if not undef  (TODO: make configurable via $options)
159          push(@{$result}, $item);                 # use item in any case
160    
161        }
162    
163      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
164      } else {
165    
166        IterHash %$obj, sub {
167          my $item;
168          
169          # if current item is a reference ...
170          if (ref $_[1]) {
171            # ... expand structure recursively
172            $item = expand($_[1], $options);
173            # destroy item via seperate callback method (a POST) if requested
174            #$options->{cb}->{destroy}->($_[1]) if $options->{destroy};
175          
176          # ... assume plain scalar
177          } else {
178            #$item = deep_copy($_[1]);
179            $item = $_[1];
180            # conversions/encodings
181            $item = scalar2utf8($item) if ($item && $options->{utf8});
182          }
183          #$result->{$_[0]} = $item if $item;   # use item only if not undef  (TODO: make configurable via $options)
184          $result->{$_[0]} = $item;               # use item in any case
185        }
186    
187      }
188    
189      # convert all values to utf8 (inside complex struct)
190        # now done in core-item-callbacks via Greg London's "Iterate" from CPAN
191        # var2utf8($result) if ($options->{utf8});
192    
193      # destroy persistent object from memory to be sure to get a fresh one next time
194      #undef $obj if $options->{destroy};
195      #$options->{cb_destroy}->($obj) if $options->{cb_destroy};
196      #$options->{cb}->{destroy}->($obj) if $options->{destroy};
197      
198      return $result;
199    }
200    
201    
202    
203  my @indexstack;  my @indexstack;
# Line 129  sub var_traverse_mixin_update { Line 245  sub var_traverse_mixin_update {
245    #return $result;    #return $result;
246  }  }
247    
248    sub object2hash {
249      my $object = shift;
250      my $options = shift;
251      #my $deref = var_deref($object);
252      my $deref = expand($object, $options);
253      #var2utf8($deref) if ($options->{utf8});
254      return $deref;
255    }
256    
257    # todo: maybe do diff(s) here sometimes!?
258    sub hash2object {
259      my $object = shift;
260      my $data = shift;
261      my $options = shift;
262    
263      #print "hash2object", "\n";
264      
265      # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
266      # this method corrects this
267      var_NumericalHashToArray($data) if ($options->{php});
268    
269      # utf8-conversion/-encoding (essential for I18N)
270      var_utf2iso($data) if ($options->{utf8});
271    
272      # get fresh object from database
273      # todo:
274      #   - handle "locked" objects !!!
275      #   - check if already another update occoured (object-revisioning needed!)
276      #my $obj = $self->getObject($oid);
277    
278      # mix changes into fresh object and save it back
279      hash2object_traverse_mixin($object, $data, 0, $options);
280    
281      # done in core mixin function?
282      #$self->{storage}->update($obj);
283    
284      # we should "undef" some objects here to destroy them in memory and enforce reloading when used next time
285      # simulate:
286      #$self->{storage}->disconnect();
287    
288    }
289    
290    # ------------------------------------
291    #   core mixin function
292    # TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data
293    
294    # remember keys of structures we are traversing
295    # this is a HACK:
296    #  - don't (!!!) "packageglobal" @indexstack
297    #  - it will lead to problems with parallelism!
298    
299    #my @indexstack;
300    
301    # traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object
302    sub hash2object_traverse_mixin {
303      my $object = shift;
304      my $data = shift;
305      my $bool_recursion = shift;
306      my $options = shift;
307    
308      # clear our key - stack if we are called from user-code (non-recursively)
309      @indexstack = () if (!$bool_recursion);
310    
311      my $classname = ref $object;
312    #  if ($classname) {
313    #    print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n";
314    #  }
315    
316      # parser: detected OBJECT (a Tangram one?)   (reftype == HASH) (assume)
317      # what's exactly done here? hmmm.... it works ;)
318      # maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!!
319      # extend! check!
320      if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
321    
322    #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
323            
324        my @fields;
325        # loop through fields of object (Tangram-object)
326        @fields = keys %{$object};
327    
328        # loop through fields of to.be.injected-data (arbitrary Perl-data-structure)
329        @fields = keys %{$data} if $options->{init};
330    #    @fields = keys %{$data} if $options->{mixin};
331        
332        foreach (@fields) {
333          my $field = $_;
334          push @indexstack, $field;
335                
336          # determine type of object
337          my $ref = ref $object->{$field};
338    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
339          if ($ref) {
340            hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
341          } else {
342            my $val = getStructSlotByStringyAddress($data, \@indexstack);
343            
344    #print Dumper($options);
345            my $field_target = $field;
346            if (my $pattern = $options->{pattern_strip_key}) {
347              print "pattern: $pattern", "\n";
348              $field_target =~ s/$pattern//;
349              print "field: $field_target", "\n";
350            }
351    
352            $object->{$field_target} = $val if defined $val;
353          }
354          pop @indexstack;
355        }
356    
357        # save object to database ...
358        # ... do an update if it already exists, do an insert if it doesn't
359    #    my $objectId = $self->{storage}->id($obj);
360    #    $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" );
361    #    if ($objectId) {
362    #      $self->{storage}->update($obj);
363    
364    #print __PACKAGE__ . ":", "\n";
365    #print Dumper($object);
366    
367    #    } else {
368    #      $self->{storage}->insert($obj);
369    #    }
370        
371      }
372    
373    #&& ( ref($obj) ne 'HASH' )
374    
375        # loop through entries of array (IntrArray, isn't it?)
376      if ((ref $object) eq 'ARRAY') {
377    #    print STDERR "===", "refttype ", attributes::reftype($obj), "\n";
378        my $i = 0;
379        foreach (@{$object}) {
380          push @indexstack, $i;
381          my $ref = ref $_;
382    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
383    #      if ($ref && $_) {
384          if ($ref) {
385            hash2object_traverse_mixin($_, $data, 1, $options);
386          } else {
387            $object->[$i] = $_ if defined $_;
388          }
389          pop @indexstack;
390          $i++;
391        }
392      }
393    
394    }
395    
396    
397    # this function seems to do similar stuff like these below (refexpr2perlref & co.)
398    # TODO: maybe this mechanism can be replaced completely through some nice module from CPAN .... ?   ;-)
399  sub getStructSlotByStringyAddress {  sub getStructSlotByStringyAddress {
400    my $var = shift;    my $var = shift;
401    my $indexstack_ref = shift;    my $indexstack_ref = shift;
# Line 147  sub getStructSlotByStringyAddress { Line 414  sub getStructSlotByStringyAddress {
414    return eval($evstring);    return eval($evstring);
415  }  }
416    
417    
418    sub refexpr2perlref {
419    
420      my $obj = shift;
421      my $expr = shift;
422      my $values = shift;
423      my $delimiter = shift;
424    
425      $delimiter ||= '->';
426    
427      my $value;
428      # detect for callback (code-reference)
429      if (ref($expr) eq 'CODE') {
430        $value = &$expr($obj);
431    
432      } elsif ($expr =~ m/$delimiter/) {
433        # use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute)
434        (my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr, $delimiter);
435        
436        # trace
437          #print "expr: $expr", "\n";
438          #print "objPerlRefString: $objPerlRefString", "\n";
439        
440        my $evstr = '$obj' . '->' . $objPerlRefString;
441        
442        # trace
443          #print "eval: $evstr", "\n";
444        
445        $value = eval($evstr);
446        die ($@) if $@;
447        
448        # if value isn't set, try to set it
449        my $callbackMap;
450        if ($values) {
451          #if (ref $values eq 'HASH') {
452          if (ref $values eq 'Data::Map') {
453            $callbackMap = $values->getAttributes();
454          } else {
455            # apply $values as single value to referenced slot
456            #print "APPLY!", "\n";
457            eval('$obj' . '->' . $objPerlRefString . ' = $values;');
458            die ($@) if $@;
459          }
460        }
461        
462        # determine $values - if it's a hashref, use it as a "valueMap"
463        # "fallback" to callbackMap
464        # callbacks are applied this way:
465        #   take the last element of the expression parts and check if this string exists as a key in the callback-map
466        if (!$value && $callbackMap) {
467          #print Dumper($callbackMap);
468          
469          # prepare needle
470          my @parts = @$parts;
471          my $count = $#parts;
472          my $needle = $parts[$count];
473    
474          # prepare haystack
475          my @haystack = keys %$callbackMap;
476          if (grep($needle, @haystack)) {
477            $value = $callbackMap->{$needle}->();
478            #print "value: ", $value, "\n";
479          }
480        }
481    
482      } else {
483        # use expr as simple scalar key (attributename)
484        $value = $obj->{$expr};
485      }
486              
487      return $value;
488      
489    }
490    
491    
492    sub refexpr2perlref_parts {
493      my $expr = shift;
494      my $delimiter = shift;
495    
496      $delimiter = quotemeta($delimiter);
497    
498      # split expression by dereference operators first
499      my @parts_pure = split(/$delimiter/, $expr);
500    
501      # wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index),
502      # use {}'s, if there are word-characters in it (=> alphanumeric = hash-key)
503      my @parts_capsule = @parts_pure;
504      map {
505        m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}");
506      } @parts_capsule;
507    
508      # join parts with dereference operators together again and return built string
509      return (join('->', @parts_capsule), \@parts_pure);
510    }
511    
512    # ACK's go to ...
513    sub deep_copy {
514      my $this = shift;
515      if (not ref $this) {
516        $this;
517      } elsif (ref $this eq "ARRAY") {
518        [map deep_copy($_), @$this];
519      } elsif (ref $this eq "HASH") {
520        +{map { $_ => deep_copy($this->{$_}) } keys %$this};
521      } elsif (ref $this eq "CODE") {
522        $this;
523      #} else { die "deep_copy asks: what type is $this?" }
524      } else { print "deep_copy asks: what type is $this?", "\n"; }
525    }
526    
527  1;  1;

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

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