Skip to content

Commit

Permalink
Add CI, various minor comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Jan 4, 2024
1 parent ea03432 commit 5693949
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Go

on:
push:
branches: [main]
pull_request:

jobs:
golangci:
name: lint
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.21'
- name: Lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.55.1
./bin/golangci-lint run --verbose
test-linux-race:
strategy:
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.21'
- name: Test
run: go test ./... -v -count=1
6 changes: 6 additions & 0 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type AggregateEvaluator interface {
Remove(ctx context.Context, eval Evaluable) error

// AggregateMatch returns all expression parts which are evaluable given the input data.
//
// It does this by iterating through the data,
AggregateMatch(ctx context.Context, data map[string]any) ([]ExpressionPart, error)

// Evaluate checks input data against all exrpesssions in the aggregate in an optimal
Expand Down Expand Up @@ -166,6 +168,10 @@ func (a *aggregator) AggregateMatch(ctx context.Context, data map[string]any) ([
}

func (a *aggregator) aggregateMatch(ctx context.Context, data map[string]any, prefix string) ([]ExpressionPart, error) {
// TODO: Flip this. Instead of iterating through all fields in a potentially large input
// array, iterate through all known variables/idents in the aggregate tree to see if
// the data has those keys set.

result := []ExpressionPart{}
for k, v := range data {
switch cast := v.(type) {
Expand Down
8 changes: 5 additions & 3 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func evaluate(b *testing.B, i int, parser TreeParser) error {
// Insert N random matches.
for n := 0; n < i; n++ {
wg.Add(1)
//nolint:all
go func() {
defer wg.Done()
byt := make([]byte, 8)
Expand Down Expand Up @@ -78,11 +79,12 @@ func TestEvaluate(t *testing.T) {
_, err = e.Add(ctx, expected)
require.NoError(t, err)

n := 1_000
n := 100_000

wg := sync.WaitGroup{}
for i := 0; i < n; i++ {
wg.Add(1)
//nolint:all
go func() {
defer wg.Done()
byt := make([]byte, 8)
Expand All @@ -108,7 +110,7 @@ func TestEvaluate(t *testing.T) {
},
},
})
total := time.Now().Sub(pre)
total := time.Since(pre)
fmt.Printf("Matched in %v ns\n", total.Nanoseconds())
fmt.Printf("Matched in %v ms\n", total.Milliseconds())

Expand All @@ -128,7 +130,7 @@ func TestEvaluate(t *testing.T) {
},
},
})
total := time.Now().Sub(pre)
total := time.Since(pre)
fmt.Printf("Matched in %v ns\n", total.Nanoseconds())
fmt.Printf("Matched in %v ms\n", total.Milliseconds())

Expand Down
4 changes: 2 additions & 2 deletions groupid.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package expr

import (
"crypto/rand"
"encoding/binary"
"math/rand"
)

// groupID represents a group ID. The first 2 byets are an int16 size of the expression group,
Expand All @@ -15,7 +15,7 @@ func (g groupID) Size() uint16 {
}

func newGroupID(size uint16) groupID {
id := make([]byte, 8, 8)
id := make([]byte, 8)
binary.NativeEndian.PutUint16(id, size)
_, _ = rand.Read(id[2:])
return [8]byte(id[0:8])
Expand Down
12 changes: 4 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package expr

import (
"context"
"crypto/md5"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -270,12 +269,14 @@ type Predicate struct {
// variable are being compared, this is nil and LiteralIdent holds a pointer to the
// name of the second variable.
Literal any

// Ident is the ident we're comparing to, eg. the variable.
Ident string

// LiteralIdent represents the second literal that we're comparing against,
// eg. in the expression "event.data.a == event.data.b this stores event.data.b
LiteralIdent *string

// Ident is the ident we're comparing to, eg. the variable.
Ident string
// Operator is the binary operator being used. NOTE: This always assumes that the
// ident is to the left of the operator, eg "event.data.value > 100". If the value
// is to the left of the operator, the operator will be switched
Expand All @@ -297,11 +298,6 @@ func (p Predicate) String() string {
}
}

func (p Predicate) hash() string {
sum := md5.Sum([]byte(fmt.Sprintf("%v", p)))
return string(sum[:])
}

func (p Predicate) LiteralAsString() string {
str, _ := p.Literal.(string)
return str
Expand Down
6 changes: 0 additions & 6 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

func newTestAggregateEvaluator(t *testing.T) AggregateEvaluator {
t.Helper()
parser, _ := newParser()
return NewAggregateEvaluator(parser, testBoolEvaluator)
}

func newEnv() *cel.Env {
env, _ := cel.NewEnv(
cel.Variable("event", cel.AnyType),
Expand Down

0 comments on commit 5693949

Please sign in to comment.