Skip to content

Commit 73ab5df

Browse files
committed
fix(config): serialize Duration as a flat string under YAML
config.Duration only implemented MarshalJSON, so gopkg.in/yaml.v2/v3 (which does not honor MarshalJSON) serialized the embedded time.Duration as a nested `duration:` map. Any config that serializes a Duration to YAML and is read back through the viper/mapstructure config loader (TagName json + StringToTimeDurationHookFunc, which expects a string like "30s") would fail to decode. This first surfaced with the connector keepalive config: the operator writes the connector configmap with yaml.v2, emitting `time:\n duration: 30s`, which the connector plugin could not read back. Add MarshalYAML/UnmarshalYAML so Duration round-trips as a flat "30s" string under YAML. Signed-off-by: Kevin Su <pingsutw@apache.org>
1 parent d2f9024 commit 73ab5df

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

flytestdlib/config/duration.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
4646
return nil
4747
}
4848

49+
// MarshalYAML serializes the duration as a string (e.g. "30s") so that
50+
// gopkg.in/yaml.v2/v3 (which does not honor MarshalJSON) emits a flat value
51+
// instead of leaking the embedded time.Duration as a nested `duration:` map.
52+
func (d Duration) MarshalYAML() (interface{}, error) {
53+
return d.String(), nil
54+
}
55+
56+
// UnmarshalYAML parses the string form (e.g. "30s") produced by MarshalYAML.
57+
func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
58+
var s string
59+
if err := unmarshal(&s); err != nil {
60+
return err
61+
}
62+
if len(s) == 0 {
63+
d.Duration = time.Duration(0)
64+
return nil
65+
}
66+
parsed, err := time.ParseDuration(s)
67+
if err != nil {
68+
return err
69+
}
70+
d.Duration = parsed
71+
return nil
72+
}
73+
4974
// Set implements PFlag's Value interface's set method to set the value of duration from string.
5075
func (d *Duration) Set(val string) error {
5176
var err error

0 commit comments

Comments
 (0)