Skip to content
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

RUMM-2659 editable additional attributes #1089

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.squareup.kotlinpoet.ARRAY
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MAP
import com.squareup.kotlinpoet.MUTABLE_MAP
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
Expand Down Expand Up @@ -310,7 +310,7 @@ class ClassGenerator(
val mapType = definition.additionalProperties.additionalPropertyType(rootTypeName)
constructorBuilder.addParameter(
ParameterSpec.builder(Identifier.PARAM_ADDITIONAL_PROPS, mapType)
.defaultValue("emptyMap()")
.defaultValue("mutableMapOf()")
.build()
)
}
Expand Down Expand Up @@ -447,7 +447,7 @@ class ClassGenerator(
} else {
ANY.copy(nullable = true)
}
return MAP.parameterizedBy(STRING, valueType)
return MUTABLE_MAP.parameterizedBy(STRING, valueType)
}

// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ internal class CommentForgeryFactory : ForgeryFactory<Comment> {
ratings = forge.aNullable {
Comment.Ratings(
global = aLong(),
additionalProperties = aMap { anAlphabeticalString() to aLong() }
additionalProperties = aMap {
anAlphabeticalString() to aLong()
}.toMutableMap()
)
},
flags = forge.aNullable {
Comment.Flags(
additionalProperties = aMap { anAlphabeticalString() to aBool() }
additionalProperties = aMap {
anAlphabeticalString() to aBool()
}.toMutableMap()
)
},
tags = forge.aNullable {
Comment.Tags(
additionalProperties = aMap { anAlphabeticalString() to anHexadecimalString() }
additionalProperties = aMap {
anAlphabeticalString() to anHexadecimalString()
}.toMutableMap()
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ internal class CompanyForgeryFactory : ForgeryFactory<Company> {
ratings = forge.aNullable {
Company.Ratings(
global = aLong(),
additionalProperties = aMap { anAlphabeticalString() to aLong() }
additionalProperties = aMap {
anAlphabeticalString() to aLong()
}.toMutableMap()
)
},
information = forge.aNullable {
Company.Information(
forge.aNullable { forge.aLong() },
forge.aNullable { forge.aLong() },
additionalProperties = forge.aMap { anAlphabeticalString() to aMap { anHexadecimalString() to aLong() } }
additionalProperties = forge.aMap {
anAlphabeticalString() to aMap { anHexadecimalString() to aLong() }
}.toMutableMap()
)
},
additionalProperties = forge.aMap { anAlphabeticalString() to aNullable { anHexadecimalString() } }
additionalProperties = forge.aMap {
anAlphabeticalString() to aNullable { anHexadecimalString() }
}.toMutableMap()
)
}
}
8 changes: 4 additions & 4 deletions buildSrc/src/test/kotlin/com/example/model/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.Array
import kotlin.Boolean
import kotlin.Long
import kotlin.String
import kotlin.collections.Map
import kotlin.collections.MutableMap
import kotlin.jvm.JvmStatic
import kotlin.jvm.Throws

