-
Notifications
You must be signed in to change notification settings - Fork 122
Add logic for Analytics parameters with maps #1660
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
Changes from 9 commits
a2ab4dc
fb6f866
8d93389
eb775e1
25de448
15e076c
9681ec3
04adec0
6caa93f
3c6eb9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,6 +231,69 @@ void LogEvent(const char* name) { | |
[FIRAnalytics logEventWithName:@(name) parameters:@{}]; | ||
} | ||
|
||
// Declared here so that it can be used, defined below. | ||
NSDictionary* MapToDictionary(const std::map<Variant, Variant>& map); | ||
|
||
// Converts the given vector into an ObjC NSArray of ObjC objects. | ||
NSArray* VectorToArray(const std::vector<Variant>& vector) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (and the Android version above) seem slightly misnamed since they are specifically for converting a vector of maps to an array There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair, renamed to VectorOfMapsToArray. Originally on iOS I converted everything, but on Android that isn't as feasible with how the ArrayList works, and then looking over the parameters the only times Arrays are used are with Dictionaries, so went with that. |
||
NSMutableArray* array = [NSMutableArray arrayWithCapacity:vector.size()]; | ||
for (const Variant& element : vector) { | ||
if (element.is_map()) { | ||
NSDictionary* dict = MapToDictionary(element.map()); | ||
[array addObject:dict]; | ||
} else { | ||
LogError("VectorToArray: Unsupported type (%s) within vector.", | ||
Variant::TypeName(element.type())); | ||
} | ||
} | ||
return array; | ||
} | ||
|
||
// Converts and adds the Variant to the given Dictionary. | ||
bool AddVariantToDictionary(NSMutableDictionary* dict, NSString* key, const Variant& value) { | ||
if (value.is_int64()) { | ||
[dict setObject:[NSNumber numberWithLongLong:value.int64_value()] forKey:key]; | ||
} else if (value.is_double()) { | ||
[dict setObject:[NSNumber numberWithDouble:value.double_value()] forKey:key]; | ||
} else if (value.is_string()) { | ||
[dict setObject:SafeString(value.string_value()) forKey:key]; | ||
} else if (value.is_bool()) { | ||
// Just use integer 0 or 1. | ||
[dict setObject:[NSNumber numberWithLongLong:value.bool_value() ? 1 : 0] forKey:key]; | ||
} else if (value.is_null()) { | ||
// Just use integer 0 for null. | ||
[dict setObject:[NSNumber numberWithLongLong:0] forKey:key]; | ||
} else if (value.is_vector()) { | ||
NSArray* array = VectorToArray(value.vector()); | ||
[dict setObject:array forKey:key]; | ||
} else if (value.is_map()) { | ||
NSDictionary* inner_dict = MapToDictionary(value.map()); | ||
[dict setObject:inner_dict forKey:key]; | ||
} else { | ||
// A Variant type that couldn't be handled was passed in. | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
// Converts the given map into an ObjC dictionary of ObjC objects. | ||
NSDictionary* MapToDictionary(const std::map<Variant, Variant>& map) { | ||
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:map.size()]; | ||
for (const auto& pair : map) { | ||
// Only add elements that use a string key | ||
if (!pair.first.is_string()) { | ||
continue; | ||
} | ||
NSString* key = SafeString(pair.first.string_value()); | ||
const Variant& value = pair.second; | ||
if (!AddVariantToDictionary(dict, key, value)) { | ||
LogError("MapToDictionary: Unsupported type (%s) within map with key %s.", | ||
Variant::TypeName(value.type()), key); | ||
} | ||
} | ||
return dict; | ||
} | ||
|
||
// Log an event with associated parameters. | ||
void LogEvent(const char* name, const Parameter* parameters, size_t number_of_parameters) { | ||
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized()); | ||
|
@@ -239,25 +302,10 @@ void LogEvent(const char* name, const Parameter* parameters, size_t number_of_pa | |
for (size_t i = 0; i < number_of_parameters; ++i) { | ||
const Parameter& parameter = parameters[i]; | ||
NSString* parameter_name = SafeString(parameter.name); | ||
if (parameter.value.is_int64()) { | ||
[parameters_dict setObject:[NSNumber numberWithLongLong:parameter.value.int64_value()] | ||
forKey:parameter_name]; | ||
} else if (parameter.value.is_double()) { | ||
[parameters_dict setObject:[NSNumber numberWithDouble:parameter.value.double_value()] | ||
forKey:parameter_name]; | ||
} else if (parameter.value.is_string()) { | ||
[parameters_dict setObject:SafeString(parameter.value.string_value()) forKey:parameter_name]; | ||
} else if (parameter.value.is_bool()) { | ||
// Just use integer 0 or 1. | ||
[parameters_dict setObject:[NSNumber numberWithLongLong:parameter.value.bool_value() ? 1 : 0] | ||
forKey:parameter_name]; | ||
} else if (parameter.value.is_null()) { | ||
// Just use integer 0 for null. | ||
[parameters_dict setObject:[NSNumber numberWithLongLong:0] forKey:parameter_name]; | ||
} else { | ||
// Vector or Map were passed in. | ||
if (!AddVariantToDictionary(parameters_dict, parameter_name, parameter.value)) { | ||
// A Variant type that couldn't be handled was passed in. | ||
LogError("LogEvent(%s): %s is not a valid parameter value type. " | ||
"Container types are not allowed. No event was logged.", | ||
"No event was logged.", | ||
parameter.name, Variant::TypeName(parameter.value.type())); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just making sure there are no numeric keys allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it is currently only strings, and I doubt that'll change any time soon.