From f10c5e89acd1e6a651fca6077e69c0a98e79ae66 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Fri, 24 May 2024 11:33:43 -0700 Subject: [PATCH] Polymorphic support --- .../snapshots/SnapshotImageMetadata.kt | 59 ++++++++++--------- .../emergetools/snapshots/SnapshotSaver.kt | 35 ++++++----- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt index 20415c85..06c84523 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotImageMetadata.kt @@ -1,7 +1,9 @@ package com.emergetools.snapshots import com.emergetools.snapshots.shared.ComposePreviewSnapshotConfig +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonClassDiscriminator enum class SnapshotType { COMPOSABLE, @@ -9,38 +11,41 @@ enum class SnapshotType { ACTIVITY, } +@OptIn(ExperimentalSerializationApi::class) +@Serializable +@JsonClassDiscriminator("metadataType") sealed class SnapshotMetadata { abstract val name: String abstract val displayName: String? abstract val fqn: String abstract val type: SnapshotType abstract val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? -} -@Serializable -internal data class SnapshotImageMetadata( - // Used as the primary key - override val name: String, - // User defined name, or set to defaults by our backend - override val displayName: String?, - // Filename of the outputted image - val filename: String, - // FQN of the test class - override val fqn: String, - override val type: SnapshotType, - // Compose-specific metadata, only set if type == COMPOSABLE - override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, -) : SnapshotMetadata() + @Serializable + internal class SuccessMetadata( + // Used as the primary key + override val name: String, + // User defined name, or set to defaults by our backend + override val displayName: String?, + // Filename of the outputted image + val filename: String, + // FQN of the test class + override val fqn: String, + override val type: SnapshotType, + // Compose-specific metadata, only set if type == COMPOSABLE + override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, + ) : SnapshotMetadata() -@Serializable -internal data class SnapshotErrorMetadata( - // Used as the primary key - override val name: String, - // User defined name, or set to defaults by our backend - override val displayName: String?, - // FQN of the test class - override val fqn: String, - override val type: SnapshotType, - // Compose-specific metadata, only set if type == COMPOSABLE - override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, -) : SnapshotMetadata() + @Serializable + internal class ErrorMetadata( + // Used as the primary key + override val name: String, + // User defined name, or set to defaults by our backend + override val displayName: String?, + // FQN of the test class + override val fqn: String, + override val type: SnapshotType, + // Compose-specific metadata, only set if type == COMPOSABLE + override val composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, + ) : SnapshotMetadata() +} diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt index 63e6bbde..dd8fe88d 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/SnapshotSaver.kt @@ -31,6 +31,10 @@ internal object SnapshotSaver { private val args: Bundle get() = InstrumentationRegistry.getArguments() + private val saveMetadata: Boolean + get() = args.getBoolean(ARG_KEY_SAVE_METADATA, false) || + args.getString(ARG_KEY_SAVE_METADATA, "false").toBoolean() + fun save( displayName: String?, bitmap: Bitmap, @@ -56,10 +60,7 @@ internal object SnapshotSaver { keyName = keyName, bitmap = bitmap ) - if ( - args.getBoolean(ARG_KEY_SAVE_METADATA, false) || - args.getString(ARG_KEY_SAVE_METADATA, "false").toBoolean() - ) { + if (saveMetadata) { saveMetadata( snapshotsDir = snapshotsDir, displayName = displayName, @@ -90,14 +91,16 @@ internal object SnapshotSaver { displayName = displayName, composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) - saveErrorMetadata( - snapshotsDir = snapshotsDir, - displayName = displayName, - keyName = keyName, - type = type, - fqn = fqn, - composePreviewSnapshotConfig = composePreviewSnapshotConfig, - ) + if (saveMetadata) { + saveErrorMetadata( + snapshotsDir = snapshotsDir, + displayName = displayName, + keyName = keyName, + type = type, + fqn = fqn, + composePreviewSnapshotConfig = composePreviewSnapshotConfig, + ) + } } private fun saveImage( @@ -118,10 +121,8 @@ internal object SnapshotSaver { type: SnapshotType, composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) { - val metadata = SnapshotImageMetadata( + val metadata: SnapshotMetadata = SnapshotMetadata.SuccessMetadata( name = keyName, - // TODO: Ryan remove in future - keyName = keyName, displayName = displayName, filename = "$keyName$PNG_EXTENSION", fqn = fqn, @@ -129,6 +130,7 @@ internal object SnapshotSaver { composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) + Log.d(TAG, "Saving error metadata for $keyName") val jsonString = Json.encodeToString(metadata) saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") { @@ -144,7 +146,7 @@ internal object SnapshotSaver { type: SnapshotType, composePreviewSnapshotConfig: ComposePreviewSnapshotConfig? = null, ) { - val metadata = SnapshotErrorMetadata( + val metadata: SnapshotMetadata = SnapshotMetadata.ErrorMetadata( name = keyName, displayName = displayName, fqn = fqn, @@ -152,6 +154,7 @@ internal object SnapshotSaver { composePreviewSnapshotConfig = composePreviewSnapshotConfig, ) + Log.d(TAG, "Saving error metadata for $keyName") val jsonString = Json.encodeToString(metadata) saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") {