fakecloud-stepfunctions 0.51.0

Step Functions implementation for FakeCloud
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use serde_json::Value;

use crate::jsonpath::{JsonPath, PathError, Step};

/// A Step Functions error: `(error, cause)`.
pub type StatesError = (String, String);

/// Apply InputPath to extract a subset of the raw input.
/// - `None` or `Some("$")` -> return input unchanged
/// - `Some("null")` handled at call site (pass `{}`)
/// - any other JSONPath -> the selected value; a definite path that matches
///   nothing fails the state with `States.Runtime`, as on AWS
pub fn apply_input_path(input: &Value, path: Option<&str>) -> Result<Value, StatesError> {
    match path {
        None | Some("$") => Ok(input.clone()),
        Some(p) => resolve_reference(input, p),
    }
}

/// Apply OutputPath to extract a subset of the effective output.
/// Same semantics as InputPath.
pub fn apply_output_path(output: &Value, path: Option<&str>) -> Result<Value, StatesError> {
    match path {
        None | Some("$") => Ok(output.clone()),
        Some(p) => resolve_reference(output, p),
    }
}

/// Apply ResultPath to merge a state's result into the input.
/// - `None` or `Some("$")` -> result replaces input entirely
/// - `Some("null")` -> discard result, return original input
/// - `Some("$.foo")` -> set result at that path within input
pub fn apply_result_path(input: &Value, result: &Value, path: Option<&str>) -> Value {
    match path {
        None | Some("$") => result.clone(),
        Some("null") => input.clone(),
        Some(p) => set_at_path(input, p, result),
    }
}

/// Evaluate a JSONPath against `root` (see [`crate::jsonpath`]).
pub fn resolve_path(root: &Value, path: &str) -> Result<Value, PathError> {
    crate::jsonpath::evaluate(root, path)
}

/// Resolve a path, mapping a miss to the `States.Runtime` error AWS raises
/// for InputPath / OutputPath / ItemsPath / SecondsPath and friends:
/// `Invalid path '$.x' : No results for path: $['x']`.
pub fn resolve_reference(root: &Value, path: &str) -> Result<Value, StatesError> {
    resolve_path(root, path).map_err(|e| runtime_error(path_error_cause(path, &e)))
}

/// [`resolve_reference`] for a path that may address the context object
/// (`$$...`), as `ItemsPath`, `SecondsPath` and friends may.
pub fn resolve_reference_with_context(
    input: &Value,
    context: Option<&Value>,
    path: &str,
) -> Result<Value, StatesError> {
    resolve_with_context(input, context, path)
        .map_err(|e| runtime_error(path_error_cause(path, &e)))
}

/// Resolve a path that may address the context object (`$$...`). Without a
/// context object, `$$` paths resolve to null.
pub fn resolve_with_context(
    input: &Value,
    context: Option<&Value>,
    path: &str,
) -> Result<Value, PathError> {
    match path.strip_prefix("$$") {
        Some(rest) => match context {
            Some(ctx) => resolve_path(ctx, &format!("${rest}")),
            None => Ok(Value::Null),
        },
        None => resolve_path(input, path),
    }
}

pub fn path_error_cause(path: &str, err: &PathError) -> String {
    match err {
        PathError::NoResults(norm) => {
            format!("Invalid path '{path}' : No results for path: {norm}")
        }
        PathError::Invalid(msg) => format!("Invalid path '{path}' : {msg}"),
    }
}

pub fn runtime_error(cause: String) -> StatesError {
    ("States.Runtime".to_string(), cause)
}

