Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct nesting of cgi/nutrient.pl API response #6031

Merged
merged 4 commits into from
Oct 29, 2021
Merged

Conversation

stephanegigandet
Copy link
Contributor

Fixes #5997 . Code from @monsieurtanuki , slightly changed.

This is to output correctly nested nutrients.

Demo: https://fr.openfoodfacts.dev/cgi/nutrients.pl (login and password: off )

Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very familiar ;)
Beyond my minor comments for nutrients.pl, there's also something to fix in Food.pm that I already mentioned:

'fiber', # should probably be '-fiber'
'--soluble-fiber-', # or should be '-soluble-fiber-'
'--insoluble-fiber-', # and '-insoluble-fiber-'

Without that, soluble and insoluble fibers are linked to the latest level 1 parent, which is polyols. Instead of fibers.

cgi/nutrients.pl Outdated
else {

if ($prefix_length == 0) {
# I'm on level 2, my parent is the latest level 1 parent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confusion with the comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks

elsif ($prefix_length == 1) {
# I'm on level 0, I have no parent, and I'm the latest level 0 parent
@{$parent_level0->{nutrients}} = () unless defined $parent_level0->{nutrients};
push @{$parent_level0->{nutrients}}, $current_ref unless not defined $current_ref;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious from my no-perl background: how could $current_ref not be defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I just changed that code a bit.

@stephanegigandet
Copy link
Contributor Author

Looks very familiar ;)

Indeed. :)

Beyond my minor comments for nutrients.pl, there's also something to fix in Food.pm that I already mentioned:

Thank you @monsieurtanuki , now fixed as well.

Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stephanegigandet!
Regarding the "fiber fix", in some cases it looks strange to have the fiber and the sub fibers at the same level.
Please have a look at my detailed comments, and ignore them if they're not relevant.

Comment on lines 637 to 638
'-soluble-fiber-',
'-insoluble-fiber-',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible, but this time fiber was already at level 1, so my assumption is that the "sub-fibers" should stay on level 2. Or fiber should be at level 0. Probably.
And is it right to put fibers under carbohydrates?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I made a search and replace too quickly.

Regarding fibers, in the US they are counted as carbohydrates: https://www.accessdata.fda.gov/scripts/interactivenutritionfactslabel/assets/InteractiveNFL_TotalCarbohydrate_March2020.pdf

One issue we have is that on OFF, we have only one "carbohydrates" field, but in Europe the law says it does not include fibers, while in the US it does. We don't have a good way to handle this difference (or we would need 2 different fields I guess).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine 2 "virtual" nutrients, "US-style-carbohydrates" and "non-US-style-carbohydrates", that would be computed from carbohydrates, fibers and nutrition country.
A bit like what could be done between salt and sodium if sometimes you know only one of them.
But that's another story...

Comment on lines 866 to 867
'-soluble-fiber-',
'-insoluble-fiber-',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

Comment on lines 974 to 976
'-fiber',
'--soluble-fiber-',
'--insoluble-fiber-',
'-soluble-fiber-',
'-insoluble-fiber-',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@monsieurtanuki monsieurtanuki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now my voice does matter! :)

@monsieurtanuki
Copy link
Contributor

Is it compliant with the OFF etiquette for you to merge your own code? May I merge now?

@stephanegigandet
Copy link
Contributor Author

Is it compliant with the OFF etiquette for you to merge your own code? May I merge now?

We don't have hard rules, usually whoever makes the PR merges it after it's been approved. This one is special though as it's mostly your code. So please do merge it :)

@monsieurtanuki monsieurtanuki merged commit 636156c into main Oct 29, 2021
@monsieurtanuki monsieurtanuki deleted the nutrients branch October 29, 2021 15:57
@monsieurtanuki
Copy link
Contributor

@stephanegigandet Ok then!
What's the next step? I guess there's an additional delivery process to TEST or PROD env, isn't there?
Please ping me when I can use this PR in openfoodfacts/openfoodfacts-dart#210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Incorrect nesting of nutrients and sub-nutrients in /cgi/nutrients.pl API
2 participants