Go SDK: unify conf and TaskFlow field-name matching
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:
- With two embedded structs at the same depth, one declaring an untagged
Sourcefield and the other a field taggedjson:"Source",encoding/jsonfills the tagged one andUnmarshalConffills neither. Two identically named fields at the same depth are fine —encoding/jsondrops those too. - A field of an embedded pointer the SDK skips still takes the key the SDK matched to a deeper field of the same name.
- A tagged field inside such a pointer still takes the key the SDK matched to an untagged field at the same
depth, because
encoding/jsonlets 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
sdkandpkg/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
byNameas shadow-only, so a key that lands on one is dropped, or accept the divergence and document it. - [ ] Add tests for the
conf.gobranches that have none today: thejson.Unmarshalershort-circuit,json:"-,",json:",omitempty", the self-embedding guard, the unexported-field skip, and a tagged embedded struct.
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!