Skip to content

Refactor schedule_parser: split 1163-line file into focused modules#11588

Merged
pelikhan merged 5 commits intomainfrom
copilot/refactor-schedule-parser-file
Jan 24, 2026
Merged

Refactor schedule_parser: split 1163-line file into focused modules#11588
pelikhan merged 5 commits intomainfrom
copilot/refactor-schedule-parser-file

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

pkg/parser/schedule_parser.go exceeded healthy size limits at 1163 lines, mixing cron detection, fuzzy scheduling, time parsing, and parser orchestration in a single file.

Changes

Split into three focused modules:

  • schedule_cron_detection.go (140 lines): Cron pattern detection and validation

    • IsDailyCron, IsHourlyCron, IsWeeklyCron, IsFuzzyCron, IsCronExpression
  • schedule_fuzzy_scatter.go (319 lines): Deterministic time distribution for fuzzy schedules

    • ScatterSchedule with support for DAILY, HOURLY, WEEKLY, BI_WEEKLY, TRI_WEEKLY patterns
    • stableHash for consistent workflow time assignment
  • schedule_time_utils.go (245 lines): Time parsing and conversion utilities

    • parseTime, parseTimeToMinutes, mapWeekday, parseUTCOffset, parseHourMinute
    • Timezone normalization helpers

Main file reduced: 1163 → 512 lines (56% reduction)

Each module includes:

  • Dedicated test file with comprehensive coverage
  • Focused logger for debugging
  • Clear responsibility boundary

All existing tests pass without modification. Public API unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>[file-diet] Refactor Large Go File: pkg/parser/schedule_parser.go (1084 lines)</issue_title>
<issue_description>### Overview

The file pkg/parser/schedule_parser.go has grown to 1084 lines, significantly exceeding the healthy 800-line threshold. While it has excellent test coverage (2031 test lines, ~1.87 ratio), the file's size makes it difficult to navigate and maintain. This task involves refactoring it into smaller, focused modules with clear boundaries.

Current State

  • File: pkg/parser/schedule_parser.go
  • Size: 1084 lines
  • Test Coverage: 2031 lines in schedule_parser_test.go (~1.87 ratio)
  • Complexity: High - contains 5 distinct functional areas mixed together

Problem Analysis

The file mixes multiple concerns:

  1. Cron detection - Pattern matching for different cron types
  2. Fuzzy scattering - Complex deterministic time distribution (290+ lines in ScatterSchedule)
  3. Parser orchestration - Tokenization and high-level parsing
  4. Expression parsing - Interval and base schedule parsing
  5. Time utilities - Time parsing and conversion helpers

Refactoring Strategy

Split the file into focused modules aligned with functional boundaries:

1. schedule_cron_detection.go (~150 lines)

Functions to move:

  • IsDailyCron(cron string) bool
  • IsHourlyCron(cron string) bool
  • IsWeeklyCron(cron string) bool
  • IsFuzzyCron(cron string) bool
  • IsCronExpression(input string) bool

Responsibility: Cron pattern detection and classification

Rationale: These are pure functions that analyze cron strings. They have no dependencies on the parser state and can be tested independently.

2. schedule_fuzzy_scatter.go (~320 lines)

Functions to move:

  • ScatterSchedule(fuzzyCron, workflowIdentifier string) (string, error) (290+ lines)
  • stableHash(s string, modulo int) int

Responsibility: Deterministic fuzzy schedule time distribution

Rationale: The ScatterSchedule function is self-contained with complex logic for different fuzzy patterns (DAILY, HOURLY, WEEKLY, etc.). Extracting it reduces the main file by ~30%.

3. schedule_time_utils.go (~150 lines)

Functions to move:

  • parseTime(timeStr string) (minute string, hour string) (120 lines)
  • parseTimeToMinutes(hourStr, minuteStr string) int
  • mapWeekday(day string) string

Responsibility: Time parsing and conversion utilities

Rationale: These are helper functions used by the parser but don't depend on parser state. Can be reused by other modules.

4. schedule_parser_core.go (~250 lines)

Keep in this file:

  • ScheduleParser struct
  • ParseSchedule(input string) (cron, original, error) (entry point)
  • (p *ScheduleParser) tokenize() error
  • (p *ScheduleParser) parse() (string, error)

Responsibility: High-level parsing orchestration

Rationale: This is the main API surface. Keeping it focused on orchestration makes the flow clear.

5. schedule_parser_expressions.go (~250 lines)

Functions to move:

  • (p *ScheduleParser) parseInterval() (string, error) (150+ lines)
  • (p *ScheduleParser) parseBase() (string, error) (180+ lines)
  • (p *ScheduleParser) extractTime(startPos int) (string, error)
  • (p *ScheduleParser) extractTimeBetween(startPos, endPos int) (string, error)
  • (p *ScheduleParser) extractTimeAfter(startPos int) (string, error)

Responsibility: Schedule expression parsing (interval, base, time extraction)

Rationale: These are the detailed parsing methods that consume tokens. They depend on parser state but can be in a separate file via schedule_parser_core.go.

Implementation Plan

Step-by-step refactoring approach
  1. Create test infrastructure first

    • Run make test-unit to establish baseline
    • Ensure all existing tests pass before changes
  2. Extract utilities (no dependencies)

    • Create schedule_time_utils.go with time parsing functions
    • Create schedule_time_utils_test.go with relevant tests
    • Run tests to verify behavior unchanged
  3. Extract detection functions

    • Create schedule_cron_detection.go with Is*Cron functions
    • Create schedule_cron_detection_test.go with relevant tests
    • Run tests to verify behavior unchanged
  4. Extract fuzzy scattering

    • Create schedule_fuzzy_scatter.go with ScatterSchedule and stableHash
    • Create schedule_fuzzy_scatter_test.go with relevant tests
    • Run tests to verify behavior unchanged
  5. Split parser methods

    • Create schedule_parser_expressions.go with parsing methods
    • Move parseInterval, parseBase, extractTime* methods
    • Ensure methods can still access ScheduleParser receiver
    • Run tests to verify behavior unchanged
  6. Rename original file

    • Rename schedule_parser.go to schedule_parser_core.go
    • Keep only core orch...

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits January 24, 2026 03:34
…h tests

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor large Go file pkg/parser/schedule_parser.go Refactor schedule_parser: split 1163-line file into focused modules Jan 24, 2026
Copilot AI requested a review from pelikhan January 24, 2026 03:49
@pelikhan pelikhan marked this pull request as ready for review January 24, 2026 05:32
@pelikhan pelikhan merged commit 4d82f23 into main Jan 24, 2026
118 checks passed
@pelikhan pelikhan deleted the copilot/refactor-schedule-parser-file branch January 24, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[file-diet] Refactor Large Go File: pkg/parser/schedule_parser.go (1084 lines)

2 participants