Skip to content

Commit

Permalink
fix!: fix for the uk/gb country code case
Browse files Browse the repository at this point in the history
Impacted files:
* `country_helper.dart`: `fromOffTag` now includes a fix for the uk/gb case; breaking change - removed class `CountryHelper`
* `open_food_api_client.dart`: breaking change - `getOrderedNutrients` now uses parameter `OpenFoodFactsCountry country`
* `ordered_nutrient_test.dart`: minor refactoring
  • Loading branch information
monsieurtanuki committed Sep 7, 2023
1 parent a26ec2a commit 5849c0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/src/open_food_api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1084,14 +1084,14 @@ class OpenFoodAPIClient {
/// print(orderedNutrients.nutrients[10].name); // Vitamin A
/// ```
static Future<OrderedNutrients> getOrderedNutrients({
required final String? cc,
required final OpenFoodFactsCountry country,
required final OpenFoodFactsLanguage language,
final QueryType? queryType,
}) async =>
OrderedNutrients.fromJson(
HttpHelper().jsonDecode(
await getOrderedNutrientsJsonString(
country: CountryHelper.fromJson(cc)!,
country: country,
language: language,
),
),
Expand Down
20 changes: 6 additions & 14 deletions lib/src/utils/country_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -761,20 +761,12 @@ enum OpenFoodFactsCountry implements OffTagged {
final String offTag;

/// Returns the first [OpenFoodFactsCountry] that matches the [offTag].
static OpenFoodFactsCountry? fromOffTag(final String? offTag) =>
OffTagged.fromOffTag(offTag, OpenFoodFactsCountry.values)
as OpenFoodFactsCountry?;
}

/// Helper class around [OpenFoodFactsCountry]
class CountryHelper {
/// Converts an ISO 2 code into an [OpenFoodFactsCountry] (case-insensitive).
///
/// E.g. 'fr' and 'FR' will give the same result: OpenFoodFactsCountry.FRANCE.
static OpenFoodFactsCountry? fromJson(String? code) {
if (code == null) {
return null;
static OpenFoodFactsCountry? fromOffTag(final String? offTag) {
// special case as we use 'uk' in off-dart
if (offTag == 'gb') {
return OpenFoodFactsCountry.UNITED_KINGDOM;
}
return OpenFoodFactsCountry.fromOffTag(code.toLowerCase());
return OffTagged.fromOffTag(offTag, OpenFoodFactsCountry.values)
as OpenFoodFactsCountry?;
}
}
12 changes: 8 additions & 4 deletions test/ordered_nutrient_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void main() {
for (final OpenFoodFactsCountry country in countries) {
checkNutrients(
await OpenFoodAPIClient.getOrderedNutrients(
cc: country.offTag,
country: country,
language: language,
),
country,
Expand All @@ -191,12 +191,16 @@ void main() {
OpenFoodFactsLanguage.ENGLISH: 'Energy',
OpenFoodFactsLanguage.PORTUGUESE: 'Energia',
};
const Set<String> countries = {'us', 'it', 'br'};
const Set<OpenFoodFactsCountry> countries = {
OpenFoodFactsCountry.USA,
OpenFoodFactsCountry.ITALY,
OpenFoodFactsCountry.BRAZIL,
};
for (final OpenFoodFactsLanguage language in energies.keys) {
for (final String country in countries) {
for (final OpenFoodFactsCountry country in countries) {
final OrderedNutrients orderedNutrients =
await OpenFoodAPIClient.getOrderedNutrients(
cc: country,
country: country,
language: language,
);
final OrderedNutrient? found =
Expand Down

0 comments on commit 5849c0a

Please sign in to comment.