在一个toml文档中查找未解码的值。

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

Go find undecoded values in a toml document

问题

我正在使用BurntSushi的go解析器(https://godocs.io/github.com/BurntSushi/toml),并按照他的示例来解码值,但似乎无法解码哈希表中的toml文档,以查找意外添加的额外键。

官方文档中解释了如何解码toml文档以查找任何额外的键(https://godocs.io/github.com/BurntSushi/toml#MetaData.Undecoded)。
按照上面的示例,我只想确保哈希表也是正确的:

  1. var blob = `
  2. [kiwi]
  3. key1 = "value1"
  4. key2 = "value2"
  5. key3 = "value3"
  6. `
  7. var conf struct {
  8. Key1 string
  9. Key3 string
  10. }
  11. md, err := toml.Decode(blob, &conf)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
  16. // 期望输出
  17. // Undecoded keys: ["kiwi.key2"]
  18. // 实际输出
  19. // Undecoded keys: ["kiwi" "kiwi.key1" "kiwi.key2" "kiwi.key3"]

我该如何将哈希表[kiwi]表示为conf结构体?

Go Playground链接:
https://go.dev/play/p/uHKbCEDwkg_p

谢谢阅读,任何帮助都将不胜感激。

英文:

I am using BurntSushi's go parser (https://godocs.io/github.com/BurntSushi/toml) and following his example to undecode values, It seems like I can't undecode a toml document inside a hash table to find extra keys that were added accidentaly.

Official docs, explaining how to decode a toml document to find any extra keys (https://godocs.io/github.com/BurntSushi/toml#MetaData.Undecoded)
Following the example above, I just want to ensure that the hash table is also correct:

  1. var blob = `
  2. [kiwi]
  3. key1 = "value1"
  4. key2 = "value2"
  5. key3 = "value3"
  6. `
  7. var conf struct {
  8. Key1 string
  9. Key3 string
  10. }
  11. md, err := toml.Decode(blob, &conf)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
  16. // Desired output
  17. // Undecoded keys: ["kiwi.key2"]
  18. // Actual output
  19. // Undecoded keys: ["kiwi" "kiwi.key1" "kiwi.key2" "kiwi.key3"]

How do I represent the hash table [kiwi] into the conf struct?

Go Playground link:
https://go.dev/play/p/uHKbCEDwkg_p

Thanks for reading, any help is appreciated.

答案1

得分: 0

  1. type kiwi struct {
  2. Key1 string
  3. Key3 string
  4. }
  5. func main() {
  6. var blob = `
  7. [kiwi]
  8. key1 = "value1"
  9. key2 = "value2"
  10. key3 = "value3"
  11. `
  12. var conf map[string]kiwi
  13. md, err := toml.Decode(blob, &conf)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. fmt.Printf("未解码的键: %q\n", md.Undecoded())
  18. //未解码的键: ["kiwi.key2"]
  19. }

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:

https://github.com/BurntSushi/toml/blob/master/_example/example.toml

  1. type kiwi struct {
  2. Key1 string
  3. Key3 string
  4. }
  5. func main() {
  6. var blob = `
  7. [kiwi]
  8. key1 = "value1"
  9. key2 = "value2"
  10. key3 = "value3"
  11. `
  12. var conf map[string]kiwi
  13. md, err := toml.Decode(blob, &conf)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
  18. //Undecoded keys: ["kiwi.key2"]
  19. }

答案2

得分: 0

最终,使用以下结构体,它将仅成功检查表名:

  1. type config struct {
  2. Kiwi struct {
  3. Key1 string
  4. Key3 string
  5. }
  6. }
  7. var conf config
  8. md, err := toml.Decode(blob, &conf)
  9. if err != nil {
  10. panic(err)
  11. }
  12. fmt.Printf("未解码的键:%q\n", md.Undecoded())
英文:

Finally with the following struct, it will only succesfully check for the table name:

  1. type config struct {
  2. Kiwi struct {
  3. Key1 string
  4. Key3 string
  5. }
  6. }
  7. var conf config
  8. md, err := toml.Decode(blob, &conf)
  9. if err != nil {
  10. panic(err)
  11. }
  12. fmt.Printf("Undecoded keys: %q\n", md.Undecoded())

huangapple
  • 本文由 发表于 2022年10月18日 04:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/74102892.html
匿名

发表评论

匿名网友

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

确定