airflow icon indicating copy to clipboard operation
airflow copied to clipboard

Go SDK: unify conf and TaskFlow field-name matching

Open henry3260 opened this issue 3 weeks ago • 1 comments

Follow-up from a review comment on #75602 (comment):

Follow-up: unify with binding.go foldArgName/collectStructFields; they disagree on shadowing.

DagRun.UnmarshalConf folds conf keys onto struct fields, and pkg/binding already does the same for TaskFlow arguments. The helpers are duplicated: sdk.foldConfName (conf.go L347) and binding.foldArgName (binding.go L703) have identical bodies, as do implementsConfUnmarshaler (L358) and implementsUnmarshaler (L888). pkg/binding imports go-sdk/sdk, so sharing them needs a third package.

The two also resolve names differently. binding folds only untagged fields, only at the top level, and logs a warning when two names fold onto one field. UnmarshalConf folds json: tag names at every depth and returns an error instead.

Three cases diverge from encoding/json:

  1. With two embedded structs at the same depth, one declaring an untagged Source field and the other a field tagged json:"Source", encoding/json fills the tagged one and UnmarshalConf fills neither. Two identically named fields at the same depth are fine — encoding/json drops those too.
  2. A field of an embedded pointer the SDK skips still takes the key the SDK matched to a deeper field of the same name.
  3. A tagged field inside such a pointer still takes the key the SDK matched to an untagged field at the same depth, because encoding/json lets a tagged field win at equal depth.

Cases 2 and 3 come from the embedded-pointer skip that #75602 added in collectConfFields: the skip removes those fields from the SDK's index, but encoding/json still sees them, so a key the SDK matched to a visible field can still be delivered to a skipped one.

type etlTable struct{ TableName string }      // unexported, so *etlTable is skipped
type deepInner struct{ TableName string }
type DeepWrapper struct{ deepInner }

type case2 struct {
	*etlTable   // TableName at depth 1, skipped by the SDK index
	DeepWrapper // TableName at depth 2, so the SDK matches the key to this one

	Zone string
}

type tagged struct{ TableName string `json:"TableName"` }
type sibling struct{ TableName string }

type case3 struct {
	*tagged // skipped by the SDK index, but its field is tagged
	sibling // untagged, so the SDK matches the key to this one

	Zone string
}

With the conf {"table_name": "sales", "zone": "eu"}, both shapes behave identically:

The skipped pointer Result
nil cannot set embedded pointer to unexported struct type; Zone is still filled
set by the task before the call no error; the skipped field takes "sales", and the field the SDK matched stays ""

Both are the same before and after #75602, and the same as plain encoding/json, so they are drift rather than regressions. They are also why the UnmarshalConf godoc states "take no key" flatly: stating the exact rule needs two same-named fields at different depths, or a tag.

  • [ ] Share the folding helpers between sdk and pkg/binding.
  • [ ] Decide whether the two rule sets should converge, or be documented as deliberately different.
  • [ ] Settle the same-depth embedded-struct rule: let the tagged field win, or reject the struct with the existing fold-clash error.
  • [ ] Settle the skipped embedded pointer: keep its fields in byName as shadow-only, so a key that lands on one is dropped, or accept the divergence and document it.
  • [ ] Add tests for the conf.go branches that have none today: the json.Unmarshaler short-circuit, json:"-,", json:",omitempty", the self-embedding guard, the unexported-field skip, and a tagged embedded struct.

henry3260 avatar Nov 11 '26 08:11 henry3260

Hi @henry3260,

I saw you self-assigned this follow-up from #75602. If you are already actively working on this on a local branch, no worries at all!

If not and you'd like a hand, I have solid Go experience with struct tag reflection and JSON unmarshaling and would be very happy to take this on (extracting the shared folding/unmarshaler helper package, aligning the tag precedence with encoding/json rules, and adding the missing test coverage in conf_test.go).

Please let me know if you'd be open to me opening a PR for it!

SachinyadavAug20 avatar Nov 16 '26 06:11 SachinyadavAug20