Skip to content

Commit

Permalink
add localized additives and allergens
Browse files Browse the repository at this point in the history
  • Loading branch information
goerlitz committed May 15, 2024
1 parent a034d79 commit 60d8553
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/src/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,25 @@ class Product extends JsonObject {
fromJson: Additives.additivesFromJson,
toJson: Additives.additivesToJson)
Additives? additives;
@JsonKey(
name: 'additives_tags_in_languages',
toJson: LanguageHelper.toJsonStringsListMap,
fromJson: LanguageHelper.fromJsonStringsListMap,
includeIfNull: false)
Map<OpenFoodFactsLanguage, List<String>>? additivesTagsInLanguages;

@JsonKey(
name: 'allergens_tags',
includeIfNull: false,
fromJson: Allergens.allergensFromJson,
toJson: Allergens.allergensToJson)
Allergens? allergens;
@JsonKey(
name: 'allergens_tags_in_languages',
toJson: LanguageHelper.toJsonStringsListMap,
fromJson: LanguageHelper.fromJsonStringsListMap,
includeIfNull: false)
Map<OpenFoodFactsLanguage, List<String>>? allergensTagsInLanguages;

@JsonKey(
name: 'nutrient_levels',
Expand Down Expand Up @@ -669,6 +681,14 @@ class Product extends JsonObject {
result.categoriesTagsInLanguages ??= {};
result.categoriesTagsInLanguages![language] = labels;
break;
case ProductField.ADDITIVES_TAGS_IN_LANGUAGES:
result.additivesTagsInLanguages ??= {};
result.additivesTagsInLanguages![language] = labels;
break;
case ProductField.ALLERGENS_TAGS_IN_LANGUAGES:
result.allergensTagsInLanguages ??= {};
result.allergensTagsInLanguages![language] = labels;
break;
case ProductField.TRACES_TAGS_IN_LANGUAGES:
result.tracesTagsInLanguages ??= {};
result.tracesTagsInLanguages![language] = labels;
Expand Down Expand Up @@ -726,6 +746,8 @@ class Product extends JsonObject {
setLanguageString(productField, language, json[key]);
return;
case ProductField.CATEGORIES_TAGS_IN_LANGUAGES:
case ProductField.ADDITIVES_TAGS_IN_LANGUAGES:
case ProductField.ALLERGENS_TAGS_IN_LANGUAGES:
case ProductField.TRACES_TAGS_IN_LANGUAGES:
case ProductField.BRANDS_TAGS_IN_LANGUAGES:
case ProductField.STATES_TAGS_IN_LANGUAGES:
Expand Down
8 changes: 8 additions & 0 deletions lib/src/model/product.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/src/utils/product_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum ProductField implements OffTagged {
NO_NUTRITION_DATA(offTag: 'no_nutrition_data'),
NUTRIMENTS(offTag: 'nutriments'),
ADDITIVES(offTag: 'additives_tags'),
ADDITIVES_TAGS_IN_LANGUAGES(offTag: 'additives_tags_'),
NUTRIENT_LEVELS(offTag: 'nutrient_levels'),
INGREDIENTS_TEXT(offTag: 'ingredients_text'),
INGREDIENTS_TEXT_IN_LANGUAGES(offTag: 'ingredients_text_'),
Expand Down Expand Up @@ -75,6 +76,7 @@ enum ProductField implements OffTagged {
INGREDIENTS_ANALYSIS_TAGS(offTag: 'ingredients_analysis_tags'),
INGREDIENTS_ANALYSIS_TAGS_IN_LANGUAGES(offTag: 'ingredients_analysis_tags_'),
ALLERGENS(offTag: 'allergens_tags'),
ALLERGENS_TAGS_IN_LANGUAGES(offTag: 'allergens_tags_'),
ATTRIBUTE_GROUPS(offTag: 'attribute_groups'),
LAST_MODIFIED(offTag: 'last_modified_t'),
LAST_MODIFIER(offTag: 'last_modified_by'),
Expand Down Expand Up @@ -126,6 +128,8 @@ const Set<ProductField> fieldsInLanguages = {
ProductField.BRANDS_TAGS_IN_LANGUAGES,
ProductField.MISC_TAGS_IN_LANGUAGES,
ProductField.INGREDIENTS_ANALYSIS_TAGS_IN_LANGUAGES,
ProductField.ADDITIVES_TAGS_IN_LANGUAGES,
ProductField.ALLERGENS_TAGS_IN_LANGUAGES,
ProductField.LABELS_TAGS_IN_LANGUAGES,
ProductField.COUNTRIES_TAGS_IN_LANGUAGES,
ProductField.INGREDIENTS_TAGS_IN_LANGUAGES,
Expand Down
13 changes: 13 additions & 0 deletions test/api_get_localized_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ void main() {
fields: [
// tags in languages
ProductField.CATEGORIES_TAGS_IN_LANGUAGES,
ProductField.ADDITIVES_TAGS_IN_LANGUAGES,
ProductField.ALLERGENS_TAGS_IN_LANGUAGES,
ProductField.TRACES_TAGS_IN_LANGUAGES,
ProductField.STORES_TAGS_IN_LANGUAGES,
ProductField.STATES_TAGS_IN_LANGUAGES,
Expand Down Expand Up @@ -187,6 +189,17 @@ void main() {
product.categoriesTagsInLanguages,
ProductField.CATEGORIES_TAGS_IN_LANGUAGES,
);
check(
product.additives?.ids ?? [],
product.additivesTagsInLanguages,
ProductField.ADDITIVES_TAGS_IN_LANGUAGES,
);
check(
// note that the test product has no allergens
product.allergens?.ids ?? [],
product.allergensTagsInLanguages,
ProductField.ALLERGENS_TAGS_IN_LANGUAGES,
);
check(
product.tracesTags,
product.tracesTagsInLanguages,
Expand Down

0 comments on commit 60d8553

Please sign in to comment.