uranusjr commented on code in PR #69100:
URL: https://github.com/apache/airflow/pull/69100#discussion_r3526364816
##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/Context.kt:
##########
@@ -19,17 +19,76 @@
package org.apache.airflow.sdk
+import org.apache.airflow.sdk.execution.Logger
+import org.apache.airflow.sdk.execution.comm.DagRun.DagRunType
import org.apache.airflow.sdk.execution.comm.StartupDetails
+import java.time.OffsetDateTime
+
+private val logger = Logger(Context::class)
+
+private fun Any?.toDateTime(): OffsetDateTime? =
+ when (this) {
+ null -> null
+ is OffsetDateTime -> this
+ is String ->
+ runCatching { OffsetDateTime.parse(this) }.getOrElse {
+ logger.warning("Ignoring unparsable date-time in run context",
mapOf("value" to this))
+ null
+ }
+ else -> {
+ logger.warning("Ignoring unexpected date-time value in run context",
mapOf("value" to this))
+ null
+ }
+ }
+
+private fun Any?.toConf(): Map<String, Any?> =
+ when (this) {
+ null -> emptyMap()
+ is Map<*, *> -> entries.mapNotNull { (key, value) -> (key as? String)?.let
{ it to value } }.toMap()
+ else -> {
+ logger.warning("Ignoring unexpected conf value in run context",
mapOf("value" to this))
+ emptyMap()
+ }
+ }
+
+private fun DagRunType?.toRunType(): RunType? =
+ this?.let { dagRunType ->
+ runCatching { RunType.valueOf(dagRunType.name) }.getOrElse {
+ logger.warning("Ignoring unrecognized run type in run context",
mapOf("value" to dagRunType.name))
+ null
+ }
Review Comment:
This would drift out of sync too easily if `DagRunType` receives new values.
Use a simple `when` to make the compiler check for missing items instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]