uranusjr commented on code in PR #69257:
URL: https://github.com/apache/airflow/pull/69257#discussion_r3526397669
##########
java-sdk/processor/src/main/kotlin/org/apache/airflow/sdk/BuilderProcessor.kt:
##########
@@ -215,25 +213,49 @@ private val NUMBER_ACCESSORS: Map<TypeName, String> =
}
}
-private fun xcomAccess(xcom: RequiredXCom): CodeBlock {
- val call = CodeBlock.of($$"client.getXCom($S)", xcom.taskId)
+private fun addXComAccess(
+ executeSpec: MethodSpec.Builder,
+ xcom: RequiredXCom,
+) {
val type = TypeName.get(xcom.paramType)
val accessor = NUMBER_ACCESSORS[type]
val number = ClassName.get(Number::class.java)
// Wire integers decode to Long and floats to Double, so a direct
(Integer)/(Float)
// cast throws ClassCastException; widen via Number instead.
- return when {
- accessor == null -> CodeBlock.of($$"($T) $L", if (type.isPrimitive)
type.box() else type, call)
- type.isPrimitive -> CodeBlock.of($$"(($T) $L).$L()", number, call,
accessor)
- else ->
- CodeBlock.of(
- $$"$T.ofNullable(($T) $L).map($T::$L).orElse(null)",
- ClassName.get(Optional::class.java),
- number,
- call,
- number,
- accessor,
- )
+ if (!type.isPrimitive) {
+ val getXCom = CodeBlock.of($$"client.getXCom($S)", xcom.taskId)
+ val value =
+ if (accessor == null) {
+ CodeBlock.of($$"($T) $L", type, getXCom)
+ } else {
+ CodeBlock.of(
+ $$"$T.ofNullable(($T) $L).map($T::$L).orElse(null)",
+ ClassName.get(Optional::class.java),
+ number,
+ getXCom,
+ number,
+ accessor,
+ )
+ }
+ executeSpec.addStatement($$"var $L = $L", xcom.paramName, value)
+ return
+ }
+ // A primitive parameter cannot hold null. Read the raw value first and fail
with a
+ // clear error instead of an opaque NullPointerException while unboxing when
absent.
+ val raw = "${xcom.paramName}Raw"
Review Comment:
This may (theoratically) collide if the user passes two XComs, one called
`foo` and the other `fooRaw`. Tools generally use names containing `$` to avoid
this (since humans don’t tend to use it).
If possible, it may be a good idea to reduce the variable assignments to
both clean up the generated code and avoid this kind of collisions entirely.
--
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]