Skip to content

Commit 0e44a52

Browse files
committed
Fix inefficiency in the setCustomKeys Kotlin extension
1 parent 1b24b82 commit 0e44a52

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

firebase-crashlytics/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Unreleased
2-
2+
* [fixed] Fixed inefficiency in the Kotlin `FirebaseCrashlytics.setCustomKeys` extension.
33

44
# 19.2.1
55
* [changed] Updated protobuf dependency to `3.25.5` to fix

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/CrashlyticsTests.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ class CrashlyticsTests {
5959
Firebase.app.get(UserAgentPublisher::class.java)
6060
}
6161

62+
@Test
63+
fun keyValueBuilder() {
64+
val keyValueBuilder = KeyValueBuilder()
65+
keyValueBuilder.key("hello", "world")
66+
keyValueBuilder.key("hello2", 23)
67+
keyValueBuilder.key("hello3", 0.1)
68+
69+
val result: Map<String, String> = keyValueBuilder.build().keysAndValues
70+
71+
// TODO(mrober): Make unit tests for this.
72+
assertThat(result).isNotEmpty()
73+
}
74+
6275
companion object {
6376
private const val APP_ID = "1:1:android:1a"
6477
private const val API_KEY = "API-KEY-API-KEY-API-KEY-API-KEY-API-KEY"

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/FirebaseCrashlytics.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ val Firebase.crashlytics: FirebaseCrashlytics
2727
get() = FirebaseCrashlytics.getInstance()
2828

2929
/** Associates all key-value parameters with the reports */
30-
fun FirebaseCrashlytics.setCustomKeys(init: KeyValueBuilder.() -> Unit) {
31-
val builder = KeyValueBuilder(this)
32-
builder.init()
33-
}
30+
fun FirebaseCrashlytics.setCustomKeys(init: KeyValueBuilder.() -> Unit) =
31+
setCustomKeys(KeyValueBuilder().apply(init).build())
3432

3533
/** @suppress */
3634
@Keep

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/KeyValueBuilder.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,38 @@
1717
package com.google.firebase.crashlytics
1818

1919
/** Helper class to enable fluent syntax in [setCustomKeys] */
20-
class KeyValueBuilder(private val crashlytics: FirebaseCrashlytics) {
20+
class KeyValueBuilder internal constructor() {
21+
private val builder = CustomKeysAndValues.Builder()
22+
23+
internal fun build(): CustomKeysAndValues = builder.build()
2124

2225
/** Sets a custom key and value that are associated with reports. */
23-
fun key(key: String, value: Boolean) = crashlytics.setCustomKey(key, value)
26+
fun key(key: String, value: Boolean) {
27+
builder.putBoolean(key, value)
28+
}
2429

2530
/** Sets a custom key and value that are associated with reports. */
26-
fun key(key: String, value: Double) = crashlytics.setCustomKey(key, value)
31+
fun key(key: String, value: Double) {
32+
builder.putDouble(key, value)
33+
}
2734

2835
/** Sets a custom key and value that are associated with reports. */
29-
fun key(key: String, value: Float) = crashlytics.setCustomKey(key, value)
36+
fun key(key: String, value: Float) {
37+
builder.putFloat(key, value)
38+
}
3039

3140
/** Sets a custom key and value that are associated with reports. */
32-
fun key(key: String, value: Int) = crashlytics.setCustomKey(key, value)
41+
fun key(key: String, value: Int) {
42+
builder.putInt(key, value)
43+
}
3344

3445
/** Sets a custom key and value that are associated with reports. */
35-
fun key(key: String, value: Long) = crashlytics.setCustomKey(key, value)
46+
fun key(key: String, value: Long) {
47+
builder.putLong(key, value)
48+
}
3649

3750
/** Sets a custom key and value that are associated with reports. */
38-
fun key(key: String, value: String) = crashlytics.setCustomKey(key, value)
51+
fun key(key: String, value: String) {
52+
builder.putString(key, value)
53+
}
3954
}

0 commit comments

Comments
 (0)