From de1ffed9e12eb99ad2e14f22bd9f9e448aca4b65 Mon Sep 17 00:00:00 2001 From: Tony Holdstock-Brown Date: Sat, 6 Jan 2024 09:59:12 -0800 Subject: [PATCH] Rename Evaluable methods --- expr.go | 17 ++++++++++++----- expr_test.go | 10 +++++----- parser.go | 4 ++-- tree.go | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/expr.go b/expr.go index 6492201..24ff590 100644 --- a/expr.go +++ b/expr.go @@ -73,14 +73,21 @@ func NewAggregateEvaluator( } } +// Evaluable represents an evaluable expression with a unique identifier. type Evaluable interface { - // Identifier returns a unique identifier for the evaluable item. If there are + // GetID returns a unique identifier for the evaluable item. If there are // two instances of the same expression, the identifier should return a unique // string for each instance of the expression (eg. for two pauses). - Identifier() string + // + // It has the Get prefix to reduce collisions with implementations who expose an + // ID member. + GetID() string - // Expression returns an expression as a raw string. - Expression() string + // GetExpression returns an expression as a raw string. + // + // It has the Get prefix to reduce collisions with implementations who expose an + // Expression member. + GetExpression() string } type aggregator struct { @@ -285,7 +292,7 @@ func (a *aggregator) Remove(ctx context.Context, eval Evaluable) error { // Find the index of the evaluable in constants and yank out. idx := -1 for n, item := range a.constants { - if item.Evaluable.Identifier() == eval.Identifier() { + if item.Evaluable.GetID() == eval.GetID() { idx = n break } diff --git a/expr_test.go b/expr_test.go index a2c3f00..16d307e 100644 --- a/expr_test.go +++ b/expr_test.go @@ -271,7 +271,7 @@ func TestAggregateMatch(t *testing.T) { require.EqualValues(t, 1, len(matched)) require.EqualValues(t, `event.data.a == "yes"`, - matched[0].Parsed.Evaluable.Expression(), + matched[0].Parsed.Evaluable.GetExpression(), ) }) @@ -358,7 +358,7 @@ func TestAddRemove(t *testing.T) { require.NoError(t, err) require.EqualValues(t, 1, len(eval)) require.EqualValues(t, 1, count) - require.EqualValues(t, firstExpr.Identifier(), eval[0].Identifier()) + require.EqualValues(t, firstExpr.GetID(), eval[0].GetID()) }) // Add a new expression @@ -462,15 +462,15 @@ type testEvaluable struct { id string } -func (e testEvaluable) Expression() string { return e.expr } -func (e testEvaluable) Identifier() string { return e.expr + e.id } +func (e testEvaluable) GetExpression() string { return e.expr } +func (e testEvaluable) GetID() string { return e.expr + e.id } func testBoolEvaluator(ctx context.Context, e Evaluable, input map[string]any) (bool, error) { env, _ := cel.NewEnv( cel.Variable("event", cel.AnyType), cel.Variable("async", cel.AnyType), ) - ast, _ := env.Parse(e.Expression()) + ast, _ := env.Parse(e.GetExpression()) // Create the program, refusing to short circuit if a match is found. // diff --git a/parser.go b/parser.go index eba2418..7e2c220 100644 --- a/parser.go +++ b/parser.go @@ -63,7 +63,7 @@ type parser struct { } func (p *parser) Parse(ctx context.Context, eval Evaluable) (*ParsedExpression, error) { - ast, issues, vars := p.ep.Parse(eval.Expression()) + ast, issues, vars := p.ep.Parse(eval.GetExpression()) if issues != nil { return nil, issues.Err() } @@ -76,7 +76,7 @@ func (p *parser) Parse(ctx context.Context, eval Evaluable) (*ParsedExpression, // group IDs will be deterministic as the randomness is sourced from the ID. // // We only overwrite this if rander is not nil so that we can inject rander during tests. - digest := sha256.Sum256([]byte(eval.Identifier())) + digest := sha256.Sum256([]byte(eval.GetID())) seed := int64(binary.NativeEndian.Uint64(digest[:8])) r = rand.New(rand.NewSource(seed)).Read } diff --git a/tree.go b/tree.go index f6f99e1..97ab389 100644 --- a/tree.go +++ b/tree.go @@ -60,5 +60,5 @@ func (p ExpressionPart) Equals(n ExpressionPart) bool { if p.Predicate.String() != n.Predicate.String() { return false } - return p.Parsed.Evaluable.Expression() == n.Parsed.Evaluable.Expression() + return p.Parsed.Evaluable.GetExpression() == n.Parsed.Evaluable.GetExpression() }