Skip to content

Commit

Permalink
Make the BootstrapBlock decoder deal with unquoted key/value pairs.
Browse files Browse the repository at this point in the history
Updates #630
  • Loading branch information
LukedFitzpatrick committed Jan 20, 2017
1 parent 56ef3b5 commit 13b5170
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Biodiverse/TreeNode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ sub to_table {
my $number = $node->get_value ('NODE_NUMBER');
my %data;

my $colour = $node->get_colour_string();
my $colour = $node->get_bootstrap_value(key => "color");

# add to the basestruct object
if( $export_colours ) {
Expand Down
19 changes: 16 additions & 3 deletions lib/Biodiverse/TreeNode/BootstrapBlock.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ sub decode_bootstrap_block {

# fix up unquoted key/value pairs i.e. add quotes because the json
# decoder doesn't work without them.
print "Input going in $input\n";
$input = $self->fix_up_unquoted_bootstrap_block( block => $input );
print "After fix up: $input\n";

# will replace first and last use of [ and ] respectively.
$input =~ s/\[/\{/;
Expand Down Expand Up @@ -97,14 +99,25 @@ sub encode_bootstrap_block {



# add quotes to unquoted bootstrap blocks
# add quotes to unquoted bootstrap blocks. Needed for the json decoder
# e.g. [key:value,key2:value2] goes to ["key":"value","key2":"value2"]
sub fix_up_unquoted_bootstrap_block {
my ($self, %args) = @_;
my $block = $args{block};

# do some crazy regex here


# Basic idea is to find a block starting and ending with '[' or
# ','. Take what is inside this block, and find a 'key' and
# 'value' separated by a ':'. If these aren't already quoted, put
# quotes around them. We need to do this loop because the final
# comma of one block is the starting comma of the next block.

my $old = "";
while(!($old eq $block)) {
$old = $block;
# crazy regex here
$block =~ s/([\[,])([^\"]*?)\:([^\"]*?)([\],])/$1\"$2\":\"$3\"$4/;
}
return $block;
}

Expand Down
2 changes: 1 addition & 1 deletion t/32-BootstrapBlock.t
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ sub test_basic_operations {
}

sub test_decode {
my @raw_inputs = ('["foo":"bar",\'footwo\':"bartwo",foothree:barthree]');
my @raw_inputs = ('["foo":"bar","footwo":"bartwo",foothree:barthree]');


my %hash = ( "foo" => "bar",
Expand Down

0 comments on commit 13b5170

Please sign in to comment.