@@ -4,8 +4,11 @@ import (
4
4
"context"
5
5
6
6
"github.com/pkg/errors"
7
+ "github.com/vim-volt/volt/config"
7
8
"github.com/vim-volt/volt/dsl/op"
8
9
"github.com/vim-volt/volt/dsl/types"
10
+ "github.com/vim-volt/volt/lockjson"
11
+ "github.com/vim-volt/volt/transaction"
9
12
)
10
13
11
14
// CtxKeyType is the type of the key of context specified for Execute()
@@ -22,17 +25,47 @@ const (
22
25
23
26
// Execute executes given expr with given ctx.
24
27
func Execute (ctx context.Context , expr * types.Expr ) (val types.Value , rollback func (), err error ) {
25
- for _ , st := range []struct {
26
- key CtxKeyType
27
- name string
28
+ for _ , required := range []struct {
29
+ key CtxKeyType
30
+ validate func ( interface {}) error
28
31
}{
29
- {CtxLockJSONKey , "lock.json" },
30
- {CtxConfigKey , "config.toml" },
31
- {CtxTrxIDKey , "transaction ID" },
32
+ {CtxLockJSONKey , validateLockJSON },
33
+ {CtxConfigKey , validateConfig },
34
+ {CtxTrxIDKey , validateTrxID },
32
35
} {
33
- if ctx .Value (st .key ) = = nil {
34
- return nil , op .NoRollback , errors . Errorf ( "no %s key in context" , st . name )
36
+ if err := required . validate ( ctx .Value (required .key )); err ! = nil {
37
+ return nil , op .NoRollback , err
35
38
}
36
39
}
37
40
return expr .Eval (ctx )
38
41
}
42
+
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" )
49
+ }
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
+ }
62
+
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" )
69
+ }
70
+ return nil
71
+ }
0 commit comments