Skip to content

Commit

Permalink
fix: allow logEvent to accept items inside parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitadol committed Sep 29, 2024
1 parent 15ab6ab commit 9332cf4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class FirebaseAnalytics extends FirebasePluginPlatform {
}) async {
_logEventNameValidation(name);

_assertParameterTypesAreCorrect(parameters);
_assertParameterTypesAreCorrect(
parameters,
allowItems: true,
);

await _delegate.logEvent(
name: name,
Expand Down Expand Up @@ -1385,14 +1388,37 @@ List<Map<String, dynamic>>? _marshalItems(List<AnalyticsEventItem>? items) {
}

void _assertParameterTypesAreCorrect(
Map<String, Object>? parameters,
) =>
parameters?.forEach((key, value) {
Map<String, Object>? parameters, {
bool allowItems = false,
}) {
if (parameters == null) {
return;
}

for (final MapEntry(:key, :value) in parameters.entries) {
if (allowItems && key == _ITEMS) {
assert(value is List, "$_ITEMS must be a 'List'");

if (value is List) {
for (final item in value) {
assert(
item is Map<String, Object>,
"item in $_ITEMS must be a 'Map<String, Object>'",
);

if (item is Map<String, Object>) {
_assertParameterTypesAreCorrect(item);
}
}
}
} else {
assert(
value is String || value is num,
"'string' OR 'number' must be set as the value of the parameter: $key. $value found instead",
);
});
}
}
}

void _assertItemsParameterTypesAreCorrect(List<AnalyticsEventItem>? items) =>
items?.forEach((item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,74 @@ void main() {
expect(analytics!.logEvent(name: 'firebase_foo'), throwsArgumentError);
});

test('reject events with wrong param value', () async {
expect(
() => analytics!.logEvent(
name: 'test_event',
parameters: {
'a': true,
},
),
throwsAssertionError,
);
});

test('reject events with wrong param items', () async {
expect(
() => analytics!.logEvent(
name: 'test_event',
parameters: {
'items': true,
},
),
throwsAssertionError,
);
});

test('reject events with wrong param items values', () async {
expect(
() => analytics!.logEvent(
name: 'test_event',
parameters: {
'items': [
{
'a': 123,
},
true,
],
},
),
throwsAssertionError,
);
});

test('reject events with wrong param items items', () async {
expect(
() => analytics!.logEvent(
name: 'test_event',
parameters: {
'items': [
{
'items': [],
},
],
},
),
throwsAssertionError,
);
});

test('custom event with correct parameters', () async {
await analytics!.logEvent(
name: 'test-event',
parameters: {'a': 'b'},
parameters: {
'a': 'b',
'items': [
{
'c': 'd',
},
],
},
);
expect(
methodCallLog,
Expand All @@ -148,7 +212,14 @@ void main() {
'Analytics#logEvent',
arguments: {
'eventName': 'test-event',
'parameters': {'a': 'b'},
'parameters': {
'a': 'b',
'items': [
{
'c': 'd',
},
],
},
},
),
],
Expand Down

0 comments on commit 9332cf4

Please sign in to comment.