Skip to content

Commit 00f1fd4

Browse files
committed
logging#Config: Fix Options can no longer be parsed by go-yaml
Previously, commit ccf002a introduced `UnmarshalText()` to allow `Options` to be parsed by the `env` package. With this change, the `go-yaml` package detects that `Options` should be decoded by that unmarshaller [1, 2]. However, since `Options` is not a text type, `go-yaml` does not use the unmarshaller [3], resulting in unmarshalling failures [4]. This change implements `UnmarshalYAML()` on `Options`, which is tried before `UnmarshalText()` [5], restoring unmarshalling functionality. Additionally, it's essential that `Options` is no longer inlined within `Config`, ensuring that `UnmarshalYAML()` receives only the relevant `options` section instead of the entire YAML. Interestingly, this used to work previously. [1] https://github.com/goccy/go-yaml/blob/v1.12.0/decode.go#L703 [2] https://github.com/goccy/go-yaml/blob/v1.12.0/decode.go#L833 [3] https://github.com/goccy/go-yaml/blob/v1.12.0/decode.go#L788 [4] https://github.com/goccy/go-yaml/blob/v1.12.0/decode.go#L818 [5] https://github.com/goccy/go-yaml/blob/v1.12.0/decode.go#L763
1 parent 3950425 commit 00f1fd4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

logging/config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,27 @@ func (o *Options) UnmarshalText(text []byte) error {
3939
return nil
4040
}
4141

42+
// UnmarshalYAML implements yaml.InterfaceUnmarshaler to allow Options to be parsed go-yaml.
43+
func (o *Options) UnmarshalYAML(unmarshal func(any) error) error {
44+
optionsMap := make(map[string]zapcore.Level)
45+
46+
if err := unmarshal(&optionsMap); err != nil {
47+
return err
48+
}
49+
50+
*o = optionsMap
51+
52+
return nil
53+
}
54+
4255
// Config defines Logger configuration.
4356
type Config struct {
4457
// zapcore.Level at 0 is for info level.
4558
Level zapcore.Level `yaml:"level" env:"LEVEL" default:"0"`
4659
Output string `yaml:"output" env:"OUTPUT"`
4760
// Interval for periodic logging.
4861
Interval time.Duration `yaml:"interval" env:"INTERVAL" default:"20s"`
49-
50-
Options `yaml:"options" env:"OPTIONS"`
62+
Options Options `yaml:"options" env:"OPTIONS"`
5163
}
5264

5365
// SetDefaults implements defaults.Setter to configure the log output if it is not set:

0 commit comments

Comments
 (0)