Skip to content
Open
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func main() {

script := `
// The ctx object from the Go inputData map
name := ctx.get("name")
let name = ctx.get("name")

p := "."
if ctx.get("excited") {
let p = "."
if (ctx.get("excited")) {
p = "!"
}
message := "Hello, " + name + p

let message = "Hello, " + name + p

// Return a map with our result
{
"greeting": message,
Expand Down
52 changes: 26 additions & 26 deletions engines/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func TestEngineDataHandlingIntegration(t *testing.T) {
// Risor script that accesses data via ctx variable
risorScript := `
// Access data through ctx variable
name := ctx["name"]
version := ctx["version"]
debug := ctx["config"]["debug"]
timeout := ctx["config"]["timeout"]
tags := ctx["tags"]
let name = ctx["name"]
let version = ctx["version"]
let debug = ctx["config"]["debug"]
let timeout = ctx["config"]["timeout"]
let tags = ctx["tags"]

// Create result
{
Expand Down Expand Up @@ -209,9 +209,9 @@ func TestEngineDataHandlingWithDynamicData(t *testing.T) {
t.Run("risor_dynamic_data", func(t *testing.T) {
risorScript := `
// Access both static and dynamic data
app_name := ctx["app_name"]
user_id := ctx["user_id"]
action := ctx["action"]
let app_name = ctx["app_name"]
let user_id = ctx["user_id"]
let action = ctx["action"]

{
"result": "processed",
Expand Down Expand Up @@ -349,8 +349,8 @@ func TestEngineDataStructureDocumentation(t *testing.T) {

t.Run("risor_documentation_example", func(t *testing.T) {
script := `
name := ctx["name"]
debug := ctx["config"]["debug"]
let name = ctx["name"]
let debug = ctx["config"]["debug"]

{
"name": name,
Expand Down Expand Up @@ -439,8 +439,8 @@ func TestDataProviderPatterns(t *testing.T) {
"risor_context_provider",
func(t *testing.T) {
script := `
config := ctx["config"]
user_data := ctx["user_data"]
let config = ctx["config"]
let user_data = ctx["user_data"]

{
"config": config,
Expand Down Expand Up @@ -596,9 +596,9 @@ _ = result

t.Run("risor_static_provider", func(t *testing.T) {
script := `
app_name := ctx["config"]["app_name"]
version := ctx["config"]["version"]
max_retries := ctx["constants"]["max_retries"]
let app_name = ctx["config"]["app_name"]
let version = ctx["config"]["version"]
let max_retries = ctx["constants"]["max_retries"]

{
"app_name": app_name,
Expand Down Expand Up @@ -713,10 +713,10 @@ _ = result
t.Run( //nolint:dupl // Each engine test demonstrates different syntax and behavior
"risor_composite_provider", func(t *testing.T) {
script := `
app_name := ctx["config"]["app_name"]
version := ctx["config"]["version"]
user_id := ctx["user_id"]
request_id := ctx["request_id"]
let app_name = ctx["config"]["app_name"]
let version = ctx["config"]["version"]
let user_id = ctx["user_id"]
let request_id = ctx["request_id"]

{
"app_name": app_name,
Expand Down Expand Up @@ -875,10 +875,10 @@ func TestHttpRequestDataAccess(t *testing.T) {
"risor_http_request", func(t *testing.T) {
script := `
// HTTP request data as documented in platform/data/README.md
request_method := ctx["request"]["Method"]
url_path := ctx["request"]["URL_Path"]
request_body := ctx["request"]["Body"]
content_type := ctx["request"]["Headers"]["Content-Type"][0]
let request_method = ctx["request"]["Method"]
let url_path = ctx["request"]["URL_Path"]
let request_body = ctx["request"]["Body"]
let content_type = ctx["request"]["Headers"]["Content-Type"][0]

{
"method": request_method,
Expand Down Expand Up @@ -1005,9 +1005,9 @@ _ = result

t.Run("risor_explicit_keys", func(t *testing.T) {
script := `
request_data := ctx["request"]
user_data := ctx["user"]
config_data := ctx["config"]
let request_data = ctx["request"]
let user_data = ctx["user"]
let config_data = ctx["config"]

{
"has_request": request_data != nil,
Expand Down
4 changes: 2 additions & 2 deletions engines/risor/adapters/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package adapters

import risorCompiler "github.com/risor-io/risor/compiler"
import "github.com/deepnoodle-ai/risor/v2/pkg/bytecode"

type RisorExecutable struct {
GetRisorByteCode func() *risorCompiler.Code
GetRisorByteCode func() *bytecode.Code
}
2 changes: 1 addition & 1 deletion engines/risor/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *Compiler) compile(scriptBodyBytes []byte) (*executable, error) {
isCommentOnly := true
for line := range strings.SplitSeq(trimmedScript, "\n") {
if trimmedLine := strings.TrimSpace(line); trimmedLine != "" &&
!strings.HasPrefix(trimmedLine, "#") {
!strings.HasPrefix(trimmedLine, "//") {
// Found a non-comment line, so we can stop checking lines because there's some real code here!
isCommentOnly = false
break
Expand Down
40 changes: 20 additions & 20 deletions engines/risor/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ func TestCompiler_Compile(t *testing.T) {
}{
{
name: "valid script",
script: `print("Hello, World!")`,
script: `"Hello, World!"`,
globals: []string{"request"},
},
{
name: "with multiple globals",
script: `print(request, response)`,
script: `[request, response]`,
globals: []string{"request", "response"},
},
{
name: "complex valid script with global override",
script: `
request = true
func main() {
if request {
print("Yes")
function main() {
if (request) {
"Yes"
} else {
print("No")
"No"
}
}
main()
Expand All @@ -115,11 +115,11 @@ main()
{
name: "complex valid script with condition",
script: `
func main() {
if condition {
print("Yes")
function main() {
if (condition) {
"Yes"
} else {
print("No")
"No"
}
}
main()
Expand Down Expand Up @@ -176,8 +176,8 @@ main()
err error
}{
{
name: "syntax error - missing closing parenthesis",
script: `print("Hello, World!"`,
name: "syntax error - unterminated string",
script: `"Hello, World!`,
globals: []string{"request"},
err: ErrValidationFailed,
},
Expand All @@ -189,13 +189,13 @@ main()
},
{
name: "undefined global",
script: `print(undefined_global)`,
script: `undefined_global`,
globals: []string{"request"},
err: ErrValidationFailed,
},
{
name: "script using undefined global",
script: `print(undefined)`,
script: `undefined`,
globals: []string{"request"},
err: ErrValidationFailed,
},
Expand Down Expand Up @@ -270,7 +270,7 @@ main()
require.NotNil(t, comp, "Expected compiler to be non-nil")

// Create a reader that will return an error on close
reader := newMockScriptReaderCloser(`print("Hello, World!")`)
reader := newMockScriptReaderCloser(`"Hello, World!"`)
reader.On("Close").Return(errors.New("test error")).Once()

execContent, err := comp.Compile(reader)
Expand All @@ -294,7 +294,7 @@ main()
require.NotNil(t, comp, "Expected compiler to be non-nil")

// Here we test that we can directly call the compile method with a byteslice
scriptBytes := []byte(`print("Hello, World!")`)
scriptBytes := []byte(`"Hello, World!"`)
executable, err := comp.compile(scriptBytes)
require.NoError(t, err, "Did not expect an error but got one")
require.NotNil(t, executable, "Expected execContent to be non-nil")
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestCompilerOptions(t *testing.T) {
require.NotNil(t, comp)

// Test with a script using the globals
script := `print(request, response)`
script := `[request, response]`
reader := io.ReadCloser(newMockScriptReaderCloser(script))
if mockReader, ok := reader.(*mockScriptReaderCloser); ok {
mockReader.On("Close").Return(nil)
Expand All @@ -361,7 +361,7 @@ func TestCompilerOptions(t *testing.T) {
require.NotNil(t, comp)

// Simple script that doesn't require globals
script := `print("Hello")`
script := `"Hello"`
reader := io.ReadCloser(newMockScriptReaderCloser(script))
if mockReader, ok := reader.(*mockScriptReaderCloser); ok {
mockReader.On("Close").Return(nil)
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestCompileWithBytecode(t *testing.T) {
require.NotNil(t, comp, "Expected compiler to be non-nil")

// Here we test that we can directly call the compile method with a byteslice
scriptBytes := []byte(`print("Hello, World!")`)
scriptBytes := []byte(`"Hello, World!"`)
executable, err := comp.compile(scriptBytes)
require.NoError(t, err, "Did not expect an error but got one")
require.NotNil(t, executable, "Expected execContent to be non-nil")
Expand Down Expand Up @@ -452,7 +452,7 @@ func TestCompileCloseError(t *testing.T) {
require.NotNil(t, comp, "Expected compiler to be non-nil")

// Create a reader that will return an error on close
reader := newMockScriptReaderCloser(`print("Hello, World!")`)
reader := newMockScriptReaderCloser(`"Hello, World!"`)
reader.On("Close").Return(errors.New("test error")).Once()

execContent, err := comp.Compile(reader)
Expand Down
8 changes: 4 additions & 4 deletions engines/risor/compiler/executable.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package compiler

import (
risorCompiler "github.com/risor-io/risor/compiler"
"github.com/deepnoodle-ai/risor/v2/pkg/bytecode"
machineTypes "github.com/robbyt/go-polyscript/engines/types"
)

type executable struct {
scriptBodyBytes []byte
ByteCode *risorCompiler.Code
ByteCode *bytecode.Code
}

func newExecutable(scriptBodyBytes []byte, byteCode *risorCompiler.Code) *executable {
func newExecutable(scriptBodyBytes []byte, byteCode *bytecode.Code) *executable {
if len(scriptBodyBytes) == 0 || byteCode == nil {
return nil
}
Expand All @@ -29,7 +29,7 @@ func (e *executable) GetByteCode() any {
return e.ByteCode
}

func (e *executable) GetRisorByteCode() *risorCompiler.Code {
func (e *executable) GetRisorByteCode() *bytecode.Code {
return e.ByteCode
}

Expand Down
30 changes: 15 additions & 15 deletions engines/risor/compiler/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package compiler
import (
"testing"

risorCompiler "github.com/risor-io/risor/compiler"
"github.com/deepnoodle-ai/risor/v2/pkg/bytecode"
machineTypes "github.com/robbyt/go-polyscript/engines/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,25 +16,25 @@ func TestExecutable(t *testing.T) {
// Test creation scenarios
t.Run("Creation", func(t *testing.T) {
t.Run("valid creation", func(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}
content := "'Hello, World!'"
bc := bytecode.NewCode(bytecode.CodeParams{})

exe := newExecutable([]byte(content), bytecode)
exe := newExecutable([]byte(content), bc)
require.NotNil(t, exe)
assert.Equal(t, content, exe.GetSource())
assert.Equal(t, bytecode, exe.GetByteCode())
assert.Equal(t, bytecode, exe.GetRisorByteCode())
assert.Equal(t, bc, exe.GetByteCode())
assert.Equal(t, bc, exe.GetRisorByteCode())
assert.Equal(t, machineTypes.Risor, exe.GetMachineType())
})

t.Run("nil content", func(t *testing.T) {
bytecode := &risorCompiler.Code{}
exe := newExecutable(nil, bytecode)
bc := bytecode.NewCode(bytecode.CodeParams{})
exe := newExecutable(nil, bc)
assert.Nil(t, exe)
})

t.Run("nil bytecode", func(t *testing.T) {
content := "print('test')"
content := "'test'"
exe := newExecutable([]byte(content), nil)
assert.Nil(t, exe)
})
Expand All @@ -47,9 +47,9 @@ func TestExecutable(t *testing.T) {

// Test getters
t.Run("Getters", func(t *testing.T) {
content := "print('Hello, World!')"
bytecode := &risorCompiler.Code{}
executable := newExecutable([]byte(content), bytecode)
content := "'Hello, World!'"
bc := bytecode.NewCode(bytecode.CodeParams{})
executable := newExecutable([]byte(content), bc)
require.NotNil(t, executable)

t.Run("GetSource", func(t *testing.T) {
Expand All @@ -59,16 +59,16 @@ func TestExecutable(t *testing.T) {

t.Run("GetByteCode", func(t *testing.T) {
code := executable.GetByteCode()
assert.Equal(t, bytecode, code)
assert.Equal(t, bc, code)

// Test type assertion
_, ok := code.(*risorCompiler.Code)
_, ok := code.(*bytecode.Code)
assert.True(t, ok)
})

t.Run("GetRisorByteCode", func(t *testing.T) {
code := executable.GetRisorByteCode()
assert.Equal(t, bytecode, code)
assert.Equal(t, bc, code)
})

t.Run("GetMachineType", func(t *testing.T) {
Expand Down
Loading