Debian Codegraph @

execution/options.go

raw symbols SCIP
1 package execution
2
3 import "github.com/tomwright/dasel/v3/model"
4
5 // ExecuteOptionFn is a function that can be used to set options on the execution of the selector.
6 type ExecuteOptionFn func(*Options)
7
8 // Options contains the options for the execution of the selector.
9 type Options struct {
10 Funcs FuncCollection
11 Vars map[string]*model.Value
12 Unstable bool
13 }
14
15 // NewOptions creates a new Options struct with the given options.
16 func NewOptions(opts ...ExecuteOptionFn) *Options {
17 o := &Options{
18 Funcs: DefaultFuncCollection,
19 Vars: map[string]*model.Value{},
20 }
21 for _, opt := range opts {
22 if opt == nil {
23 continue
24 }
25 opt(o)
26 }
27 return o
28 }
29
30 // WithFuncs sets the functions that can be used in the selector.
31 func WithFuncs(fc FuncCollection) ExecuteOptionFn {
32 return func(o *Options) {
33 o.Funcs = fc
34 }
35 }
36
37 // WithVariable sets a variable for use in the selector.
38 func WithVariable(key string, val *model.Value) ExecuteOptionFn {
39 return func(o *Options) {
40 o.Vars[key] = val
41 }
42 }
43
44 // WithUnstable allows access to potentially unstable features.
45 func WithUnstable() ExecuteOptionFn {
46 return func(o *Options) {
47 o.Unstable = true
48 }
49 }
50
51 // WithoutUnstable disallows access to potentially unstable features.
52 func WithoutUnstable() ExecuteOptionFn {
53 return func(o *Options) {
54 o.Unstable = false
55 }
56 }
57
58 // withKeyVar sets the $key variable to the given value and returns a function
59 // that restores the previous value (or deletes it if it didn't exist).
60 func withKeyVar(options *Options, key *model.Value) func() {
61 prev, had := options.Vars["key"]
62 options.Vars["key"] = key
63 return func() {
64 if had {
65 options.Vars["key"] = prev
66 } else {
67 delete(options.Vars, "key")
68 }
69 }
70 }