ReadNextDoc reads and decodes the next document. For JSON files this still goes through the yaml.Node bridge (via UnmarshalDoc -> jsonDocToYamlDoc) so callers that rely on NetworkTrafficDoc.Spec.Decode(&concreteSpec) keep working. Hot paths that want to stay yaml-free on JSON files should use ReadN
()
| 179 | // NetworkTrafficDoc.Spec.Decode(&concreteSpec) keep working. Hot paths that |
| 180 | // want to stay yaml-free on JSON files should use ReadNextDocJSON instead. |
| 181 | func (r *MockReader) ReadNextDoc() (*NetworkTrafficDoc, error) { |
| 182 | data, err := r.ReadNextDocument() |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | |
| 187 | if len(bytes.TrimSpace(data)) == 0 { |
| 188 | return r.ReadNextDoc() |
| 189 | } |
| 190 | |
| 191 | if r.format == FormatJSON { |
| 192 | doc, err := UnmarshalDoc(FormatJSON, data) |
| 193 | if err != nil { |
| 194 | return nil, fmt.Errorf("failed to decode JSON at line %d: %w", r.lineNum, err) |
| 195 | } |
| 196 | return doc, nil |
| 197 | } |
| 198 | |
| 199 | var doc NetworkTrafficDoc |
| 200 | if err := yamlLib.Unmarshal(data, &doc); err != nil { |
| 201 | return nil, fmt.Errorf("failed to decode YAML at line %d: %w", r.lineNum, err) |
| 202 | } |
| 203 | |
| 204 | return &doc, nil |
| 205 | } |
| 206 | |
| 207 | // ReadNextDocJSON reads the next NDJSON line and returns it as a |
| 208 | // NetworkTrafficDocJSON with the spec kept as a json.RawMessage. Only valid |