|
1 | 1 | package configtest |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bufio" |
5 | | - "fmt" |
6 | 4 | "io" |
7 | | - "reflect" |
8 | | - "strings" |
| 5 | + |
| 6 | + "github.com/smartcontractkit/chainlink-common/pkg/config/configdoc" |
9 | 7 | ) |
10 | 8 |
|
11 | 9 | // DocDefaultsOnly reads only the default values from a docs TOML file and decodes in to cfg. |
12 | 10 | // Fields without defaults will set to zero values. |
13 | 11 | // Arrays of tables are ignored. |
| 12 | +// Deprecated: use configdoc.DefaultsOnly |
14 | 13 | func DocDefaultsOnly(r io.Reader, cfg any, decode func(io.Reader, any) error) error { |
15 | | - pr, pw := io.Pipe() |
16 | | - defer pr.Close() |
17 | | - go writeDefaults(r, pw) |
18 | | - if err := decode(pr, cfg); err != nil { |
19 | | - return fmt.Errorf("failed to decode default core configuration: %w", err) |
20 | | - } |
21 | | - // replace niled examples with zero values. |
22 | | - nilToZero(reflect.ValueOf(cfg)) |
23 | | - return nil |
24 | | -} |
25 | | - |
26 | | -// writeDefaults writes default lines from defaultsTOML to w. |
27 | | -func writeDefaults(r io.Reader, w *io.PipeWriter) { |
28 | | - defer w.Close() |
29 | | - s := bufio.NewScanner(r) |
30 | | - var skipUntil func(line string) bool |
31 | | - for s.Scan() { |
32 | | - t := s.Text() |
33 | | - if skipUntil != nil { |
34 | | - if skipUntil(t) { |
35 | | - skipUntil = nil |
36 | | - } |
37 | | - continue |
38 | | - } |
39 | | - // Skip comments and examples (which become zero values) |
40 | | - if strings.HasPrefix(t, "#") || strings.HasSuffix(t, "# Example") { |
41 | | - continue |
42 | | - } |
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 | | - } |
49 | | - if _, err := io.WriteString(w, t); err != nil { |
50 | | - w.CloseWithError(err) |
51 | | - } |
52 | | - if _, err := w.Write([]byte{'\n'}); err != nil { |
53 | | - w.CloseWithError(err) |
54 | | - } |
55 | | - } |
56 | | - if err := s.Err(); err != nil { |
57 | | - w.CloseWithError(fmt.Errorf("failed to scan core defaults: %w", err)) |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -func nilToZero(val reflect.Value) { |
62 | | - if val.Kind() == reflect.Ptr { |
63 | | - if val.IsNil() { |
64 | | - t := val.Type().Elem() |
65 | | - val.Set(reflect.New(t)) |
66 | | - } |
67 | | - if val.Type().Implements(textUnmarshalerType) { |
68 | | - return // don't descend inside - leave whole zero value |
69 | | - } |
70 | | - val = val.Elem() |
71 | | - } |
72 | | - switch val.Kind() { |
73 | | - case reflect.Struct: |
74 | | - if val.Type().Implements(textUnmarshalerType) { |
75 | | - return // skip values unmarshaled from strings |
76 | | - } |
77 | | - for i := 0; i < val.NumField(); i++ { |
78 | | - f := val.Field(i) |
79 | | - nilToZero(f) |
80 | | - } |
81 | | - return |
82 | | - case reflect.Map: |
83 | | - if !val.IsNil() { |
84 | | - for _, k := range val.MapKeys() { |
85 | | - nilToZero(val.MapIndex(k)) |
86 | | - } |
87 | | - } |
88 | | - return |
89 | | - case reflect.Slice: |
90 | | - if !val.IsNil() { |
91 | | - for i := 0; i < val.Len(); i++ { |
92 | | - nilToZero(val.Index(i)) |
93 | | - } |
94 | | - } |
95 | | - return |
96 | | - default: |
97 | | - return |
98 | | - } |
| 14 | + return configdoc.DefaultsOnly(r, cfg, decode) |
99 | 15 | } |
0 commit comments