Skip to content

Commit 328f423

Browse files
committed
fix: pass context outside dsl/ops package
Don't use context.Background() in dsl/ops package.
1 parent 0917af8 commit 328f423

File tree

10 files changed

+30
-33
lines changed

10 files changed

+30
-33
lines changed

dsl/ops/func_do.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func (*doOp) Bind(args ...types.Value) (types.Expr, error) {
3030
return types.NewExpr(DoOp, args, retType), nil
3131
}
3232

33-
func (*doOp) InvertExpr(args []types.Value) (types.Value, error) {
33+
func (*doOp) InvertExpr(ctx context.Context, args []types.Value) (types.Value, error) {
3434
newargs := make([]types.Value, len(args))
3535
for i := range args {
36-
a, err := args[i].Invert()
36+
a, err := args[i].Invert(ctx)
3737
if err != nil {
3838
return nil, err
3939
}

dsl/ops/func_lockjson_write.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (*lockJSONWriteOp) Bind(args ...types.Value) (types.Expr, error) {
2929
return types.NewExpr(LockJSONWriteOp, args, retType), nil
3030
}
3131

32-
func (*lockJSONWriteOp) InvertExpr(args []types.Value) (types.Value, error) {
32+
func (*lockJSONWriteOp) InvertExpr(_ context.Context, args []types.Value) (types.Value, error) {
3333
return LockJSONWriteOp.Bind(args...)
3434
}
3535

dsl/ops/macro.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ops
22

33
import (
4+
"context"
5+
46
"github.com/vim-volt/volt/dsl/types"
57
)
68

@@ -15,9 +17,9 @@ func (*macroBase) IsMacro() bool {
1517
}
1618

1719
// macroInvertExpr inverts the result of op.Execute() which expands an expression
18-
func (*macroBase) macroInvertExpr(val types.Value, _ func(), err error) (types.Value, error) {
20+
func (*macroBase) macroInvertExpr(ctx context.Context, val types.Value, _ func(), err error) (types.Value, error) {
1921
if err != nil {
2022
return nil, err
2123
}
22-
return val.Invert()
24+
return val.Invert(ctx)
2325
}

dsl/ops/macro_array.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ type arrayOp struct {
1717
// ArrayOp is "$array" operator
1818
var ArrayOp = &arrayOp{macroBase("$array")}
1919

20-
func (op *arrayOp) InvertExpr(args []types.Value) (types.Value, error) {
21-
return op.macroInvertExpr(op.EvalExpr(context.Background(), args))
20+
func (op *arrayOp) InvertExpr(ctx context.Context, args []types.Value) (types.Value, error) {
21+
val, rollback, err := op.EvalExpr(ctx, args)
22+
return op.macroInvertExpr(ctx, val, rollback, err)
2223
}
2324

2425
func (*arrayOp) Bind(args ...types.Value) (types.Expr, error) {

dsl/ops/macro_eval.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type evalOp struct {
1818
// EvalOp is "$eval" operator
1919
var EvalOp = &evalOp{macroBase("$eval")}
2020

21-
func (op *evalOp) InvertExpr(args []types.Value) (types.Value, error) {
22-
return op.macroInvertExpr(op.EvalExpr(context.Background(), args))
21+
func (op *evalOp) InvertExpr(ctx context.Context, args []types.Value) (types.Value, error) {
22+
val, rollback, err := op.EvalExpr(ctx, args)
23+
return op.macroInvertExpr(ctx, val, rollback, err)
2324
}
2425

2526
func (*evalOp) Bind(args ...types.Value) (types.Expr, error) {
@@ -31,5 +32,5 @@ func (*evalOp) EvalExpr(ctx context.Context, args []types.Value) (types.Value, f
3132
if err := util.Signature(types.AnyValue).Check(args); err != nil {
3233
return nil, NoRollback, err
3334
}
34-
return args[0].Eval(context.Background())
35+
return args[0].Eval(ctx)
3536
}

dsl/ops/macro_invert.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ type invertOp struct {
1818
// InvertOp is "$invert" operator
1919
var InvertOp = &invertOp{macroBase("$invert")}
2020

21-
func (op *invertOp) InvertExpr(args []types.Value) (types.Value, error) {
22-
return op.macroInvertExpr(op.EvalExpr(context.Background(), args))
21+
func (op *invertOp) InvertExpr(ctx context.Context, args []types.Value) (types.Value, error) {
22+
val, rollback, err := op.EvalExpr(ctx, args)
23+
return op.macroInvertExpr(ctx, val, rollback, err)
2324
}
2425

2526
func (*invertOp) Bind(args ...types.Value) (types.Expr, error) {
@@ -31,6 +32,6 @@ func (*invertOp) EvalExpr(ctx context.Context, args []types.Value) (types.Value,
3132
if err := util.Signature(types.AnyValue).Check(args); err != nil {
3233
return nil, NoRollback, err
3334
}
34-
val, err := args[0].Invert()
35+
val, err := args[0].Invert(ctx)
3536
return val, NoRollback, err
3637
}

dsl/types/expr.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import "context"
44

55
// Expr has an operation and its arguments
66
type Expr interface {
7+
Value
8+
79
// Op returns operator of Expr
810
Op() Op
911

@@ -12,16 +14,6 @@ type Expr interface {
1214

1315
// RetType returns return type of Expr
1416
RetType() Type
15-
16-
// Eval evaluates given expression expr with given transaction ID trxID.
17-
Eval(ctx context.Context) (val Value, rollback func(), err error)
18-
19-
// Invert inverts this expression.
20-
// This just calls Op().InvertExpr() with saved arguments.
21-
Invert() (Value, error)
22-
23-
// Type returns the type.
24-
Type() Type
2517
}
2618

2719
// NewExpr creates Expr instance
@@ -51,8 +43,8 @@ func (expr *expr) Eval(ctx context.Context) (val Value, rollback func(), err err
5143
return expr.op.EvalExpr(ctx, expr.args)
5244
}
5345

54-
func (expr *expr) Invert() (Value, error) {
55-
return expr.op.InvertExpr(expr.args)
46+
func (expr *expr) Invert(ctx context.Context) (Value, error) {
47+
return expr.op.InvertExpr(ctx, expr.args)
5648
}
5749

5850
func (expr *expr) Type() Type {

dsl/types/json.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var NullValue = &nullT{}
99

1010
type nullT struct{}
1111

12-
func (*nullT) Invert() (Value, error) {
12+
func (*nullT) Invert(context.Context) (Value, error) {
1313
return NullValue, nil
1414
}
1515

@@ -53,7 +53,7 @@ func (v *boolT) Value() bool {
5353
return v.value
5454
}
5555

56-
func (v *boolT) Invert() (Value, error) {
56+
func (v *boolT) Invert(context.Context) (Value, error) {
5757
return v, nil
5858
}
5959

@@ -88,7 +88,7 @@ func (v *numberT) Value() float64 {
8888
return v.value
8989
}
9090

91-
func (v *numberT) Invert() (Value, error) {
91+
func (v *numberT) Invert(context.Context) (Value, error) {
9292
return v, nil
9393
}
9494

@@ -123,7 +123,7 @@ func (v *stringT) Value() string {
123123
return v.value
124124
}
125125

126-
func (v *stringT) Invert() (Value, error) {
126+
func (v *stringT) Invert(context.Context) (Value, error) {
127127
return v, nil
128128
}
129129

@@ -161,7 +161,7 @@ func (v *arrayT) Value() []Value {
161161
return v.value
162162
}
163163

164-
func (v *arrayT) Invert() (Value, error) {
164+
func (v *arrayT) Invert(context.Context) (Value, error) {
165165
return v, nil
166166
}
167167

@@ -199,7 +199,7 @@ func (v *objectT) Value() map[string]Value {
199199
return v.value
200200
}
201201

202-
func (v *objectT) Invert() (Value, error) {
202+
func (v *objectT) Invert(context.Context) (Value, error) {
203203
return v, nil
204204
}
205205

dsl/types/op.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Op interface {
88
String() string
99

1010
// InvertExpr returns inverted expression
11-
InvertExpr(args []Value) (Value, error)
11+
InvertExpr(ctx context.Context, args []Value) (Value, error)
1212

1313
// Bind binds its arguments, and check if the types of values are correct
1414
Bind(args ...Value) (Expr, error)

dsl/types/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Value interface {
88
// All type values are invertible.
99
// Literals like string,number,... return itself as-is.
1010
// If argument type or arity is different, this returns non-nil error.
11-
Invert() (Value, error)
11+
Invert(ctx context.Context) (Value, error)
1212

1313
// Eval returns a evaluated value.
1414
// Literals like string,number,... return itself as-is.

0 commit comments

Comments
 (0)