Google cloud platform GCP工作流:附加到阵列

Google cloud platform GCP工作流:附加到阵列,google-cloud-platform,google-workflows,Google Cloud Platform,Google Workflows,使用GCP/Google工作流,我希望获取一个数组,并对调用http端点的每个数组应用一个转换。为此,我正在寻找一种方法来应用转换,然后将结果重新组合到另一个数组中。一种方法是,但这是一个实验性的特性,可能会发生变化,所以我有点犹豫是否使用它。另一种方法是从一个空数组开始,并在应用转换时附加到该数组。有办法做到这一点吗?连接字符串似乎是可行的,但似乎没有内置的附加到数组中。我尝试了类似的方法,它确实成功地执行了,但没有产生预期的结果: main: steps: - assignVar

使用GCP/Google工作流,我希望获取一个数组,并对调用http端点的每个数组应用一个转换。为此,我正在寻找一种方法来应用转换,然后将结果重新组合到另一个数组中。一种方法是,但这是一个实验性的特性,可能会发生变化,所以我有点犹豫是否使用它。另一种方法是从一个空数组开始,并在应用转换时附加到该数组。有办法做到这一点吗?连接字符串似乎是可行的,但似乎没有内置的附加到数组中。我尝试了类似的方法,它确实成功地执行了,但没有产生预期的结果:

main:
  steps:
    - assignVariables:
        assign:
            - sourceArray: ["one", "two", "three"]
            - hashedArray: []
            - hashedString: ""
            - i: 0
    - checkCondition:
        switch:
            - condition: ${i < len(sourceArray)}
              next: iterate
        next: returnResult
    - iterate:
        assign:
            - hashedArray[i]: ${"#" + sourceArray[i]}
            - hashedString: ${hashedString + "#" + sourceArray[i] + " "}
            - i: ${i + 1}
        next: checkCondition
    - returnResult:
        return:
            - hashedString: ${hashedString}
            - hashedArray: ${hashedArray}
[
  {
    "hashedString": "#one #two #three "
  },
  {
    "hashedArray": ["#one", "#two", "#three"]
  }
]
[
  "#one",
  "#two",
  "#three"
]
预期结果:

main:
  steps:
    - assignVariables:
        assign:
            - sourceArray: ["one", "two", "three"]
            - hashedArray: []
            - hashedString: ""
            - i: 0
    - checkCondition:
        switch:
            - condition: ${i < len(sourceArray)}
              next: iterate
        next: returnResult
    - iterate:
        assign:
            - hashedArray[i]: ${"#" + sourceArray[i]}
            - hashedString: ${hashedString + "#" + sourceArray[i] + " "}
            - i: ${i + 1}
        next: checkCondition
    - returnResult:
        return:
            - hashedString: ${hashedString}
            - hashedArray: ${hashedArray}
[
  {
    "hashedString": "#one #two #three "
  },
  {
    "hashedArray": ["#one", "#two", "#three"]
  }
]
[
  "#one",
  "#two",
  "#three"
]

对于那些想知道如何使用实验功能解决此问题的人,您可以这样做:

首先创建一个可调用的工作流来转换数组。我给这个工作流命名为
散列项
。此名称很重要,因为我们稍后将其称为
workflow\u id

main:
    params: [item]
    steps:
    - transform:
        assign:
            - hashed: ${"#" + item}
    - returnResult:
        return: ${hashed}
然后创建您的主工作流,该工作流使用
experimental.executions.map
调用
hash item

main:
  steps:
    - assignVariables:
        assign:
            - sourceArray: ["one", "two", "three"]
    - transform:
        call: experimental.executions.map
        args:
            workflow_id: hash-item
            arguments: ${sourceArray}
        result: result
    - returnResult:
        return: ${result}
用于执行主工作流的服务帐户将需要
工作流.executions.create
权限(通常通过
角色/workflows.invoker
角色)。要分配此任务,可以使用Cloud SDK CLI执行此操作:

gcloud projects add-iam-policy-binding <project-id> --member="serviceAccount:<service-account-email>" --role="roles/workflows.invoker"
注:是实验性的,因此可能会发生变化