@@ -4,68 +4,32 @@ import (
4
4
"context"
5
5
6
6
"github.com/pkg/errors"
7
- "github.com/vim-volt/volt/config"
8
- "github.com/vim-volt/volt/dsl/ops"
7
+ "github.com/vim-volt/volt/dsl/dslctx"
9
8
"github.com/vim-volt/volt/dsl/types"
10
- "github.com/vim-volt/volt/lockjson"
11
9
"github.com/vim-volt/volt/transaction"
12
10
)
13
11
14
- // CtxKeyType is the type of the key of context specified for Execute()
15
- type CtxKeyType uint
16
-
17
- const (
18
- // CtxTrxIDKey is the key to get transaction ID
19
- CtxTrxIDKey CtxKeyType = iota
20
- // CtxLockJSONKey is the key to get *lockjson.LockJSON value
21
- CtxLockJSONKey
22
- // CtxConfigKey is the key to get *config.Config value
23
- CtxConfigKey
24
- )
25
-
26
12
// Execute executes given expr with given ctx.
27
- func Execute (ctx context.Context , expr types.Expr ) (val types.Value , rollback func (), err error ) {
28
- for _ , required := range []struct {
29
- key CtxKeyType
30
- validate func (interface {}) error
31
- }{
32
- {CtxLockJSONKey , validateLockJSON },
33
- {CtxConfigKey , validateConfig },
34
- {CtxTrxIDKey , validateTrxID },
35
- } {
36
- if err := required .validate (ctx .Value (required .key )); err != nil {
37
- return nil , ops .NoRollback , err
38
- }
13
+ func Execute (ctx context.Context , expr types.Expr ) (_ types.Value , result error ) {
14
+ if err := dslctx .Validate (ctx ); err != nil {
15
+ return nil , err
39
16
}
40
- return expr .Eval (ctx )
41
- }
42
17
43
- func validateLockJSON (v interface {}) error {
44
- if v == nil {
45
- return errors .New ("no lock.json key in context" )
46
- }
47
- if _ , ok := v .(* lockjson.LockJSON ); ! ok {
48
- return errors .New ("invalid lock.json data in context" )
18
+ // Begin transaction
19
+ trx , err := transaction .Start ()
20
+ if err != nil {
21
+ return nil , err
49
22
}
50
- return nil
51
- }
52
-
53
- func validateConfig (v interface {}) error {
54
- if v == nil {
55
- return errors .New ("no config.toml key in context" )
56
- }
57
- if _ , ok := v .(* config.Config ); ! ok {
58
- return errors .New ("invalid config.toml data in context" )
59
- }
60
- return nil
61
- }
23
+ defer func () {
24
+ if err := trx .Done (); err != nil {
25
+ result = err
26
+ }
27
+ }()
62
28
63
- func validateTrxID (v interface {}) error {
64
- if v == nil {
65
- return errors .New ("no transaction ID key in context" )
66
- }
67
- if _ , ok := v .(transaction.TrxID ); ! ok {
68
- return errors .New ("invalid transaction ID data in context" )
29
+ val , rollback , err := expr .Eval (ctx )
30
+ if err != nil {
31
+ rollback ()
32
+ return nil , errors .Wrap (err , "expression returned an error" )
69
33
}
70
- return nil
34
+ return val , nil
71
35
}
0 commit comments