Skip to content

Commit 4186ff6

Browse files
authored
pkg/config/configtest: DocDefaultsOnly ignore arrays of tables (#1189)
1 parent 49b2298 commit 4186ff6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

pkg/config/configtest/defaults.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
// DocDefaultsOnly reads only the default values from a docs TOML file and decodes in to cfg.
1212
// Fields without defaults will set to zero values.
13+
// Arrays of tables are ignored.
1314
func DocDefaultsOnly(r io.Reader, cfg any, decode func(io.Reader, any) error) error {
1415
pr, pw := io.Pipe()
1516
defer pr.Close()
@@ -26,12 +27,25 @@ func DocDefaultsOnly(r io.Reader, cfg any, decode func(io.Reader, any) error) er
2627
func writeDefaults(r io.Reader, w *io.PipeWriter) {
2728
defer w.Close()
2829
s := bufio.NewScanner(r)
30+
var skipUntil func(line string) bool
2931
for s.Scan() {
3032
t := s.Text()
33+
if skipUntil != nil {
34+
if skipUntil(t) {
35+
skipUntil = nil
36+
}
37+
continue
38+
}
3139
// Skip comments and examples (which become zero values)
3240
if strings.HasPrefix(t, "#") || strings.HasSuffix(t, "# Example") {
3341
continue
3442
}
43+
// Skip arrays of tables
44+
if strings.HasPrefix(t, "[[") {
45+
// skip fields until next empty line
46+
skipUntil = func(line string) bool { return strings.TrimSpace(line) == "" }
47+
continue
48+
}
3549
if _, err := io.WriteString(w, t); err != nil {
3650
w.CloseWithError(err)
3751
}

0 commit comments

Comments
 (0)