forked from TrueCloudLab/frostfs-sdk-go
[#280] policy: Use policyVisitor
as ErrorListener
for QueryLexer
Share `policyVisitor` instance as an `ErrorListener` provider for both `QueryLexer` and `Query`. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
dea3d9c419
commit
ab4d1e34a8
2 changed files with 13 additions and 3 deletions
|
@ -551,16 +551,17 @@ func writeFilterStringTo(w io.StringWriter, f netmap.Filter) error {
|
|||
// DecodeString decodes PlacementPolicy from the string composed using
|
||||
// WriteStringTo. Returns error if s is malformed.
|
||||
func (p *PlacementPolicy) DecodeString(s string) error {
|
||||
var v policyVisitor
|
||||
|
||||
input := antlr.NewInputStream(s)
|
||||
lexer := parser.NewQueryLexer(input)
|
||||
lexer.RemoveErrorListeners()
|
||||
lexer.AddErrorListener(&v)
|
||||
stream := antlr.NewCommonTokenStream(lexer, 0)
|
||||
|
||||
pp := parser.NewQuery(stream)
|
||||
pp.BuildParseTrees = true
|
||||
|
||||
var v policyVisitor
|
||||
|
||||
pp.RemoveErrorListeners()
|
||||
pp.AddErrorListener(&v)
|
||||
pl := pp.Policy().Accept(&v)
|
||||
|
|
|
@ -25,8 +25,9 @@ SELECT 1 IN City FROM SPBSSD AS SPB
|
|||
FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
|
||||
}
|
||||
|
||||
var p PlacementPolicy
|
||||
|
||||
for _, testCase := range testCases {
|
||||
var p PlacementPolicy
|
||||
|
||||
require.NoError(t, p.DecodeString(testCase))
|
||||
|
||||
|
@ -35,6 +36,14 @@ FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
|
|||
|
||||
require.Equal(t, testCase, b.String())
|
||||
}
|
||||
|
||||
invalidTestCases := []string{
|
||||
`?REP 1`,
|
||||
}
|
||||
|
||||
for i := range invalidTestCases {
|
||||
require.Error(t, p.DecodeString(invalidTestCases[i]), "#%d", i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlacementPolicyEncoding(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue