解析Yaml地图错误 [英] Parse yaml error for map

查看:48
本文介绍了解析Yaml地图错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序需要解析yaml具有以下结构

I've the following program in which I need to parse yaml with the following structure

https://codebeautify.org/yaml-validator/cbabd352 这是有效的Yaml ,我使用字节来使其更简单也许在复制粘贴到问题的过程中缩进已更改,但是您可以在链接中看到yaml有效

https://codebeautify.org/yaml-validator/cbabd352 this is valid yaml and I use byte to make it more simple maybe the indentation was changed during the copy paste to the question but you can see in the link that the yaml is valid

Yaml具有api_version和跑步者,对于每个跑步者(键是名称),我都有一个命令列表并且我需要为 function1 function4 打印这些命令,我​​在这里做错了什么?

The yaml have an api_version and runners, for each runner (key which is name) I've a list of commands and I need to print those commands for function1 and function4 ,what am I doing wrong here ?

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

var runContent = []byte(`
api_ver: 1
runners:
 - name: function1
   type:
    - command: spawn child process
    - command: build
    - command: gulp
 - name: function2
   type:
    - command: run function 1
 - name: function3
   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)
    }
    fmt.Printf("%+v", runners[0])
}

我得到的错误无法解组!! map到[] main.Result

我无法更改Yaml,应该完全像这样

I cannot change the yaml and it should be exactly like this

https://codebeautify.org/yaml-validator/cbabd352

这是代码 https://play.golang.org/p/zidjOA6-gc7

推荐答案

您提供的Yaml包含令牌错误.在这里 https://codebeautify.org/yaml-validator/cbaabb32

The yaml that you have provided contains error in token. Validate the yaml used in your code here https://codebeautify.org/yaml-validator/cbaabb32

之后,创建结构类型的变量结果不是数组.因为您使用的Yaml正在使用Runners数组和api_version创建结构.

After that Create a variable of struct type result not an array. Because the yaml that you are using is creating a struct with Runners array and api_version.

var runners []Result

应该是

var runners Result

因为该结构不是切片.为yaml中使用的功能名称获取命令列表.您需要遍历runners数组以查找函数名称并获取该函数的命令值.下面是工作代码:

Since because the struct is not a slice. To fetch the list of command for a name of function used in yaml. You need to loop over the runners array to find the name of function and get the value of commands for that function. Below is the working code:

package main

import (
    "fmt"
    "log"

    "gopkg.in/yaml.v2"
)

var runContent = []byte(`
api_ver: 1
runners:
  - name: function1
    type:
    - command: spawn child process
    - command: build
    - command: gulp
  - name: function2
    type:
    - command: run function 1
    - name: function3
    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
    // parse mta yaml
    err := yaml.Unmarshal(runContent, &runners)
    if err != nil {
        log.Fatalf("Error : %v", err)
    }
    commandList := getCommandList("function1", runners.Runners)
    fmt.Printf("%+v", commandList)
}

func getCommandList(name string, runners []Runners) []Command {
    var commandList []Command
    for _, value := range runners {
        if value.Name == name {
            commandList = value.Type
        }
    }
    return commandList
}

游乐场示例

这篇关于解析Yaml地图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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