build-a-cron-parser / lesson-01.md
Lesson 01 · The five fields

Splitting a cron line into five fields

A cron expression is five fields separated by whitespace - minute, hour, day-of-month, month, day-of-week. Before parsing anything you have to split the line into exactly those five parts, and reject a line that has the wrong number.

The goal

Split an expression on whitespace into five fields, or return an error if there are not exactly five.

Start here - the target
TO DO
Scenario: A cron line splits into exactly five fields
Giventhe expression '*/15 0 1,15 * 1-5'
WhenSplitFields is called on it
Thenit returns the five fields '*/15', '0', '1,15', '*', '1-5' in order
AndSplitFields('1 2 3') returns an error, and SplitFields('1 2 3 4 5 6') returns an error
Background

A standard cron expression is a single line of five fields separated by spaces or tabs: minute, hour, day-of-month, month, and day-of-week, always in that order. Everything else in this project is about interpreting one field at a time, so the very first job is to chop the line into exactly five pieces. Splitting on any run of whitespace (not a single space) means extra spacing between fields does not matter, which is how real crontabs are written.

The one rule to enforce today is the count: a line with four or six fields is not a valid five-field cron expression, so reject it with a clear error rather than guessing what the writer meant. Pin both the good split and the wrong-count failure now - every later lesson receives exactly five field strings and never has to worry about the shape of the line again.

Make it work
func SplitFields(expr string) ([]string, error) {
f := strings.Fields(expr) // splits on any run of whitespace
if len(f) != 5 {
return nil, fmt.Errorf("expected 5 fields, got %d", len(f))
}
return f, nil
}
CheckpointDONE
You can split a cron line into its five fields and reject the wrong count. Commit and stop here.