diff --git a/ucl/inst.go b/ucl/inst.go index a42376b..5c42bf7 100644 --- a/ucl/inst.go +++ b/ucl/inst.go @@ -56,7 +56,7 @@ func New(opts ...InstOption) *Inst { rootEC.root = rootEC rootEC.addCmd("echo", invokableFunc(echoBuiltin)) - rootEC.addCmd("var", invokableFunc(setBuiltin)) + rootEC.addCmd("set", invokableFunc(setBuiltin)) rootEC.addCmd("toUpper", invokableFunc(toUpperBuiltin)) rootEC.addCmd("len", invokableFunc(lenBuiltin)) rootEC.addCmd("keys", invokableFunc(keysBuiltin)) diff --git a/ucl/testbuiltins_test.go b/ucl/testbuiltins_test.go index 5fa3a15..edaa5e8 100644 --- a/ucl/testbuiltins_test.go +++ b/ucl/testbuiltins_test.go @@ -192,7 +192,7 @@ func TestBuiltins_If(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -382,7 +382,7 @@ func TestBuiltins_Try(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) if tt.wantErr != "" { assert.Error(t, err) @@ -422,7 +422,7 @@ func TestBuiltins_ForEach(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -466,7 +466,7 @@ func TestBuiltins_Break(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -507,7 +507,7 @@ func TestBuiltins_Continue(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -587,7 +587,7 @@ func TestBuiltins_Procs(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -712,7 +712,7 @@ func TestBuiltins_Return(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -774,7 +774,7 @@ func TestBuiltins_Seq(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -821,7 +821,7 @@ func TestBuiltins_Map(t *testing.T) { outW := bytes.NewBuffer(nil) inst := New(WithOut(outW), WithTestBuiltin()) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -921,7 +921,7 @@ func TestBuiltins_Index(t *testing.T) { }, }, nil }) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -980,7 +980,7 @@ func TestBuiltins_Len(t *testing.T) { missing: "missing", }, nil }) - err := EvalAndDisplay(ctx, inst, tt.expr) + err := evalAndDisplay(ctx, inst, tt.expr) assert.NoError(t, err) assert.Equal(t, tt.want, outW.String()) @@ -1437,3 +1437,32 @@ func TestBuiltins_Cat(t *testing.T) { }) } } + +func evalAndDisplay(ctx context.Context, inst *Inst, expr string) error { + res, err := inst.eval(ctx, expr) + if err != nil { + return err + } + + return displayResult(ctx, inst, res) +} + +func displayResult(ctx context.Context, inst *Inst, res Object) (err error) { + switch v := res.(type) { + case nil: + if _, err = fmt.Fprintln(inst.out, "(nil)"); err != nil { + return err + } + case Listable: + for i := 0; i < v.Len(); i++ { + if err = displayResult(ctx, inst, v.Index(i)); err != nil { + return err + } + } + default: + if _, err = fmt.Fprintln(inst.out, v.String()); err != nil { + return err + } + } + return nil +}