From 3613a0ec2ecc987e4c7078ace20c42a05c3c5fa6 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 14 Sep 2024 14:25:03 +0200 Subject: [PATCH] feat: 972 - new "product_type" field for Product (#973) New file: * `product_type.dart`: Type used at the product level (e.g. "this is a pet food product"). Impacted files: * `openfoodfacts.dart`: exported new file `product_type.dart` * `product.dart`: added a new `ProductType?` field * `product.g.dart`: (generated) --- lib/openfoodfacts.dart | 1 + lib/src/model/product.dart | 5 +++++ lib/src/model/product.g.dart | 10 ++++++++++ lib/src/model/product_type.dart | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 lib/src/model/product_type.dart diff --git a/lib/openfoodfacts.dart b/lib/openfoodfacts.dart index 70a331db4..04f176c1d 100644 --- a/lib/openfoodfacts.dart +++ b/lib/openfoodfacts.dart @@ -48,6 +48,7 @@ export 'src/model/parameter/without_additives.dart'; export 'src/model/per_size.dart'; export 'src/model/product.dart'; export 'src/model/product_freshness.dart'; +export 'src/model/product_type.dart'; export 'src/model/product_image.dart'; export 'src/model/product_packaging.dart'; export 'src/model/product_result_field_answer.dart'; diff --git a/lib/src/model/product.dart b/lib/src/model/product.dart index abc233bf0..43420c11c 100644 --- a/lib/src/model/product.dart +++ b/lib/src/model/product.dart @@ -13,6 +13,7 @@ import 'nutriments.dart'; import 'owner_field.dart'; import 'product_image.dart'; import 'product_packaging.dart'; +import 'product_type.dart'; import '../interface/json_object.dart'; import '../utils/json_helper.dart'; import '../utils/language_helper.dart'; @@ -90,6 +91,10 @@ class Product extends JsonObject { @JsonKey(name: 'code') String? barcode; + /// Type of the product (e.g. "pet food"). + @JsonKey(name: 'product_type') + ProductType? productType; + /// Product name, either set directly or taken from one of the localizations. /// /// Rather use [productNameInLanguages] instead. diff --git a/lib/src/model/product.g.dart b/lib/src/model/product.g.dart index 32e0df226..bf0ce3b0d 100644 --- a/lib/src/model/product.g.dart +++ b/lib/src/model/product.g.dart @@ -101,6 +101,8 @@ Product _$ProductFromJson(Map json) => Product( : Nutriments.fromJson(json['nutriments'] as Map), noNutritionData: JsonHelper.checkboxFromJSON(json['no_nutrition_data']), ) + ..productType = + $enumDecodeNullable(_$ProductTypeEnumMap, json['product_type']) ..genericNameInLanguages = LanguageHelper.fromJsonStringMap(json['generic_name_in_languages']) ..abbreviatedName = json['abbreviated_product_name'] as String? @@ -179,6 +181,7 @@ Product _$ProductFromJson(Map json) => Product( Map _$ProductToJson(Product instance) { final val = { 'code': instance.barcode, + 'product_type': _$ProductTypeEnumMap[instance.productType], }; void writeNotNull(String key, dynamic value) { @@ -317,6 +320,13 @@ Map _$ProductToJson(Product instance) { return val; } +const _$ProductTypeEnumMap = { + ProductType.food: 'food', + ProductType.beauty: 'beauty', + ProductType.petFood: 'petfood', + ProductType.product: 'product', +}; + const _$ImageFieldEnumMap = { ImageField.FRONT: 'FRONT', ImageField.INGREDIENTS: 'INGREDIENTS', diff --git a/lib/src/model/product_type.dart b/lib/src/model/product_type.dart new file mode 100644 index 000000000..229e5924c --- /dev/null +++ b/lib/src/model/product_type.dart @@ -0,0 +1,32 @@ +import 'package:json_annotation/json_annotation.dart'; +import 'off_tagged.dart'; +import '../prices/flavor.dart'; +import '../utils/server_type.dart'; + +/// Type used at the [Product] level (e.g. "this is a pet food product"). +/// +/// Somehow redundant with [ServerType] and [Flavor]. +enum ProductType implements OffTagged { + @JsonValue('food') + food(offTag: 'food'), + + @JsonValue('beauty') + beauty(offTag: 'beauty'), + + @JsonValue('petfood') + petFood(offTag: 'petfood'), + + @JsonValue('product') + product(offTag: 'product'); + + const ProductType({ + required this.offTag, + }); + + @override + final String offTag; + + /// Returns the first [ProductType] that matches the [offTag]. + static ProductType? fromOffTag(final String? offTag) => + OffTagged.fromOffTag(offTag, ProductType.values) as ProductType?; +}