Skip to content

Commit 1688a52

Browse files
committed
Make eval an internal package. Change interface to explicitly require a Config object.
1 parent fa23501 commit 1688a52

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

cmd/wasm/wasm.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/prequel-dev/preq/internal/pkg/config"
13+
"github.com/prequel-dev/preq/internal/pkg/eval"
1314
"github.com/prequel-dev/preq/internal/pkg/ux"
1415
"github.com/prequel-dev/preq/internal/pkg/verz"
15-
"github.com/prequel-dev/preq/pkg/eval"
1616
"github.com/rs/zerolog/log"
1717
)
1818

@@ -66,10 +66,10 @@ func detectWrapper(ctx context.Context) js.Func {
6666
detectFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
6767

6868
var (
69-
cfg, inputData, ruleData string
70-
reportDoc ux.ReportDocT
71-
stats ux.StatsT
72-
err error
69+
inputData, ruleData string
70+
reportDoc ux.ReportDocT
71+
stats ux.StatsT
72+
err error
7373
)
7474

7575
log.Info().
@@ -82,7 +82,7 @@ func detectWrapper(ctx context.Context) js.Func {
8282
ruleData = args[1].String()
8383

8484
// Permit events to arrive out of order within a 1 hour window by default
85-
cfg = config.Marshal(config.WithWindow(time.Hour))
85+
cfg := config.DefaultConfig(config.WithWindow(time.Hour))
8686

8787
if reportDoc, stats, err = eval.Detect(ctx, cfg, inputData, ruleData); err != nil {
8888
return errJson(err)

internal/pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func parseOpts(opts ...OptT) *optsT {
5555
}
5656

5757
// LoadConfig loads the configuration from the specified directory and file.
58-
// If the file does not exist, it creates a return a default configuration.
58+
// If the file does not exist, it returns a default configuration.
5959

6060
func LoadConfig(dir, file string, opts ...OptT) (*Config, error) {
6161

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package eval
22

33
import (
44
"context"
5-
"strings"
65

76
"github.com/prequel-dev/preq/internal/pkg/config"
87
"github.com/prequel-dev/preq/internal/pkg/engine"
@@ -13,10 +12,9 @@ import (
1312
"github.com/rs/zerolog/log"
1413
)
1514

16-
func Detect(ctx context.Context, cfg, data, rule string) (ux.ReportDocT, ux.StatsT, error) {
15+
func Detect(ctx context.Context, c *config.Config, data, rule string) (ux.ReportDocT, ux.StatsT, error) {
1716

1817
var (
19-
c *config.Config
2018
run *engine.RuntimeT
2119
report *ux.ReportT
2220
ruleMatchers *engine.RuleMatchersT
@@ -26,16 +24,6 @@ func Detect(ctx context.Context, cfg, data, rule string) (ux.ReportDocT, ux.Stat
2624
err error
2725
)
2826

29-
switch len(cfg) {
30-
case 0:
31-
c = config.DefaultConfig()
32-
default:
33-
if c, err = config.ReadConfig(strings.NewReader(cfg)); err != nil {
34-
log.Error().Err(err).Msg("Failed to load config")
35-
return nil, nil, err
36-
}
37-
}
38-
3927
opts := c.ResolveOpts()
4028
opts = append(opts, resolve.WithTimestampTries(timez.DefaultSkip))
4129

test/preq_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/prequel-dev/preq/internal/pkg/config"
9+
"github.com/prequel-dev/preq/internal/pkg/eval"
810
"github.com/prequel-dev/preq/internal/pkg/logs"
9-
"github.com/prequel-dev/preq/pkg/eval"
1011
"github.com/rs/zerolog/log"
1112
)
1213

@@ -138,7 +139,7 @@ func TestSuccessExamples(t *testing.T) {
138139
t.Fatalf("Error reading data file %s: %v", test.dataPath, err)
139140
}
140141

141-
_, stats, err := eval.Detect(ctx, "", string(data), string(ruleData))
142+
_, stats, err := eval.Detect(ctx, config.DefaultConfig(), string(data), string(ruleData))
142143
if err != nil {
143144
t.Fatalf("Error running detection: %v", err)
144145
}
@@ -226,7 +227,7 @@ func TestMissExamples(t *testing.T) {
226227
t.Fatalf("Error reading data file %s: %v", test.dataPath, err)
227228
}
228229

229-
_, stats, err := eval.Detect(ctx, "", string(data), string(ruleData))
230+
_, stats, err := eval.Detect(ctx, config.DefaultConfig(), string(data), string(ruleData))
230231
if err != nil {
231232
t.Fatalf("Error running detection: %v", err)
232233
}
@@ -294,7 +295,7 @@ func TestFailureExamples(t *testing.T) {
294295
t.Fatalf("Error reading data file %s: %v", test.dataPath, err)
295296
}
296297

297-
_, _, err = eval.Detect(ctx, "", string(data), string(ruleData))
298+
_, _, err = eval.Detect(ctx, config.DefaultConfig(), string(data), string(ruleData))
298299
if err == nil {
299300
t.Fatalf("Expected error running detection: %v", err)
300301
}

0 commit comments

Comments
 (0)