-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.dart
More file actions
144 lines (133 loc) · 5.06 KB
/
utils.dart
File metadata and controls
144 lines (133 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/// **************************************************************************
/// Copyright 2022, Optimizely, Inc. and contributors *
/// *
/// Licensed under the Apache License, Version 2.0 (the "License"); *
/// you may not use this file except in compliance with the License. *
/// You may obtain a copy of the License at *
/// *
/// http://www.apache.org/licenses/LICENSE-2.0 *
/// *
/// Unless required by applicable law or agreed to in writing, software *
/// distributed under the License is distributed on an "AS IS" BASIS, *
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
/// See the License for the specific language governing permissions and *
/// limitations under the License. *
///**************************************************************************/
import 'dart:io' show Platform;
import 'package:optimizely_flutter_sdk/src/user_context/optimizely_user_context.dart';
import 'package:optimizely_flutter_sdk/src/utils/constants.dart';
import 'package:optimizely_flutter_sdk/src/data_objects/log_level.dart';
class Utils {
static Map<OptimizelyDecideOption, String> decideOptions = {
OptimizelyDecideOption.disableDecisionEvent: "disableDecisionEvent",
OptimizelyDecideOption.enabledFlagsOnly: "enabledFlagsOnly",
OptimizelyDecideOption.ignoreUserProfileService: "ignoreUserProfileService",
OptimizelyDecideOption.includeReasons: "includeReasons",
OptimizelyDecideOption.excludeVariables: "excludeVariables",
};
static Map<OptimizelySegmentOption, String> segmentOptions = {
OptimizelySegmentOption.ignoreCache: "ignoreCache",
OptimizelySegmentOption.resetCache: "resetCache",
};
/// Converts a map to platform-specific typed format
///
/// On iOS, returns a typed map with type information for proper native conversion.
/// On Android, returns the original primitive map.
///
/// The [forceIOSFormat] parameter is used for testing purposes only to test
/// iOS format conversion without running on actual iOS platform.
static Map<String, dynamic> convertToTypedMap(
Map<String, dynamic> map, {
bool forceIOSFormat = false,
}) {
if (map.isEmpty) {
return map;
}
// Send type along with value so typecasting is easily possible (only for iOS)
Map<String, dynamic> typedMap = {};
// Only keep primitive values
Map<String, dynamic> primitiveMap = {};
for (MapEntry e in map.entries) {
dynamic processedValue = _processValue(e.value);
if (processedValue != null) {
primitiveMap[e.key] = e.value;
typedMap[e.key] = processedValue;
}
}
if (Platform.isIOS || forceIOSFormat) {
return typedMap;
}
return primitiveMap;
}
/// Recursively processes values to add type information for iOS
static dynamic _processValue(dynamic value) {
if (value is String) {
return {
Constants.value: value,
Constants.type: Constants.stringType
};
}
if (value is double) {
return {
Constants.value: value,
Constants.type: Constants.doubleType
};
}
if (value is int) {
return {
Constants.value: value,
Constants.type: Constants.intType
};
}
if (value is bool) {
return {
Constants.value: value,
Constants.type: Constants.boolType
};
}
if (value is Map) {
// Handle nested maps
Map<String, dynamic> nestedMap = {};
(value as Map).forEach((k, v) {
dynamic processedValue = _processValue(v);
if (processedValue != null) {
nestedMap[k.toString()] = processedValue;
}
});
return {
Constants.value: nestedMap,
Constants.type: Constants.mapType
};
}
if (value is List) {
// Handle arrays
List<dynamic> nestedList = [];
for (var item in value) {
dynamic processedValue = _processValue(item);
if (processedValue != null) {
nestedList.add(processedValue);
}
}
return {
Constants.value: nestedList,
Constants.type: Constants.listType
};
}
// ignore: avoid_print
print('Unsupported value type: ${value.runtimeType}');
return null;
}
static List<String> convertDecideOptions(
Set<OptimizelyDecideOption> options) {
return options.map((option) => Utils.decideOptions[option]!).toList();
}
static List<String> convertSegmentOptions(
Set<OptimizelySegmentOption> options) {
return options.map((option) => Utils.segmentOptions[option]!).toList();
}
static String convertLogLevel(OptimizelyLogLevel logLevel) {
// OptimizelyLogLevel.error -> "error"
// OptimizelyLogLevel.debug -> "debug"
return logLevel.toString().split('.').last;
}
}