Skip to content

Commit

Permalink
Merge pull request #6031 from openfoodfacts/nutrients
Browse files Browse the repository at this point in the history
fix: correct nesting of cgi/nutrient.pl API response
  • Loading branch information
monsieurtanuki authored Oct 29, 2021
2 parents 48522db + d77e094 commit 636156c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 38 deletions.
73 changes: 37 additions & 36 deletions cgi/nutrients.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This file is part of Product Opener.
#
# Product Opener
# Copyright (C) 2011-2019 Association Open Food Facts
# Copyright (C) 2011-2021 Association Open Food Facts
# Contact: contact@openfoodfacts.org
# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France
#
Expand All @@ -30,29 +30,22 @@
use ProductOpener::Display qw/:all/;
use ProductOpener::Food qw/:all/;

use Log::Any qw($log);
use CGI qw/:cgi :form escapeHTML/;
use JSON::PP;

ProductOpener::Display::init();

# Recursively remove parent association to avoid redundant JSON data.
sub _remove_parent {
my $current_ref = shift;

if (defined $current_ref->{nutrients}) {
foreach my $nutrient (@{$current_ref->{nutrients}}) {
_remove_parent($nutrient);
}
}

delete $current_ref->{parent};

return;
}
# Turn the flat nutriments table array into a nested array of nutrients
# The level of each nutrient is indicated by leading dashes before its id:
# nutrient
# -sub-nutrient
# --sub-sub-nutrient

my @table = ();
my $previous_ref;
my $previous_prefix_length = 0;
my $parent_level0;
my $parent_level1;

foreach (@{$nutriments_tables{$nutriment_table}}) {
my $nid = $_; # Copy instead of alias

Expand All @@ -62,33 +55,41 @@ sub _remove_parent {
my $default_edit_form = $nid =~ /-$/ ? JSON::PP::false : JSON::PP::true;
$nid =~ s/-$//g;

my $onid = $nid =~ s/^(-+)//gr;
my $prefix_length = defined $1 ? length($1) : 0;
my %current = ( id => $onid, important => $important, display_in_edit_form => $default_edit_form );
my $current_ref = \%current;
my $onid = $nid =~ s/^(\-+)//gr;

my $current_ref = { id => $onid, important => $important, display_in_edit_form => $default_edit_form };
my $name = get_nutrient_label($onid, $lc);
if (defined $name) {
$current_ref->{name} = $name;
}

if (($prefix_length gt 0) or ($prefix_length gt $previous_prefix_length)) {
@{$previous_ref->{nutrients}} = () unless defined $previous_ref->{nutrients};
push @{$previous_ref->{nutrients}}, $current_ref unless not defined $current_ref;
$current_ref->{parent} = $previous_ref;
}
else {
push @table, $current_ref unless not defined $current_ref;
my $prefix_length = 0;
if ($nid =~ s/^--//g) {
$prefix_length = 2;
} elsif($nid =~ s/^-//g) {
$prefix_length = 1;
}

if (($prefix_length gt $previous_prefix_length) or ($prefix_length eq 0)) {
$previous_ref = $current_ref;
$previous_prefix_length = $prefix_length;
if ($prefix_length == 0) {
# I'm on level 0, I have no parent, and I'm the new level 0 parent
push @table, $current_ref;
$parent_level0 = $current_ref;
$parent_level1 = undef;
}
elsif (($prefix_length == 1) and (defined $parent_level0)) {
# I'm on level 1, my parent is the latest level 0 parent, and I'm the new level 1 parent
defined $parent_level0->{nutrients} or $parent_level0->{nutrients} = [];
push @{$parent_level0->{nutrients}}, $current_ref unless not defined $current_ref;
$parent_level1 = $current_ref;
}
elsif (($prefix_length == 2) and (defined $parent_level1)) {
# I'm on level 2, my parent is the latest level 1 parent
defined $parent_level1->{nutrients} or $parent_level1->{nutrients} = [];
push @{$parent_level1->{nutrients}}, $current_ref;
}
else {
$log->error("invalid nesting of nutrients", { nutriment_table => $nutriment_table, nid => $nid, prefix_length => $prefix_length, current_ref => $current_ref, parent_level0 => $parent_level0, parent_level1 => $parent_level1 }) if $log->is_error();
}
}

# The parent attribute is only used to build up the structure. Just remove it here to avoid circular dependency in JSON.
foreach my $nutrient (@table) {
_remove_parent($nutrient);
}

my %result = ( nutrients => \@table );
Expand Down
4 changes: 2 additions & 2 deletions lib/ProductOpener/Food.pm
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ sub mmoll_to_unit {
'-starch-',
'-polyols-',
'fiber',
'--soluble-fiber-',
'--insoluble-fiber-',
'-soluble-fiber-',
'-insoluble-fiber-',
'!proteins',
'-casein-',
'-serum-proteins-',
Expand Down

0 comments on commit 636156c

Please sign in to comment.