From c59807db1d5f5e5c1692ddf669a44f2afb2dd035 Mon Sep 17 00:00:00 2001 From: Jeff Ober Date: Sat, 18 Jan 2020 15:16:35 -0500 Subject: [PATCH] Remove automatic upgrade of numerical types --- Changes | 2 ++ README.pod | 28 +++++++++++++++++++++++++++- cpanfile | 2 +- lib/TOML/Tiny.pm | 26 ++++++++++++++++++++++++++ lib/TOML/Tiny/Parser.pm | 33 +++++---------------------------- lib/TOML/Tiny/Util.pm | 20 +++++++++++--------- lib/TOML/Tiny/Writer.pm | 1 - 7 files changed, 72 insertions(+), 40 deletions(-) diff --git a/Changes b/Changes index 54e6531..fa8815f 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,6 @@ {{$NEXT}} +-Remove automatic inflation of numerical types using Math::Big(Int|Float); + this may now be done with inflate_integer and inflate_float 0.02 2020-01-17 -Add MANIFEST.SKIP to prevent dzil from including author scripts diff --git a/README.pod b/README.pod index 0f68f58..66ee8b1 100644 --- a/README.pod +++ b/README.pod @@ -8,7 +8,7 @@ TOML::Tiny - a minimal, pure perl TOML parser and serializer =head1 VERSION -version 0.02 +version 0.03 =head1 SYNOPSIS @@ -131,6 +131,32 @@ If you wish to override this, you can provide your own routine to generate value }, ); +=item inflate_integer + +TOML integers are 64 bit and may not match the size of the compiled perl's +internal integer type. By default, integers are left as-is as perl strings +which may be upgraded as needed by the caller. + + my $parser = TOML::Tiny->new( + inflate_integer => sub{ + use bignum; + return 0 + shift; + } + ); + +=item inflate_float + +TOML floats are 64 bit and may not match the size of the compiled perl's +internal float type. By default, integers are left as-is as perl strings which +may be upgraded as needed by the caller. + + my $parser = TOML::Tiny->new( + inflate_float => sub{ + use bignum; + return 0 + shift; + } + ); + =item strict_arrays C specified homogenous arrays. This has since been removed and will no diff --git a/cpanfile b/cpanfile index e0bd396..3870dde 100644 --- a/cpanfile +++ b/cpanfile @@ -4,7 +4,6 @@ requires 'Carp' => '0'; requires 'Data::Dumper' => '0'; requires 'DateTime::Format::RFC3339' => '0'; requires 'Exporter' => '0'; -requires 'Math::BigInt' => '>= 1.999718'; requires 'Scalar::Util' => '>= 1.14'; recommends 'Types::Serialiser' => 0; @@ -12,6 +11,7 @@ recommends 'Types::Serialiser' => 0; on test => sub{ requires 'Data::Dumper' => '0'; requires 'DateTime::Format::RFC3339' => '0'; + requires 'Math::BigInt' => '>= 1.999718'; requires 'TOML::Parser' => '0'; requires 'Test2::V0' => '0'; requires 'Test::Pod' => '0'; diff --git a/lib/TOML/Tiny.pm b/lib/TOML/Tiny.pm index 91595ec..9d9d521 100644 --- a/lib/TOML/Tiny.pm +++ b/lib/TOML/Tiny.pm @@ -187,6 +187,32 @@ If you wish to override this, you can provide your own routine to generate value }, ); +=item inflate_integer + +TOML integers are 64 bit and may not match the size of the compiled perl's +internal integer type. By default, integers are left as-is as perl strings +which may be upgraded as needed by the caller. + + my $parser = TOML::Tiny->new( + inflate_integer => sub{ + use bignum; + return 0 + shift; + } + ); + +=item inflate_float + +TOML floats are 64 bit and may not match the size of the compiled perl's +internal float type. By default, integers are left as-is as perl strings which +may be upgraded as needed by the caller. + + my $parser = TOML::Tiny->new( + inflate_float => sub{ + use bignum; + return 0 + shift; + } + ); + =item strict_arrays C specified homogenous arrays. This has since been removed and will no diff --git a/lib/TOML/Tiny/Parser.pm b/lib/TOML/Tiny/Parser.pm index a967368..6b2b9a5 100644 --- a/lib/TOML/Tiny/Parser.pm +++ b/lib/TOML/Tiny/Parser.pm @@ -24,6 +24,9 @@ eval{ sub new { my ($class, %param) = @_; bless{ + inflate_integer => $param{inflate_integer} || sub{ shift }, + inflate_float => $param{inflate_float} || sub{ shift }, + inflate_number => $param{inflate_number} || sub{ shift }, inflate_datetime => $param{inflate_datetime} || sub{ shift }, inflate_boolean => $param{inflate_boolean} || sub{ shift eq 'true' ? $TRUE : $FALSE }, strict_arrays => $param{strict_arrays}, @@ -260,35 +263,9 @@ sub parse_value { my $token = shift // $self->next_token; for ($token->{type}) { - when ('float') { - use bignum; - return $token->{value} + 0; - } - - when ('integer') { - for (my $n = $token->{value}) { - use bigint; - - when ($Oct) { - $n =~ s/^0o/0/; # convert to perl's octal format - return oct $n; - } - - when ($Bin) { - return oct $n; - } - - when ($Hex) { - return hex $n; - } - - default{ - return $n + 0; - } - } - } - return $token->{value} when 'string'; + return $self->{inflate_float}->($token->{value}) when 'float'; + return $self->{inflate_integer}->($token->{value}) when 'integer'; return $self->{inflate_boolean}->($token->{value}) when 'bool'; return $self->{inflate_datetime}->($token->{value}) when 'datetime'; return $self->parse_inline_table when 'inline_table'; diff --git a/lib/TOML/Tiny/Util.pm b/lib/TOML/Tiny/Util.pm index 344b1b6..b92c729 100644 --- a/lib/TOML/Tiny/Util.pm +++ b/lib/TOML/Tiny/Util.pm @@ -22,18 +22,20 @@ sub is_strict_array { my $type; for (ref $value) { - $type = 'array' when /ARRAY/; - $type = 'table' when /HASH/; - $type = 'float' when /Math::BigFloat/; - $type = 'integer' when /Math::BigInt/; - $type = 'bool' when /JSON::PP::Boolean/; + $type = 'array' when 'ARRAY'; + $type = 'table' when 'HASH'; + + # Do a little heuristic guess-work + $type = 'float' when /Float/; + $type = 'integer' when /Int/; + $type = 'bool' when /Boolean/; when ('') { for ($value) { - $type = 'bool' when /$Boolean/; - $type = 'float' when /$Float/; - $type = 'integer' when /$Integer/; - $type = 'datetime' when /$DateTime/; + $type = 'bool' when /$Boolean/; + $type = 'float' when /$Float/; + $type = 'integer' when /$Integer/; + $type = 'datetime' when /$DateTime/; default{ $type = 'string' }; } } diff --git a/lib/TOML/Tiny/Writer.pm b/lib/TOML/Tiny/Writer.pm index 12178b7..a0a0b38 100644 --- a/lib/TOML/Tiny/Writer.pm +++ b/lib/TOML/Tiny/Writer.pm @@ -7,7 +7,6 @@ use v5.18; use Data::Dumper; use DateTime::Format::RFC3339; -use Math::BigFloat; use Scalar::Util qw(looks_like_number); use TOML::Tiny::Grammar; use TOML::Tiny::Util qw(is_strict_array); -- 2.30.2