|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +// Package xyaml contains utility functions for parsing YAML. |
| 6 | +package xyaml |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "reflect" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +// UnmarshalStrict decodes YAML document validating that there are no extra fields found. |
| 18 | +func UnmarshalStrict[T any](data []byte, t T) error { |
| 19 | + var node yaml.Node |
| 20 | + |
| 21 | + if err := yaml.Unmarshal(data, &node); err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + if err := checkUnknownKeys(t, &node); err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + |
| 29 | + return node.Decode(t) |
| 30 | +} |
| 31 | + |
| 32 | +func checkUnknownKeys(t any, node *yaml.Node) error { |
| 33 | + if node.Kind == yaml.DocumentNode { |
| 34 | + if len(node.Content) == 0 { |
| 35 | + return nil |
| 36 | + } |
| 37 | + |
| 38 | + node = node.Content[0] |
| 39 | + } |
| 40 | + |
| 41 | + unknown, err := internalCheckUnknownKeys(reflect.TypeOf(t), node) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + if unknown != nil { |
| 47 | + var data []byte |
| 48 | + |
| 49 | + if data, err = yaml.Marshal(unknown); err != nil { |
| 50 | + return fmt.Errorf("failed to marshal error summary %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + return fmt.Errorf("unknown keys found during decoding:\n%s", string(data)) |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// structKeys builds a set of known YAML fields by name and their indexes in the struct. |
| 60 | +// |
| 61 | +//nolint:gocyclo |
| 62 | +func structKeys(typ reflect.Type) (map[string][]int, reflect.Type) { |
| 63 | + fields := reflect.VisibleFields(typ) |
| 64 | + |
| 65 | + availableKeys := make(map[string][]int, len(fields)) |
| 66 | + |
| 67 | + for _, field := range fields { |
| 68 | + if tag := field.Tag.Get("yaml"); tag != "" { |
| 69 | + if tag == "-" { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + idx := strings.IndexByte(tag, ',') |
| 74 | + |
| 75 | + inlined := false |
| 76 | + |
| 77 | + if idx >= 0 { |
| 78 | + options := strings.Split(tag[idx+1:], ",") |
| 79 | + |
| 80 | + for _, opt := range options { |
| 81 | + if opt == "inline" { |
| 82 | + inlined = true |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // handle inlined `map` objects, inlining structs in general is not supported yet |
| 88 | + if inlined { |
| 89 | + inlinedTyp := field.Type |
| 90 | + |
| 91 | + if inlinedTyp.Kind() == reflect.Map { |
| 92 | + return nil, inlinedTyp |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if idx == -1 { |
| 97 | + availableKeys[tag] = field.Index |
| 98 | + } else if idx > 0 { |
| 99 | + availableKeys[tag[:idx]] = field.Index |
| 100 | + } |
| 101 | + } else { |
| 102 | + availableKeys[strings.ToLower(field.Name)] = field.Index |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return availableKeys, typ |
| 107 | +} |
| 108 | + |
| 109 | +var typeOfInterfaceAny = reflect.TypeOf((*any)(nil)).Elem() |
| 110 | + |
| 111 | +//nolint:gocyclo,cyclop |
| 112 | +func internalCheckUnknownKeys(typ reflect.Type, spec *yaml.Node) (unknown any, err error) { |
| 113 | + for typ.Kind() == reflect.Ptr { |
| 114 | + typ = typ.Elem() |
| 115 | + } |
| 116 | + |
| 117 | + // anything can be unmarshaled into `interface{}` |
| 118 | + if typ == typeOfInterfaceAny { |
| 119 | + return nil, nil |
| 120 | + } |
| 121 | + |
| 122 | + switch spec.Kind { //nolint:exhaustive // not checking for scalar types |
| 123 | + case yaml.MappingNode: |
| 124 | + var availableKeys map[string][]int |
| 125 | + |
| 126 | + switch typ.Kind() { //nolint:exhaustive |
| 127 | + case reflect.Map: |
| 128 | + // any key is fine in the map |
| 129 | + case reflect.Struct: |
| 130 | + availableKeys, typ = structKeys(typ) |
| 131 | + default: |
| 132 | + return unknown, fmt.Errorf("unexpected type for yaml mapping: %s", typ) |
| 133 | + } |
| 134 | + |
| 135 | + for i := 0; i < len(spec.Content); i += 2 { |
| 136 | + keyNode := spec.Content[i] |
| 137 | + |
| 138 | + if keyNode.Kind != yaml.ScalarNode { |
| 139 | + return unknown, errors.New("unexpected mapping key type") |
| 140 | + } |
| 141 | + |
| 142 | + key := keyNode.Value |
| 143 | + |
| 144 | + var elemType reflect.Type |
| 145 | + |
| 146 | + switch typ.Kind() { //nolint:exhaustive |
| 147 | + case reflect.Struct: |
| 148 | + fieldIndex, ok := availableKeys[key] |
| 149 | + if !ok { |
| 150 | + if unknown == nil { |
| 151 | + unknown = map[string]any{} |
| 152 | + } |
| 153 | + |
| 154 | + unknown.(map[string]any)[key] = spec.Content[i+1] |
| 155 | + |
| 156 | + continue |
| 157 | + } |
| 158 | + |
| 159 | + elemType = typ.FieldByIndex(fieldIndex).Type |
| 160 | + case reflect.Map: |
| 161 | + elemType = typ.Elem() |
| 162 | + } |
| 163 | + |
| 164 | + // validate nested values |
| 165 | + innerUnknown, err := internalCheckUnknownKeys(elemType, spec.Content[i+1]) |
| 166 | + if err != nil { |
| 167 | + return unknown, err |
| 168 | + } |
| 169 | + |
| 170 | + if innerUnknown != nil { |
| 171 | + if unknown == nil { |
| 172 | + unknown = map[string]any{} |
| 173 | + } |
| 174 | + |
| 175 | + unknown.(map[string]any)[key] = innerUnknown |
| 176 | + } |
| 177 | + } |
| 178 | + case yaml.SequenceNode: |
| 179 | + if typ.Kind() != reflect.Slice { |
| 180 | + return unknown, fmt.Errorf("unexpected type for yaml sequence: %s", typ) |
| 181 | + } |
| 182 | + |
| 183 | + for i := range len(spec.Content) { |
| 184 | + innerUnknown, err := internalCheckUnknownKeys(typ.Elem(), spec.Content[i]) |
| 185 | + if err != nil { |
| 186 | + return unknown, err |
| 187 | + } |
| 188 | + |
| 189 | + if innerUnknown != nil { |
| 190 | + if unknown == nil { |
| 191 | + unknown = []any{} |
| 192 | + } |
| 193 | + |
| 194 | + unknown = append(unknown.([]any), innerUnknown) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return unknown, nil |
| 200 | +} |
0 commit comments