summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2017-12-03 13:14:21 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2017-12-03 13:14:21 (GMT)
commit9f89acb180d22d30bb4ffed659969d57cf5cabce (patch) (unidiff)
tree9dfe0b30b92cccd45b28158f4442c242dd988968
parentceb4bd03d48d161548e00f1db170b376583894a8 (diff)
downloadconfig-ast-9f89acb180d22d30bb4ffed659969d57cf5cabce.tar.gz
config-ast-9f89acb180d22d30bb4ffed659969d57cf5cabce.tar.bz2
Implement add_node and add_value methods
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--lib/Config/Tree.pm43
-rw-r--r--t/TestConfig.pm2
-rw-r--r--t/conf11.t2
3 files changed, 32 insertions, 15 deletions
diff --git a/lib/Config/Tree.pm b/lib/Config/Tree.pm
index 39e0de9..f22d8e5 100644
--- a/lib/Config/Tree.pm
+++ b/lib/Config/Tree.pm
@@ -509,3 +509,3 @@ sub _get_section_synt {
509sub add_node { 509sub add_node {
510 my ($self, $path, $v, $locus) = @_; 510 my ($self, $path, $node) = @_;
511 511
@@ -516,5 +516,6 @@ sub add_node {
516 my $kw = $self->{_parameters} // { '*' => '*' }; 516 my $kw = $self->{_parameters} // { '*' => '*' };
517 my $node = $self->tree; 517 my $tree = $self->tree;
518 my $pn = $#{$path}; 518 my $pn = $#{$path};
519 my $name; 519 my $name;
520 my $locus = $node->locus;
520 for (my $i = 0; $i < $pn; $i++) { 521 for (my $i = 0; $i < $pn; $i++) {
@@ -522,3 +523,3 @@ sub add_node {
522 523
523 unless ($node->is_section) { 524 unless ($tree->is_section) {
524 $self->error(join('.', @{$path}[0..$i]) . ": not a section"); 525 $self->error(join('.', @{$path}[0..$i]) . ": not a section");
@@ -535,6 +536,6 @@ sub add_node {
535 536
536 if (my $subtree = $node->subtree($name)) { 537 if (my $subtree = $tree->subtree($name)) {
537 $node = $subtree; 538 $tree = $subtree;
538 } else { 539 } else {
539 $node = $node->subtree( 540 $tree = $tree->subtree(
540 $name => new Config::Tree::Node::Section( 541 $name => new Config::Tree::Node::Section(
@@ -555,2 +556,14 @@ sub add_node {
555 556
557 if ($node->is_section) {
558 if ($tree->has_key($name)) {
559 $tree->locus->add($locus);
560 $tree->subtree($name)->merge($node);
561 } else {
562 $tree->subtree($name => $node);
563 }
564 return $node;
565 }
566
567 my $v = $node->value;
568
556 if (ref($x) eq 'HASH') { 569 if (ref($x) eq 'HASH') {
@@ -558,5 +571,5 @@ sub add_node {
558 my $prev_val; 571 my $prev_val;
559 if ($node->has_key($name)) { 572 if ($tree->has_key($name)) {
560 # FIXME: is_value? 573 # FIXME: is_value?
561 $prev_val = $node->subtree($name)->value; 574 $prev_val = $tree->subtree($name)->value;
562 } 575 }
@@ -587,11 +600,9 @@ sub add_node {
587 600
588 $node->locus->add($locus->clone); 601 $tree->locus->add($locus->clone);
589 602
590 my $newnode; 603 my $newnode;
591 if ($newnode = $node->subtree($name)) { 604 if ($newnode = $tree->subtree($name)) {
592 $newnode->locus->add($locus); 605 $newnode->locus->add($locus);
593 } else { 606 } else {
594 $newnode = $node->subtree( 607 $newnode = $tree->subtree($name => $node);
595 $name => new Config::Tree::Node::Value(locus => $locus)
596 );
597 } 608 }
@@ -602,2 +613,8 @@ sub add_node {
602 613
614sub add_value {
615 my ($self, $path, $value, $locus) = @_;
616 $self->add_node($path, new Config::Tree::Node::Value(value => $value,
617 locus => $locus));
618}
619
603sub commit { 620sub commit {
diff --git a/t/TestConfig.pm b/t/TestConfig.pm
index de1ade9..847ff0c 100644
--- a/t/TestConfig.pm
+++ b/t/TestConfig.pm
@@ -21,3 +21,3 @@ sub new {
21# while (my ($k,$v) = each %$config) { 21# while (my ($k,$v) = each %$config) {
22 $self->add_node($k, $v, new Config::Tree::Locus('input', $i++)); 22 $self->add_value($k, $v, new Config::Tree::Locus('input', $i++));
23 } 23 }
diff --git a/t/conf11.t b/t/conf11.t
index 13516b8..07a94cf 100644
--- a/t/conf11.t
+++ b/t/conf11.t
@@ -14,3 +14,3 @@ my $cfg = new TestConfig(
14 ); 14 );
15ok(join(',', $cfg->getnode('core')->keys), 'retain-interval,tempdir'); 15ok(join(',', sort $cfg->getnode('core')->keys), 'retain-interval,tempdir');
16ok($cfg->getnode('core')->keys, 2); 16ok($cfg->getnode('core')->keys, 2);

Return to:

Send suggestions and report system problems to the System administrator.