-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathmethods.rs
More file actions
295 lines (279 loc) · 10 KB
/
methods.rs
File metadata and controls
295 lines (279 loc) · 10 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use serde_json_bytes::Value as JSON;
use shape::Shape;
use super::ApplyToError;
use super::MethodArgs;
use super::VarsWithPathsMap;
use super::immutable::InputPath;
use super::location::WithRange;
use crate::connectors::json_selection::ShapeContext;
use crate::connectors::spec::ConnectSpec;
mod common;
// Two kinds of methods: public ones and not-yet-public ones. The future ones
// have proposed implementations and tests, and some are even used within the
// tests of other methods, but are not yet exposed for use in connector schemas.
// Graduating to public status requires updated documentation, careful review,
// and team discussion to make sure the method is one we want to support
// long-term. Once we have a better story for checking method type signatures
// and versioning any behavioral changes, we should be able to expand/improve
// the list of public::* methods more quickly/confidently.
mod future;
mod public;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ArrowMethod {
// Public methods:
As,
Echo,
Map,
Match,
First,
Last,
Slice,
Size,
Entries,
JsonParse,
JsonStringify,
JoinNotNull,
Filter,
Find,
Gte,
Lte,
Eq,
Ne,
Or,
And,
Gt,
Lt,
Not,
In,
Contains,
Get,
ToString,
ParseInt,
Add,
Sub,
Mul,
Div,
Mod,
KeysToCamelCase,
KeysToCamelCaseDeep,
// Future methods:
TypeOf,
MatchIf,
Has,
Keys,
Values,
}
#[macro_export]
macro_rules! impl_arrow_method {
($struct_name:ident, $impl_fn_name:ident, $shape_fn_name:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct $struct_name;
impl $crate::connectors::json_selection::methods::ArrowMethodImpl for $struct_name {
fn apply(
&self,
method_name: &WithRange<String>,
method_args: Option<&MethodArgs>,
data: &JSON,
vars: &VarsWithPathsMap,
input_path: &InputPath<JSON>,
spec: $crate::connectors::spec::ConnectSpec,
) -> (Option<JSON>, Vec<ApplyToError>) {
$impl_fn_name(method_name, method_args, data, vars, input_path, spec)
}
fn shape(
&self,
context: &$crate::connectors::json_selection::apply_to::ShapeContext,
method_name: &WithRange<String>,
method_args: Option<&MethodArgs>,
input_shape: Shape,
dollar_shape: Shape,
) -> Shape {
$shape_fn_name(context, method_name, method_args, input_shape, dollar_shape)
}
}
};
}
#[allow(dead_code)] // method type-checking disabled until we add name resolution
pub(super) trait ArrowMethodImpl {
fn apply(
&self,
method_name: &WithRange<String>,
method_args: Option<&MethodArgs>,
data: &JSON,
vars: &VarsWithPathsMap,
input_path: &InputPath<JSON>,
spec: ConnectSpec,
) -> (Option<JSON>, Vec<ApplyToError>);
fn shape(
&self,
context: &ShapeContext,
// Shape processing errors for methods can benefit from knowing the name
// of the method and its source range. Note that ArrowMethodImpl::shape
// is invoked for every invocation of a method, with appropriately
// different source ranges.
method_name: &WithRange<String>,
// Most methods implementing ArrowMethodImpl::shape will need to know
// the shapes of their arguments, which can be computed from MethodArgs
// using the compute_output_shape method.
method_args: Option<&MethodArgs>,
// The input_shape is the shape of the @ variable, or the value from the
// left hand side of the -> token.
input_shape: Shape,
// The dollar_shape is the shape of the $ variable, or the input object
// associated with the closest enclosing subselection.
dollar_shape: Shape,
) -> Shape;
}
// This Deref implementation allows us to call .apply(...) directly on the
// ArrowMethod enum.
impl std::ops::Deref for ArrowMethod {
type Target = dyn ArrowMethodImpl;
fn deref(&self) -> &Self::Target {
match self {
// Public methods:
Self::As => &public::AsMethod,
Self::Echo => &public::EchoMethod,
Self::Map => &public::MapMethod,
Self::Match => &public::MatchMethod,
Self::First => &public::FirstMethod,
Self::Last => &public::LastMethod,
Self::Slice => &public::SliceMethod,
Self::Size => &public::SizeMethod,
Self::Entries => &public::EntriesMethod,
Self::JsonParse => &public::JsonParseMethod,
Self::JsonStringify => &public::JsonStringifyMethod,
Self::JoinNotNull => &public::JoinNotNullMethod,
Self::Filter => &public::FilterMethod,
Self::Find => &public::FindMethod,
Self::Gte => &public::GteMethod,
Self::Lte => &public::LteMethod,
Self::Eq => &public::EqMethod,
Self::Ne => &public::NeMethod,
Self::Or => &public::OrMethod,
Self::And => &public::AndMethod,
Self::Gt => &public::GtMethod,
Self::Lt => &public::LtMethod,
Self::Not => &public::NotMethod,
Self::In => &public::InMethod,
Self::Contains => &public::ContainsMethod,
Self::Get => &public::GetMethod,
Self::ToString => &public::ToStringMethod,
Self::ParseInt => &public::ParseIntMethod,
Self::Add => &public::AddMethod,
Self::Sub => &public::SubMethod,
Self::Mul => &public::MulMethod,
Self::Div => &public::DivMethod,
Self::Mod => &public::ModMethod,
Self::KeysToCamelCase => &public::KeysToCamelCaseMethod,
Self::KeysToCamelCaseDeep => &public::KeysToCamelCaseDeepMethod,
// Future methods:
Self::TypeOf => &future::TypeOfMethod,
Self::MatchIf => &future::MatchIfMethod,
Self::Has => &future::HasMethod,
Self::Keys => &future::KeysMethod,
Self::Values => &future::ValuesMethod,
}
}
}
impl ArrowMethod {
// This method is currently used at runtime to look up methods by &str name,
// but it could be hoisted parsing time, and then we'd store an ArrowMethod
// instead of a String for the method name in the AST.
pub(super) fn lookup(name: &str) -> Option<Self> {
let method_opt = match name {
"as" => Some(Self::As),
"echo" => Some(Self::Echo),
"map" => Some(Self::Map),
"eq" => Some(Self::Eq),
"match" => Some(Self::Match),
// As this case suggests, we can't necessarily provide a name()
// method for ArrowMethod (the opposite of lookup), because method
// implementations can be used under multiple names.
"matchIf" | "match_if" => Some(Self::MatchIf),
"typeof" => Some(Self::TypeOf),
"add" => Some(Self::Add),
"sub" => Some(Self::Sub),
"mul" => Some(Self::Mul),
"div" => Some(Self::Div),
"mod" => Some(Self::Mod),
"first" => Some(Self::First),
"last" => Some(Self::Last),
"slice" => Some(Self::Slice),
"size" => Some(Self::Size),
"has" => Some(Self::Has),
"get" => Some(Self::Get),
"keys" => Some(Self::Keys),
"values" => Some(Self::Values),
"entries" => Some(Self::Entries),
"not" => Some(Self::Not),
"or" => Some(Self::Or),
"and" => Some(Self::And),
"jsonParse" => Some(Self::JsonParse),
"jsonStringify" => Some(Self::JsonStringify),
"joinNotNull" => Some(Self::JoinNotNull),
"filter" => Some(Self::Filter),
"find" => Some(Self::Find),
"gte" => Some(Self::Gte),
"lte" => Some(Self::Lte),
"ne" => Some(Self::Ne),
"gt" => Some(Self::Gt),
"lt" => Some(Self::Lt),
"in" => Some(Self::In),
"contains" => Some(Self::Contains),
"toString" => Some(Self::ToString),
"parseInt" => Some(Self::ParseInt),
"keysToCamelCase" => Some(Self::KeysToCamelCase),
"keysToCamelCaseDeep" => Some(Self::KeysToCamelCaseDeep),
_ => None,
};
match method_opt {
Some(method) if cfg!(test) || method.is_public() => Some(method),
_ => None,
}
}
pub(super) const fn is_public(&self) -> bool {
// This set controls which ->methods are exposed for use in connector
// schemas. Non-public methods are still implemented and tested, but
// will not be returned from lookup_arrow_method outside of tests.
matches!(
self,
Self::As
| Self::Echo
| Self::Map
| Self::Match
| Self::First
| Self::Last
| Self::Slice
| Self::Size
| Self::Entries
| Self::JsonParse
| Self::JsonStringify
| Self::JoinNotNull
| Self::Filter
| Self::Find
| Self::Gte
| Self::Lte
| Self::Eq
| Self::Ne
| Self::Or
| Self::And
| Self::Gt
| Self::Lt
| Self::Not
| Self::In
| Self::Contains
| Self::Get
| Self::ToString
| Self::ParseInt
| Self::Add
| Self::Sub
| Self::Mul
| Self::Div
| Self::Mod
| Self::KeysToCamelCase
| Self::KeysToCamelCaseDeep
)
}
}