| 123 | } |
| 124 | |
| 125 | func (r *MockReader) readNextYAMLDocument() ([]byte, error) { |
| 126 | var buffer bytes.Buffer |
| 127 | isFirstDoc := r.lineNum == 0 |
| 128 | |
| 129 | for { |
| 130 | select { |
| 131 | case <-r.ctx.Done(): |
| 132 | return nil, r.ctx.Err() |
| 133 | default: |
| 134 | } |
| 135 | |
| 136 | line, err := r.reader.ReadString('\n') |
| 137 | r.lineNum++ |
| 138 | |
| 139 | if err != nil { |
| 140 | if err == io.EOF { |
| 141 | r.done = true |
| 142 | if buffer.Len() > 0 { |
| 143 | return buffer.Bytes(), nil |
| 144 | } |
| 145 | return nil, io.EOF |
| 146 | } |
| 147 | return nil, fmt.Errorf("failed to read line %d: %w", r.lineNum, err) |
| 148 | } |
| 149 | |
| 150 | trimmedLine := strings.TrimSpace(line) |
| 151 | |
| 152 | if trimmedLine == "---" { |
| 153 | if buffer.Len() == 0 { |
| 154 | continue |
| 155 | } |
| 156 | return buffer.Bytes(), nil |
| 157 | } |
| 158 | |
| 159 | if isFirstDoc && buffer.Len() == 0 && strings.HasPrefix(trimmedLine, "#") { |
| 160 | continue |
| 161 | } |
| 162 | |
| 163 | buffer.WriteString(line) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Format returns the format the reader is decoding. Callers can use this to |
| 168 | // pick between ReadNextDoc (NetworkTrafficDoc with yaml.Node spec) and |