Expand Down Expand Up @@ -76,7 +76,7 @@ public data class Comment(

public data class Ratings(
public val global: Long,
public val additionalProperties: Map<String, Long> = emptyMap(),
public val additionalProperties: MutableMap<String, Long> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down Expand Up @@ -126,7 +126,7 @@ public data class Comment(
}

public data class Flags(
public val additionalProperties: Map<String, Boolean> = emptyMap(),
public val additionalProperties: MutableMap<String, Boolean> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down Expand Up @@ -168,7 +168,7 @@ public data class Comment(
}

public data class Tags(
public val additionalProperties: Map<String, String> = emptyMap(),
public val additionalProperties: MutableMap<String, String> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down
8 changes: 4 additions & 4 deletions buildSrc/src/test/kotlin/com/example/model/Company.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import kotlin.Any
import kotlin.Array
import kotlin.Long
import kotlin.String
import kotlin.collections.Map
import kotlin.collections.MutableMap
import kotlin.jvm.JvmStatic
import kotlin.jvm.Throws

public data class Company(
public val name: String? = null,
public val ratings: Ratings? = null,
public val information: Information? = null,
public val additionalProperties: Map<String, Any?> = emptyMap(),
public val additionalProperties: MutableMap<String, Any?> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down Expand Up @@ -84,7 +84,7 @@ public data class Company(

public data class Ratings(
public val global: Long,
public val additionalProperties: Map<String, Long> = emptyMap(),
public val additionalProperties: MutableMap<String, Long> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down Expand Up @@ -136,7 +136,7 @@ public data class Company(
public data class Information(
public val date: Long? = null,
public val priority: Long? = null,
public val additionalProperties: Map<String, Any?> = emptyMap(),
public val additionalProperties: MutableMap<String, Any?> = mutableMapOf(),
) {
public fun toJson(): JsonElement {
val json = JsonObject()
Expand Down
34 changes: 17 additions & 17 deletions dd-sdk-android/apiSurface
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ data class com.datadog.android.core.model.NetworkInfo
companion object
fun fromJson(kotlin.String): Connectivity
data class com.datadog.android.core.model.UserInfo
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): UserInfo
Expand Down Expand Up @@ -239,7 +239,7 @@ class com.datadog.android.log.Logger
fun removeTag(String)
fun removeTagsWithKey(String)
data class com.datadog.android.log.model.LogEvent
constructor(Status, kotlin.String, kotlin.String, kotlin.String, Logger, Usr? = null, Network? = null, Error? = null, kotlin.String, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(Status, kotlin.String, kotlin.String, kotlin.String, Logger, Usr? = null, Network? = null, Error? = null, kotlin.String, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): LogEvent
Expand All @@ -249,7 +249,7 @@ data class com.datadog.android.log.model.LogEvent
companion object
fun fromJson(kotlin.String): Logger
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -433,7 +433,7 @@ data class com.datadog.android.rum.model.ActionEvent
companion object
fun fromJson(kotlin.String): View
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -474,7 +474,7 @@ data class com.datadog.android.rum.model.ActionEvent
companion object
fun fromJson(kotlin.String): Dd
data class Context
constructor(kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Context
Expand Down Expand Up @@ -647,7 +647,7 @@ data class com.datadog.android.rum.model.ErrorEvent
companion object
fun fromJson(kotlin.String): View
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -688,7 +688,7 @@ data class com.datadog.android.rum.model.ErrorEvent
companion object
fun fromJson(kotlin.String): Dd
data class Context
constructor(kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Context
Expand Down Expand Up @@ -869,7 +869,7 @@ data class com.datadog.android.rum.model.LongTaskEvent
companion object
fun fromJson(kotlin.String): View
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -910,7 +910,7 @@ data class com.datadog.android.rum.model.LongTaskEvent
companion object
fun fromJson(kotlin.String): Dd
data class Context
constructor(kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Context
Expand Down Expand Up @@ -1021,7 +1021,7 @@ data class com.datadog.android.rum.model.ResourceEvent
companion object
fun fromJson(kotlin.String): View
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -1062,7 +1062,7 @@ data class com.datadog.android.rum.model.ResourceEvent
companion object
fun fromJson(kotlin.String): Dd
data class Context
constructor(kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Context
Expand Down Expand Up @@ -1259,7 +1259,7 @@ data class com.datadog.android.rum.model.ViewEvent
companion object
fun fromJson(kotlin.String): View
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down Expand Up @@ -1300,12 +1300,12 @@ data class com.datadog.android.rum.model.ViewEvent
companion object
fun fromJson(kotlin.String): Dd
data class Context
constructor(kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Context
data class CustomTimings
constructor(kotlin.collections.Map<kotlin.String, kotlin.Long> = emptyMap())
constructor(kotlin.collections.MutableMap<kotlin.String, kotlin.Long> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): CustomTimings
Expand Down Expand Up @@ -1676,12 +1676,12 @@ data class com.datadog.android.tracing.model.SpanEvent
companion object
fun fromJson(kotlin.String): SpanEvent
data class Metrics
constructor(kotlin.Long? = null, kotlin.collections.Map<kotlin.String, kotlin.Number> = emptyMap())
constructor(kotlin.Long? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Number> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Metrics
data class Meta
constructor(kotlin.String, Dd, Span, Tracer, Usr, Network, kotlin.collections.Map<kotlin.String, kotlin.String> = emptyMap())
constructor(kotlin.String, Dd, Span, Tracer, Usr, Network, kotlin.collections.MutableMap<kotlin.String, kotlin.String> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Meta
Expand All @@ -1700,7 +1700,7 @@ data class com.datadog.android.tracing.model.SpanEvent
companion object
fun fromJson(kotlin.String): Tracer
data class Usr
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.Map<kotlin.String, kotlin.Any?> = emptyMap())
constructor(kotlin.String? = null, kotlin.String? = null, kotlin.String? = null, kotlin.collections.MutableMap<kotlin.String, kotlin.Any?> = mutableMapOf())
fun toJson(): com.google.gson.JsonElement
companion object
fun fromJson(kotlin.String): Usr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ object Datadog {
id,
name,
email,
extraInfo
extraInfo.toMutableMap()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal interface DataConstraints {
keyPrefix: String? = null,
attributesGroupName: String? = null,
reservedKeys: Set<String> = emptySet()
): Map<String, T>
): MutableMap<String, T>

fun validateTags(tags: List<String>): List<String>

fun validateTimings(timings: Map<String, Long>): Map<String, Long>
fun validateTimings(timings: Map<String, Long>): MutableMap<String, Long>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.datadog.android.core.internal.constraints

import com.datadog.android.core.internal.utils.devLogger
import com.datadog.android.core.internal.utils.toMutableMap
import java.util.Locale

internal typealias StringTransform = (String) -> String?
Expand Down Expand Up @@ -37,7 +38,7 @@ internal class DatadogDataConstraints : DataConstraints {
keyPrefix: String?,
attributesGroupName: String?,
reservedKeys: Set<String>
): Map<String, T> {
): MutableMap<String, T> {
// prefix = "a.b" => dotCount = 1+1 ("a.b." + key)
val prefixDotCount = keyPrefix?.let { it.count { character -> character == '.' } + 1 } ?: 0
val convertedAttributes = attributes.mapNotNull {
Expand Down Expand Up @@ -69,10 +70,10 @@ internal class DatadogDataConstraints : DataConstraints {
)
devLogger.w(warningMessage)
}
return convertedAttributes.take(MAX_ATTR_COUNT).toMap()
return convertedAttributes.take(MAX_ATTR_COUNT).toMutableMap()
}

override fun validateTimings(timings: Map<String, Long>): Map<String, Long> {
override fun validateTimings(timings: Map<String, Long>): MutableMap<String, Long> {
return timings.mapKeys { entry ->
val sanitizedKey =
entry.key.replace(Regex("[^a-zA-Z0-9\\-_.@$]"), "_")
Expand All @@ -86,7 +87,7 @@ internal class DatadogDataConstraints : DataConstraints {
)
}
sanitizedKey
}
}.toMutableMap()
}

private fun resolveDiscardedAttrsWarning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,19 @@
package com.datadog.android.core.internal.utils

internal val NULL_MAP_VALUE = Object()

/**
* @return a new mutable map containing all key-value pairs from the given array of pairs.
*
* The returned map preserves the entry iteration order of the original array.
* If any of two pairs would have the same key the last one gets added to the map.
*/
internal fun <K, V> Iterable<Pair<K, V>>.toMutableMap(): MutableMap<K, V> {
return toMap(mutableMapOf())
}
/**
* @return the [MutableMap] if its not `null`, or the empty [MutableMap] otherwise.
*/
internal fun <K, V> MutableMap<K, V>?.orEmpty(): MutableMap<K, V> {
return this ?: mutableMapOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class LogEventSerializer(
}
return log.copy(
ddtags = sanitizedTags,
additionalProperties = sanitizedAttributes,
additionalProperties = sanitizedAttributes.toMutableMap(),
usr = usr
)
}
Expand Down
Loading