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