-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new "Barcodes" parameter; deprecated configurations (#538)
New files: * `BarcodeParameter.dart`: "Barcodes" search API parameter. * `BoolMapParameter.dart`: Abstract map of `bool` as `Parameter`. Impacted files: * `AllergensParameter.dart`: refactored as `BoolMapParameter<AllergensTag>` * `api_getToBeCompletedProducts_test.dart`: minor refactoring around a deprecated class * `api_matchedProductV2_test.dart`: minor refactoring around a deprecated class * `api_searchProducts_test.dart`: fixed test duration; refactoring * `openfoodfacts.dart`: minor refactoring around a deprecated class * `ProductListQueryConfiguration.dart`: deprecated * `StatesTagsParameter.dart`: refactored as `BoolMapParameter<State>` * `ToBeCompletedConfiguration.dart`: deprecated
- Loading branch information
1 parent
3872351
commit 582775b
Showing
11 changed files
with
120 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,17 @@ | ||
import 'package:openfoodfacts/interface/Parameter.dart'; | ||
import 'package:openfoodfacts/model/Allergens.dart'; | ||
import 'package:openfoodfacts/model/parameter/BoolMapParameter.dart'; | ||
|
||
/// List of allergens to filter in and out. | ||
/// | ||
/// When we have several items, the results returned use a logical AND. | ||
class AllergensParameter extends Parameter { | ||
class AllergensParameter extends BoolMapParameter<AllergensTag> { | ||
@override | ||
String getName() => 'allergens_tags'; | ||
|
||
@override | ||
String getValue() { | ||
final List<String> result = <String>[]; | ||
if (include != null) { | ||
for (final AllergensTag value in include!) { | ||
result.add(value.tag); | ||
} | ||
} | ||
if (exclude != null) { | ||
for (final AllergensTag value in exclude!) { | ||
result.add('-${value.tag}'); | ||
} | ||
} | ||
return result.join(','); | ||
} | ||
String getTag(final AllergensTag key, final bool value) => | ||
value ? key.tag : '-${key.tag}'; | ||
|
||
final Iterable<AllergensTag>? include; | ||
final Iterable<AllergensTag>? exclude; | ||
|
||
const AllergensParameter({this.include, this.exclude}); | ||
const AllergensParameter({required final Map<AllergensTag, bool> map}) | ||
: super(map: map); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:openfoodfacts/interface/Parameter.dart'; | ||
|
||
/// "Barcodes" search API parameter. | ||
class BarcodeParameter extends Parameter { | ||
@override | ||
String getName() => 'code'; | ||
|
||
@override | ||
String getValue() => codes.join(','); | ||
|
||
final List<String> codes; | ||
|
||
BarcodeParameter(final String code) : this.list([code]); | ||
|
||
const BarcodeParameter.list(this.codes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'package:openfoodfacts/interface/Parameter.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
|
||
/// Abstract map of [bool] as [Parameter]. | ||
/// | ||
/// Typical use-case with objects that have an on/off quality, | ||
/// like "with/without gluten". | ||
/// A query like "I want the products with eggs but without gluten" would be | ||
/// something like "{'eggs': true, 'gluten': false}". | ||
abstract class BoolMapParameter<T> extends Parameter { | ||
const BoolMapParameter({required this.map}); | ||
|
||
final Map<T, bool> map; | ||
|
||
@override | ||
String getValue() { | ||
final List<String> result = <String>[]; | ||
for (final MapEntry<T, bool> item in map.entries) { | ||
result.add(getTag(item.key, item.value)); | ||
} | ||
return result.join(','); | ||
} | ||
|
||
/// Returns the tag as on or off, like "gluten:with" or "gluten:without" | ||
@protected | ||
String getTag(final T key, final bool value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,14 @@ | ||
import 'package:openfoodfacts/interface/Parameter.dart'; | ||
import 'package:openfoodfacts/model/State.dart'; | ||
import 'package:openfoodfacts/model/parameter/BoolMapParameter.dart'; | ||
|
||
/// States Tags as completed or to-be-completed. | ||
class StatesTagsParameter extends Parameter { | ||
class StatesTagsParameter extends BoolMapParameter<State> { | ||
@override | ||
String getName() => 'states_tags'; | ||
|
||
@override | ||
String getValue() { | ||
final List<String> result = <String>[]; | ||
for (final MapEntry<State, bool> item in completed.entries) { | ||
result.add( | ||
item.value ? item.key.completedTag : item.key.toBeCompletedTag, | ||
); | ||
} | ||
return result.join(','); | ||
} | ||
String getTag(final State key, final bool value) => | ||
value ? key.completedTag : key.toBeCompletedTag; | ||
|
||
/// [State]s to be considered as completed (true) or to-be-completed (false). | ||
/// | ||
/// When we have several items, the results returned use a logical AND. | ||
final Map<State, bool> completed; | ||
|
||
StatesTagsParameter({required this.completed}); | ||
StatesTagsParameter({required final Map<State, bool> map}) : super(map: map); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.