/[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.3 by joko, Tue Dec 3 05:34:55 2002 UTC
# Line 3  Line 3 
3  #  $Id$  #  $Id$
4  #  #
5  #  $Log$  #  $Log$
6    #  Revision 1.3  2002/12/03 05:34:55  joko
7    #  + bugfix: now utilizing var_utf2iso from Data::Transform::Encode
8    #
9    #  Revision 1.2  2002/12/01 04:44:07  joko
10    #  + code from Data::Transform::OO
11    #
12  #  Revision 1.1  2002/11/29 04:49:20  joko  #  Revision 1.1  2002/11/29 04:49:20  joko
13  #  + initial check-in  #  + initial check-in
14  #  #
# Line 19  use warnings; Line 25  use warnings;
25    
26  require Exporter;  require Exporter;
27  our @ISA = qw( Exporter );  our @ISA = qw( Exporter );
28  our @EXPORT_OK = qw(  our @EXPORT_OK = qw(
29    &var_NumericalHashToArray    &var_NumericalHashToArray
30    &var_deref    &var_deref
31    &var_mixin    &var_mixin
32    &getStructSlotByStringyAddress    &object2hash
33      &hash2object
34      &refexpr2perlref
35  );  );
36    #  &getStructSlotByStringyAddress
37    
38  use attributes;  use attributes;
39    use Data::Dumper;
40    use Data::Transform::Encode qw( var_utf2iso );
41    
42  sub var_NumericalHashToArray {  sub var_NumericalHashToArray {
43    my $vref = shift;    my $vref = shift;
# Line 67  sub var_deref { Line 77  sub var_deref {
77          push(@{$result}, $_);          push(@{$result}, $_);
78        }        }
79      }      }
80      # TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }"  ???
81    } else {    } else {
82      foreach (keys %{$obj}) {      foreach (keys %{$obj}) {
83        my $key = $_;        my $key = $_;
# Line 129  sub var_traverse_mixin_update { Line 140  sub var_traverse_mixin_update {
140    #return $result;    #return $result;
141  }  }
142    
143    sub object2hash {
144      my $object = shift;
145      my $options = shift;
146      my $deref = var_deref($object);
147      var2utf8($deref) if ($options->{utf8});
148      return $deref;
149    }
150    
151    # todo: maybe do diff(s) here sometimes!?
152    sub hash2object {
153      my $object = shift;
154      my $data = shift;
155      my $options = shift;
156    
157      #print "hash2object", "\n";
158      
159      # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
160      # this method corrects this
161      var_NumericalHashToArray($data) if ($options->{php});
162    
163      # utf8-conversion/-encoding (essential for I18N)
164      var_utf2iso($data) if ($options->{utf8});
165    
166      # get fresh object from database
167      # todo:
168      #   - handle "locked" objects !!!
169      #   - check if already another update occoured (object-revisioning needed!)
170      #my $obj = $self->getObject($oid);
171    
172      # mix changes into fresh object and save it back
173      hash2object_traverse_mixin($object, $data);
174    
175      # done in core mixin function?
176      #$self->{storage}->update($obj);
177    
178      # we should "undef" some objects here to destroy them in memory and enforce reloading when used next time
179      # simulate:
180      #$self->{storage}->disconnect();
181    
182    }
183    
184    # ------------------------------------
185    #   core mixin function
186    # TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data
187    
188    # remember keys of structures we are traversing
189    # this is a HACK:
190    #  - don't (!!!) "packageglobal" @indexstack
191    #  - it will lead to problems with parallelism!
192    
193    #my @indexstack;
194    
195    # traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object
196    sub hash2object_traverse_mixin {
197      my $object = shift;
198      my $data = shift;
199      my $bool_recursion = shift;
200    
201      # clear our key - stack if we are called from user-code (non-recursively)
202      @indexstack = () if (!$bool_recursion);
203    
204      my $classname = ref $object;
205    #  if ($classname) {
206    #    print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n";
207    #  }
208    
209      # parser: detected OBJECT (a Tangram one?)   (reftype == HASH) (assume)
210      # what's exactly done here? hmmm.... it works ;)
211      # maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!!
212      # extend! check!
213      if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
214    
215    #    print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
216            
217        # loop through fields of object (Tangram-object)
218        foreach (keys %{$object}) {
219          push @indexstack, $_;
220                
221          # determine type of object
222          my $ref = ref $object->{$_};
223    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
224          if ($ref) {
225            hash2object_traverse_mixin($object->{$_}, $data, 1);
226          } else {
227            my $val = getStructSlotByStringyAddress($data, \@indexstack);
228            $object->{$_} = $val;        
229          }
230          pop @indexstack;
231        }
232    
233        # save object to database ...
234        # ... do an update if it already exists, do an insert if it doesn't
235    #    my $objectId = $self->{storage}->id($obj);
236    #    $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" );
237    #    if ($objectId) {
238    #      $self->{storage}->update($obj);
239    
240    #print __PACKAGE__ . ":", "\n";
241    #print Dumper($object);
242    
243    #    } else {
244    #      $self->{storage}->insert($obj);
245    #    }
246        
247      }
248    
249    #&& ( ref($obj) ne 'HASH' )
250    
251        # loop through entries of array (IntrArray, isn't it?)
252      if ((ref $object) eq 'ARRAY') {
253    #    print STDERR "===", "refttype ", attributes::reftype($obj), "\n";
254        my $i = 0;
255        foreach (@{$object}) {
256          push @indexstack, $i;
257          my $ref = ref $_;
258    #      print STDERR "attrname: $_  ATTRref: $ref", "\n";
259          if ($ref && $_) {
260            hash2object_traverse_mixin($_, $data, 1);
261          } else {
262            $object->[$i] = $_;
263          }
264          pop @indexstack;
265          $i++;
266        }
267      }
268    
269    }
270    
271    
272    # this function seems to do similar stuff like these below (refexpr2perlref & co.)
273  sub getStructSlotByStringyAddress {  sub getStructSlotByStringyAddress {
274    my $var = shift;    my $var = shift;
275    my $indexstack_ref = shift;    my $indexstack_ref = shift;
# Line 147  sub getStructSlotByStringyAddress { Line 288  sub getStructSlotByStringyAddress {
288    return eval($evstring);    return eval($evstring);
289  }  }
290    
291    
292    sub refexpr2perlref {
293    
294      my $obj = shift;
295      my $expr = shift;
296      my $callbackMap = shift;
297    
298      my $value;
299      # detect for callback (code-reference)
300      if (ref($expr) eq 'CODE') {
301        $value = &$expr($obj);
302    
303      } elsif ($expr =~ m/->/) {
304        # use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute)
305        (my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr);
306        #print "\n", "expr: $expr";
307        #print "\n", "objPerlRefString: $objPerlRefString";
308        $value = eval('$obj' . '->' . $objPerlRefString);
309        
310        # if value isn't set, try to "fallback" to callbackMap
311        # callbacks are applied this way:
312        #   take the last element of the expression parts and check if this string exists as a key in the callback-map
313        if (!$value && $callbackMap) {
314          #print Dumper($callbackMap);
315          
316          # prepare needle
317          my @parts = @$parts;
318          my $count = $#parts;
319          my $needle = $parts[$count];
320    
321          # prepare haystack
322          my @haystack = keys %$callbackMap;
323          if (grep($needle, @haystack)) {
324            $value = $callbackMap->{$needle}->();
325            #print "value: ", $value, "\n";
326          }
327        }
328    
329      } else {
330        # use expr as simple scalar key (attributename)
331        $value = $obj->{$expr};
332      }
333              
334      return $value;
335      
336    }
337    
338    
339    sub refexpr2perlref_parts {
340      my $expr = shift;
341    
342      # split expression by dereference operators first
343      my @parts_pure = split(/->/, $expr);
344    
345      # wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index),
346      # use {}'s, if there are word-characters in it (=> alphanumeric = hash-key)
347      my @parts_capsule = @parts_pure;
348      map {
349        m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}");
350      } @parts_capsule;
351    
352      # join parts with dereference operators together again and return built string
353      return (join('->', @parts_capsule), \@parts_pure);
354    }
355    
356  1;  1;

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

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