summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2019-08-23 11:43:52 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2019-08-23 11:45:35 (GMT)
commitd69ba51bb8498146d07a08031de3685e6cda0768 (patch) (side-by-side diff)
treee67c2e6903544dc007422fa616481c8984446da0
parent21cbaaf979d12447c56382d353d7f03853f49907 (diff)
downloadconfig-parser-master.tar.gz
config-parser-master.tar.bz2
Implement BOOLEAN data type.HEADv1.03master
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Changes3
-rw-r--r--lib/Config/Parser.pm58
-rw-r--r--t/ConfigSpec.pm1
-rw-r--r--t/conf00.t3
-rw-r--r--t/conf09.t8
5 files changed, 63 insertions, 10 deletions
diff --git a/Changes b/Changes
index c2dfadd..39c85ac 100644
--- a/Changes
+++ b/Changes
@@ -2,2 +2,5 @@ Revision history for Perl extension Config::Parser
+1.03 Fri Aug 23 14:44:18 2019
+ - implement the BOOLEAN data type
+
1.02 Thu Aug 22 09:47:42 2019
diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm
index 0c771ed..bd230b5 100644
--- a/lib/Config/Parser.pm
+++ b/lib/Config/Parser.pm
@@ -9,3 +9,3 @@ use mro;
-our $VERSION = "1.02";
+our $VERSION = "1.03";
@@ -114,2 +114,4 @@ sub loadsynt {
$ret->{re} = '^([0-9][A-Fa-f])+$';
+ } elsif ($val =~ /^BOOL(EAN)?$/) {
+ $ret->{check} = \&check_bool;
} else {
@@ -144,2 +146,25 @@ sub loadsynt {
+sub check_bool {
+ my ($self, $valref, undef, $locus) = @_;
+ my %bv = (
+ yes => 1,
+ no => 0,
+ true => 1,
+ false => 0,
+ on => 1,
+ off => 0,
+ t => 1,
+ nil => 0,
+ 1 => 1,
+ 0 => 0
+ );
+
+ if (exists($bv{$$valref})) {
+ $$valref = $bv{$$valref};
+ return 1;
+ }
+ $self->error("$$valref is not a valid boolean value", locus => $locus);
+ return 0;
+}
+
1;
@@ -204,3 +229,3 @@ applies the validation rules to the created syntax tree. If all rules pass,
the configuration is correct and the constructor returns a valid object.
-Otherwise, it issues proper diagnostics and returns B<undef>.
+Otherwise, it issues proper diagnostics and croaks.
@@ -231,5 +256,8 @@ Creates a new parser object. Keyword arguments are:
-Name of the file to parse. If not supplied, you will have to
-call the B<$cfg-E<gt>parse> method explicitly after you are returned a
-valid B<$cfg>.
+Name of the file to parse. If supplied, the constructor will call
+the B<parse> and B<commit> methods automatically and will croak if
+the latter returns false. The B<parse> method is given B<filename>,
+B<line> and B<fh> keyword-value pairs (if present) as its arguments.
+
+If not supplied, the caller is supposed to call both methods later.
@@ -241,2 +269,4 @@ not supplied, B<1> is assumed.
+Valid only together with B<filename>.
+
=item B<fh>
@@ -246,2 +276,4 @@ created by using B<open> on the supplied filename.
+Valid only together with B<filename>.
+
=item B<lexicon>
@@ -331,2 +363,8 @@ Hex number.
+=item B<BOOL> or B<BOOLEAN>
+
+Boolean value. Allowed values are:
+B<yes>, B<true>, B<on>, B<t>, B<1>, for C<true> and
+B<no>, B<false>, B<off>, B<nil>, B<0>, for C<false>.
+
=back
@@ -406,2 +444,12 @@ keyword itself is removed from the lexicon.
+=head1 OTHER METHODS
+
+=head2 $cfg->check($valref, $prev, $locus)
+
+This method implements syntax checking and translation for C<BOOLEAN> data
+types. If B<$$valref> is one of the valid boolean values (as described
+above), it translates it to B<1> or B<0>, stores that value in B<$valref>,
+and returns 1. Otherwise, it emits error message using B<$cfg->error> and
+returns 0.
+
=head1 SEE ALSO
diff --git a/t/ConfigSpec.pm b/t/ConfigSpec.pm
index 30812ed..b94c1df 100644
--- a/t/ConfigSpec.pm
+++ b/t/ConfigSpec.pm
@@ -18,2 +18,3 @@ __DATA__
size = STRING :re='\d+(?:(?i) *[kmg])'
+ enable = BOOL
[load]
diff --git a/t/conf00.t b/t/conf00.t
index b497c86..1ee18c5 100644
--- a/t/conf00.t
+++ b/t/conf00.t
@@ -10,3 +10,3 @@ my $c = new ConfigSpec;
ok($c->canonical,
- q{core.base=4 core.number=[5,10] core.size="10 k" load.file="/etc/passwd" load.foobar="baz"});
+ q{core.base=4 core.enable=1 core.number=[5,10] core.size="10 k" load.file="/etc/passwd" load.foobar="baz"});
@@ -18,2 +18,3 @@ __DATA__
number = 10
+ enable = true
[load]
diff --git a/t/conf09.t b/t/conf09.t
index c533747..66f8c60 100644
--- a/t/conf09.t
+++ b/t/conf09.t
@@ -4,3 +4,3 @@ use strict;
use Test;
-use ConfigSpec;
+use ConfigSpec2;
use ConfigSpec3;
@@ -9,7 +9,7 @@ plan(tests => 2);
-my $c = new ConfigSpec;
+my $c2 = new ConfigSpec2;
my $c3 = new ConfigSpec3;
-ok($c->canonical_lexicon,
- q{{"core" => {"section" => {"base" => {"default" => "null","mandatory" => 1},"number" => {"array" => 1,"re" => "^\\\\d+\\$"},"size" => {"re" => "\\\\d+(?:(?i) *[kmg])"}}},"load" => {"section" => {"*" => {},"file" => {"check" => "_check_abs_name","mandatory" => 1}}}}});
+ok($c2->canonical_lexicon,
+ q{{"core" => {"section" => {"base" => {"default" => "null","mandatory" => 1}}},"load" => {"section" => {"*" => {"section" => {"param" => {"mandatory" => 1,"section" => {"mode" => {"re" => "^[0-7]+\$"},"owner" => {}}}}}}}}});

Return to:

Send suggestions and report system problems to the System administrator.