chiark / gitweb /
Remove automatic upgrade of numerical types
authorJeff Ober <jober@ziprecruiter.com>
Sat, 18 Jan 2020 20:16:35 +0000 (15:16 -0500)
committerJeff Ober <jober@ziprecruiter.com>
Sat, 18 Jan 2020 20:16:35 +0000 (15:16 -0500)
Changes
README.pod
cpanfile
lib/TOML/Tiny.pm
lib/TOML/Tiny/Parser.pm
lib/TOML/Tiny/Util.pm
lib/TOML/Tiny/Writer.pm

diff --git a/Changes b/Changes
index 54e6531b782dd2d7731bce8c5f7b20e4698dd496..fa8815fc172f862ff782ca10b0a6c2bba1f15a61 100644 (file)
--- 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
index 0f68f58a0d64bbc3237912ba9131d7f715ef8475..66ee8b1e0d334c9b6e7356f22c498fc097cb538f 100644 (file)
@@ -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<TOML v5> specified homogenous arrays. This has since been removed and will no
index e0bd39659bc7e745822c40457a504867930fcabd..3870ddecd12e9410fd9c38227a83fb8a62b33c47 100644 (file)
--- 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';
index 91595ec60329c7193e832cf4f5097885131d1d11..9d9d521f4570475a3ec312c2019e0acd179a260e 100644 (file)
@@ -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<TOML v5> specified homogenous arrays. This has since been removed and will no
index a9673689bef5db9ea9e7d4116e4a1c19d6106cd8..6b2b9a56fd59e1d8aae4784682c8d04c3da3360f 100644 (file)
@@ -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';
index 344b1b6549346140a29fa9e2fe03bc6e18afc765..b92c729d313d91997851c86aa7d353c3742857b8 100644 (file)
@@ -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' };
         }
       }
index 12178b7aac722ef373a77d0d75852c6297cb1f1d..a0a0b380a081b161ee74e1d6e563de833453ab58 100644 (file)
@@ -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);