-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Fastly Rate Limiting (#60)
* add penaltybox declaration * remove newline * Add penaltybox parser * fix typos * add new keywords * remove example * add to plugin * add new rules * Add penatlybox and ratecounter to parser * Add support to lint penaltybox and ratecounter * Add built in functions for ratelimit * lint empty penaltyboxes and ratecounters blocks * test linting penaltybox and ratecounter * fix an error message * match the nonempty block errors Co-authored-by: shadi-altarsha <shadi.altarsha@reddit.com>
- Loading branch information
1 parent
4746ad8
commit e85cced
Showing
20 changed files
with
554 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ast | ||
|
||
import "bytes" | ||
|
||
type PenaltyboxDeclaration struct { | ||
*Meta | ||
Name *Ident | ||
Block *BlockStatement | ||
} | ||
|
||
func (p *PenaltyboxDeclaration) statement() {} | ||
func (p *PenaltyboxDeclaration) GetMeta() *Meta { return p.Meta } | ||
func (p *PenaltyboxDeclaration) String() string { | ||
var buf bytes.Buffer | ||
|
||
buf.WriteString(p.LeadingComment()) | ||
buf.WriteString("penaltybox ") | ||
buf.WriteString(p.Name.String()) | ||
buf.WriteString(" " + p.Block.String()) | ||
buf.WriteString(p.TrailingComment()) | ||
buf.WriteString("\n") | ||
|
||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ast | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPenaltyboxStatement(t *testing.T) { | ||
p := &PenaltyboxDeclaration{ | ||
Meta: New(T, 0, comments("// This is comment"), comments("/* This is comment */")), | ||
Name: &Ident{ | ||
Meta: New(T, 0), | ||
Value: "ip_pbox", | ||
}, | ||
Block: &BlockStatement{ | ||
Meta: New(T, 0, comments("/* This is comment */")), | ||
}, | ||
} | ||
|
||
expect := `// This is comment | ||
penaltybox ip_pbox { | ||
} /* This is comment */ | ||
` | ||
|
||
if p.String() != expect { | ||
t.Errorf("stringer error.\nexpect:\n%s\nactual:\n%s\n", expect, p.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ast | ||
|
||
import "bytes" | ||
|
||
type RatecounterDeclaration struct { | ||
*Meta | ||
Name *Ident | ||
Block *BlockStatement | ||
} | ||
|
||
func (r *RatecounterDeclaration) statement() {} | ||
func (r *RatecounterDeclaration) GetMeta() *Meta { return r.Meta } | ||
func (r *RatecounterDeclaration) String() string { | ||
var buf bytes.Buffer | ||
|
||
buf.WriteString(r.LeadingComment()) | ||
buf.WriteString("ratecounter ") | ||
buf.WriteString(r.Name.String()) | ||
buf.WriteString(" " + r.Block.String()) | ||
buf.WriteString(r.TrailingComment()) | ||
buf.WriteString("\n") | ||
|
||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ast | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRatecounterStatement(t *testing.T) { | ||
r := &RatecounterDeclaration{ | ||
Meta: New(T, 0, comments("// This is comment"), comments("/* This is comment */")), | ||
Name: &Ident{ | ||
Meta: New(T, 0), | ||
Value: "requests_rate", | ||
}, | ||
Block: &BlockStatement{ | ||
Meta: New(T, 0, comments("/* This is comment */")), | ||
}, | ||
} | ||
|
||
expect := `// This is comment | ||
ratecounter requests_rate { | ||
} /* This is comment */ | ||
` | ||
|
||
if r.String() != expect { | ||
t.Errorf("stringer error.\nexpect:\n%s\nactual:\n%s\n", expect, r.String()) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.