/[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.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.3  2002/12/03 05:34:55  joko  ##  +- modified 'sub refexpr2perlref': now can get the $delimiter passed, too
7  #  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode  ##
8  #  ##  Revision 1.8  2003/02/09 05:10:56  joko
9  #  Revision 1.2  2002/12/01 04:44:07  joko  ##  + minor update
10  #  + code from Data::Transform::OO  ##
11  #  ##  Revision 1.7  2003/01/19 03:26:59  joko
12  #  Revision 1.1  2002/11/29 04:49:20  joko  ##  + added 'sub deep_copy'  -  refactored from libp
13  #  + initial check-in  ##  + add 'sub merge'  -  exported from CPAN's 'Hash::Merge'
14  #  ##
15  #  Revision 1.1  2002/10/10 03:26:00  cvsjoko  ##  Revision 1.6  2002/12/23 11:27:53  jonen
16  #  + new  ##  + 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 27  require Exporter; Line 45  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
   &var_deref  
48    &var_mixin    &var_mixin
49    &object2hash    &object2hash
50    &hash2object    &hash2object
51    &refexpr2perlref    &refexpr2perlref
52      &expand
53      &deep_copy
54      &merge
55  );  );
56    #  &var_deref
57  #  &getStructSlotByStringyAddress  #  &getStructSlotByStringyAddress
58    
59  use attributes;  use attributes;
60  use Data::Dumper;  use Data::Dumper;
61  use Data::Transform::Encode qw( var_utf2iso );  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 65  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'; }"  ???    # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
113    } else {    } else {
# Line 83  sub var_deref { Line 115  sub var_deref {
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 143  sub var_traverse_mixin_update { Line 248  sub var_traverse_mixin_update {
248  sub object2hash {  sub object2hash {
249    my $object = shift;    my $object = shift;
250    my $options = shift;    my $options = shift;
251    my $deref = var_deref($object);    #my $deref = var_deref($object);
252    var2utf8($deref) if ($options->{utf8});    my $deref = expand($object, $options);
253      #var2utf8($deref) if ($options->{utf8});
254    return $deref;    return $deref;
255  }  }
256    
# Line 170  sub hash2object { Line 276  sub hash2object {
276    #my $obj = $self->getObject($oid);    #my $obj = $self->getObject($oid);
277    
278    # mix changes into fresh object and save it back    # mix changes into fresh object and save it back
279    hash2object_traverse_mixin($object, $data);    hash2object_traverse_mixin($object, $data, 0, $options);
280    
281    # done in core mixin function?    # done in core mixin function?
282    #$self->{storage}->update($obj);    #$self->{storage}->update($obj);
# Line 197  sub hash2object_traverse_mixin { Line 303  sub hash2object_traverse_mixin {
303    my $object = shift;    my $object = shift;
304    my $data = shift;    my $data = shift;
305    my $bool_recursion = shift;    my $bool_recursion = shift;
306      my $options = shift;
307    
308    # 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)
309    @indexstack = () if (!$bool_recursion);    @indexstack = () if (!$bool_recursion);
# Line 214  sub hash2object_traverse_mixin { Line 321  sub hash2object_traverse_mixin {
321    
322  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";  #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
323                    
324        my @fields;
325      # loop through fields of object (Tangram-object)      # loop through fields of object (Tangram-object)
326      foreach (keys %{$object}) {      @fields = keys %{$object};
327        push @indexstack, $_;  
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        # determine type of object
337        my $ref = ref $object->{$_};        my $ref = ref $object->{$field};
338  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
339        if ($ref) {        if ($ref) {
340          hash2object_traverse_mixin($object->{$_}, $data, 1);          hash2object_traverse_mixin($object->{$field}, $data, 1, $options);
341        } else {        } else {
342          my $val = getStructSlotByStringyAddress($data, \@indexstack);          my $val = getStructSlotByStringyAddress($data, \@indexstack);
343          $object->{$_} = $val;                  
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;        pop @indexstack;
355      }      }
# Line 256  sub hash2object_traverse_mixin { Line 380  sub hash2object_traverse_mixin {
380        push @indexstack, $i;        push @indexstack, $i;
381        my $ref = ref $_;        my $ref = ref $_;
382  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";  #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
383        if ($ref && $_) {  #      if ($ref && $_) {
384          hash2object_traverse_mixin($_, $data, 1);        if ($ref) {
385            hash2object_traverse_mixin($_, $data, 1, $options);
386        } else {        } else {
387          $object->[$i] = $_;          $object->[$i] = $_ if defined $_;
388        }        }
389        pop @indexstack;        pop @indexstack;
390        $i++;        $i++;
# Line 270  sub hash2object_traverse_mixin { Line 395  sub hash2object_traverse_mixin {
395    
396    
397  # this function seems to do similar stuff like these below (refexpr2perlref & co.)  # 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 293  sub refexpr2perlref { Line 419  sub refexpr2perlref {
419    
420    my $obj = shift;    my $obj = shift;
421    my $expr = shift;    my $expr = shift;
422    my $callbackMap = shift;    my $values = shift;
423      my $delimiter = shift;
424    
425      $delimiter ||= '->';
426    
427    my $value;    my $value;
428    # detect for callback (code-reference)    # detect for callback (code-reference)
429    if (ref($expr) eq 'CODE') {    if (ref($expr) eq 'CODE') {
430      $value = &$expr($obj);      $value = &$expr($obj);
431    
432    } elsif ($expr =~ m/->/) {    } elsif ($expr =~ m/$delimiter/) {
433      # use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute)      # use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute)
434      (my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr);      (my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr, $delimiter);
435      #print "\n", "expr: $expr";      
436      #print "\n", "objPerlRefString: $objPerlRefString";      # trace
437      $value = eval('$obj' . '->' . $objPerlRefString);        #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      # if value isn't set, try to "fallback" to callbackMap      # determine $values - if it's a hashref, use it as a "valueMap"
463        # "fallback" to callbackMap
464      # callbacks are applied this way:      # 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      #   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) {      if (!$value && $callbackMap) {
# Line 338  sub refexpr2perlref { Line 491  sub refexpr2perlref {
491    
492  sub refexpr2perlref_parts {  sub refexpr2perlref_parts {
493    my $expr = shift;    my $expr = shift;
494      my $delimiter = shift;
495    
496      $delimiter = quotemeta($delimiter);
497    
498    # split expression by dereference operators first    # split expression by dereference operators first
499    my @parts_pure = split(/->/, $expr);    my @parts_pure = split(/$delimiter/, $expr);
500    
501    # wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index),    # 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)    # use {}'s, if there are word-characters in it (=> alphanumeric = hash-key)
# Line 353  sub refexpr2perlref_parts { Line 509  sub refexpr2perlref_parts {
509    return (join('->', @parts_capsule), \@parts_pure);    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.3  
changed lines
  Added in v.1.9

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