jason810496 commented on code in PR #71188: URL: https://github.com/apache/airflow/pull/71188#discussion_r4080958971
########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/internal/ArgValues.kt: ########## @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") + +package org.apache.airflow.sdk.internal + +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.json.JsonMapper +import org.apache.airflow.sdk.Client +import org.apache.airflow.sdk.MissingXComException +import org.apache.airflow.sdk.TaskInput +import org.apache.airflow.sdk.execution.ArgBinding +import java.lang.reflect.Field +import java.lang.reflect.Type + +/** + * @suppress + * + * Resolves a task's data parameters from the arg bindings the supervisor + * delivered, and decodes their raw wire values into the declared types. Public + * so that processor-generated task classes can call it; not user-facing API. + * + * The bindings come from the Python `@task.stub` call site, which is also the + * graph the scheduler ordered the run by. Flat data parameters resolve the + * binding at their position (through [TaskArgs]); [TaskInput] fields resolve + * bindings by name. + */ +object ArgValues { + private val mapper: ObjectMapper = JsonMapper.builder().build().findAndRegisterModules() + + /** + * Materializes a [TaskInput] with every field bound by the argument name it + * claims. + * + * The single populator behind both authoring APIs — the annotation processor + * emits a call to it for a `@Builder.Task` [TaskInput] parameter, and + * [org.apache.airflow.sdk.InputTask] calls it before handing the input to a + * task written against the interface. + * + * @throws IllegalArgumentException if the input cannot be populated. + * @throws MissingXComException if a primitive field's binding resolves to + * nothing. + */ + @JvmStatic + fun <I : TaskInput> bindInput( + client: Client, + type: Class<I>, + ): I { + val input = newInput(type) + val arguments = ArgIndex(client.argBindings) + bindableFields(type).forEach { field -> field.set(input, resolveField(client, arguments, field)) } + return input + } + + /** + * Resolves the data parameter at [position] into [type], passing null + * through. Backs [TaskArgs]; a parameter that cannot be null goes through + * [TaskArgs.require], which turns null into [missing]. [TaskArgs.of] has + * already matched the declared parameters against the bindings, so a + * position always names one. + * + * @param position Zero-based index among the task's data parameters, in + * declaration order. + */ + internal fun valueAt( + client: Client, + position: Int, + type: Type, + ): Any? = decode(client.resolveBinding(client.argBindings[position]), type) + + /** + * Builds the failure for a binding that resolved to nothing where a value is + * required, naming [target] — the stub argument, or the [TaskInput] field + * that claimed it. + */ + internal fun missing( + binding: ArgBinding, + taskId: String, + target: String = binding.name, + ): MissingXComException = + when (binding) { + is ArgBinding.XCom -> MissingXComException(binding.taskId, target) + is ArgBinding.Literal -> + MissingXComException( + "Task parameter '$target' of task '$taskId' is bound to a null literal, but has a primitive " + + "type that cannot be null; declare a boxed type (e.g. Integer instead of int) to receive null.", + ) + } + + /** + * Resolves one [TaskInput] field from the argument it claims. A primitive + * field cannot hold null, so it fails with a clear [MissingXComException] + * when the binding resolves to nothing; boxed and reference fields receive + * null instead. + */ + private fun resolveField( + client: Client, + arguments: ArgIndex, + field: Field, + ): Any? { + val argName = argNameOf(field) + val binding = arguments.find(argName, pinned = isPinned(field)) + if (!field.type.isPrimitive) return binding?.let { decode(client.resolveBinding(it), field.genericType) } + + checkNotNull(binding) { Review Comment: There're two parts for your comment, the first one is a real bug and the second one is intended. - **No argument of that name at all.** The Java signature and the Python stub disagree. This now fails for every field type, not just primitives. - **An argument that resolves to nothing** (a null literal, or an upstream that pushed no XCom). Intended and unchanged: boxed and reference fields take `null`, primitive fields fail. Declaring a boxed type is how a field says the value is optional. Recorded in ADR-0001 rather than ADR-0007. Since ADR-0007 is the language-neutral wire contract 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]
