diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-13 20:04:48 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-13 20:04:48 +0300 |
| commit | 251894cf3375812564ecf28392179b395cdda9c7 (patch) | |
| tree | 83c3609ab591702e29a375923670e7622a33b5c7 /internal/generate/format.go | |
| parent | 78ea9e22e596255c5e23ce445d80641870674ca9 (diff) | |
refactor: break down functions exceeding 50 lines into smaller helpers
Split 22 production files across the codebase — event loop, TUI models,
probe manager, dashboard, export, flag parsing, code generation, and
ioworkload scenarios — so that no function body exceeds 50 lines. Each
extracted helper carries its own comment explaining its role.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/generate/format.go')
| -rw-r--r-- | internal/generate/format.go | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/internal/generate/format.go b/internal/generate/format.go index ea579b6..ef51ba8 100644 --- a/internal/generate/format.go +++ b/internal/generate/format.go @@ -44,52 +44,60 @@ func ParseFormats(r io.Reader) ([]Format, error) { isExternal := false for scanner.Scan() { - line := scanner.Text() - trimmed := strings.TrimSpace(line) - - switch { - case strings.HasPrefix(trimmed, "name:"): - f := Format{} - f.Name = strings.TrimSpace(strings.TrimPrefix(trimmed, "name:")) - formats = append(formats, f) - current = &formats[len(formats)-1] - isExternal = false - - case strings.HasPrefix(trimmed, "ID:"): - if current == nil { - return nil, fmt.Errorf("ID without name") - } - id, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(trimmed, "ID:"))) - if err != nil { - return nil, fmt.Errorf("parsing ID: %w", err) - } - current.ID = id - - case strings.HasPrefix(trimmed, "field:"): - if current == nil { - return nil, fmt.Errorf("field without name") - } - field, err := parseField(trimmed) - if err != nil { - return nil, fmt.Errorf("parsing field in %s: %w", current.Name, err) - } - if field.Name == "__syscall_nr" { - isExternal = true - } - if isExternal { - current.ExternalFields = append(current.ExternalFields, field) - } else { - current.InternalFields = append(current.InternalFields, field) - } + var err error + current, isExternal, err = applyFormatLine(scanner.Text(), formats, current, isExternal, &formats) + if err != nil { + return nil, err } } - if err := scanner.Err(); err != nil { return nil, fmt.Errorf("scanning input: %w", err) } return formats, nil } +// applyFormatLine processes one line from the format file, updating the +// running parse state (current format, isExternal flag, and formats slice). +func applyFormatLine(line string, _ []Format, current *Format, isExternal bool, formats *[]Format) (*Format, bool, error) { + trimmed := strings.TrimSpace(line) + switch { + case strings.HasPrefix(trimmed, "name:"): + f := Format{} + f.Name = strings.TrimSpace(strings.TrimPrefix(trimmed, "name:")) + *formats = append(*formats, f) + current = &(*formats)[len(*formats)-1] + isExternal = false + + case strings.HasPrefix(trimmed, "ID:"): + if current == nil { + return nil, false, fmt.Errorf("ID without name") + } + id, err := strconv.Atoi(strings.TrimSpace(strings.TrimPrefix(trimmed, "ID:"))) + if err != nil { + return nil, false, fmt.Errorf("parsing ID: %w", err) + } + current.ID = id + + case strings.HasPrefix(trimmed, "field:"): + if current == nil { + return nil, false, fmt.Errorf("field without name") + } + field, err := parseField(trimmed) + if err != nil { + return nil, false, fmt.Errorf("parsing field in %s: %w", current.Name, err) + } + if field.Name == "__syscall_nr" { + isExternal = true + } + if isExternal { + current.ExternalFields = append(current.ExternalFields, field) + } else { + current.InternalFields = append(current.InternalFields, field) + } + } + return current, isExternal, nil +} + func parseField(line string) (Field, error) { // Format: "field:TYPE NAME; offset:N; size:N; signed:N;" line = strings.TrimPrefix(line, "field:") |
