YAML List of Unnamed Strings With Optional Boolean

huangapple go评论154阅读模式
英文:

YAML List of Unnamed Strings With Optional Boolean

问题

我想定义一个默认情况下定义字符串列表的YAML。我不希望这个字符串列表是一个命名属性。我还想有一个可选的布尔参数。代码示例如下:

package main

import (
    "fmt"
    "log"

    yaml "gopkg.in/yaml.v2"
)

type ThingAndGroups struct {
    Groups  []string
    BoolVal bool `yaml:"boolval,omitempty"`
}

var someStr = `
thing1:
  - g1
  - g2
  boolval:
    y

thing2:
  - g1
  - g2
`

func main() {
    t := make(map[string]ThingAndGroups)

    err := yaml.Unmarshal([]byte(someStr), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:\n%v\n\n", t)
}

这样会返回:

map[thing1:{[g1, g2] true} thing2:{[g1, g2] false}]

这样做是可行的。

我不想将YAML定义为:

var someStr = `
thing1:
  groups:
    - g1
    - g2
  boolval:
    y`

如果YAML中没有boolval,我可以这样做:

func main() {
    // 注意这是一个map
    t := make(map[string][]string)

    err := yaml.Unmarshal([]byte(someStr), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:\n%v\n\n", t)
}
英文:

I want to define YAML that defines a list of strings by default. I don't want this list of strings to be a named attribute. I also want to have an optional boolean parameter. As in

package main

import (
    "fmt"
    "log"

    yaml "gopkg.in/yaml.v2"
)

type ThingAndGroups struct {
    Groups   []string
    boolval  boolean
}

var someStr = `
thing1:
  - g1
  - g2
  boolval:
    y

thing2:
  - g1
  - g2
`

func main() {
    t := make(map[string]ThingAndGroups)

    err := yaml.Unmarshal([]byte(someStr), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:\n%v\n\n", t)
}

Would then return

> map[thing1:{[g1, g2] true}, thing2:{[g1, g2] false}]

Is that possible?

I DO NOT want to define the YAML as

var someStr = `
thing1:
  groups:
    - g1
    - g2
  boolval:
    y`

And if the YAML didn't have boolval I could just do

 func main() {
    // NOTE THIS IS A MAP
    t := make(map[string][]string)

    err := yaml.Unmarshal([]byte(someStr), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:\n%v\n\n", t)
}

答案1

得分: 1

问题

  • 在不使用映射键的情况下定义一个YAML序列。

解决方案

  • 将BEFORE更改为AFTER

BEFORE

  var someStr = `
  thing1:
    - g1
    - g2
    boolval:
      y

  thing2:
    - g1
    - g2
  `

AFTER

  var someStr = `
  thing1:
    - g1
    - g2
    - boolval: y

  thing2:
    - g1
    - g2
  `

注意事项

  • 这种方法不理想,因为它使用YAML序列来存储两种不同类型的数据(组和布尔值)。
  • 另一个回答(后来被删除)建议使用groups键,这是一个很好的建议。
英文:

Problem

  • Defining a YAML sequence without using a mapping key.

Solution

  • Change BEFORE into AFTER

BEFORE

  var someStr = `
  thing1:
    - g1
    - g2
    boolval:
      y

  thing2:
    - g1
    - g2
  `

AFTER

  var someStr = `
  thing1:
    - g1
    - g2
    - boolval: y

  thing2:
    - g1
    - g2
  `

Pitfalls

  • This approach is not desirable because it uses the YAML sequence to store two different kinds of data (the groups and the boolean value).
  • Another answer (that was later deleted) suggested using a groups key, and that was a good suggestion.

huangapple
  • 本文由 发表于 2017年5月8日 03:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/43835778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定