根据值匹配数组 [英] Match array according to value

查看:41
本文介绍了根据值匹配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来解析 yaml 并且应该将输出作为 runners 对象和函数 build 应该更改数据结构并根据以下结构提供输出

I'm using the following code to parse yaml and should get output as runners object and the function buildshould change the data structure and provide output according to below struct

type Exec struct {
    NameVal string
    Executer []string
}

这是我尝试过的,但我不知道如何替换函数运行器中的硬编码值来自我在 yaml 中获得的值

This is what I have tried but I'm not sure how to replace the hard-code values inside the function runner from the value I'm getting inside the yaml

return []Exec{
    {"#mytest",
        []string{"spawn child process", "build", "gulp"}},
}

使用来自 parsed runner

这就是我尝试过的所有方法,知道怎么做吗?

This is all what I have tried any idea how it could be done?

package main

import (
    "log"

    "gopkg.in/yaml.v2"
)

var runContent = []byte(`
api_ver: 1
runners:
  - name: function1
    data: mytest
    type:
    - command: spawn child process
    - command: build
    - command: gulp
  - name: function2
    data: mytest2
    type:
    - command: webpack
  - name: function3
    data: mytest3
    type:
    - command: ruby build
  - name: function4
    type:
  - command: go build
`)

type Result struct {
    Version string    `yaml:"api_ver"`
    Runners []Runners `yaml:"runners"`
}

type Runners struct {
    Name string    `yaml:"name"`
    Type []Command `yaml:"type"`
}

type Command struct {
    Command string `yaml:"command"`
}

func main() {

    var runners Result
    err := yaml.Unmarshal(runContent, &runners)
    if err != nil {
        log.Fatalf("Error : %v", err)
    }

    //Here Im calling to the function with the parsed structured data  which need to return the list of Exec
    build("function1", runners)

}

type Exec struct {
    NameVal  string
    Executer []string
}

func build(name string, runners Result) []Exec {

    for _, runner := range runners.Runners {

        if name == runner.Name {
            return []Exec{
                // this just for example, nameVal and Command
                {"# mytest",
                    []string{"spawn child process", "build", "gulp"}},
            }
        }
    }
}

推荐答案

将 runners 对象的名称分配给 struct Exec 字段作为 name 并将命令列表附加到 []string 键入与名称匹配的函数命令的字段:

Assign the name of runners object to the struct Exec field for name and append the command list to the []string type field with the commands of the function that matched the name as:

func build(name string, runners Result) []Exec {
    exec := make([]Exec, len(runners.Runners))
    for i, runner := range runners.Runners {

        if name == runner.Name {
            exec[i].NameVal = runner.Name
            for _, cmd := range runner.Type {
                exec[i].Executer = append(exec[i].Executer, cmd.Command)
            }
            fmt.Printf("%+v", exec)
            return exec
        }
    }
    return exec
}

Playground

这篇关于根据值匹配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