blib/lib/Dallycot/JSON/LD.pm untested

filter: Statementstmt 0.0% 0 / 134 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 281 CC 24 SCAR 59.5
CC
24
Cov
0%
_expand
552.0
expand
6.0
to_rdf
2.0
CRAP 385.6
1 package Dallycot::JSON::LD;
2 our $AUTHORITY = 'cpan:JSMITH';
3  
4 use Moose;
5  
6 use utf8;
7  
8 use Carp qw(croak);
9 use Promises qw(deferred);
10  
11 use experimental qw(switch);
12  
13 sub to_rdf {
14 my($self, $input, %options) = @_;
15  
16 return $self -> expand($input) -> then(
17 sub {
18 $self -> generate_node_map(@_);
19 }
20 ) -> then(
21 sub {
22  
23 }
24 );
25 }
26  
27 sub expand {
28 my($self, $element, $active_context, $active_property) = @_;
29  
30  
31 my $xp = $self -> _expand($element, $active_context, $active_property);
32 if(blessed $xp) {
33 return $xp;
34 }
35 else {
36 my $d = deferred;
37 $d -> resolve($xp);
38 return $d -> promise;
39 }
40 }
41  
42 sub _expand {
43 my($self, $element, $active_context, $active_property) = @_;
44  
45 if(!defined($element)) {
46 return;
47 }
48 else {
49 given(reftype $element) {
50 when(undef) {
51 if(!defined($active_property) || $active_property eq '@graph') {
52 return;
53 }
54 else {
55 return $self -> expand_value($element, $active_context, $active_property);
56 }
57 }
58 when('ARRAY') {
59 my @result;
60 foreach my $el (@$element) {
61 my @xp = $self->_expand($el, $active_context, $active_property);
62 if(@xp > 1) {
63 if($active_property eq '@list' || container_mapping($active_property) eq '@list') {
64 croak "List of lists detected in JSON-LD";
65 }
66 }
67 push @result, grep { defined } @xp;
68 }
69 if(any { blessed $_ } @result) {
70 return collect( grep { blessed $_ } @result )->then(sub {
71 my(@finally) = map { @$_ } @_;
72 return (@finally, grep { !blessed $_ } @result);
73 });
74 }
75 else {
76 return @result;
77 }
78 }
79 when('SCALAR') {
80 if(!defined($active_property) || $active_property eq '@graph') {
81 return;
82 }
83 else {
84 return $self -> expand_value($$element, $active_context, $active_property);
85 }
86 }
87 }
88 when('HASH') {
89 my $ctx_promise;
90 if(exists($element->{'@context'}) && defined($element->{'@context'})) {
91 $ctx_promise = $self->process_context($element->{'@context'}, $active_context);
92 }
93 else {
94 $ctx_promise = deferred;
95 $ctx_promise -> resolve($active_context);
96 $ctx_promise = $ctx_promise -> promise;
97 }
98 return $ctx_promise -> then(sub {
99 my($new_context) = @_;
100 my $result = {};
101  
102 foreach my $key (keys %$element) {
103 next if $key eq '@context';
104  
105 my $value = $element->{$key};
106 my $xv = $value;
107  
108 my $xp = expand_iri($key, $new_context, vocab => 1);
109 next if !defined($xp) || !$xp =~ /^@/ && !$xp =~ /:/;
110  
111 if($xp =~ /^@/ && $active_property eq '@reverse') {
112 croak "invalid reverse property map ($key)";
113 }
114  
115 if(defined $result->{$xp}) {
116 croak "colliding keywords ($xp) for $key";
117 }
118  
119 given($xp) {
120 when('@id') {
121 if(!defined($value) || ref $value) {
122 croak "invalid \@id value ($value) for $key";
123 }
124 $xv = $self->expand_iri($value, $new_context, document_relative => 1);
125 }
126 when('@type') {
127 if(!defined($value) || ref($value) && reftype($value) ne 'ARRAY' || reftype($value) eq 'ARRAY' && !all { defined && !ref } @$value) {
128 croak "invalid \@type value";
129 }
130 if(ref $value) {
131 $xv = [ map { $self->expand_iri($_, $new_context, document_relative => 1, vocab => 1) } @$value ];
132 }
133 else {
134 $xv = $self -> expand_iri($value, $new_context, document_relative => 1, vocab => 1);
135 }
136 }
137 when('@graph') {
138 $xv = $self->_expand($value, $new_context, '@graph');
139 }
140 when('@value') {
141 if(defined($value) && ref($value)) {
142 croak "invalid value object value for $key";
143 }
144 if(!defined($value)) {
145 $result->{'@value'} = undef;
146 next;
147 }
148 }
149 when('@language') {
150 if(!defined($value) || ref($value)) {
151 croak "invalid language-tagged string for $key";
152 }
153 $xv = lc($value);
154 }
155 when('@index') {
156 if(!defined($value) || ref($value)) {
157 croak "invalid \@index value for $key";
158 }
159 }
160 when('@list') {
161 if(!defined($active_property) || $active_property eq '@graph') {
162 next;
163 }
164 $xv = $self -> _expand($value, $new_context, $active_property);
165 if(blessed($xv) && $xv->can('then')) {
166 $xv = $xv -> then(sub {
167 my(@val) = @_;
168 if(@val > 1) {
169 croak "list of lists detected for $key";
170 }
171 return @val;
172 });
173 }
174 }
175 when('@set') {
176 $xv = $self -> _expand($value, $new_context, $active_property);
177 }
178 when('@reverse') {
179 if('HASH' ne reftype($value)) {
180 croak "invalid \@reverse value for $key";
181 }
182  
183 }
184 }
185 }
186 });
187 }
188 default {
189 croak "Unable to expand ($element)";
190 }
191 }
192 }
193  
194 sub generate_node_map {
195 my($self, $expanded_input) = @_;
196  
197 my $node_map = { '@default' => {} };
198  
199 $self -> _generate_node_map($expanded_input, $node_map);
200  
201 return $node_map;
202 }
203  
204 sub _generate_node_map {
205 my($self, $input, $node_map) = @_;
206  
207 return;
208 }
209  
210 1;