Skip to content

Commit

Permalink
fromLabel static method renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasyishak committed Mar 27, 2024
1 parent 760d9b7 commit 3977ae8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
22 changes: 6 additions & 16 deletions pkgs/unified_analytics/lib/src/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:collection/collection.dart';

/// The valid dash tool labels stored in the [DashTool] enum.
List<String> get validDashTools =>
DashTool.values.map((e) => e.label).toList()..sort();
Expand Down Expand Up @@ -156,13 +158,8 @@ enum DashEvent {

/// This takes in the string label for a given [DashEvent] and returns the
/// enum for that string label.
static DashEvent? getDashEventByLabel(String label) {
for (final event in DashEvent.values) {
if (event.label == label) return event;
}

return null;
}
static DashEvent? fromLabel(String label) =>
DashEvent.values.firstWhereOrNull((e) => e.label == label);
}

/// Officially-supported clients of this package as logical
Expand Down Expand Up @@ -209,15 +206,8 @@ enum DashTool {

/// This takes in the string label for a given [DashTool] and returns the
/// enum for that string label.
static DashTool getDashToolByLabel(String label) {
for (final tool in DashTool.values) {
if (tool.label == label) return tool;
}

throw Exception('The tool $label from the survey metadata file is not '
'a valid DashTool enum value\n'
'Valid labels for dash tools: ${validDashTools.join(', ')}');
}
static DashTool? fromLabel(String label) =>
DashTool.values.firstWhereOrNull((e) => e.label == label);
}

/// Enumerate options for platforms supported.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/unified_analytics/lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,9 @@ final class Event {
case {
'eventName': final String eventName,
'eventData': final Map<String, Object?> eventData,
} when DashEvent.getDashEventByLabel(eventName) != null) {
} when DashEvent.fromLabel(eventName) != null) {
return Event._(
eventName: DashEvent.getDashEventByLabel(eventName)!,
eventName: DashEvent.fromLabel(eventName)!,
eventData: eventData,
);
}
Expand Down
11 changes: 7 additions & 4 deletions pkgs/unified_analytics/lib/src/survey_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:convert';

import 'package:clock/clock.dart';
import 'package:collection/collection.dart';
import 'package:file/file.dart';
import 'package:http/http.dart' as http;

Expand Down Expand Up @@ -134,10 +135,12 @@ class Survey {
samplingRate = json['samplingRate'] is String
? double.parse(json['samplingRate'] as String)
: json['samplingRate'] as double,
excludeDashToolList =
(json['excludeDashTools'] as List<dynamic>).map((e) {
return DashTool.getDashToolByLabel(e as String);
}).toList(),
excludeDashToolList = (json['excludeDashTools'] as List<dynamic>)
.map((e) {
return DashTool.fromLabel(e as String);
})
.whereType<DashTool>()
.toList(),
conditionList = (json['conditions'] as List<dynamic>).map((e) {
return Condition.fromJson(e as Map<String, dynamic>);
}).toList(),
Expand Down

0 comments on commit 3977ae8

Please sign in to comment.