YAML List of Unnamed Strings With Optional Boolean

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

YAML List of Unnamed Strings With Optional Boolean

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. yaml "gopkg.in/yaml.v2"
  6. )
  7. type ThingAndGroups struct {
  8. Groups []string
  9. BoolVal bool `yaml:"boolval,omitempty"`
  10. }
  11. var someStr = `
  12. thing1:
  13. - g1
  14. - g2
  15. boolval:
  16. y
  17. thing2:
  18. - g1
  19. - g2
  20. `
  21. func main() {
  22. t := make(map[string]ThingAndGroups)
  23. err := yaml.Unmarshal([]byte(someStr), &t)
  24. if err != nil {
  25. log.Fatalf("error: %v", err)
  26. }
  27. fmt.Printf("--- t:\n%v\n\n", t)
  28. }

这样会返回:

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

这样做是可行的。

我不想将YAML定义为:

  1. var someStr = `
  2. thing1:
  3. groups:
  4. - g1
  5. - g2
  6. boolval:
  7. y`

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

  1. func main() {
  2. // 注意这是一个map
  3. t := make(map[string][]string)
  4. err := yaml.Unmarshal([]byte(someStr), &t)
  5. if err != nil {
  6. log.Fatalf("error: %v", err)
  7. }
  8. fmt.Printf("--- t:\n%v\n\n", t)
  9. }
英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. yaml "gopkg.in/yaml.v2"
  6. )
  7. type ThingAndGroups struct {
  8. Groups []string
  9. boolval boolean
  10. }
  11. var someStr = `
  12. thing1:
  13. - g1
  14. - g2
  15. boolval:
  16. y
  17. thing2:
  18. - g1
  19. - g2
  20. `
  21. func main() {
  22. t := make(map[string]ThingAndGroups)
  23. err := yaml.Unmarshal([]byte(someStr), &t)
  24. if err != nil {
  25. log.Fatalf("error: %v", err)
  26. }
  27. fmt.Printf("--- t:\n%v\n\n", t)
  28. }

Would then return

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

Is that possible?

I DO NOT want to define the YAML as

  1. var someStr = `
  2. thing1:
  3. groups:
  4. - g1
  5. - g2
  6. boolval:
  7. y`

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

  1. func main() {
  2. // NOTE THIS IS A MAP
  3. t := make(map[string][]string)
  4. err := yaml.Unmarshal([]byte(someStr), &t)
  5. if err != nil {
  6. log.Fatalf("error: %v", err)
  7. }
  8. fmt.Printf("--- t:\n%v\n\n", t)
  9. }

答案1

得分: 1

问题

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

解决方案

  • 将BEFORE更改为AFTER

BEFORE

  1. var someStr = `
  2. thing1:
  3. - g1
  4. - g2
  5. boolval:
  6. y
  7. thing2:
  8. - g1
  9. - g2
  10. `

AFTER

  1. var someStr = `
  2. thing1:
  3. - g1
  4. - g2
  5. - boolval: y
  6. thing2:
  7. - g1
  8. - g2
  9. `

注意事项

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

Problem

  • Defining a YAML sequence without using a mapping key.

Solution

  • Change BEFORE into AFTER

BEFORE

  1. var someStr = `
  2. thing1:
  3. - g1
  4. - g2
  5. boolval:
  6. y
  7. thing2:
  8. - g1
  9. - g2
  10. `

AFTER

  1. var someStr = `
  2. thing1:
  3. - g1
  4. - g2
  5. - boolval: y
  6. thing2:
  7. - g1
  8. - g2
  9. `

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:

确定