Skip to content

Commit

Permalink
refactor: deduplicate cron parsing (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
gak authored Jun 13, 2024
1 parent 249ce0e commit a27720b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/schema/metadatacronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type MetadataCronJob struct {
Pos Position `parser:"" protobuf:"1,optional"`

Cron string `parser:"'+' 'cron' Whitespace @( (Number ('s' | 'm' | 'h')) | ('Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun') | (' ' | Number | '-' | '/' | '*' | ',')+ )" protobuf:"2"`
Cron string `parser:"'+' 'cron' Whitespace @(' ' | ~EOL)*" protobuf:"2"`
}

var _ Metadata = (*MetadataCronJob)(nil)
Expand Down
3 changes: 2 additions & 1 deletion backend/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
}

Lexer = lexer.MustSimple([]lexer.SimpleRule{
{Name: "EOL", Pattern: `[\r\n]`},
{Name: "Whitespace", Pattern: `\s+`},
{Name: "Ident", Pattern: `\b[a-zA-Z_][a-zA-Z0-9_]*\b`},
{Name: "Comment", Pattern: `//.*`},
Expand All @@ -45,7 +46,7 @@ var (

commonParserOptions = []participle.Option{
participle.Lexer(Lexer),
participle.Elide("Whitespace"),
participle.Elide("Whitespace", "EOL"),
participle.Unquote(),
// Increase lookahead to allow comments to be attached to the next token.
participle.UseLookahead(participle.MaxLookahead),
Expand Down
49 changes: 49 additions & 0 deletions backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,55 @@ func TestParsing(t *testing.T) {
`5:24-24: unknown reference "test.topicB", is the type annotated and exported?`,
},
},
{name: "Cron",
input: `
module test {
verb A(Unit) Unit
+cron Wed
verb B(Unit) Unit
+cron */10 * * * * * *
verb C(Unit) Unit
+cron 12h
}
`,
expected: &Schema{
Modules: []*Module{{
Name: "test",
Decls: []Decl{
&Verb{
Name: "A",
Request: &Unit{Unit: true},
Response: &Unit{Unit: true},
Metadata: []Metadata{
&MetadataCronJob{
Cron: "Wed",
},
},
},
&Verb{
Name: "B",
Request: &Unit{Unit: true},
Response: &Unit{Unit: true},
Metadata: []Metadata{
&MetadataCronJob{
Cron: "*/10 * * * * * *",
},
},
},
&Verb{
Name: "C",
Request: &Unit{Unit: true},
Response: &Unit{Unit: true},
Metadata: []Metadata{
&MetadataCronJob{
Cron: "12h",
},
},
},
},
}},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit a27720b

Please sign in to comment.