Parse a tabular array.
(
lines: List[str],
start_idx: int,
base_indent: int,
count: int,
fields: List[str],
opts: DecoderOptions,
detected_delimiter: Optional[str] = None,
indent_size: int = 2
)
| 245 | |
| 246 | |
| 247 | def _parse_tabular_array( |
| 248 | lines: List[str], |
| 249 | start_idx: int, |
| 250 | base_indent: int, |
| 251 | count: int, |
| 252 | fields: List[str], |
| 253 | opts: DecoderOptions, |
| 254 | detected_delimiter: Optional[str] = None, |
| 255 | indent_size: int = 2 |
| 256 | ) -> Tuple[List[Dict], int]: |
| 257 | """Parse a tabular array.""" |
| 258 | result = [] |
| 259 | i = start_idx |
| 260 | expected_indent = base_indent + indent_size |
| 261 | |
| 262 | # Use provided delimiter from header indicator, or detect from first row |
| 263 | if detected_delimiter: |
| 264 | delimiter = detected_delimiter |
| 265 | else: |
| 266 | delimiter = opts.default_delimiter |
| 267 | if i < len(lines): |
| 268 | first_row = lines[i].strip() |
| 269 | if TAB in first_row: |
| 270 | delimiter = TAB |
| 271 | elif PIPE in first_row: |
| 272 | delimiter = PIPE |
| 273 | |
| 274 | for _ in range(count): |
| 275 | if i >= len(lines): |
| 276 | break |
| 277 | |
| 278 | line = lines[i] |
| 279 | indent = len(line) - len(line.lstrip()) |
| 280 | |
| 281 | if indent != expected_indent: |
| 282 | if opts.strict: |
| 283 | break |
| 284 | i += 1 |
| 285 | continue |
| 286 | |
| 287 | # Parse row values |
| 288 | row_str = line.strip() |
| 289 | values = _split_row(row_str, delimiter) |
| 290 | |
| 291 | # Create object from fields and values |
| 292 | obj = {} |
| 293 | for j, field in enumerate(fields): |
| 294 | if j < len(values): |
| 295 | obj[field] = _parse_value(values[j], opts) |
| 296 | else: |
| 297 | obj[field] = None |
| 298 | |
| 299 | result.append(obj) |
| 300 | i += 1 |
| 301 | |
| 302 | # Strict mode: validate count matches |
| 303 | if opts.strict and len(result) != count: |
| 304 | raise ValueError(f'Array length mismatch: expected {count}, got {len(result)}') |
no test coverage detected
searching dependent graphs…