Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to get OpenFoodFactsCountry from countryCode #314

Merged
merged 6 commits into from Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/openfoodfacts.dart
@@ -1,7 +1,7 @@
library openfoodfacts;

import 'dart:convert';
import 'dart:async';
import 'dart:convert';
import 'dart:developer';

import 'package:http/http.dart';
Expand Down Expand Up @@ -31,15 +31,14 @@ import 'package:openfoodfacts/utils/TaxonomyQueryConfiguration.dart';
import 'package:openfoodfacts/utils/UriHelper.dart';

import 'model/Insight.dart';
import 'model/RobotoffQuestion.dart';
import 'model/SendImage.dart';
import 'model/Product.dart';
import 'model/ProductResult.dart';
import 'model/RobotoffQuestion.dart';
import 'model/SearchResult.dart';
import 'model/SendImage.dart';
import 'model/SpellingCorrections.dart';
import 'model/Status.dart';
import 'model/User.dart';

import 'utils/HttpHelper.dart';
import 'utils/LanguageHelper.dart';
import 'utils/ProductHelper.dart';
Expand All @@ -50,7 +49,6 @@ export 'interface/Parameter.dart';
export 'model/Additives.dart';
export 'model/Ingredient.dart';
export 'model/Insight.dart';
export 'model/TaxonomyCategory.dart';
export 'model/Product.dart';
export 'model/ProductImage.dart';
export 'model/ProductResult.dart';
Expand All @@ -60,13 +58,13 @@ export 'model/SendImage.dart';
export 'model/SpellingCorrections.dart';
export 'model/Status.dart';
export 'model/TagI18N.dart';
export 'model/TaxonomyCategory.dart';
export 'model/User.dart';
export 'model/parameter/OutputFormat.dart';
export 'model/parameter/Page.dart';
export 'model/parameter/PageSize.dart';
export 'model/parameter/SearchSimple.dart';
export 'model/parameter/SortBy.dart';

export 'utils/HttpHelper.dart';
export 'utils/ImageHelper.dart';
export 'utils/JsonHelper.dart';
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/CountryHelper.dart
Expand Up @@ -1004,3 +1004,12 @@ extension OpenFoodFactsCoutryExtension on OpenFoodFactsCountry {

String get iso2Code => _ISO_2_CODES[this]!;
}

/// Helper class around [OpenFoodFactsCountry]
class CountryHelper {
/// Converts an ISO 2 code into an [OpenFoodFactsCountry]
static OpenFoodFactsCountry? fromJson(String? code) =>
OpenFoodFactsCountry.values.firstWhere(
(final OpenFoodFactsCountry country) => country.iso2Code == code,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not work for an unknown code: crashes, instead of an expected null.
firstWhere is not adequate, as it never returns null.
Your previous syntax was correct (it's just that the return; was useless).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know that it theoretically worked but it didn't make sense since the extension only worked for specific values of the enum, not for the enum itself, so there you would have to write OpenFoodFactsCountry.FRANCE.fromCountryCode('de') which would return germany, that's why I moved it and now the structure is similar to the one in the OpenFoodFactsLanguage.

Regarding the null problem, it's not possible to return null from orElse so we could either add a UNKNOW value to the enum as in the OpenFoodFactsLanguage but I would rather just switch back to the old forEach loop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, static methods don't seem to be allowed in extensions.

Similarly to what you wrote initially, and which seems to work:

class CountryHelper {
  static OpenFoodFactsCountry? fromJson(String? code) {
    OpenFoodFactsCountry? result;
    OpenFoodFactsCountry.values.forEach((final OpenFoodFactsCountry key) {
      if (key.iso2Code == code) {
        result = key;
      }
    });
    return result;
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even better (I don't find the dart map methods always 100% relevant):

class CountryHelper {
  static OpenFoodFactsCountry? fromJson(String? code) {
    if (code == null) {
      return null;
    }
    for (final OpenFoodFactsCountry key in OpenFoodFactsCountry.values) {
      if (key.iso2Code == code) {
        return key;
      }
    }
    return null;
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it, good suggestion

}