aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2019-08-23 14:43:52 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2019-08-23 14:45:35 +0300
commitd69ba51bb8498146d07a08031de3685e6cda0768 (patch)
treee67c2e6903544dc007422fa616481c8984446da0
parent21cbaaf979d12447c56382d353d7f03853f49907 (diff)
downloadconfig-parser-d69ba51bb8498146d07a08031de3685e6cda0768.tar.gz
config-parser-d69ba51bb8498146d07a08031de3685e6cda0768.tar.bz2
Implement BOOLEAN data type.v1.03
-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
2 2
31.03 Fri Aug 23 14:44:18 2019
4 - implement the BOOLEAN data type
5
31.02 Thu Aug 22 09:47:42 2019 61.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;
9 9
10our $VERSION = "1.02"; 10our $VERSION = "1.03";
11 11
@@ -114,2 +114,4 @@ sub loadsynt {
114 $ret->{re} = '^([0-9][A-Fa-f])+$'; 114 $ret->{re} = '^([0-9][A-Fa-f])+$';
115 } elsif ($val =~ /^BOOL(EAN)?$/) {
116 $ret->{check} = \&check_bool;
115 } else { 117 } else {
@@ -144,2 +146,25 @@ sub loadsynt {
144 146
147sub check_bool {
148 my ($self, $valref, undef, $locus) = @_;
149 my %bv = (
150 yes => 1,
151 no => 0,
152 true => 1,
153 false => 0,
154 on => 1,
155 off => 0,
156 t => 1,
157 nil => 0,
158 1 => 1,
159 0 => 0
160 );
161
162 if (exists($bv{$$valref})) {
163 $$valref = $bv{$$valref};
164 return 1;
165 }
166 $self->error("$$valref is not a valid boolean value", locus => $locus);
167 return 0;
168}
169
1451; 1701;
@@ -204,3 +229,3 @@ applies the validation rules to the created syntax tree. If all rules pass,
204the configuration is correct and the constructor returns a valid object. 229the configuration is correct and the constructor returns a valid object.
205Otherwise, it issues proper diagnostics and returns B<undef>. 230Otherwise, it issues proper diagnostics and croaks.
206 231
@@ -231,5 +256,8 @@ Creates a new parser object. Keyword arguments are:
231 256
232Name of the file to parse. If not supplied, you will have to 257Name of the file to parse. If supplied, the constructor will call
233call the B<$cfg-E<gt>parse> method explicitly after you are returned a 258the B<parse> and B<commit> methods automatically and will croak if
234valid B<$cfg>. 259the latter returns false. The B<parse> method is given B<filename>,
260B<line> and B<fh> keyword-value pairs (if present) as its arguments.
261
262If not supplied, the caller is supposed to call both methods later.
235 263
@@ -241,2 +269,4 @@ not supplied, B<1> is assumed.
241 269
270Valid only together with B<filename>.
271
242=item B<fh> 272=item B<fh>
@@ -246,2 +276,4 @@ created by using B<open> on the supplied filename.
246 276
277Valid only together with B<filename>.
278
247=item B<lexicon> 279=item B<lexicon>
@@ -331,2 +363,8 @@ Hex number.
331 363
364=item B<BOOL> or B<BOOLEAN>
365
366Boolean value. Allowed values are:
367B<yes>, B<true>, B<on>, B<t>, B<1>, for C<true> and
368B<no>, B<false>, B<off>, B<nil>, B<0>, for C<false>.
369
332=back 370=back
@@ -406,2 +444,12 @@ keyword itself is removed from the lexicon.
406 444
445=head1 OTHER METHODS
446
447=head2 $cfg->check($valref, $prev, $locus)
448
449This method implements syntax checking and translation for C<BOOLEAN> data
450types. If B<$$valref> is one of the valid boolean values (as described
451above), it translates it to B<1> or B<0>, stores that value in B<$valref>,
452and returns 1. Otherwise, it emits error message using B<$cfg->error> and
453returns 0.
454
407=head1 SEE ALSO 455=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__
18 size = STRING :re='\d+(?:(?i) *[kmg])' 18 size = STRING :re='\d+(?:(?i) *[kmg])'
19 enable = BOOL
19[load] 20[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;
10ok($c->canonical, 10ok($c->canonical,
11 q{core.base=4 core.number=[5,10] core.size="10 k" load.file="/etc/passwd" load.foobar="baz"}); 11 q{core.base=4 core.enable=1 core.number=[5,10] core.size="10 k" load.file="/etc/passwd" load.foobar="baz"});
12 12
@@ -18,2 +18,3 @@ __DATA__
18 number = 10 18 number = 10
19 enable = true
19[load] 20[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;
4use Test; 4use Test;
5use ConfigSpec; 5use ConfigSpec2;
6use ConfigSpec3; 6use ConfigSpec3;
@@ -9,7 +9,7 @@ plan(tests => 2);
9 9
10my $c = new ConfigSpec; 10my $c2 = new ConfigSpec2;
11my $c3 = new ConfigSpec3; 11my $c3 = new ConfigSpec3;
12 12
13ok($c->canonical_lexicon, 13ok($c2->canonical_lexicon,
14 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}}}}}); 14 q{{"core" => {"section" => {"base" => {"default" => "null","mandatory" => 1}}},"load" => {"section" => {"*" => {"section" => {"param" => {"mandatory" => 1,"section" => {"mode" => {"re" => "^[0-7]+\$"},"owner" => {}}}}}}}}});
15 15

Return to:

Send suggestions and report system problems to the System administrator.