首页 > 解决方案 > CDK 传递步骤:如何在输入对象的根目录添加属性

问题描述

我想向输入数据的根目录添加一些属性。假设我们有

{
  "f1": 1,
  "cx": {
          "cxf1": 113,
          "cxf2": "f23"
        },
  "cx2": {
          "cxf12": 11,
          "cxf22": "f2"
        }
}

我想创建 CDK Pass 步骤以将简单值和复杂值添加到输入的根并进一步传递组合数据。我应该有输出:

{
  "f1": 1,
  "cx": {
          "cxf1": 113,
          "cxf2": "f23"
        },
  "cx2": {
          "cxf12": 11,
          "cxf22": "f2"
        },
  "out1": "simple",
  "out2complex": {
          "f1A": 111,
          "f2A": "f22"
        }             
}

我尝试了 inputPath、outputhPath、resultpath 的 Whatevr 组合不起作用。它仅在指定结果路径时才有效,并且我的结果将作为复杂元素进入路径。
我认为这是设计使然。如果我只指定 result,这意味着它将覆盖输入。
有没有办法将简单属性和复杂属性添加到输入对象的根并进一步传递?

标签: amazon-web-servicesaws-cdk

解决方案


我们需要将 pass 步骤的输出传递给resultPath

可以说通过步骤输出是一个字符串simple,它将被附加到现有的输入 Json 与out1resultPath: "$.out1"

const firstStep = new stepfunctions.Pass(this, "Build Out1", {
  result: stepfunctions.Result.fromString("simple"),
  resultPath: "$.out1",
});

const secondStep = new stepfunctions.Pass(this, "Build out2complex", {
  result: stepfunctions.Result.fromObject({
    f1A: 111,
    f2A: "f22",
  }),
  resultPath: "$.out2complex",
});
const def = firstStep.next(secondStep);

new stepfunctions.StateMachine(this, "StateMachine", {
  definition: def,
});

在此处输入图像描述

输入:

{
  "f1": 1,
  "cx": {
    "cxf1": 113,
    "cxf2": "f23"
  },
  "cx2": {
    "cxf12": 11,
    "cxf22": "f2"
  }
}

输出:

{
  "f1": 1,
  "cx": {
    "cxf1": 113,
    "cxf2": "f23"
  },
  "cx2": {
    "cxf12": 11,
    "cxf22": "f2"
  },
  "out1": "simple",
  "out2complex": {
    "f1A": 111,
    "f2A": "f22"
  }
}

推荐阅读