Skip to content

rename WithLogger to WithLogHandler, move options package #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/options"
"github.com/robbyt/go-polyscript/engine/options"
)

func main() {
Expand Down Expand Up @@ -79,7 +79,7 @@ func main() {
evaluator, err := polyscript.FromRisorString(
scriptContent,
options.WithDefaults(),
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(dataProvider),
risor.WithGlobals([]string{"ctx"}),
)
Expand Down
16 changes: 8 additions & 8 deletions engine/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (
"testing"

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/machines/starlark"
"github.com/robbyt/go-polyscript/options"
)

// quietHandler is a slog.Handler that discards all logs
Expand Down Expand Up @@ -70,7 +70,7 @@ func BenchmarkEvaluationPatterns(b *testing.B) {
scriptContent,
options.WithDefaults(),
options.WithDataProvider(dataProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand All @@ -94,7 +94,7 @@ func BenchmarkEvaluationPatterns(b *testing.B) {
scriptContent,
options.WithDefaults(),
options.WithDataProvider(dataProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand Down Expand Up @@ -138,7 +138,7 @@ func BenchmarkDataProviders(b *testing.B) {
scriptContent,
options.WithDefaults(),
options.WithDataProvider(dataProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand All @@ -160,7 +160,7 @@ func BenchmarkDataProviders(b *testing.B) {
scriptContent,
options.WithDefaults(),
options.WithDataProvider(dataProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand All @@ -187,7 +187,7 @@ func BenchmarkDataProviders(b *testing.B) {
scriptContent,
options.WithDefaults(),
options.WithDataProvider(compositeProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand Down Expand Up @@ -244,7 +244,7 @@ message = "Hello, " + name + "!"
risorScript,
options.WithDefaults(),
options.WithDataProvider(staticProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
risor.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand All @@ -265,7 +265,7 @@ message = "Hello, " + name + "!"
starlarkScript,
options.WithDefaults(),
options.WithDataProvider(staticProvider),
options.WithLogger(quietHandler),
options.WithLogHandler(quietHandler),
starlark.WithGlobals([]string{constants.Ctx}),
)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions engine/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"testing"

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/options"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -30,7 +30,7 @@ method := ctx["request"]["Method"]
greeting := ctx["input_data"]["greeting"]
method + " " + greeting
`,
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(provider),
risor.WithGlobals([]string{constants.Ctx}),
)
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestEvaluatorWithPrepErrors(t *testing.T) {
// Test with StaticProvider (which doesn't support adding data)
staticProvider := data.NewStaticProvider(map[string]any{"static": "data"})
evaluator, err := polyscript.FromRisorString(`ctx["static"]`,
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(staticProvider),
risor.WithGlobals([]string{constants.Ctx}),
)
Expand All @@ -106,7 +106,7 @@ func TestEvaluatorWithPrepErrors(t *testing.T) {
// Test with evaluator that has a ContextProvider
contextProvider := data.NewContextProvider(constants.EvalData)
evaluator, err = polyscript.FromRisorString(`ctx["request"]["ID"] || "no id"`,
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(contextProvider),
risor.WithGlobals([]string{constants.Ctx}),
)
Expand Down
File renamed without changes.
14 changes: 12 additions & 2 deletions options/options.go → engine/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Config struct {
// Option is a function that modifies Config
type Option func(*Config) error

// WithLogger sets the logger for the script engine
func WithLogger(handler slog.Handler) Option {
// WithLogHandler sets the logger for the script engine
func WithLogHandler(handler slog.Handler) Option {
return func(c *Config) error {
if handler != nil {
c.handler = handler
Expand All @@ -37,6 +37,16 @@ func WithLogger(handler slog.Handler) Option {
}
}

// WithSlog sets the slog logger for the script engine
func WithSlog(logger *slog.Logger) Option {
return func(c *Config) error {
if logger != nil {
c.handler = logger.Handler()
}
return nil
}
}

// WithDataProvider sets the data provider for the script engine
func WithDataProvider(provider data.Provider) Option {
return func(c *Config) error {
Expand Down
2 changes: 1 addition & 1 deletion options/options_test.go → engine/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestWithOptions(t *testing.T) {
testLoader := NewMockLoader()

// Create and apply options
loggerOpt := WithLogger(testHandler)
loggerOpt := WithLogHandler(testHandler)
dataProviderOpt := WithDataProvider(testDataProvider)
loaderOpt := WithLoader(testLoader)

Expand Down
4 changes: 2 additions & 2 deletions examples/data-prep/extism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/extism"
"github.com/robbyt/go-polyscript/options"
)

// ExtismEvaluator is a type alias to make testing cleaner
Expand All @@ -39,7 +39,7 @@ func createExtismEvaluator(
// Create evaluator using the functional options pattern
return polyscript.FromExtismFile(
wasmFilePath,
options.WithLogger(logger.Handler()),
options.WithLogHandler(logger.Handler()),
options.WithDataProvider(compositeProvider),
extism.WithEntryPoint("greet"),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/data-prep/risor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/options"
)

// RisorEvaluator is a type alias to make testing cleaner
Expand Down Expand Up @@ -45,7 +45,7 @@ func createRisorEvaluator(
return polyscript.FromRisorString(
scriptContent,
options.WithDefaults(),
options.WithLogger(logger.Handler()),
options.WithLogHandler(logger.Handler()),
options.WithDataProvider(compositeProvider),
risor.WithGlobals(globals),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/data-prep/starlark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/starlark"
"github.com/robbyt/go-polyscript/options"
)

// StarlarkEvaluator is a type alias to make testing cleaner
Expand Down Expand Up @@ -47,7 +47,7 @@ func createStarlarkEvaluator(
return polyscript.FromStarlarkString(
scriptContent,
options.WithDefaults(),
options.WithLogger(logger.Handler()),
options.WithLogHandler(logger.Handler()),
options.WithDataProvider(compositeProvider),
starlark.WithGlobals(globals),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-instantiation/extism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/extism"
"github.com/robbyt/go-polyscript/options"
)

// ExtismEvaluator is a type alias to make testing cleaner
Expand Down Expand Up @@ -65,7 +65,7 @@ func createEvaluator(handler slog.Handler) (ExtismEvaluator, error) {
// Create the evaluator
evaluator, err := polyscript.FromExtismFile(
wasmFilePath,
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(dataProvider),
extism.WithEntryPoint("greet"),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-instantiation/risor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/options"
)

// RisorEvaluator is a type alias to make testing cleaner
Expand All @@ -38,7 +38,7 @@ func createEvaluator(handler slog.Handler) (RisorEvaluator, error) {
evaluator, err := polyscript.FromRisorString(
risorScript,
options.WithDefaults(),
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(ctxProvider),
risor.WithGlobals(globals),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-instantiation/starlark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/starlark"
"github.com/robbyt/go-polyscript/options"
)

// StarlarkEvaluator is a type alias to make testing cleaner
Expand All @@ -38,7 +38,7 @@ func createEvaluator(handler slog.Handler) (StarlarkEvaluator, error) {
evaluator, err := polyscript.FromStarlarkString(
starlarkScript,
options.WithDefaults(),
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(ctxProvider),
starlark.WithGlobals(globals),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/extism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"time"

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/extism"
"github.com/robbyt/go-polyscript/options"
)

// findWasmFile searches for the Extism WASM file in various likely locations
Expand Down Expand Up @@ -64,7 +64,7 @@ func runExtismExample(handler slog.Handler) (map[string]any, error) {
evaluator, err := polyscript.FromExtismFile(
wasmFilePath,
options.WithDefaults(),
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(dataProvider),
extism.WithEntryPoint("greet"),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/risor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"os"

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/risor"
"github.com/robbyt/go-polyscript/options"
)

//go:embed testdata/script.risor
Expand All @@ -37,7 +37,7 @@ func runRisorExample(handler slog.Handler) (map[string]any, error) {
evaluator, err := polyscript.FromRisorString(
risorScript,
options.WithDefaults(), // Add defaults option to ensure all required fields are set
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(dataProvider),
risor.WithGlobals(globals),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/starlark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"os"

"github.com/robbyt/go-polyscript"
"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/execution/constants"
"github.com/robbyt/go-polyscript/execution/data"
"github.com/robbyt/go-polyscript/machines/starlark"
"github.com/robbyt/go-polyscript/options"
)

//go:embed testdata/script.star
Expand All @@ -37,7 +37,7 @@ func runStarlarkExample(handler slog.Handler) (map[string]any, error) {
evaluator, err := polyscript.FromStarlarkString(
starlarkScript,
options.WithDefaults(),
options.WithLogger(handler),
options.WithLogHandler(handler),
options.WithDataProvider(dataProvider),
starlark.WithGlobals(globals),
)
Expand Down
2 changes: 1 addition & 1 deletion machines/extism/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package extism
import (
"fmt"

"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/machines/types"
"github.com/robbyt/go-polyscript/options"
)

// ExtismOptions provides configuration options for the Extism compiler
Expand Down
2 changes: 1 addition & 1 deletion machines/extism/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package extism
import (
"testing"

"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/machines/types"
"github.com/robbyt/go-polyscript/options"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion machines/risor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package risor
import (
"fmt"

"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/machines/types"
"github.com/robbyt/go-polyscript/options"
)

// RisorOptions provides configuration options for the Risor compiler
Expand Down
2 changes: 1 addition & 1 deletion machines/risor/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package risor
import (
"testing"

"github.com/robbyt/go-polyscript/engine/options"
"github.com/robbyt/go-polyscript/machines/types"
"github.com/robbyt/go-polyscript/options"
"github.com/stretchr/testify/require"
)

Expand Down
Loading