Skip to content

Commit 9c4bfce

Browse files
committed
fix: dsl.Execute(): validate types of props in context
1 parent 0c59fa9 commit 9c4bfce

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

dsl/execute.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55

66
"github.com/pkg/errors"
7+
"github.com/vim-volt/volt/config"
78
"github.com/vim-volt/volt/dsl/op"
89
"github.com/vim-volt/volt/dsl/types"
10+
"github.com/vim-volt/volt/lockjson"
11+
"github.com/vim-volt/volt/transaction"
912
)
1013

1114
// CtxKeyType is the type of the key of context specified for Execute()
@@ -22,17 +25,47 @@ const (
2225

2326
// Execute executes given expr with given ctx.
2427
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
2831
}{
29-
{CtxLockJSONKey, "lock.json"},
30-
{CtxConfigKey, "config.toml"},
31-
{CtxTrxIDKey, "transaction ID"},
32+
{CtxLockJSONKey, validateLockJSON},
33+
{CtxConfigKey, validateConfig},
34+
{CtxTrxIDKey, validateTrxID},
3235
} {
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
3538
}
3639
}
3740
return expr.Eval(ctx)
3841
}
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

Comments
 (0)