-
Notifications
You must be signed in to change notification settings - Fork 782
/
context.go
233 lines (194 loc) · 6.55 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package parser
import (
"errors"
"io"
"os"
"path/filepath"
"github.com/benthosdev/benthos/v4/internal/bloblang/query"
)
// Context contains context used throughout a Bloblang parser for
// accessing function and method constructors.
type Context struct {
Functions *query.FunctionSet
Methods *query.MethodSet
namedContext *namedContext
importer Importer
}
// EmptyContext returns a parser context with no functions, methods or import
// capabilities.
func EmptyContext() Context {
return Context{
Functions: query.NewFunctionSet(),
Methods: query.NewMethodSet(),
importer: newOSImporter(),
}
}
// GlobalContext returns a parser context with globally defined functions and
// methods.
func GlobalContext() Context {
return Context{
Functions: query.AllFunctions,
Methods: query.AllMethods,
importer: newOSImporter(),
}
}
type namedContext struct {
name string
next *namedContext
}
// WithNamedContext returns a Context with a named execution context.
func (pCtx Context) WithNamedContext(name string) Context {
next := pCtx.namedContext
pCtx.namedContext = &namedContext{name: name, next: next}
return pCtx
}
// HasNamedContext returns true if a given name exists as a named context.
func (pCtx Context) HasNamedContext(name string) bool {
tmp := pCtx.namedContext
for tmp != nil {
if tmp.name == name {
return true
}
tmp = tmp.next
}
return false
}
// InitFunction attempts to initialise a function from the available
// constructors of the parser context.
func (pCtx Context) InitFunction(name string, args *query.ParsedParams) (query.Function, error) {
return pCtx.Functions.Init(name, args)
}
// InitMethod attempts to initialise a method from the available constructors of
// the parser context.
func (pCtx Context) InitMethod(name string, target query.Function, args *query.ParsedParams) (query.Function, error) {
return pCtx.Methods.Init(name, target, args)
}
// WithImporter returns a Context where imports are made from the provided
// Importer implementation.
func (pCtx Context) WithImporter(importer Importer) Context {
pCtx.importer = importer
return pCtx
}
// WithImporterRelativeToFile returns a Context where any relative imports will
// be made from the directory of the provided file path. The provided path can
// itself be relative (to the current importer directory) or absolute.
func (pCtx Context) WithImporterRelativeToFile(pathStr string) Context {
pCtx.importer = pCtx.importer.RelativeToFile(pathStr)
return pCtx
}
// Deactivated returns a version of the parser context where all functions and
// methods exist but can no longer be instantiated. This means it's possible to
// parse and validate mappings but not execute them. If the context also has an
// importer then it will also be replaced with an implementation that always
// returns empty files.
func (pCtx Context) Deactivated() Context {
nextCtx := pCtx
nextCtx.Functions = pCtx.Functions.Deactivated()
nextCtx.Methods = pCtx.Methods.Deactivated()
return nextCtx
}
// CustomImporter returns a version of the parser context where file imports are
// done exclusively through a provided closure function, which takes an import
// path (relative or absolute).
func (pCtx Context) CustomImporter(fn func(name string) ([]byte, error)) Context {
nextCtx := pCtx
nextCtx.importer = newCustomImporter(fn)
return nextCtx
}
// DisabledImports returns a version of the parser context where file imports
// are entirely disabled. Any import statement within parsed mappings will
// return parse errors explaining that file imports are disabled.
func (pCtx Context) DisabledImports() Context {
nextCtx := pCtx
nextCtx.importer = disabledImporter{}
return nextCtx
}
// ImportFile attempts to read a file for import via the customised Importer.
func (pCtx Context) ImportFile(name string) ([]byte, error) {
return pCtx.importer.Import(name)
}
//------------------------------------------------------------------------------
// Importer represents a repository of bloblang files that can be imported by
// mappings. It's possible for mappings to import files using relative paths, if
// the import is from a mapping which was itself imported then the path should
// be interpretted as relative to that file.
type Importer interface {
// Import a file from a relative or absolute path.
Import(pathStr string) ([]byte, error)
// Derive a new importer where relative import paths are resolved from the
// directory of the provided file path. The provided path could be absolute,
// or relative itself in which case it should be resolved from the
// pre-existing relative directory.
RelativeToFile(filePath string) Importer
}
//------------------------------------------------------------------------------
type osImporter struct {
relativePath string
}
func newOSImporter() Importer {
pwd, _ := os.Getwd()
return &osImporter{
relativePath: pwd,
}
}
func (i *osImporter) Import(pathStr string) ([]byte, error) {
if !filepath.IsAbs(pathStr) {
pathStr = filepath.Join(i.relativePath, pathStr)
}
f, err := os.Open(pathStr)
if err != nil {
return nil, err
}
return io.ReadAll(f)
}
func (i *osImporter) RelativeToFile(filePath string) Importer {
dir := filepath.Dir(filePath)
if dir == "" || dir == "." {
return i
}
pathStr := filepath.Dir(filePath)
if !filepath.IsAbs(pathStr) && i.relativePath != "" {
pathStr = filepath.Join(i.relativePath, pathStr)
}
newI := *i
newI.relativePath = pathStr
return &newI
}
//------------------------------------------------------------------------------
type customImporter struct {
relativePath string
readFn func(name string) ([]byte, error)
}
func newCustomImporter(readFn func(name string) ([]byte, error)) Importer {
return &customImporter{
relativePath: ".",
readFn: readFn,
}
}
func (i *customImporter) Import(pathStr string) ([]byte, error) {
if !filepath.IsAbs(pathStr) {
pathStr = filepath.Join(i.relativePath, pathStr)
}
return i.readFn(pathStr)
}
func (i *customImporter) RelativeToFile(filePath string) Importer {
dir := filepath.Dir(filePath)
if dir == "" || dir == "." {
return i
}
pathStr := filepath.Dir(filePath)
if !filepath.IsAbs(pathStr) && i.relativePath != "" {
pathStr = filepath.Join(i.relativePath, pathStr)
}
newI := *i
newI.relativePath = pathStr
return &newI
}
//------------------------------------------------------------------------------
type disabledImporter struct{}
func (d disabledImporter) Import(pathStr string) ([]byte, error) {
return nil, errors.New("imports are disabled in this context")
}
func (d disabledImporter) RelativeToFile(filePath string) Importer {
return d
}