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: #132 - new method OpenFoodAPIClient.addProductFields #436

Closed
wants to merge 2 commits into from

Conversation

monsieurtanuki
Copy link
Contributor

New files:

  • AddableProductFields.dart: Fields of a Product that can be simply added.
  • api_addProductFields_test.dart: Unit/Integration tests around OpenFoodAPIClient.addProductFields

Impacted file:

  • openfoodfacts.dart: new method addProductFields

What

  • new method OpenFoodAPIClient.addProductFields

Part of

New files:
* `AddableProductFields.dart`: Fields of a `Product` that can be simply added.
* `api_addProductFields_test.dart`: Unit/Integration tests around `OpenFoodAPIClient.addProductFields`

Impacted file:
* `openfoodfacts.dart`: new method `addProductFields`
@monsieurtanuki monsieurtanuki requested a review from a team as a code owner April 9, 2022 07:22
lib/openfoodfacts.dart Outdated Show resolved Hide resolved
lib/utils/AddableProductFields.dart Outdated Show resolved Hide resolved
test/api_addProductFields_test.dart Outdated Show resolved Hide resolved
Impacted files:
* `AddableProductFields.dart`: added suggested exception.
* `api_addProductFields_test.dart`: added Unit/Integration tests for stores and countries.
* `openfoodfacts.dart`: replaced exception with suggested label.
@monsieurtanuki
Copy link
Contributor Author

@g123k ping

}

final Map<String, String> parameterMap = <String, String>{};
parameterMap.addAll(user.toData());
Copy link
Contributor

Choose a reason for hiding this comment

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

By curiosity, why don't you call Map.from(user.toData)?

_checkStatus(status);

// cumulative list
String expectedValue = _getValue(field, product)!;
Copy link
Contributor

@g123k g123k Apr 10, 2022

Choose a reason for hiding this comment

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

A StringBuffer would be better, since you concatenate strings multiple times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@g123k StringBuffer is never used in the whole project. Here we're talking about adding a String to a String twice, and checking the resulting value 3 times (that would mean 3 additional toString() calls with StringBuffer). In a test. That makes async calls to the back-end that take longer time than a String allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@g123k I had a hunch about StringBuffer not being significantly faster than String when you only concatenate two Strings and convert each time the result as a String.
I've coded a test with the 2 solutions: with String and with StringBuffer.

test code
      const String initialString = "init";
      const List<String> additionalValues = <String>['djobi', 'djoba'];
      final List<String> result = <String>[];

      late int start;
      late int end;

      result.clear();
      start = DateTime.now().microsecondsSinceEpoch;
      final StringBuffer asStringBuffer = StringBuffer(initialString);
      result.add(asStringBuffer.toString());
      for (final String additionalValue in additionalValues) {
        asStringBuffer.write(', $additionalValue');
        result.add(asStringBuffer.toString());
      }
      end = DateTime.now().microsecondsSinceEpoch;
      print('${end - start}: $result');

      result.clear();
      start = DateTime.now().microsecondsSinceEpoch;
      String asString = initialString;
      result.add(asString);
      for (final String additionalValue in additionalValues) {
        asString += ', $additionalValue';
        result.add(asString);
      }
      end = DateTime.now().microsecondsSinceEpoch;
      print('${end - start}: $result');

After several runs, I've noticed that the first block run is a bit slower than the other - regardless of StringBuffer and String. By approximately 1ms.
Can you afford that 1ms uncertainty for tests? I think so. If that's OK with you, please approve my PR. If you think there is a hidden performance potential, don't hesitate to keep this PR rejected and to run performance tests.

@monsieurtanuki
Copy link
Contributor Author

@g123k "Requested changes" don't look like an appropriate term to me in that case: "By curiosity", "would be better". We're talking about mere comments.
At least the way I see things, "Requested changes" is a bazooka when something problematic happens in the code, like obvious use-cases not taken into account or daft copy/pastes.

@g123k
Copy link
Contributor

g123k commented Apr 10, 2022

@g123k "Requested changes" don't look like an appropriate term to me in that case: "By curiosity", "would be better". We're talking about mere comments. At least the way I see things, "Requested changes" is a bazooka when something problematic happens in the code, like obvious use-cases not taken into account or daft copy/pastes.

True, but this one requires a change

@M123-dev
Copy link
Member

Ready to merge?

@monsieurtanuki
Copy link
Contributor Author

I'm afraid not.

@monsieurtanuki monsieurtanuki self-assigned this Apr 19, 2022
@monsieurtanuki monsieurtanuki linked an issue Apr 19, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Add support for non-destructive value addition
4 participants