Skip to content

Commit

Permalink
feat: 972 - new "product_type" field for Product (openfoodfacts#973)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
monsieurtanuki authored Sep 14, 2024
1 parent ddeb7eb commit 3613a0e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/openfoodfacts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions lib/src/model/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 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.

32 changes: 32 additions & 0 deletions lib/src/model/product_type.dart
Original file line number Diff line number Diff line change
@@ -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?;
}

0 comments on commit 3613a0e

Please sign in to comment.