Home > Blockchain >  Go find undecoded values in a toml document
Go find undecoded values in a toml document

Time:10-20

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.

CodePudding user response:

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"]  

}

CodePudding user response:

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())
  •  Tags:  
  • go
  • Related