3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.4 2002/12/05 13:56:49 joko |
7 |
|
# - var_deref |
8 |
|
# + expand - more sophisticated dereferencing with callbacks using Iterate |
9 |
|
# |
10 |
|
# Revision 1.3 2002/12/03 05:34:55 joko |
11 |
|
# + bugfix: now utilizing var_utf2iso from Data::Transform::Encode |
12 |
|
# |
13 |
|
# Revision 1.2 2002/12/01 04:44:07 joko |
14 |
|
# + code from Data::Transform::OO |
15 |
|
# |
16 |
# Revision 1.1 2002/11/29 04:49:20 joko |
# Revision 1.1 2002/11/29 04:49:20 joko |
17 |
# + initial check-in |
# + initial check-in |
18 |
# |
# |
29 |
|
|
30 |
require Exporter; |
require Exporter; |
31 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
32 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
33 |
&var_NumericalHashToArray |
&var_NumericalHashToArray |
34 |
&var_deref |
&var_mixin |
35 |
&var_mixin |
&object2hash |
36 |
&getStructSlotByStringyAddress |
&hash2object |
37 |
|
&refexpr2perlref |
38 |
|
&expand |
39 |
); |
); |
40 |
|
# &var_deref |
41 |
|
# &getStructSlotByStringyAddress |
42 |
|
|
43 |
use attributes; |
use attributes; |
44 |
|
use Data::Dumper; |
45 |
|
use Data::Transform::Encode qw( var_utf2iso var2utf8 scalar2utf8 ); |
46 |
|
use libp qw( deep_copy ); |
47 |
|
use Iterate; |
48 |
|
|
49 |
sub var_NumericalHashToArray { |
sub var_NumericalHashToArray { |
50 |
my $vref = shift; |
my $vref = shift; |
74 |
|
|
75 |
sub var_deref { |
sub var_deref { |
76 |
my $obj = shift; |
my $obj = shift; |
77 |
|
my $options = shift; |
78 |
my $result; |
my $result; |
79 |
if ((ref $obj) eq 'ARRAY') { |
if ((ref $obj) eq 'ARRAY') { |
80 |
foreach (@{$obj}) { |
foreach (@{$obj}) { |
81 |
my $ref = ref $_; |
my $ref = ref $_; |
82 |
if ($ref) { |
if ($ref) { |
83 |
push(@{$result}, var_deref($_)); |
push(@{$result}, var_deref($_, $options)); |
84 |
|
#undef $_; |
85 |
} else { |
} else { |
86 |
push(@{$result}, $_); |
#push(@{$result}, $_); |
87 |
|
push(@{$result}, deep_copy($_)); |
88 |
} |
} |
89 |
|
#undef $_ if $options->{destroy}; |
90 |
|
#$options->{destroy}->($_) if $options->{destroy}; |
91 |
} |
} |
92 |
|
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
93 |
} else { |
} else { |
94 |
foreach (keys %{$obj}) { |
foreach (keys %{$obj}) { |
95 |
my $key = $_; |
my $key = $_; |
96 |
my $ref = ref $obj->{$_}; |
my $ref = ref $obj->{$_}; |
97 |
if ($ref) { |
if ($ref) { |
98 |
$result->{$_} = var_deref($obj->{$_}); |
$result->{$_} = var_deref($obj->{$_}, $options); |
99 |
|
#undef $obj->{$_}; |
100 |
} else { |
} else { |
101 |
$result->{$_} = $obj->{$_}; |
#$result->{$_} = $obj->{$_}; |
102 |
|
$result->{$_} = deep_copy($obj->{$_}); |
103 |
} |
} |
104 |
|
#undef $obj->{$_} if $options->{destroy}; |
105 |
|
#$options->{destroy}->($obj->{$_}) if $options->{destroy}; |
106 |
} |
} |
107 |
} |
} |
108 |
|
#undef $obj if $options->{destroy}; |
109 |
|
$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
110 |
return $result; |
return $result; |
111 |
} |
} |
112 |
|
|
113 |
|
|
114 |
|
sub expand { |
115 |
|
|
116 |
|
my $obj = shift; |
117 |
|
my $options = shift; |
118 |
|
my $result; |
119 |
|
|
120 |
|
if ((ref $obj) eq 'ARRAY') { |
121 |
|
|
122 |
|
IterArray @$obj, sub { |
123 |
|
my $item; |
124 |
|
# if current item is a reference ... |
125 |
|
if (ref $_[0]) { |
126 |
|
# ... expand structure recursively |
127 |
|
$item = expand($_[0], $options); |
128 |
|
# destroy item via seperate callback method (a POST) if requested |
129 |
|
#$options->{cb}->{destroy}->($_[0]) if $options->{destroy}; |
130 |
|
|
131 |
|
# ... assume plain scalar |
132 |
|
} else { |
133 |
|
#$item = deep_copy($_[0]); |
134 |
|
$item = $_[0]; |
135 |
|
# conversions/encodings |
136 |
|
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
137 |
|
} |
138 |
|
#push(@{$result}, $item) if $item; # use item only if not undef (TODO: make configurable via $options) |
139 |
|
push(@{$result}, $item); # use item in any case |
140 |
|
|
141 |
|
} |
142 |
|
|
143 |
|
# TODO: "} elsif (ref $obj eq 'HASH') { [...] } else { croak 'could not deref blah'; }" ??? |
144 |
|
} else { |
145 |
|
|
146 |
|
IterHash %$obj, sub { |
147 |
|
my $item; |
148 |
|
|
149 |
|
# if current item is a reference ... |
150 |
|
if (ref $_[1]) { |
151 |
|
# ... expand structure recursively |
152 |
|
$item = expand($_[1], $options); |
153 |
|
# destroy item via seperate callback method (a POST) if requested |
154 |
|
#$options->{cb}->{destroy}->($_[1]) if $options->{destroy}; |
155 |
|
|
156 |
|
# ... assume plain scalar |
157 |
|
} else { |
158 |
|
#$item = deep_copy($_[1]); |
159 |
|
$item = $_[1]; |
160 |
|
# conversions/encodings |
161 |
|
$item = scalar2utf8($item) if ($item && $options->{utf8}); |
162 |
|
} |
163 |
|
#$result->{$_[0]} = $item if $item; # use item only if not undef (TODO: make configurable via $options) |
164 |
|
$result->{$_[0]} = $item; # use item in any case |
165 |
|
} |
166 |
|
|
167 |
|
} |
168 |
|
|
169 |
|
# convert all values to utf8 (inside complex struct) |
170 |
|
# now done in core-item-callbacks via Greg London's "Iterate" from CPAN |
171 |
|
# var2utf8($result) if ($options->{utf8}); |
172 |
|
|
173 |
|
# destroy persistent object from memory to be sure to get a fresh one next time |
174 |
|
#undef $obj if $options->{destroy}; |
175 |
|
#$options->{cb_destroy}->($obj) if $options->{cb_destroy}; |
176 |
|
#$options->{cb}->{destroy}->($obj) if $options->{destroy}; |
177 |
|
|
178 |
|
return $result; |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
my @indexstack; |
my @indexstack; |
225 |
#return $result; |
#return $result; |
226 |
} |
} |
227 |
|
|
228 |
|
sub object2hash { |
229 |
|
my $object = shift; |
230 |
|
my $options = shift; |
231 |
|
#my $deref = var_deref($object); |
232 |
|
my $deref = expand($object, $options); |
233 |
|
#var2utf8($deref) if ($options->{utf8}); |
234 |
|
return $deref; |
235 |
|
} |
236 |
|
|
237 |
|
# todo: maybe do diff(s) here sometimes!? |
238 |
|
sub hash2object { |
239 |
|
my $object = shift; |
240 |
|
my $data = shift; |
241 |
|
my $options = shift; |
242 |
|
|
243 |
|
#print "hash2object", "\n"; |
244 |
|
|
245 |
|
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
246 |
|
# this method corrects this |
247 |
|
var_NumericalHashToArray($data) if ($options->{php}); |
248 |
|
|
249 |
|
# utf8-conversion/-encoding (essential for I18N) |
250 |
|
var_utf2iso($data) if ($options->{utf8}); |
251 |
|
|
252 |
|
# get fresh object from database |
253 |
|
# todo: |
254 |
|
# - handle "locked" objects !!! |
255 |
|
# - check if already another update occoured (object-revisioning needed!) |
256 |
|
#my $obj = $self->getObject($oid); |
257 |
|
|
258 |
|
# mix changes into fresh object and save it back |
259 |
|
hash2object_traverse_mixin($object, $data); |
260 |
|
|
261 |
|
# done in core mixin function? |
262 |
|
#$self->{storage}->update($obj); |
263 |
|
|
264 |
|
# we should "undef" some objects here to destroy them in memory and enforce reloading when used next time |
265 |
|
# simulate: |
266 |
|
#$self->{storage}->disconnect(); |
267 |
|
|
268 |
|
} |
269 |
|
|
270 |
|
# ------------------------------------ |
271 |
|
# core mixin function |
272 |
|
# TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data |
273 |
|
|
274 |
|
# remember keys of structures we are traversing |
275 |
|
# this is a HACK: |
276 |
|
# - don't (!!!) "packageglobal" @indexstack |
277 |
|
# - it will lead to problems with parallelism! |
278 |
|
|
279 |
|
#my @indexstack; |
280 |
|
|
281 |
|
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
282 |
|
sub hash2object_traverse_mixin { |
283 |
|
my $object = shift; |
284 |
|
my $data = shift; |
285 |
|
my $bool_recursion = shift; |
286 |
|
|
287 |
|
# clear our key - stack if we are called from user-code (non-recursively) |
288 |
|
@indexstack = () if (!$bool_recursion); |
289 |
|
|
290 |
|
my $classname = ref $object; |
291 |
|
# if ($classname) { |
292 |
|
# print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n"; |
293 |
|
# } |
294 |
|
|
295 |
|
# parser: detected OBJECT (a Tangram one?) (reftype == HASH) (assume) |
296 |
|
# what's exactly done here? hmmm.... it works ;) |
297 |
|
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
298 |
|
# extend! check! |
299 |
|
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
300 |
|
|
301 |
|
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
302 |
|
|
303 |
|
# loop through fields of object (Tangram-object) |
304 |
|
foreach (keys %{$object}) { |
305 |
|
push @indexstack, $_; |
306 |
|
|
307 |
|
# determine type of object |
308 |
|
my $ref = ref $object->{$_}; |
309 |
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
310 |
|
if ($ref) { |
311 |
|
hash2object_traverse_mixin($object->{$_}, $data, 1); |
312 |
|
} else { |
313 |
|
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
314 |
|
$object->{$_} = $val; |
315 |
|
} |
316 |
|
pop @indexstack; |
317 |
|
} |
318 |
|
|
319 |
|
# save object to database ... |
320 |
|
# ... do an update if it already exists, do an insert if it doesn't |
321 |
|
# my $objectId = $self->{storage}->id($obj); |
322 |
|
# $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" ); |
323 |
|
# if ($objectId) { |
324 |
|
# $self->{storage}->update($obj); |
325 |
|
|
326 |
|
#print __PACKAGE__ . ":", "\n"; |
327 |
|
#print Dumper($object); |
328 |
|
|
329 |
|
# } else { |
330 |
|
# $self->{storage}->insert($obj); |
331 |
|
# } |
332 |
|
|
333 |
|
} |
334 |
|
|
335 |
|
#&& ( ref($obj) ne 'HASH' ) |
336 |
|
|
337 |
|
# loop through entries of array (IntrArray, isn't it?) |
338 |
|
if ((ref $object) eq 'ARRAY') { |
339 |
|
# print STDERR "===", "refttype ", attributes::reftype($obj), "\n"; |
340 |
|
my $i = 0; |
341 |
|
foreach (@{$object}) { |
342 |
|
push @indexstack, $i; |
343 |
|
my $ref = ref $_; |
344 |
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
345 |
|
if ($ref && $_) { |
346 |
|
hash2object_traverse_mixin($_, $data, 1); |
347 |
|
} else { |
348 |
|
$object->[$i] = $_; |
349 |
|
} |
350 |
|
pop @indexstack; |
351 |
|
$i++; |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
# this function seems to do similar stuff like these below (refexpr2perlref & co.) |
359 |
sub getStructSlotByStringyAddress { |
sub getStructSlotByStringyAddress { |
360 |
my $var = shift; |
my $var = shift; |
361 |
my $indexstack_ref = shift; |
my $indexstack_ref = shift; |
374 |
return eval($evstring); |
return eval($evstring); |
375 |
} |
} |
376 |
|
|
377 |
|
|
378 |
|
sub refexpr2perlref { |
379 |
|
|
380 |
|
my $obj = shift; |
381 |
|
my $expr = shift; |
382 |
|
my $callbackMap = shift; |
383 |
|
|
384 |
|
my $value; |
385 |
|
# detect for callback (code-reference) |
386 |
|
if (ref($expr) eq 'CODE') { |
387 |
|
$value = &$expr($obj); |
388 |
|
|
389 |
|
} elsif ($expr =~ m/->/) { |
390 |
|
# use expr as complex object reference declaration (obj->subObj->subSubObj->0->attribute) |
391 |
|
(my $objPerlRefString, my $parts) = refexpr2perlref_parts($expr); |
392 |
|
#print "\n", "expr: $expr"; |
393 |
|
#print "\n", "objPerlRefString: $objPerlRefString"; |
394 |
|
$value = eval('$obj' . '->' . $objPerlRefString); |
395 |
|
|
396 |
|
# if value isn't set, try to "fallback" to callbackMap |
397 |
|
# callbacks are applied this way: |
398 |
|
# take the last element of the expression parts and check if this string exists as a key in the callback-map |
399 |
|
if (!$value && $callbackMap) { |
400 |
|
#print Dumper($callbackMap); |
401 |
|
|
402 |
|
# prepare needle |
403 |
|
my @parts = @$parts; |
404 |
|
my $count = $#parts; |
405 |
|
my $needle = $parts[$count]; |
406 |
|
|
407 |
|
# prepare haystack |
408 |
|
my @haystack = keys %$callbackMap; |
409 |
|
if (grep($needle, @haystack)) { |
410 |
|
$value = $callbackMap->{$needle}->(); |
411 |
|
#print "value: ", $value, "\n"; |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
|
} else { |
416 |
|
# use expr as simple scalar key (attributename) |
417 |
|
$value = $obj->{$expr}; |
418 |
|
} |
419 |
|
|
420 |
|
return $value; |
421 |
|
|
422 |
|
} |
423 |
|
|
424 |
|
|
425 |
|
sub refexpr2perlref_parts { |
426 |
|
my $expr = shift; |
427 |
|
|
428 |
|
# split expression by dereference operators first |
429 |
|
my @parts_pure = split(/->/, $expr); |
430 |
|
|
431 |
|
# wrap []'s around each part, if it consists of numeric characters only (=> numeric = array-index), |
432 |
|
# use {}'s, if there are word-characters in it (=> alphanumeric = hash-key) |
433 |
|
my @parts_capsule = @parts_pure; |
434 |
|
map { |
435 |
|
m/^\d+$/ && ($_ = "[$_]") || ($_ = "{$_}"); |
436 |
|
} @parts_capsule; |
437 |
|
|
438 |
|
# join parts with dereference operators together again and return built string |
439 |
|
return (join('->', @parts_capsule), \@parts_pure); |
440 |
|
} |
441 |
|
|
442 |
1; |
1; |