/// Set a value at a reference path (`$.a.b`, `$['a'][0]`) within a JSON
/// structure. An index step descends into the array stored at that point,
/// growing it with nulls as needed. A path that runs into a scalar leaves the
/// input unchanged.
fn set_at_path(root: &Value, path: &str, value: &Value) -> Value {
    let mut result = root.clone();
    let Some(steps) = JsonPath::parse(path).ok().and_then(|p| p.reference_steps()) else {
        return result;
    };

    // The empty container a missing intermediate node is created as.
    fn container_for(next: &Step) -> Value {
        match next {
            Step::Name(_) => serde_json::json!({}),
            Step::Index(_) => Value::Array(Vec::new()),
        }
    }

    fn assign(current: &mut Value, steps: &[Step], value: &Value) {
        let Some((step, rest)) = steps.split_first() else {
            *current = value.clone();
            return;
        };
        match step {
            Step::Name(name) => {
                if let Some(obj) = current.as_object_mut() {
                    match rest.first() {
                        None => {
                            obj.insert(name.clone(), value.clone());
                        }
                        Some(next) => {
                            let child = obj
                                .entry(name.clone())
                                .or_insert_with(|| container_for(next));
                            assign(child, rest, value);
                        }
                    }
                }
            }
            Step::Index(idx) => {
                if let Some(arr) = current.as_array_mut() {
                    let idx = if *idx < 0 {
                        match usize::try_from(arr.len() as i64 + idx) {
                            Ok(i) => i,
                            Err(_) => return,
                        }
                    } else {
                        *idx as usize
                    };
                    if arr.len() <= idx {
                        arr.resize(idx + 1, Value::Null);
                    }
                    if let Some(next) = rest.first() {
                        if arr[idx].is_null() {
                            arr[idx] = container_for(next);
                        }
                    }
                    assign(&mut arr[idx], rest, value);
                }
            }
        }
    }

    assign(&mut result, &steps, value);
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_resolve_path_root() {
        let input = json!({"a": 1});
        assert_eq!(resolve_path(&input, "$").unwrap(), input);
    }

    #[test]
    fn test_resolve_path_simple_field() {
        let input = json!({"name": "hello", "value": 42});
        assert_eq!(resolve_path(&input, "$.name").unwrap(), json!("hello"));
        assert_eq!(resolve_path(&input, "$.value").unwrap(), json!(42));
    }

    #[test]
    fn test_resolve_path_nested() {
        let input = json!({"a": {"b": {"c": 99}}});
        assert_eq!(resolve_path(&input, "$.a.b.c").unwrap(), json!(99));
    }

    #[test]
    fn test_resolve_path_missing() {
        let input = json!({"a": 1});
        assert_eq!(
            resolve_path(&input, "$.missing").unwrap_err(),
            PathError::NoResults("$['missing']".into())
        );
    }

    #[test]
    fn test_resolve_path_array_index() {
        let input = json!({"items": [10, 20, 30]});
        assert_eq!(resolve_path(&input, "$.items[0]").unwrap(), json!(10));
        assert_eq!(resolve_path(&input, "$.items[2]").unwrap(), json!(30));
    }

    #[test]
    fn test_apply_input_path_default() {
        let input = json!({"x": 1});
        assert_eq!(apply_input_path(&input, None).unwrap(), input);
        assert_eq!(apply_input_path(&input, Some("$")).unwrap(), input);
    }

    #[test]
    fn test_apply_result_path_default() {
        let input = json!({"x": 1});
        let result = json!({"y": 2});
        // Default: result replaces input
        assert_eq!(apply_result_path(&input, &result, None), result);
        assert_eq!(apply_result_path(&input, &result, Some("$")), result);
    }

    #[test]
    fn test_apply_result_path_null() {
        let input = json!({"x": 1});
        let result = json!({"y": 2});
        // null: discard result, keep input
        assert_eq!(apply_result_path(&input, &result, Some("null")), input);
    }

    #[test]
    fn test_apply_result_path_nested() {
        let input = json!({"x": 1});
        let result = json!("hello");
        let output = apply_result_path(&input, &result, Some("$.result"));
        assert_eq!(output, json!({"x": 1, "result": "hello"}));
    }

    #[test]
    fn test_set_at_path_non_object_intermediate() {
        // When an intermediate path segment is a non-object (e.g., a number),
        // set_at_path should not panic — it should bail out gracefully.
        let input = json!({"x": 42});
        let result = json!("hello");
        let output = apply_result_path(&input, &result, Some("$.x.nested"));
        // x is a number, can't set nested on it — should return input unchanged
        assert_eq!(output, json!({"x": 42}));
    }

    // L8: ResultPath with an array-index segment must descend into the array,
    // not insert a literal key like "a[0]".
    #[test]
    fn test_set_at_path_array_index_leaf() {
        let input = json!({"a": [1, 2, 3]});
        let result = json!(99);
        let output = apply_result_path(&input, &result, Some("$.a[1]"));
        assert_eq!(output, json!({"a": [1, 99, 3]}));
        // No stray literal "a[1]" key was created.
        assert!(output.get("a[1]").is_none());
    }

    #[test]
    fn test_set_at_path_array_index_then_field() {
        let input = json!({"items": [{"v": 1}]});
        let result = json!("done");
        let output = apply_result_path(&input, &result, Some("$.items[0].status"));
        assert_eq!(output, json!({"items": [{"v": 1, "status": "done"}]}));
    }

    #[test]
    fn test_set_at_path_array_index_grows_array() {
        let input = json!({});
        let result = json!("x");
        let output = apply_result_path(&input, &result, Some("$.a[2]"));
        assert_eq!(output, json!({"a": [null, null, "x"]}));
    }

    #[test]
    fn test_apply_output_path() {
        let output = json!({"a": 1, "b": 2});
        assert_eq!(apply_output_path(&output, Some("$.a")).unwrap(), json!(1));
        assert_eq!(apply_output_path(&output, None).unwrap(), output);
    }

    #[test]
    fn test_resolve_path_unclosed_bracket_does_not_panic() {
        // Malformed JSONPath: `[` with no trailing `]`. Previously this sliced
        // `[bracket_pos + 1 .. part.len() - 1]` which underflows -> panic.
        let input = json!({"arr": [1, 2, 3]});
        // No field literally named "arr[" exists, so this resolves to Null,
        // but the key requirement is that it must NOT panic.
        assert!(matches!(
            resolve_path(&input, "$.arr["),
            Err(PathError::Invalid(_))
        ));
    }

    #[test]
    fn test_resolve_path_multibyte_after_bracket_does_not_panic() {
        // Multibyte char where the close bracket would be. `part.len() - 1`
        // previously landed mid-char -> "byte index is not a char boundary".
        let input = json!({"x": [1, 2, 3]});
        assert!(matches!(
            resolve_path(&input, "$.x[é"),
            Err(PathError::Invalid(_))
        ));
        // Also the closed-but-multibyte-inner case.
        assert!(matches!(
            resolve_path(&input, "$.x[é]"),
            Err(PathError::Invalid(_))
        ));
    }

    #[test]
    fn test_resolve_path_empty_brackets_do_not_panic() {
        let input = json!({"x": [1, 2, 3]});
        assert!(matches!(
            resolve_path(&input, "$.x[]"),
            Err(PathError::Invalid(_))
        ));
    }

    #[test]
    fn test_resolve_path_bracket_only_segment() {
        // A bare `[` as an entire segment.
        let input = json!({"a": 1});
        assert!(matches!(
            resolve_path(&input, "$.["),
            Err(PathError::Invalid(_))
        ));
        assert!(matches!(
            resolve_path(&input, "$.]"),
            Err(PathError::Invalid(_))
        ));
    }

    #[test]
    fn test_split_path_segments_well_formed_index_still_works() {
        // Ensure the hardening did not break the happy path.
        let input = json!({"items": [10, 20, 30]});
        assert_eq!(resolve_path(&input, "$.items[1]").unwrap(), json!(20));
        let nested = json!({"a": {"b": [{"c": 7}]}});
        assert_eq!(resolve_path(&nested, "$.a.b[0].c").unwrap(), json!(7));
    }

    #[test]
    fn test_apply_input_path_malformed_does_not_panic() {
        let input = json!({"arr": [1, 2, 3]});
        // Exercised via the public apply_* entrypoints used by the interpreter.
        let (err, _) = apply_input_path(&input, Some("$.arr[")).unwrap_err();
        assert_eq!(err, "States.Runtime");
        assert!(apply_output_path(&input, Some("$.x[é")).is_err());
    }
}

