Skip to content

Commit

Permalink
fix!: fix for the uk/gb country code case (#795)
Browse files Browse the repository at this point in the history
* fix!: fix for the uk/gb country code case

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

* fix!: fix for the uk/gb country code case

New file:
* `api_off_tag_test.dart`: Tests about OffTagged enums.

Impacted file:
* `country_helper.dart`: case-insensitive search.
  • Loading branch information
monsieurtanuki committed Sep 7, 2023
1 parent 0bd8f90 commit fc2e907
Show file tree
Hide file tree
Showing 4 changed files with 45 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
24 changes: 10 additions & 14 deletions lib/src/utils/country_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,21 +760,17 @@ enum OpenFoodFactsCountry implements OffTagged {
@override
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).
/// Returns the [OpenFoodFactsCountry] that matches the [offTag].
///
/// E.g. 'fr' and 'FR' will give the same result: OpenFoodFactsCountry.FRANCE.
static OpenFoodFactsCountry? fromJson(String? code) {
if (code == null) {
return null;
/// Case-insensitive.
/// Special case: "uk" and "gb" both mean United Kingdom.
static OpenFoodFactsCountry? fromOffTag(String? offTag) {
offTag = offTag?.toLowerCase();
// 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?;
}
}
25 changes: 25 additions & 0 deletions test/api_off_tag_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:test/test.dart';

/// Tests about [OffTagged] `enum`s.
void main() {
test('$OpenFoodAPIClient country', () async {
const Map<String?, OpenFoodFactsCountry?> expectedCountries =
<String?, OpenFoodFactsCountry?>{
'fr': OpenFoodFactsCountry.FRANCE,
'FR': OpenFoodFactsCountry.FRANCE,
'fR': OpenFoodFactsCountry.FRANCE,
'Fr': OpenFoodFactsCountry.FRANCE,
'uk': OpenFoodFactsCountry.UNITED_KINGDOM,
'UK': OpenFoodFactsCountry.UNITED_KINGDOM,
'gb': OpenFoodFactsCountry.UNITED_KINGDOM,
'GB': OpenFoodFactsCountry.UNITED_KINGDOM,
null: null,
'this is not a country code': null,
};
for (final MapEntry<String?, OpenFoodFactsCountry?> entry
in expectedCountries.entries) {
expect(OpenFoodFactsCountry.fromOffTag(entry.key), entry.value);
}
});
}
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 fc2e907

Please sign in to comment.