Not being able to reliably undecode a toml under a table

This issue has been created since 2022-10-18.

Hi all, thanks for all your amazing contribution to this open source project.

I am facing an issue that I think that has not been addressed or is just very well hidden into the docs.

If I have the following code:

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

}

which decodes the key2 value under the [kiwi] table name. If I change the table name to something like [ki] it will still resolve ["ki.key2]" as the undecoded, when in reality it should detect that all the keys are from a different table, so all of them should be marked as undecoded.

Is there any way of reliably undecoding a toml document which is behind one (or more) tables?
What I mean by reliably undecoding is to also check the name of the table and to verify if the name of the table is the same as the struct which is used to decode it.

I have followed and adapted the same tutorial shown on your docs: https://godocs.io/github.com/BurntSushi/toml#example-package--StrictDecoding

If this is somewhat achievable, please you should add it into the docs so more people searching for this kind of stuff can get their issues resolved as fast as possible.

Best regards.

arp242 wrote this answer on 2022-11-17

If I understand your issue correctly, then you want a TOML document such as this:

[ki]
    key1 = "value1"
    key2 = "value2"
    key3 = "value3"

To have ki.key1, ki.key2, and ki.key3 all be undecoded, as ki is not a known table for your application (only kiwi is)?

Did I understand that correctly?

lockedinspace wrote this answer on 2022-11-17

Exactly, that's it! @arp242

arp242 wrote this answer on 2022-11-17

Coolio, just making sure.

You need to use a struct in that case, rather than a map. This way you can specify which tables you want. For example:

package main

import (
    "fmt"

    "github.com/BurntSushi/toml"
)

type (
    config struct {
        Kiwi kiwi
    }
    kiwi struct {
        Key1 string
        Key3 string
    }
)

func main() {
    try(`
        [kiwi]
            key1 = "value1"
            key2 = "value2"
            key3 = "value3"
    `)

    try(`
        [ki]
            key1 = "value1"
            key2 = "value2"
            key3 = "value3"
    `)
}

func try(blob string) {
    var conf config
    md, err := toml.Decode(blob, &conf)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Decoded: %#v\nUndecoded keys: %q\n", conf.Kiwi, md.Undecoded())
}

Outputs:

Decoded: main.kiwi{Key1:"value1", Key3:"value3"}
Undecoded keys: ["kiwi.key2"]

Decoded: main.kiwi{Key1:"", Key3:""}
Undecoded keys: ["ki" "ki.key1" "ki.key2" "ki.key3"]

You don't need to create two types, can also use:

type config struct {
    Kiwi struct {
        Key1 string
        Key3 string
    }
}

Remember that you need to export the Kiwi field on the config type.

arp242 wrote this answer on 2022-11-17

And then you use conf.Kiwi.Key1, instead of conf["kiwi"].Key1, which is better anyway. In general you should only use map[string]type if you really want to accept any key. But if you know what the data looks like: use structs instead; they're much easier to use too as you have more typing.

lockedinspace wrote this answer on 2022-11-17

Appreciate your effort into all of this! You can mark this issue as resolved/closed, you solved my case! Much thanks :)