#[cfg(test)]
mod missing_path_tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn missing_input_path_is_states_runtime() {
        let (err, cause) = apply_input_path(&json!({"a": 1}), Some("$.x")).unwrap_err();
        assert_eq!(err, "States.Runtime");
        assert_eq!(cause, "Invalid path '$.x' : No results for path: $['x']");
        let (err, _) = apply_output_path(&json!({"a": 1}), Some("$.a.b")).unwrap_err();
        assert_eq!(err, "States.Runtime");
        // A present null is fine.
        assert_eq!(
            apply_input_path(&json!({"a": null}), Some("$.a")).unwrap(),
            Value::Null
        );
    }

    #[test]
    fn full_jsonpath_syntax_in_input_path() {
        let input =
            json!({"items": [{"id": 1, "ok": true}, {"id": 2, "ok": false}], "k": {"x y": 5}});
        assert_eq!(
            apply_input_path(&input, Some("$['k']['x y']")).unwrap(),
            json!(5)
        );
        assert_eq!(
            apply_input_path(&input, Some("$.items[-1].id")).unwrap(),
            json!(2)
        );
        assert_eq!(
            apply_input_path(&input, Some("$.items[*].id")).unwrap(),
            json!([1, 2])
        );
        assert_eq!(
            apply_input_path(&input, Some("$.items[?(@.ok == true)].id")).unwrap(),
            json!([1])
        );
        assert_eq!(
            apply_input_path(&input, Some("$.items[0:1]")).unwrap(),
            json!([{"id": 1, "ok": true}])
        );
    }

    #[test]
    fn result_path_bracket_notation() {
        let out = apply_result_path(&json!({"a": {}}), &json!(1), Some("$['a']['b c']"));
        assert_eq!(out, json!({"a": {"b c": 1}}));
        let out = apply_result_path(&json!({"a": [1, 2]}), &json!(9), Some("$.a[-1]"));
        assert_eq!(out, json!({"a": [1, 9]}));
    }

    #[test]
    fn context_paths() {
        let ctx = json!({"Execution": {"Id": "arn"}});
        assert_eq!(
            resolve_with_context(&json!({}), Some(&ctx), "$$.Execution.Id").unwrap(),
            json!("arn")
        );
        assert!(resolve_with_context(&json!({}), Some(&ctx), "$$.Nope").is_err());
        assert_eq!(
            resolve_with_context(&json!({"a": 1}), None, "$.a").unwrap(),
            json!(1)
        );
    }
}