Skip to content

Commit

Permalink
Rename Evaluable methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Jan 6, 2024
1 parent 5bf5dca commit de1ffed
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
17 changes: 12 additions & 5 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
})

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
//
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit de1ffed

Please sign in to comment.