Util: Add to_bool() and use it for JSON, Pg & query encoding

To improve interop with legacy modules.
This commit is contained in:
Yorhel 2025-02-25 09:10:03 +01:00
parent 06e2f950fe
commit c7a3415485
10 changed files with 141 additions and 37 deletions

View file

@ -58,7 +58,7 @@ my @tests = (
);
my @errors = (
\1, qr/unable to format reference/,
\2, qr/unable to format reference/,
*STDOUT, qr/unable to format unknown value/,
'NaN'+0, qr/unable to format floating point NaN or Inf as JSON/,
'Inf'+0, qr/unable to format floating point NaN or Inf as JSON/,

View file

@ -92,12 +92,12 @@ subtest '$st prepare & exec', sub {
# Interleaved
{
my $a = $conn->q('SELECT 1 as a');
my $b = $conn->q('SELECT 2 as b');
is_deeply $a->columns, [ { oid => 23, name => 'a' } ];
is_deeply $b->columns, [ { oid => 23, name => 'b' } ];
is $a->val, 1;
is $b->val, 2;
my $x = $conn->q('SELECT 1 as a');
my $y = $conn->q('SELECT 2 as b');
is_deeply $x->columns, [ { oid => 23, name => 'a' } ];
is_deeply $y->columns, [ { oid => 23, name => 'b' } ];
is $x->val, 1;
is $y->val, 2;
}
};
@ -347,9 +347,9 @@ subtest 'txn', sub {
}
{
my $a = [1,2];
my $st = $conn->q('SELECT $1::int[]', $a)->text(0);
$a->[1] = 3;
my $x = [1,2];
my $st = $conn->q('SELECT $1::int[]', $x)->text(0);
$x->[1] = 3;
is_deeply $st->val, [1,3], 'not deep copy';
}

View file

@ -89,11 +89,11 @@ f float8 => $_ for ('', 'a', '123g', []);
v json => {}, undef, '{}';
# XXX: Huh, what's causing this "pretty" formatting?
v json => [1, undef, true, "hello"], undef, qq#[\n 1,\n null,\n true,\n "hello"\n]#;
f json => \1;
f json => \2;
v jsonb => {}, undef, '{}';
v jsonb => [1, undef, true, "hello"], undef, '[1, null, true, "hello"]';
f jsonb => \1;
f jsonb => \2;
v jsonpath => $_ for ('$."key"', '$."a[*]"?(@ > 2)');
f jsonpath => $_ for ('', 'hello world');

View file

@ -22,7 +22,7 @@ is_deeply
query_decode('a=&a=&b=&c==x&d=x='),
{ a => ['', ''], b => '', c => '=x', d => 'x=' };
is query_encode { a => ['', ''], b => '', c => '=x', d => 'x=' }, 'a=&a=&b=&c=%3dx&d=x%3d';
is query_encode { a => ['', '', \1], b => '', c => '=x', d => 'x=' }, 'a=&a=&a&b=&c=%3dx&d=x%3d';
sub FUTILTEST::TO_QUERY { '&'.($_[0][0] + 1) }

41
t/to_bool.t Normal file
View file

@ -0,0 +1,41 @@
use v5.36;
use Test::More;
use FU::Util 'to_bool';
use experimental 'builtin';
use builtin 'true', 'false';
is to_bool undef, undef;
is to_bool '', undef;
is to_bool 1, undef;
is to_bool [], undef;
is to_bool {}, undef;
is to_bool bless(\(my $x = 1), 'FU::Bullshit'), undef;
is to_bool builtin::true, true;
is to_bool builtin::false, false;
is to_bool \1, true;
is to_bool \0, false;
is to_bool \'1', true;
is to_bool \'0', false;
is to_bool \2, undef;
SKIP: {
eval { require Types::Serialiser; 1 } || skip 'Types::Serialiser not installed';
is to_bool Types::Serialiser::true(), true;
is to_bool Types::Serialiser::false(), false;
}
SKIP: {
eval { require JSON::Tiny; 1 } || skip 'JSON::Tiny not installed';
is to_bool JSON::Tiny::true(), true;
is to_bool JSON::Tiny::false(), false;
}
SKIP: {
eval { require Cpanel::JSON::XS; 1 } || skip 'Cpanel::JSON::XS not installed';
is to_bool Cpanel::JSON::XS::true(), true;
is to_bool Cpanel::JSON::XS::false(), false;
}
done_testing;