Skip to content

Commit

Permalink
feat: new localized product fields for generic name (#828)
Browse files Browse the repository at this point in the history
Impacted files:
* `api_get_save_product_test.dart`: set explicit language for `genericName` test to pass; also tested new field `genericNameInLanguages`
* `product.dart`: added field `genericNameInLanguages`
* `product.g.dart`: generated
* `product_fields.dart`: added fields `GENERIC_NAME_IN_LANGUAGES` and `GENERIC_NAME_ALL_LANGUAGES`
  • Loading branch information
monsieurtanuki committed Nov 16, 2023
1 parent 60c2bdc commit a98cfcb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
17 changes: 15 additions & 2 deletions lib/src/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,18 @@ class Product extends JsonObject {
includeIfNull: false)
Map<OpenFoodFactsLanguage, String>? productNameInLanguages;

///Common name
///Example: Chocolate bar with milk and hazelnuts
/// Common name. Example: "Chocolate bar with milk and hazelnuts".
@JsonKey(name: 'generic_name', includeIfNull: false)
String? genericName;

/// Localized common name.
@JsonKey(
name: 'generic_name_in_languages',
fromJson: LanguageHelper.fromJsonStringMap,
toJson: LanguageHelper.toJsonStringMap,
includeIfNull: false)
Map<OpenFoodFactsLanguage, String>? genericNameInLanguages;

@JsonKey(name: 'brands', includeIfNull: false)
String? brands;
@JsonKey(name: 'brands_tags', includeIfNull: false)
Expand Down Expand Up @@ -572,6 +579,11 @@ class Product extends JsonObject {
result.productNameInLanguages ??= {};
result.productNameInLanguages![language] = label;
break;
case ProductField.GENERIC_NAME_IN_LANGUAGES:
case ProductField.GENERIC_NAME_ALL_LANGUAGES:
result.genericNameInLanguages ??= {};
result.genericNameInLanguages![language] = label;
break;
case ProductField.INGREDIENTS_TEXT_IN_LANGUAGES:
case ProductField.INGREDIENTS_TEXT_ALL_LANGUAGES:
result.ingredientsTextInLanguages ??= {};
Expand Down Expand Up @@ -653,6 +665,7 @@ class Product extends JsonObject {
) {
switch (productField) {
case ProductField.NAME_IN_LANGUAGES:
case ProductField.GENERIC_NAME_IN_LANGUAGES:
case ProductField.INGREDIENTS_TEXT_IN_LANGUAGES:
case ProductField.PACKAGING_TEXT_IN_LANGUAGES:
setLanguageString(productField, language, json[key]);
Expand Down
4 changes: 4 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 @@ -8,6 +8,8 @@ enum ProductField implements OffTagged {
NAME_IN_LANGUAGES(offTag: 'product_name_'),
NAME_ALL_LANGUAGES(offTag: 'product_name_languages'),
GENERIC_NAME(offTag: 'generic_name'),
GENERIC_NAME_IN_LANGUAGES(offTag: 'generic_name_'),
GENERIC_NAME_ALL_LANGUAGES(offTag: 'generic_name_languages'),
BRANDS(offTag: 'brands'),
BRANDS_TAGS(offTag: 'brands_tags'),
BRANDS_TAGS_IN_LANGUAGES(offTag: 'brands_tags_'),
Expand Down Expand Up @@ -108,6 +110,7 @@ enum ProductField implements OffTagged {

const Set<ProductField> fieldsInLanguages = {
ProductField.NAME_IN_LANGUAGES,
ProductField.GENERIC_NAME_IN_LANGUAGES,
ProductField.INGREDIENTS_TEXT_IN_LANGUAGES,
ProductField.PACKAGING_TEXT_IN_LANGUAGES,
ProductField.CATEGORIES_TAGS_IN_LANGUAGES,
Expand All @@ -125,6 +128,7 @@ const Set<ProductField> fieldsInLanguages = {

const Set<ProductField> fieldsAllLanguages = {
ProductField.NAME_ALL_LANGUAGES,
ProductField.GENERIC_NAME_ALL_LANGUAGES,
ProductField.INGREDIENTS_TEXT_ALL_LANGUAGES,
ProductField.PACKAGING_TEXT_ALL_LANGUAGES,
};
Expand Down
13 changes: 9 additions & 4 deletions test/api_get_save_product_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ void main() {
group('$OpenFoodAPIClient get-save products', () {
test('get product Coca Cola Light', () async {
final String barcode = getBookBarcode(0);
const OpenFoodFactsLanguage language = OpenFoodFactsLanguage.GERMAN;
const String genericName = 'Softdrink';
const List<String> ingredientsText = <String>[
'Wasser',
'Kohlensäure',
Expand All @@ -52,7 +54,7 @@ void main() {
final Product inputProduct = Product(
barcode: barcode,
productName: 'Coca Cola Light',
genericName: 'Softdrink',
genericName: genericName,
countries: 'Frankreich,Deutschland',
brands: 'Coca Cola',
nutrimentDataPer: PerSize.serving.offTag,
Expand All @@ -66,10 +68,11 @@ void main() {
TestConstants.TEST_USER,
inputProduct,
uriHelper: uriHelper,
language: language,
);

final SendImage fontImage = SendImage(
lang: OpenFoodFactsLanguage.GERMAN,
lang: language,
barcode: barcode,
imageField: ImageField.FRONT,
imageUri: Uri.file('test/test_assets/front_coca_light_de.jpg'),
Expand All @@ -83,7 +86,7 @@ void main() {
final ProductQueryConfiguration configurations =
ProductQueryConfiguration(
barcode,
language: OpenFoodFactsLanguage.GERMAN,
language: language,
fields: [ProductField.ALL],
version: ProductQueryVersion.v3,
);
Expand All @@ -99,7 +102,9 @@ void main() {
expect(product.barcode, barcode);
expect(product.lastModified, isNotNull);

expect(product.genericName, 'Softdrink');
expect(product.genericName, genericName);
expect(product.genericNameInLanguages, isNotNull);
expect(product.genericNameInLanguages![language], genericName);

expect(product.ingredients, isNotNull);
expect(product.ingredients!.length, ingredientsText.length);
Expand Down

0 comments on commit a98cfcb

Please sign in to comment.