More Details About Repo
Owner Name BurntSushi
Repo Name toml
Full Name BurntSushi/toml
Language Go
Created Date 2013-02-26
Updated Date 2023-03-20
Star Count 4154
Watcher Count 84
Fork Count 518
Issue Count 15

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Pickle FileNotFoundError in evaluation phase of distributed training 2 2022-09-28 2023-01-29
Show all items disabled if an overrides file is read only 0 2022-01-29 2023-03-15
[Safe Apps Analytics] Relative share of transaction counts coming from Safe Apps 0 2022-11-08 2023-01-19
bug: panic when fetch result from CN 0 2022-08-11 2023-02-20
Append options to kernel 2 2019-09-10 2023-02-19
Add message to v1 that users should update to v2 3 2021-03-10 2022-09-29
Huge discrepancies between GCP provided CO2 and LUT calculations 3 2023-01-23 2023-03-11
Duplicated logging messages 5 2022-06-15 2023-02-13
Request for metric with sma function fails for empty metrics 0 2021-12-02 2023-03-16
Objective functions should not compute the sensitivities upon `set_up` 0 2021-07-22 2023-01-04
Network latency issue? 1 2022-12-12 2023-02-01
Server listen "localhost" 504 error 1 2022-12-27 2023-02-01
Annotation style only captures 'errors' type 1 2022-07-17 2023-02-08
keepaliveTime - to keep the min pool size alive after wait_timeout 3 2022-09-27 2023-02-19
Fire alarm aliases end up in info folder 3 2020-10-24 2023-01-05
APIs for SWIG + Tcl & Python 6 2022-11-23 2023-02-11
Onedrive client exits on uploading actions, exception code 141 (but downloading works). 11 2021-06-26 2023-03-15
stop recieve updates after some time 1 2021-09-28 2023-03-11
Creation of Invalid VSphereDB/ESX Login Entries possible 0 2023-01-19 2023-03-10
GZIP on POST/PUT requests 0 2019-07-02 2023-02-14
mismatch Basic Information and Bridged Device Basic Information Clusters in SDK vs spec 1 2021-10-25 2023-03-23
http-response Warning: status code shouldn't be a string / express deprecated res.status("200") 1 2022-12-20 2023-03-14
google.query is not a function 2 2023-01-10 2023-03-09
Text color seems to be automatically converted to RGB 18 2021-08-31 2023-03-15
ccl/changefeedccl: TestChangefeedTestTimesOut failed 0 2023-01-12 2023-02-11
Inline DatePicker is not working after close/open 5 2021-07-30 2023-01-29
parallel http stress test exposes multiple client related pmproxy/libpcp_web race conditions 0 2021-05-31 2022-11-02
config overrides - support additional imports for go generics 4 2022-06-06 2023-02-17
Still active? Any Alternatives 0 2021-12-15 2022-08-06
expo-camera crashes on SDK46 build or expo-barcode-scanner conflicts with expo-camera 5 2022-11-28 2023-02-13
BUG: clip function works with float arrays but not ExtendedArrays (similar problem to #45778) 2 2022-02-12 2023-03-07
Not and Issue but a Thank You 👍 2 2023-03-04 2023-03-15
info.partial not refetching query 5 2021-08-29 2023-02-12
Setting JVM option via system property (e.g. liberty.jvm.xyz ) doesn't override POM property of same name 1 2022-07-19 2023-03-01
Priv ISA v1.12: PTW report page fault instead of access fault if PTE reserved bit set 0 2021-03-18 2023-03-15
Java 13 EA CI server sanity build 3 2019-05-19 2023-02-28
Ansible role fails in --check mode 5 2021-05-19 2023-02-05
how to get Anything parameter 2 2023-01-05 2023-03-04
Subgraph Box Geo Transform 4 2020-07-29 2023-02-23
Manually overriding a mock method doesn't work 2 2021-06-09 2023-03-03
we can't define a stub on an object that we want to mock before constructing it, but the constructor for the mock prevents us from stubbing the public member 1 2021-06-04 2023-03-03
It is possible to get negative nr_selfcollect_tend in P3 2 2021-06-25 2023-02-20
Rufus formats and installs on SDCard while USB stick is selected 2 2021-11-18 2023-02-11
Update built-in zlib to fix CVE-2018-25032 0 2022-05-16 2023-02-08
Keep alive 1 2019-09-12 2022-01-12
Release Date 3 2019-11-25 2021-11-02
Add `si` to the list of supported languages 6 2022-09-21 2022-11-26
Reduce round trips to ES during rule creation/update 1 2022-01-25 2023-03-16
[Bug Report][3.0.0-beta.5] v-text-field's label when 'readonly' is set true. 1 2022-07-27 2023-02-05
FK differences between 2 tables. 5 2022-06-10 2023-02-05