| 1 | package DBIO::PostgreSQL::Age::Storage; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL storage with Apache AGE graph support | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 8 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 8 | my $JSON = JSON::MaybeXS->new(utf8 => 1, canonical => 1); | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | # Same decoder for a value that already is a character string (UTF8 flag on, | ||||||||||||||||||||||||||||||||
| 14 | # what DBD::Pg hands over with client_encoding UTF8). | ||||||||||||||||||||||||||||||||
| 15 | 8 | my $JSON_CHARS = JSON::MaybeXS->new(utf8 => 0, canonical => 1); | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub connect_call_load_age { | ||||||||||||||||||||||||||||||
|
Subroutine
connect_call_load_age: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 19 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 20 | 0 | $self->_do_query(q{LOAD 'age'}); | |||||||||||||||||||||||||||||||
| 21 | # ag_catalog anywhere in the path (also quoted) leaves the path as it is -- | ||||||||||||||||||||||||||||||||
| 22 | # the driver's order is respected, nothing is doubled. Else: with a tenant | ||||||||||||||||||||||||||||||||
| 23 | # namespace (connect key `schema`, kept as connection_schema) ag_catalog goes | ||||||||||||||||||||||||||||||||
| 24 | # BEHIND the namespace entry (the first path element), so unqualified DDL | ||||||||||||||||||||||||||||||||
| 25 | # lands in the namespace; without one it goes in front. An empty path gets | ||||||||||||||||||||||||||||||||
| 26 | # just ag_catalog. | ||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $ns = defined $self->connection_schema ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | 0 | my $with_ns = q{regexp_replace(current_setting('search_path'), } | |||||||||||||||||||||||||||||||
| 29 | . q{'^\s*("([^"]|"")*"|[^,]*)', '\1, ag_catalog')}; | ||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $self->_do_query( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | q{SELECT set_config('search_path', CASE } | ||||||||||||||||||||||||||||||||
| 32 | . q{WHEN current_setting('search_path') ~ '(^|,)\s*(ag_catalog|"ag_catalog")\s*(,|$)' } | ||||||||||||||||||||||||||||||||
| 33 | . q{THEN current_setting('search_path') } | ||||||||||||||||||||||||||||||||
| 34 | . q{WHEN btrim(current_setting('search_path')) = '' THEN 'ag_catalog' } | ||||||||||||||||||||||||||||||||
| 35 | . ($ns | ||||||||||||||||||||||||||||||||
| 36 | ? qq{ELSE $with_ns END, false)} | ||||||||||||||||||||||||||||||||
| 37 | : q{ELSE 'ag_catalog, ' || current_setting('search_path') END, false)}) | ||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub create_graph { | ||||||||||||||||||||||||||||||
|
Subroutine
create_graph: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $self->dbh_do(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | 1 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 46 | 1 | $dbh->do('SELECT * FROM ag_catalog.create_graph(?)', undef, $name); | |||||||||||||||||||||||||||||||
| 47 | 1 | }); | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub drop_graph { | ||||||||||||||||||||||||||||||
|
Subroutine
drop_graph: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 52 | 1 | my ($self, $name, $cascade) = @_; | |||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | $self->dbh_do(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||
| 54 | 1 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | $dbh->do( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 'SELECT * FROM ag_catalog.drop_graph(?, ?)', | ||||||||||||||||||||||||||||||||
| 57 | undef, $name, $cascade ? 1 : 0, | ||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||
| 59 | 1 | }); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | sub cypher { | ||||||||||||||||||||||||||||||
|
Subroutine
cypher: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 64 | 2 | my ($self, $graph, $query, $columns, $params, $opts) = @_; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 2 | my ($sql, $bind) = $self->_cypher_sql_bind($graph, $query, $columns, $params); | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my $rows = $self->dbh_do(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 69 | 2 | my (undef, $dbh) = @_; | |||||||||||||||||||||||||||||||
| 70 | 2 | return $dbh->selectall_arrayref($sql, { Slice => {} }, @$bind); | |||||||||||||||||||||||||||||||
| 71 | 2 | }); | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if ($opts && $opts->{auto_decode}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts and $opts->{"auto_decode"}
MC/DC0%0/2
$opts and $opts->{"auto_decode"}
$opts
$opts->{"auto_decode"}
Truth table33%1/3
A and B
A= $optsB= $opts->{"auto_decode"}
| |||||||||||||||||||||||||||||||||
| 74 | 0 | for my $row (@$rows) { | |||||||||||||||||||||||||||||||
| 75 | 0 | for my $col (keys %$row) { | |||||||||||||||||||||||||||||||
| 76 | 0 | $row->{$col} = $self->decode_agtype($row->{$col}); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 2 | return $rows; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | # Async dispatchers for the graph methods, mirroring core's CRUD *_async | ||||||||||||||||||||||||||||||||
| 85 | # (DBIO::Storage::select_async etc.): each is a thin shift->_run_async($op) | ||||||||||||||||||||||||||||||||
| 86 | # whose three-way contract is (1) route ${op}_async to the embedded async | ||||||||||||||||||||||||||||||||
| 87 | # backend when one is live (future_io/ev -> the composed Age async layer's real | ||||||||||||||||||||||||||||||||
| 88 | # cypher_async), (2) in 'immediate' mode run the sync method in-process and wrap | ||||||||||||||||||||||||||||||||
| 89 | # its result in an immediately-resolved Future (no event loop), (3) on a plain | ||||||||||||||||||||||||||||||||
| 90 | # sync connection croak "not an async connection". This is why cypher() (and the | ||||||||||||||||||||||||||||||||
| 91 | # graph lifecycle methods) can also be reached as *_async on the live storage, | ||||||||||||||||||||||||||||||||
| 92 | # degrading cleanly under { async => 'immediate' } exactly like select_async. | ||||||||||||||||||||||||||||||||
| 93 | # Graph async methods have a sync equivalent (cypher/create_graph/drop_graph), | ||||||||||||||||||||||||||||||||
| 94 | # so unlike backend-only ops (listen/copy) they belong in the degrade-capable | ||||||||||||||||||||||||||||||||
| 95 | # tier alongside the core CRUD methods. | ||||||||||||||||||||||||||||||||
| 96 | 2 | ▶ | sub cypher_async { shift->_run_async('cypher', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
cypher_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub create_graph_async { shift->_run_async('create_graph', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
create_graph_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub drop_graph_async { shift->_run_async('drop_graph', @_) } | ||||||||||||||||||||||||||||||
|
Subroutine
drop_graph_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | # Build the AGE cypher() SQL and its bind values. Pure (no DB) so the SQL | ||||||||||||||||||||||||||||||||
| 102 | # generation can be unit-tested offline; cypher() wraps this with execution. | ||||||||||||||||||||||||||||||||
| 103 | 21 | ▶ | sub _cypher_sql_bind { | ||||||||||||||||||||||||||||||
|
Subroutine
_cypher_sql_bind: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||
| 104 | 21 | my ($self, $graph, $query, $columns, $params) = @_; | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | # Apache AGE requires the graph name to be a string literal in the | ||||||||||||||||||||||||||||||||
| 107 | # cypher() call -- it cannot be passed as a bind parameter. Validate the | ||||||||||||||||||||||||||||||||
| 108 | # name as a plain identifier so we can safely inline it. | ||||||||||||||||||||||||||||||||
| 109 | 21 | ▶ | $graph =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | or $self->throw_exception("Invalid AGE graph name '$graph'"); | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | # The result columns go into the AS (...) list verbatim, so they get the same | ||||||||||||||||||||||||||||||||
| 113 | # plain-identifier check as the graph name. | ||||||||||||||||||||||||||||||||
| 114 | 18 | for my $col (@$columns) { | |||||||||||||||||||||||||||||||
| 115 | 22 | ▶ | defined $col && $col =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $col and $col =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
MC/DC100%2/2
defined $col and $col =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
defined $col
$col =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Truth table100%3/3
A and B
A= defined $colB= $col =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
| |||||||||||||||||||||||||||||||||
| 116 | or $self->throw_exception( | ||||||||||||||||||||||||||||||||
| 117 | 'Invalid AGE column name ' . (defined $col ? "'$col'" : '(undef)') | ||||||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 14 | my $col_spec = join ', ', map { "$_ agtype" } @$columns; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # The Cypher text is inlined as a dollar-quoted string, and a "$tag$" inside | ||||||||||||||||||||||||||||||||
| 123 | # it would end the quoting early. Pick a tag that does not occur in the text | ||||||||||||||||||||||||||||||||
| 124 | # (counting up on a collision) so any Cypher text is enclosed verbatim. | ||||||||||||||||||||||||||||||||
| 125 | 14 | my $tag = 'dbio_age'; | |||||||||||||||||||||||||||||||
| 126 | 14 | my $n = 0; | |||||||||||||||||||||||||||||||
| 127 | 14 | ▶ | $tag = 'dbio_age' . ++$n while index($query, "\$$tag\$") >= 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 14 | my $dq = "\$$tag\$"; | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 14 | my @bind; | |||||||||||||||||||||||||||||||
| 131 | 14 | ▶ | if ($params && %$params) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params and %$params
MC/DC100%2/2
$params and %$params
$params
%$params
Truth table100%3/3
A and B
A= $paramsB= %$params
| |||||||||||||||||||||||||||||||||
| 132 | # A character string: DBD::Pg encodes it for the wire itself (bytes would be encoded twice). | ||||||||||||||||||||||||||||||||
| 133 | 3 | push @bind, $JSON_CHARS->encode($params); | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | 14 | ▶ | my $param_slot = @bind ? ', ?' : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 14 | my $sql = "SELECT * FROM cypher('$graph', $dq\n$query\n$dq${param_slot}) AS ($col_spec)"; | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 14 | return ($sql, \@bind); | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | # Text of a "<number>::numeric" value without the cast: the bare number when a | ||||||||||||||||||||||||||||||||
| 142 | # Perl number holds it exactly, else a quoted JSON string with the exact digits. | ||||||||||||||||||||||||||||||||
| 143 | 24 | ▶ | sub _numeric_literal { | ||||||||||||||||||||||||||||||
|
Subroutine
_numeric_literal: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 144 | 24 | my ($num) = @_; | |||||||||||||||||||||||||||||||
| 145 | 24 | ▶ | return $num if $num =~ /\A-?\d+\z/ && ($num + 0) eq $num; # fits an IV | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$num =~ /\A-?\d+\z/ and $num + 0 eq $num
MC/DC50%1/2
$num =~ /\A-?\d+\z/ and $num + 0 eq $num
$num =~ /\A-?\d+\z/
$num + 0 eq $num
Truth table66%2/3
A and B
A= $num =~ /\A-?\d+\z/B= $num + 0 eq $num
| |||||||||||||||||||||||||||||||||
| 146 | 18 | (my $digits = $num) =~ s/[eE].*//; | |||||||||||||||||||||||||||||||
| 147 | 18 | $digits =~ s/\D//g; | |||||||||||||||||||||||||||||||
| 148 | 18 | $digits =~ s/^0+//; | |||||||||||||||||||||||||||||||
| 149 | 18 | $digits =~ s/0+\z//; | |||||||||||||||||||||||||||||||
| 150 | 18 | ▶ | return length($digits) > 15 ? qq{"$num"} : $num; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | # Decode a single agtype text value into native Perl data. | ||||||||||||||||||||||||||||||||
| 154 | # | ||||||||||||||||||||||||||||||||
| 155 | # AGE returns agtype values as text. The shapes we handle: | ||||||||||||||||||||||||||||||||
| 156 | # - JSON-like maps/lists -> decode_json into hashref/arrayref | ||||||||||||||||||||||||||||||||
| 157 | # - Quoted string scalar -> strip the surrounding quotes | ||||||||||||||||||||||||||||||||
| 158 | # - Integer / float -> return as Perl number | ||||||||||||||||||||||||||||||||
| 159 | # - true / false / null -> JSON booleans / undef | ||||||||||||||||||||||||||||||||
| 160 | # - Cast annotations -> strip every ::vertex / ::edge / ::path (AGE | ||||||||||||||||||||||||||||||||
| 161 | # annotates each element, also inside paths and | ||||||||||||||||||||||||||||||||
| 162 | # lists), but never inside a string literal. | ||||||||||||||||||||||||||||||||
| 163 | # Anything we don't recognise is returned as-is so callers can still | ||||||||||||||||||||||||||||||||
| 164 | # post-process it themselves. | ||||||||||||||||||||||||||||||||
| 165 | 4751 | ▶ | sub decode_agtype { | ||||||||||||||||||||||||||||||
|
Subroutine
decode_agtype: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 166 | 4751 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 4751 | ▶ | return undef unless defined $value; # SQL NULL cell | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | # AGE (verified against 1.8.0) appends "::vertex" / "::edge" / "::path" to | ||||||||||||||||||||||||||||||||
| 171 | # every such element of the rendered text, e.g. | ||||||||||||||||||||||||||||||||
| 172 | # "[{...}::vertex, {...}::edge, {...}::vertex]::path". Strip them all, but | ||||||||||||||||||||||||||||||||
| 173 | # skip string literals: a property value may legitimately contain | ||||||||||||||||||||||||||||||||
| 174 | # "::vertex". The first alternative consumes (and keeps) a whole literal. | ||||||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||||||
| 176 | # "<number>::numeric" is stripped the same way. A numeric a double cannot | ||||||||||||||||||||||||||||||||
| 177 | # hold exactly (more than 15 significant digits, unless it is an integer | ||||||||||||||||||||||||||||||||
| 178 | # that fits an IV) is rewritten to a JSON string literal, so the exact digits | ||||||||||||||||||||||||||||||||
| 179 | # survive both as a scalar and inside a list/map, whatever JSON backend. | ||||||||||||||||||||||||||||||||
| 180 | 4749 | my $raw = $value; | |||||||||||||||||||||||||||||||
| 181 | 4749 | $raw =~ s{("(?:[^"\\]|\\.)*")|((?<![\d.])-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)::numeric\b|::(?:vertex|edge|path)\b}{ | |||||||||||||||||||||||||||||||
| 182 | 301 | ▶ | defined $1 ? $1 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 183 | : defined $2 ? _numeric_literal($2) | ||||||||||||||||||||||||||||||||
| 184 | : '' | ||||||||||||||||||||||||||||||||
| 185 | }ge; | ||||||||||||||||||||||||||||||||
| 186 | 4749 | $raw =~ s/\s+\z//; | |||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 4749 | ▶ | return $raw unless defined $raw && length $raw; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $raw and length $raw
MC/DC0%0/2
defined $raw and length $raw
defined $raw
length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
| |||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 4749 | my $first = substr($raw, 0, 1); | |||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | # A character string (UTF8 flag) is decoded as characters, bytes as bytes. | ||||||||||||||||||||||||||||||||
| 193 | 4749 | ▶ | my $json = utf8::is_utf8($raw) ? $JSON_CHARS : $JSON; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | # Map or list: hand off to JSON. | ||||||||||||||||||||||||||||||||
| 196 | 4749 | ▶ | if ($first eq '{' || $first eq '[') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first eq "{" or $first eq "["
MC/DC100%2/2
$first eq "{" or $first eq "["
$first eq "{"
$first eq "["
Truth table100%3/3
A or B
A= $first eq "{"B= $first eq "["
| |||||||||||||||||||||||||||||||||
| 197 | 45 | my $decoded = eval { $json->decode($raw) }; | |||||||||||||||||||||||||||||||
| 198 | 45 | ▶ | return $decoded unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | # Neither the value nor the parser's message (it quotes the value) is named. | ||||||||||||||||||||||||||||||||
| 200 | 2 | my ($suffix) = $value =~ /(::(?:vertex|edge|path))\s*\z/; | |||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | $self->throw_exception( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 202 | 'decode_agtype: cannot decode agtype ' . ($first eq '{' ? 'map' : 'list') | ||||||||||||||||||||||||||||||||
| 203 | . (defined $suffix ? " ($suffix)" : '') | ||||||||||||||||||||||||||||||||
| 204 | ); | ||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | # Quoted string scalar: "foo" -> foo. Decode any JSON-escaped chars. | ||||||||||||||||||||||||||||||||
| 208 | 4704 | ▶ | if ($first eq '"') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 17 | my $decoded = eval { $json->decode($raw) }; | |||||||||||||||||||||||||||||||
| 210 | 17 | ▶ | return $decoded unless $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | # Fallback: naive strip of outer quotes. | ||||||||||||||||||||||||||||||||
| 212 | 1 | my $inner = substr($raw, 1, length($raw) - 2); | |||||||||||||||||||||||||||||||
| 213 | 1 | return $inner; | |||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | # Unquoted scalar. true/false/null/number. | ||||||||||||||||||||||||||||||||
| 217 | 4687 | ▶ | if ($raw eq 'true') { return $JSON->true; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 218 | 4685 | ▶ | if ($raw eq 'false') { return $JSON->false; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 219 | 4683 | ▶ | if ($raw eq 'null') { return undef; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | # Looks numeric? | ||||||||||||||||||||||||||||||||
| 222 | 4681 | ▶ | if ($raw =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | 4679 | return $raw + 0; # +0 keeps int as int, float as float | |||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # Anything else: leave alone. | ||||||||||||||||||||||||||||||||
| 227 | 2 | return $raw; | |||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | 8 | 1; | |||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||||||