Fixed a number of bugs
- Fixed the non-opaque slice and struct types returned from builtins as pointers to be returned from eval as pointers - Fixed syntax problems with empty blocks that contain new-lines
This commit is contained in:
parent
88417aba95
commit
94cb920c51
|
@ -40,8 +40,8 @@ type astListOrHash struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type astBlock struct {
|
type astBlock struct {
|
||||||
Names []string `parser:"LC NL? (PIPE @Ident+ PIPE NL?)?"`
|
Names []string `parser:"LC NL* (PIPE @Ident+ PIPE NL*)?"`
|
||||||
Statements []*astStatements `parser:"@@ NL? RC"`
|
Statements []*astStatements `parser:"@@? NL* RC"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type astMaybeSub struct {
|
type astMaybeSub struct {
|
||||||
|
|
20
ucl/objs.go
20
ucl/objs.go
|
@ -141,9 +141,9 @@ func toGoValue(obj object) (interface{}, bool) {
|
||||||
case proxyObject:
|
case proxyObject:
|
||||||
return v.p, true
|
return v.p, true
|
||||||
case listableProxyObject:
|
case listableProxyObject:
|
||||||
return v.v.Interface(), true
|
return v.orig.Interface(), true
|
||||||
case structProxyObject:
|
case structProxyObject:
|
||||||
return v.v.Interface(), true
|
return v.orig.Interface(), true
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -171,10 +171,17 @@ func fromGoReflectValue(resVal reflect.Value) (object, error) {
|
||||||
|
|
||||||
switch resVal.Kind() {
|
switch resVal.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return listableProxyObject{resVal}, nil
|
return listableProxyObject{v: resVal, orig: resVal}, nil
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return newStructProxyObject(resVal), nil
|
return newStructProxyObject(resVal, resVal), nil
|
||||||
case reflect.Pointer:
|
case reflect.Pointer:
|
||||||
|
switch resVal.Elem().Kind() {
|
||||||
|
case reflect.Slice:
|
||||||
|
return listableProxyObject{v: resVal.Elem(), orig: resVal}, nil
|
||||||
|
case reflect.Struct:
|
||||||
|
return newStructProxyObject(resVal.Elem(), resVal), nil
|
||||||
|
}
|
||||||
|
|
||||||
return fromGoReflectValue(resVal.Elem())
|
return fromGoReflectValue(resVal.Elem())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,6 +407,7 @@ func (p proxyObject) Truthy() bool {
|
||||||
|
|
||||||
type listableProxyObject struct {
|
type listableProxyObject struct {
|
||||||
v reflect.Value
|
v reflect.Value
|
||||||
|
orig reflect.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p listableProxyObject) String() string {
|
func (p listableProxyObject) String() string {
|
||||||
|
@ -424,12 +432,14 @@ func (p listableProxyObject) Index(i int) object {
|
||||||
|
|
||||||
type structProxyObject struct {
|
type structProxyObject struct {
|
||||||
v reflect.Value
|
v reflect.Value
|
||||||
|
orig reflect.Value
|
||||||
vf []reflect.StructField
|
vf []reflect.StructField
|
||||||
}
|
}
|
||||||
|
|
||||||
func newStructProxyObject(v reflect.Value) structProxyObject {
|
func newStructProxyObject(v reflect.Value, orig reflect.Value) structProxyObject {
|
||||||
return structProxyObject{
|
return structProxyObject{
|
||||||
v: v,
|
v: v,
|
||||||
|
orig: orig,
|
||||||
vf: slices.Filter(reflect.VisibleFields(v.Type()), func(t reflect.StructField) bool { return t.IsExported() }),
|
vf: slices.Filter(reflect.VisibleFields(v.Type()), func(t reflect.StructField) bool { return t.IsExported() }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -389,6 +389,36 @@ func TestBuiltins_Return(t *testing.T) {
|
||||||
expr string
|
expr string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
|
// syntax tests
|
||||||
|
{desc: "empty proc 1", expr: `
|
||||||
|
proc greet {}
|
||||||
|
greet
|
||||||
|
`, want: "(nil)\n"},
|
||||||
|
{desc: "empty proc 2", expr: `
|
||||||
|
proc greet {
|
||||||
|
}
|
||||||
|
|
||||||
|
greet
|
||||||
|
`, want: "(nil)\n"},
|
||||||
|
{desc: "empty proc 3", expr: `
|
||||||
|
proc greet {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
greet
|
||||||
|
`, want: "(nil)\n"},
|
||||||
|
{desc: "empty proc 4", expr: `
|
||||||
|
proc greet {
|
||||||
|
# bla
|
||||||
|
|
||||||
|
# di
|
||||||
|
# bla!
|
||||||
|
}
|
||||||
|
|
||||||
|
greet
|
||||||
|
`, want: "(nil)\n"},
|
||||||
|
|
||||||
{desc: "nil return", expr: `
|
{desc: "nil return", expr: `
|
||||||
proc greet {
|
proc greet {
|
||||||
echo "Hello"
|
echo "Hello"
|
||||||
|
@ -398,6 +428,27 @@ func TestBuiltins_Return(t *testing.T) {
|
||||||
|
|
||||||
greet
|
greet
|
||||||
`, want: "Hello\n(nil)\n"},
|
`, want: "Hello\n(nil)\n"},
|
||||||
|
|
||||||
|
{desc: "simple arg 1", expr: `
|
||||||
|
proc greet { |x|
|
||||||
|
return (cat "Hello, " $x)
|
||||||
|
}
|
||||||
|
|
||||||
|
greet "person"
|
||||||
|
`, want: "Hello, person\n"},
|
||||||
|
{desc: "simple arg 2", expr: `
|
||||||
|
proc greet {
|
||||||
|
# This will greet someone
|
||||||
|
# here are the args:
|
||||||
|
|x|
|
||||||
|
|
||||||
|
# And here is the code
|
||||||
|
return (cat "Hello, " $x)
|
||||||
|
}
|
||||||
|
|
||||||
|
greet "person"
|
||||||
|
`, want: "Hello, person\n"},
|
||||||
|
|
||||||
{desc: "simple return", expr: `
|
{desc: "simple return", expr: `
|
||||||
proc greet {
|
proc greet {
|
||||||
return "Hello, world"
|
return "Hello, world"
|
||||||
|
@ -406,6 +457,7 @@ func TestBuiltins_Return(t *testing.T) {
|
||||||
|
|
||||||
greet
|
greet
|
||||||
`, want: "Hello, world\n"},
|
`, want: "Hello, world\n"},
|
||||||
|
|
||||||
{desc: "only return current frame", expr: `
|
{desc: "only return current frame", expr: `
|
||||||
proc greetWhat {
|
proc greetWhat {
|
||||||
echo "Greet the"
|
echo "Greet the"
|
||||||
|
|
|
@ -3,6 +3,7 @@ package ucl_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -113,6 +114,27 @@ func TestInst_SetBuiltin(t *testing.T) {
|
||||||
assert.Equal(t, pair{"Hello", "World"}, res)
|
assert.Equal(t, pair{"Hello", "World"}, res)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("builtin return proxy object ptr", func(t *testing.T) {
|
||||||
|
type pair struct {
|
||||||
|
x, y string
|
||||||
|
}
|
||||||
|
|
||||||
|
inst := ucl.New()
|
||||||
|
inst.SetBuiltin("add2", func(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||||
|
var x, y string
|
||||||
|
|
||||||
|
if err := args.Bind(&x, &y); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pair{x, y}, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
res, err := inst.Eval(context.Background(), `add2 "Hello" "World"`)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, &pair{"Hello", "World"}, res)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("builtin operating on and returning proxy object", func(t *testing.T) {
|
t.Run("builtin operating on and returning proxy object", func(t *testing.T) {
|
||||||
type pair struct {
|
type pair struct {
|
||||||
x, y string
|
x, y string
|
||||||
|
@ -235,10 +257,12 @@ func TestInst_SetBuiltin(t *testing.T) {
|
||||||
descr string
|
descr string
|
||||||
expr string
|
expr string
|
||||||
want opaqueThingType
|
want opaqueThingType
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{descr: "return as is", expr: `getOpaque`, want: *opaqueThing},
|
{descr: "return as is", expr: `getOpaque`, want: *opaqueThing},
|
||||||
{descr: "update pointer 1", expr: `set x (getOpaque) ; setProp $x -x "do" -y "touch" -z "this"`, want: opaqueThingType{x: "do", y: "touch", z: "this"}},
|
{descr: "update pointer 1", expr: `set x (getOpaque) ; setProp $x -x "do" -y "touch" -z "this"`, want: opaqueThingType{x: "do", y: "touch", z: "this"}},
|
||||||
{descr: "update pointer 2", expr: `set x (getOpaque) ; setProp $x -x "yes" ; setProp $x -y "this" -z "too"`, want: opaqueThingType{x: "yes", y: "this", z: "too"}},
|
{descr: "update pointer 2", expr: `set x (getOpaque) ; setProp $x -x "yes" ; setProp $x -y "this" -z "too"`, want: opaqueThingType{x: "yes", y: "this", z: "too"}},
|
||||||
|
{descr: "bad args", expr: `set x (getOpaque) ; setProp $t -x "yes" ; setProp $bla -y "this" -z "too"`, want: *opaqueThing, wantErr: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -255,6 +279,8 @@ func TestInst_SetBuiltin(t *testing.T) {
|
||||||
|
|
||||||
if err := args.Bind(&o); err != nil {
|
if err := args.Bind(&o); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
} else if o == nil {
|
||||||
|
return nil, errors.New("is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.HasSwitch("x") {
|
if args.HasSwitch("x") {
|
||||||
|
@ -277,8 +303,12 @@ func TestInst_SetBuiltin(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err := inst.Eval(context.Background(), tt.expr)
|
_, err := inst.Eval(context.Background(), tt.expr)
|
||||||
|
if tt.wantErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
} else {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, tt.want, *opaqueThing)
|
assert.Equal(t, tt.want, *opaqueThing)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue