Repository navigation
feat(nav3): [Part 2] Add the built-in SerialName mapper #6703
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
Open
0xadam-brown
wants to merge
1
commit into
feat/nav3-serial-name-mappers-6
Choose a base branch
from
feat/nav3-serial-name-mappers-7
base: feat/nav3-serial-name-mappers-6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,638
−42
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Versions for sentry-android-navigation3's kotlinx.serialization compatibility checks, registered as | ||
| # the `nav3Libs` catalog. | ||
| # | ||
| # Kept apart from libs.versions.toml so that the Nav3 R8 check workflow can run on PRs that change | ||
| # them without also running on every unrelated dependency bump. | ||
|
|
||
| [versions] | ||
| # The oldest kotlinx.serialization version sentry-android-navigation3 supports, which it compiles and | ||
| # tests against. Nav3 builds apply it explicitly rather than via version.ref to prevent Dependabot | ||
| # from modifying it. When updating, also update MIN_KOTLINX_SERIALIZATION_VERSION in | ||
| # KotlinxMapperDiagnostics (KotlinxBackStackEntryMapper.kt); a unit test fails if the two don't match. | ||
| kotlinxSerializationFloor = "1.7.3" | ||
|
|
||
| [libraries] | ||
| kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core" } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
sentry-android-navigation3/api/sentry-android-navigation3.api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...n/kotlin/io/sentry/compose/navigation3/internal/backstack/BuiltInBackStackEntryMappers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package io.sentry.compose.navigation3.internal.backstack | ||
|
|
||
| import androidx.annotation.VisibleForTesting | ||
| import io.sentry.ILogger | ||
| import io.sentry.ScopesAdapter | ||
| import io.sentry.SentryLevel.WARNING | ||
| import io.sentry.compose.navigation3.BackStackEntryMapper | ||
| import io.sentry.compose.navigation3.SentryBackStackEntry | ||
| import io.sentry.compose.navigation3.internal.backstack.NormalizedSentryBackStackEntry.Companion.UNKNOWN_ENTRY_NAME | ||
| import io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxBackStackEntryMapper | ||
| import io.sentry.compose.navigation3.internal.backstack.kotlinx.KotlinxMapperDiagnostics.Companion.INCOMPATIBLE_KOTLINX_ADVICE | ||
| import java.util.Collections | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| internal object BuiltInBackStackEntryMappers { | ||
|
|
||
| /** | ||
| * A built-in [BackStackEntryMapper] for apps that use kotlinx.serialization and only need to | ||
| * track the serial name of each of their back stack entries (and not each entry's properties). | ||
| * | ||
| * Falls back to [UnavailableBackStackEntryMapper] if a mapper can't be created. | ||
| * | ||
| * See [BackStackEntryMapper.SerialName] for more details. | ||
| */ | ||
| val serialName: BackStackEntryMapper<Any> by | ||
| lazy(LazyThreadSafetyMode.PUBLICATION) { | ||
| // PUBLICATION avoids holding a lock while mapper creation calls the app's logger. Racing | ||
| // threads may each create and probe a mapper and log warnings, and all but the first | ||
| // published instance would be discarded. | ||
| create(logger) { | ||
| KotlinxBackStackEntryMapper(logger = logger) | ||
| } | ||
| } | ||
|
|
||
| private val logger: () -> ILogger = { ScopesAdapter.getInstance().options.logger } | ||
|
|
||
| /** | ||
| * Returns the mapper created by [newKotlinxMapper], or an [UnavailableBackStackEntryMapper] if | ||
| * the mapper can't be linked or initialized. | ||
| */ | ||
| @VisibleForTesting | ||
| fun create( | ||
| logger: () -> ILogger, | ||
| newKotlinxMapper: () -> BackStackEntryMapper<Any>, | ||
| ): BackStackEntryMapper<Any> { | ||
| return try { | ||
| newKotlinxMapper() | ||
| } catch (e: LinkageError) { | ||
| // An incompatible kotlinx.serialization version can throw a LinkageError while the mapper is | ||
| // created, either because a class the mapper uses can't be linked or because the mapper's | ||
| // compatibility probe failed to look up names. We catch the error here so that every entry | ||
| // falls back to UNKNOWN_ENTRY_NAME instead of crashing the app. | ||
| // | ||
| // Construction runs no host app code, only kotlinx.serialization's and the SDK's, so this | ||
| // also catches an ExceptionInInitializerError from either. We fall back rather than crash, | ||
| // and log the error for diagnosis. | ||
| logger() | ||
| .log( | ||
| WARNING, | ||
| // The compiler inlines INCOMPATIBLE_KOTLINX_ADVICE, and it's use here doesn't access any | ||
| // kotlinx.serialization code. | ||
| "Nav3 built-in mapper creation failed. Using $UNKNOWN_ENTRY_NAME for all entries. See " + | ||
| "the attached error. $INCOMPATIBLE_KOTLINX_ADVICE", | ||
| e, | ||
| ) | ||
| UnavailableBackStackEntryMapper(logger) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A fallback for when a built-in [BackStackEntryMapper] isn't supported by the host app. | ||
| * | ||
| * Maps every back stack entry to `null`, which Sentry records as [UNKNOWN_ENTRY_NAME]. Logs once | ||
| * per entry type. | ||
| */ | ||
| internal class UnavailableBackStackEntryMapper( | ||
| private val logger: () -> ILogger, | ||
| ) : BackStackEntryMapper<Any> { | ||
|
|
||
| private val loggedTypes = Collections.newSetFromMap(ConcurrentHashMap<Class<*>, Boolean>()) | ||
|
|
||
| override fun map(backStackEntry: Any): SentryBackStackEntry? { | ||
| val type = backStackEntry.javaClass | ||
|
|
||
| if (loggedTypes.add(type)) { | ||
| logger() | ||
| .log( | ||
| WARNING, | ||
| "Nav3 mapper unavailable for type %s. Using $UNKNOWN_ENTRY_NAME. Use a custom " + | ||
| "BackStackEntryMapper.", | ||
| type.name, | ||
| ) | ||
| } | ||
|
|
||
| return null | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nav3.versions.tomlfile alongside ourlibs.versions.toml.It lets changes to the min supported kotlinx.serialization version automatically trigger the new R8 emulator tests (introduced in a later PR – not sure what my clanker was up to w/r/t these PR splits, but whatevs).
Those tests deliberately ignore changes confined to libs.versions.toml, since otherwise Dependabot’s frequent dependency bumps would each start an emulator run. GitHub’s path filters match whole files, so keeping the floor in that shared catalog would also exclude the changes we do need to test.
My clanker told me multiple version catalogs is a common convention, but chime in if you have concerns.
Alternatives