blib/lib/UR/BoolExpr/Util.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 225 SCAR 67.0
CC
28
Cov
0%
_fixup_ur_objects_from_thawed_data
90.0
values_to_value_id
90.0
value_id_to_values
90.0
CRAP 812.0
1  
2 package UR::BoolExpr::Util;
3  
4 # Non-OO Utility methods for the rule modules.
5  
6 use strict;
7 use warnings;
8 require UR;
9 our $VERSION = "0.46"; # UR $VERSION;
10  
11 use Scalar::Util qw(blessed reftype refaddr);
12 use Data::Dumper;
13 use FreezeThaw;
14  
15 # Because the id is actually a full data structure we need some separators.
16 # Note that these are used for the common case, where FreezeThaw is for arbitrarily complicated rule identifiers.
17  
18 our $id_sep = chr(29); # spearetes id property values instead of the old \t
19 our $record_sep = chr(30); # within a value_id, delimits a distinct values
20 our $unit_sep = chr(31); # seperates items within a single value
21  
22 our $null_value = chr(21); # used for undef/null
23 our $empty_string = chr(28); # used for ""
24 our $empty_list = chr(20); # used for []
25  
26 # These are used when there is any sort of complicated data in the rule.
27  
28 sub values_to_value_id_frozen {
29 my $frozen = FreezeThaw::safeFreeze(@_);
30 return "F:" . $frozen;
31 }
32  
33 sub value_id_to_values_frozen {
34 my $value_id = shift;
35 no warnings 'redefine';
36 local *FreezeThaw::copyContents = \&_FreezeThaw__copyContents;
37 return _fixup_ur_objects_from_thawed_data(FreezeThaw::thaw($value_id));
38 }
39  
40 # FreezeThaw::thaw calls copyContents when thawing out a data structure it's seen before to
41 # copy the contents from an already thawed version into a to-be-thawed container.
42 # But this doesn't work for code references, since it can't reconstitute it. Luckily, if
43 # it's been frozen with safeFreeze, then $first and $second both point to the correct
44 # coderef and there's nothing to do.
45 my $original_FreezeThaw__copyContents = \&FreezeThaw::copyContents;
46 sub _FreezeThaw__copyContents {
47 my($first, $second) = @_;
48  
49 goto &$original_FreezeThaw__copyContents if (reftype($first) ne 'CODE' or reftype($second) ne 'CODE');
50  
51 if (refaddr($first) != refaddr($second)) {
52 my $ref = reftype($second);
53 Carp::croak("Don't know how to copyContents of type `$ref'");
54 }
55 if (ref($second) ne ref($first)) {
56 bless $_[0], ref $second; # Rebless
57 }
58 return $first;
59 }
60  
61 sub _fixup_ur_objects_from_thawed_data {
62 my @values = @_;
63  
64 our $seen;
65 local $seen = $seen;
66 $seen ||= {};
67  
68 # For things that are UR::Objects (or used to be UR objects), swap the
69 # thawed/cloned one with one from the object cache
70 #
71 # This sub is localized inside _fixup_ur_objects_from_thawed_data so it's not called
72 # externally, and uses $_ as the thing to process, which is set in the foreach loop
73 # below - both as a performance speedup of# not having to prepare an argument list while
74 # processing a possibly deep data structure, and clarity of avoiding double dereferencing
75 # as this sub needs to mutate the item it's processing
76 my $process_it = sub {
77 if (blessed($_)
78 and (
79 $_->isa('UR::Object')
80 or
81 $_->isa('UR::BoolExpr::Util::clonedThing')
82 )
83 ) {
84 my($class, $id) = ($_->class, $_->id);
85 if (refaddr($_) != refaddr($UR::Context::all_objects_loaded->{$class}->{$id})) {
86 # bless the original thing to a non-UR::Object class so UR::Object::DESTROY
87 # doesn't run on it
88 my $cloned_thing = UR::BoolExpr::Util::clonedThing->bless($_);
89 # Swap in the object from the object cache
90 $_ = $UR::Context::all_objects_loaded->{$class}->{$id};
91 }
92  
93 }
94 _fixup_ur_objects_from_thawed_data($_);
95 };
96  
97 foreach my $data ( @values ) {
98 next unless ref $data; # Don't need to recursively inspect normal scalar data
99 next if $seen->{$data}++;
100  
101 if (ref $data) {
102 my $reftype = reftype($data);
103 my $iter;
104 if ($reftype eq 'ARRAY') {
105 foreach (@$data) {
106 &$process_it;
107 }
108 } elsif ($reftype eq 'HASH') {
109 foreach (values %$data) {
110 &$process_it;
111 }
112  
113 } elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') {
114 local $_ = $$data;
115 &$process_it;
116 }
117 }
118 }
119 return @values;
120 }
121  
122 # These are used for the simple common-case rules.
123  
124 sub values_to_value_id {
125 my $value_id = "O:";
126  
127 for my $value (@_) {
128  
129 no warnings;# 'uninitialized';
130 if (length($value)) {
131 if (ref($value) eq "ARRAY") {
132 if (@$value == 0) {
133 $value_id .= $empty_list;
134 }
135 else {
136 for my $value2 (@$value) {
137 if (not defined $value2 ) {
138 $value_id .= $null_value . $unit_sep;
139 }
140 elsif ($value2 eq "") {
141 $value_id .= $empty_string . $unit_sep;
142 }
143 else {
144 if (ref($value2) or index($value2, $unit_sep) >= 0 or index($value2, $record_sep) >= 0) {
145 return values_to_value_id_frozen(@_);
146 }
147 $value_id .= $value2 . $unit_sep;
148 }
149 }
150 }
151 $value_id .= $record_sep;
152 }
153 else {
154 if (ref($value) or index($value,$unit_sep) >= 0 or index($value,$record_sep) >= 0) {
155 return values_to_value_id_frozen(@_);
156 }
157 $value_id .= $value . $record_sep;
158 }
159 } elsif (not defined $value ) {
160 $value_id .= $null_value . $record_sep;
161 }
162 else {# ($value eq "") {
163 $value_id .= $empty_string . $record_sep;
164 }
165 }
166 return $value_id;
167 }
168  
169 sub value_id_to_values {
170 my $value_id = shift;
171  
172 unless (defined $value_id) {
173 Carp::confess('No value_id passed in to value_id_to_values()!?');
174 }
175  
176 my $method_identifier = substr($value_id,0,2);
177 $value_id = substr($value_id, 2, length($value_id)-2);
178 if ($method_identifier eq "F:") {
179 return value_id_to_values_frozen($value_id);
180 }
181  
182 my @values = ($value_id =~ /(.*?)$record_sep/gs);
183 for (@values) {
184 if (substr($_,-1) eq $unit_sep) {
185 #$_ = [split($unit_sep,$_)]
186 my @values2 = /(.*?)$unit_sep/gs;
187 $_ = \@values2;
188 for (@values2) {
189 if ($_ eq $null_value) {
190 $_ = undef;
191 }
192 elsif ($_ eq $empty_string) {
193 $_ = "";
194 }
195 }
196 }
197 elsif ($_ eq $null_value) {
198 $_ = undef;
199 }
200 elsif ($_ eq $empty_string) {
201 $_ = "";
202 }
203 elsif ($_ eq $empty_list) {
204 $_ = [];
205 }
206 }
207 return @values;
208 }
209  
210 sub is_meta_param {
211 my $param_name = shift;
212 return substr($param_name, 0, 1) eq '-';
213 }
214  
215 package UR::BoolExpr::Util::clonedThing;
216  
217 sub bless {
218 my($class, $thing) = @_;
219 # return $thing if ($thing->isa(__PACKAGE__));
220  
221 $thing->{__original_class} = $thing->class;
222 bless $thing, $class;
223 }
224  
225 sub id {
226 return shift->{id};
227 }
228  
229 sub class {
230 return shift->{__original_class};
231 }
232  
233 1;
234  
235 =pod
236  
237 =head1 NAME
238  
239 UR::BoolExpr::Util - non-OO module to collect utility functions used by the BoolExpr modules
240  
241 =cut
242