| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package charttpl |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | |
| 9 | "gopkg.in/yaml.v2" |
| 10 | ) |
| 11 | |
| 12 | // DecodeYAML parses YAML bytes into Spec, applies defaults, and validates it. |
| 13 | func DecodeYAML(data []byte) (*Spec, error) { |
| 14 | var spec Spec |
| 15 | if err := yaml.UnmarshalStrict(data, &spec); err != nil { |
| 16 | return nil, fmt.Errorf("%w: %v", errDecode, err) |
| 17 | } |
| 18 | |
| 19 | applyDefaults(&spec) |
| 20 | if err := spec.Validate(); err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | return &spec, nil |
| 24 | } |
| 25 | |
| 26 | // DecodeYAMLFile reads and parses a YAML template file. |
| 27 | func DecodeYAMLFile(path string) (*Spec, error) { |
| 28 | data, err := os.ReadFile(path) |
| 29 | if err != nil { |
| 30 | return nil, fmt.Errorf("%w: read file %q: %v", errDecode, path, err) |
| 31 | } |
| 32 | return DecodeYAML(data) |
| 33 | } |