wrs*_*der 15 jsonpath amazon-web-services aws-step-functions
我想为传递给步进函数的参数设置一个默认值
例如,
"Parameters": {
"foo.$": "$.foo" OR "bar" if "$.foo" not specified
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用 JSONPath 本地做到这一点,还是我必须使用选择+传递状态?
如果在输入中未指定参数时有一种方法不会中断,我什至会选择使用选择/通过。
如果我不包含"foo": ""在输入中,我会收到类似的错误"JSONPath ... could not be found in the input."
Bre*_*yan 29
实际上有一种更简单的方法,它仍然使用额外的步骤;但恕我直言,这比选择+通过更好。这个概念是将默认值定义为一种Pass状态。这是必需的,因为我们要将默认值与输入合并。
我已经证明,您可以一次设置多个默认值,这揭示了不使用 Choice 的好处。
注意,当我想引用被调用的参数时,我总是使用$$.Execution.Input而不是依赖于步骤函数的输入。这让我可以四处移动步骤,而不必担心另一个状态会消灭它们。
{
"Define Defaults": {
"Type": "Pass",
"Next": "Apply Defaults",
"ResultPath": "$.inputDefaults",
"Parameters": {
"foo": "bar",
"x": -1,
"baz": null
}
},
"Apply Defaults": {
"Type": "Pass",
"Next": "Start Work",
"ResultPath": "$.withDefaults",
"OutputPath": "$.withDefaults.args",
"Parameters": {
"args.$": "States.JsonMerge($.inputDefaults, $$.Execution.Input, false)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后这将为您提供一个产生的值:
{
"foo": "bar",
"x": -1,
"baz": null
}
Run Code Online (Sandbox Code Playgroud)
小智 18
Choice我正在使用和状态的组合来解决这个问题Pass。假设状态机至少获得一个空输入对象,您可以使用状态IsPresent中的比较运算符检查它是否存在成员Choice。如果您的愿望变量不存在,您可以路由到Pass状态来注入默认的后备对象。
{
"keyThatMightNotExist": {
"options": {
"foo": "bar",
"baz": false
},
"id": 1234
}
}
Run Code Online (Sandbox Code Playgroud)
{
"Comment": "An example for how to deal with empty input and setting defaults.",
"StartAt": "Choice State: looking for input",
"States": {
"Choice State: looking for input": {
"Type": "Choice",
"Choices": [
{
Run Code Online (Sandbox Code Playgroud)
检查是否存在,如果存在,还验证子成员:
"And": [
{
"Variable": "$.keyThatMightNotExist",
"IsPresent": true
},
{
"Variable": "$.keyThatMightNotExist.id",
"IsNull": false
}
],
Run Code Online (Sandbox Code Playgroud)
如果 key 变量存在且其子节点"id"为true,则跳过下一个状态并跳转到“与 $.keyThatMightNotExist 配合使用的状态”
"Next": "State that works with $.keyThatMightNotExist"
}
],
"Default": "LoadDefaults"
},
Run Code Online (Sandbox Code Playgroud)
以下是我们注入默认值的地方:
"LoadDefaults": {
"Type": "Pass",
"Result": {
"id": 0,
"text": "not applicable"
},
"ResultPath": "$.keyThatMightNotExist",
"Next": "State that works with $.keyThatMightNotExist"
},
Run Code Online (Sandbox Code Playgroud)
此时,就有一个可以使用的对象了。来自实际输入或使用默认值:
"State that works with $.keyThatMightNotExist": {
"Type": "Succeed"
}
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅AWS Step Functions 开发人员指南,选择-状态-示例
| 归档时间: |
|
| 查看次数: |
4820 次 |
| 最近记录: |