Skip to content

Commit

Permalink
add casting utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed May 12, 2021
1 parent 841b83c commit 942c9a9
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ftl.domain.invoke
import ftl.presentation.cli.firebase.test.locale.toCliTable
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import ftl.util.asListOrNull
import picocli.CommandLine

@CommandLine.Command(
Expand All @@ -30,11 +31,6 @@ class AndroidLocalesListCommand :
)
override var configPath: String = FtlConstants.defaultAndroidConfig

override val out = outputLogger {
@Suppress("UNCHECKED_CAST")
(this as? List<Locale>)?.toCliTable() ?: throwUnknownType()
}

@CommandLine.Option(
names = ["-h", "--help"],
usageHelp = true,
Expand All @@ -43,4 +39,8 @@ class AndroidLocalesListCommand :
var usageHelpRequested: Boolean = false

override fun run() = invoke()

override val out = outputLogger {
asListOrNull<Locale>()?.toCliTable() ?: throwUnknownType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ftl.domain.ListAndroidOrientations
import ftl.domain.invoke
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import ftl.util.asListOrNull
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -39,7 +40,6 @@ class AndroidOrientationsListCommand :
override fun run() = invoke()

override val out = outputLogger {
@Suppress("UNCHECKED_CAST")
(this as? List<Orientation>)?.toCliTable() ?: throwUnknownType()
asListOrNull<Orientation>()?.toCliTable() ?: throwUnknownType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package ftl.presentation.cli.firebase.test.environment

import ftl.api.TestEnvironment
import ftl.environment.android.toCliTable
import ftl.environment.common.toCliTable
import ftl.environment.ios.toCliTable
import ftl.presentation.cli.firebase.test.android.orientations.toCliTable
import ftl.presentation.cli.firebase.test.ipblocks.toCliTable
import ftl.presentation.cli.firebase.test.locale.toCliTable
import ftl.presentation.cli.firebase.test.providedsoftware.toCliTable
import ftl.presentation.cli.firebase.test.networkprofiles.toCliTable
import ftl.presentation.cli.firebase.test.providedsoftware.toCliTable

fun TestEnvironment.Android.prepareOutputString() = buildString {
appendLine(osVersions.toCliTable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import picocli.CommandLine

@Suppress("UNCHECKED_CAST")
@CommandLine.Command(
name = "describe",
headerHeading = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ftl.domain.invoke
import ftl.presentation.cli.firebase.test.locale.toCliTable
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import ftl.util.asListOrNull
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -40,7 +41,6 @@ class IosLocalesListCommand :
override fun run() = invoke()

override val out = outputLogger {
@Suppress("UNCHECKED_CAST")
(this as? List<Locale>)?.toCliTable() ?: throwUnknownType()
asListOrNull<Locale>()?.toCliTable() ?: throwUnknownType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import ftl.config.FtlConstants
import ftl.domain.ListIosOrientations
import ftl.domain.invoke
import ftl.presentation.cli.firebase.test.android.orientations.toCliTable
import ftl.presentation.cli.firebase.test.locale.toCliTable
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import ftl.util.asListOrNull
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -40,7 +42,6 @@ class IosOrientationsListCommand :
override fun run() = invoke()

override val out = outputLogger {
@Suppress("UNCHECKED_CAST")
(this as? List<Orientation>)?.toCliTable() ?: throwUnknownType()
asListOrNull<Orientation>()?.toCliTable() ?: throwUnknownType()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package ftl.presentation.cli.firebase.test.networkprofiles
import ftl.api.NetworkProfile
import ftl.domain.ListNetworkProfiles
import ftl.domain.invoke
import ftl.environment.common.toCliTable
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import ftl.util.asList
import ftl.util.asListOrNull
import picocli.CommandLine

@CommandLine.Command(
Expand All @@ -25,10 +26,8 @@ class NetworkProfilesListCommand :
override fun run() = invoke()

override val out = outputLogger {

@Suppress("UNCHECKED_CAST")
when {
(this as? List<NetworkProfile>) != null -> this.toCliTable()
asListOrNull<NetworkProfile>() != null -> asList<NetworkProfile>().toCliTable()
this is String -> this
else -> throwUnknownType()
}
Expand Down
7 changes: 7 additions & 0 deletions test_runner/src/main/kotlin/ftl/util/CastingUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ftl.util

@Suppress("UNCHECKED_CAST")
fun <T> Any.asList(): List<T> = this as List<T>

@Suppress("UNCHECKED_CAST")
fun <T> Any.asListOrNull(): List<T>? = this as? List<T>

0 comments on commit 942c9a9

Please sign in to comment.