From 1486cbf97bee38d83cda75c5b1074d72a7350f23 Mon Sep 17 00:00:00 2001 From: Chiara De Guglielmo Date: Wed, 16 Apr 2025 00:27:28 +0200 Subject: [PATCH 1/2] [gis_web] Make maybeEnum more robust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #166548 This change improves the robustness of the `maybeEnum` method in the `gis_web` plugin. Previously, the method would throw an exception if it encountered an unknown value. Now, it returns `null` instead, aligning the behavior with the expectations outlined in the issue. I chose not to add logging for unknown values at this time in order to keep the implementation consistent with the similar method in `google_adsense`. Additionally, I was unsure what the best logging strategy would be, and I'm not fully convinced that logging unknown enum values is always beneficial, especially given the fluidity of the GIS API. No repository tests were modified, as the change was minimal and focused. I verified the behavior manually by feeding various inputs to the `maybeEnum` function and checking that the output matched expectations. I reviewed the implementation referenced in the issue, but I deliberately avoided using an extension method for consistency with the Flutter repository's style guide. Let me know if adding logs or converting this into an extension would be preferred, and I’d be happy to revisit. --- .../lib/src/js_interop/shared.dart | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/google_identity_services_web/lib/src/js_interop/shared.dart b/packages/google_identity_services_web/lib/src/js_interop/shared.dart index a56450383261..111706b7368e 100644 --- a/packages/google_identity_services_web/lib/src/js_interop/shared.dart +++ b/packages/google_identity_services_web/lib/src/js_interop/shared.dart @@ -2,12 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -/// Attempts to retrieve an enum value from [haystack] if [needle] is not null. + +/// Attempts to retrieve the enum value from [haystack] whose name matches [needle], or returns `null` if not found. +/// +/// This is a safe utility method that performs a reverse lookup on an enum +/// by comparing the string [needle] with each value’s [Enum.name]. +/// +/// Example: +/// ```dart +/// enum AuthMethod { google, facebook, apple } +/// +/// final result = maybeEnum('google', AuthMethod.values); // AuthMethod.google +/// final invalid = maybeEnum('github', AuthMethod.values); // null +/// ``` T? maybeEnum(String? needle, List haystack) { - if (needle == null) { - return null; + if (needle == null) return null; + for (final T value in haystack) { + if (value.name == needle) { + return value; + } } - return haystack.byName(needle); + return null; } /// The type of several functions from the library, that don't receive From e90435c5bb2e514f339fad4634e43106c6bc58d5 Mon Sep 17 00:00:00 2001 From: Chiara De Guglielmo Date: Thu, 17 Apr 2025 18:57:23 +0200 Subject: [PATCH 2/2] [gis_web] Add unit tests for maybeEnum --- .../test/maybe_enum_test.dart | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 packages/google_identity_services_web/test/maybe_enum_test.dart diff --git a/packages/google_identity_services_web/test/maybe_enum_test.dart b/packages/google_identity_services_web/test/maybe_enum_test.dart new file mode 100644 index 000000000000..f3be5e6c7d4a --- /dev/null +++ b/packages/google_identity_services_web/test/maybe_enum_test.dart @@ -0,0 +1,20 @@ +import 'package:google_identity_services_web/src/js_interop/shared.dart'; +import 'package:test/test.dart'; + +enum AuthMethod { google, facebook, apple } + +void main() { + group('maybeEnum', () { + test('returns correct enum when match is found', () { + expect(maybeEnum('google', AuthMethod.values), AuthMethod.google); + }); + + test('returns null when needle is null', () { + expect(maybeEnum(null, AuthMethod.values), isNull); + }); + + test('returns null when needle does not match', () { + expect(maybeEnum('github', AuthMethod.values), isNull); + }); + }); +}