我使用以下代码来解析 yaml 并应得到输出为runners
对象和函数build
应更改数据结构并根据以下结构提供输出
type Exec struct {
NameVal string
Executer []string
}
这是我尝试过的,但我不知道如何做代替
硬编码值在函数运行器内部,从我在 yaml 中获取的值
return []Exec{
{"#mytest",
[]string{"spawn child process", "build", "gulp"}},
}
与来自的数据parsed runner
这就是我所尝试的一切,你知道如何做到吗?
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"}},
}
}
}
}