英文:
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)。
按照上面的示例,我只想确保哈希表也是正确的:
var blob = `
[kiwi]
key1 = "value1"
key2 = "value2"
key3 = "value3"
`
var conf struct {
Key1 string
Key3 string
}
md, err := toml.Decode(blob, &conf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
// 期望输出
// Undecoded keys: ["kiwi.key2"]
// 实际输出
// 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:
var blob = `
[kiwi]
key1 = "value1"
key2 = "value2"
key3 = "value3"
`
var conf struct {
Key1 string
Key3 string
}
md, err := toml.Decode(blob, &conf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
// Desired output
// Undecoded keys: ["kiwi.key2"]
// Actual output
// 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
type kiwi struct {
Key1 string
Key3 string
}
func main() {
var blob = `
[kiwi]
key1 = "value1"
key2 = "value2"
key3 = "value3"
`
var conf map[string]kiwi
md, err := toml.Decode(blob, &conf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("未解码的键: %q\n", md.Undecoded())
//未解码的键: ["kiwi.key2"]
}
请注意,我只翻译了代码部分,其他内容不做翻译。
英文:
https://github.com/BurntSushi/toml/blob/master/_example/example.toml
type kiwi struct {
Key1 string
Key3 string
}
func main() {
var blob = `
[kiwi]
key1 = "value1"
key2 = "value2"
key3 = "value3"
`
var conf map[string]kiwi
md, err := toml.Decode(blob, &conf)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
//Undecoded keys: ["kiwi.key2"]
}
答案2
得分: 0
最终,使用以下结构体,它将仅成功检查表名:
type config struct {
Kiwi struct {
Key1 string
Key3 string
}
}
var conf config
md, err := toml.Decode(blob, &conf)
if err != nil {
panic(err)
}
fmt.Printf("未解码的键:%q\n", md.Undecoded())
英文:
Finally with the following struct, it will only succesfully check for the table name:
type config struct {
Kiwi struct {
Key1 string
Key3 string
}
}
var conf config
md, err := toml.Decode(blob, &conf)
if err != nil {
panic(err)
}
fmt.Printf("Undecoded keys: %q\n", md.Undecoded())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论