dongqing6661 2018-08-21 12:26
浏览 279
已采纳

根据值匹配数组

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
}

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"}},
}

with the data from the 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"}},
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • donglianer5064 2018-08-21 12:45
    关注

    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
    }
    

    Working code on Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