Search code examples
gostructtoml

How to parse nested arrays / sub tables in TOML?


I'm starting with Go and the official documentation seems more for people who know Go already and just want to look stuff up. I'm hoping for a little nudge in the right direction here.

What I'm looking to do: Parse a TOML config file with BurntSushi's TOML parser that consists of several elements sharing the same base characteristics.

Where I'm stuck: I want each item's respective parts to be listed with the item. So far I can only get one of them by its index. What I'm looking for is how to set it up in a way that lists all sub parts of the respective index instead of just a specific one. I can get a JSON list with [:] but can't seem to adapt that to get normal output.

Initially I had considered [[item.part.001]] and so on because it looked right in the online JSON parsers, but wasn't able to figure out how to read that properly into Go. Since I'm stuck anyway, I'm open to both types, whatever works best.

Thanks in advance. Here are the files as an abbreviated minimum working example.


demo.toml

# — — — — —  — — — — — — — — — — — — — — — — — — 
#                   First Item
# — — — — —  — — — — — — — — — — — — — — — — — — 

[[item]]
itemname = "Fragments"
itemdesc = "This one can get a bit longer."

    [item.attributes]
    material = "Basematname"
    light    = "Lightname"
    transpc  = "full"
    displace = "height"

    [[item.part]]
    partname = "Shard"
    partlink = "active"

    [[item.part]]
    partname = "Tear"
    partlink = "deferred"

    [[item.part]]
    partname = "crater"
    partlink = "disabled"


# — — — — —  — — — — — — — — — — — — — — — — — — 
#                   Second Item
# — — — — —  — — — — — — — — — — — — — — — — — — 

[[item]]
itemname = "Splash"
itemdesc = "This one also can get a bit longer."

    [item.attributes]
    material = "Other Basematname"
    light    = "Other Lightname"
    transpc  = "half"
    displace = "bump"

    [[item.part]]
    partname = "Drops"
    partlink = "active"

    [[item.part]]
    partname = "Wave"
    partlink = "deferred"

demo.go

package main 

import (
    "fmt"
    "github.com/BurntSushi/toml"
)

type item struct {
    ItemName    string
    ItemDesc    string
    Attributes  attributes
    Part        []part
}

type part struct {
    PartName    string
    PartLink    string
}

type attributes struct {
    Material    string
    Light       string
    TransPC     string
    Displace    string
}

type items struct {
    Item    []item
}



func main() {


    var allitems items

    if _, err := toml.DecodeFile("demo.toml", &allitems); err != nil {
        fmt.Println(err)
        return
    }

    fmt.Printf("\n")

    for _, items := range allitems.Item {
        fmt.Printf("    Item Name: %s \n", items.ItemName)
        fmt.Printf("  Description: %s \n\n", items.ItemDesc)

        fmt.Printf("     Material: %s \n", items.Attributes.Material)
        fmt.Printf("     Lightmap: %s \n", items.Attributes.Light)
        fmt.Printf(" TL Precision: %s \n", items.Attributes.TransPC)
        fmt.Printf("   DP Channel: %s \n", items.Attributes.Displace)


        fmt.Printf("    Part Name: %s \n", items.Part[0].PartName)
        fmt.Printf("    Part Link: %s \n", items.Part[0].PartLink)
        #                                             ^
        #                              That's where [:] won't do it.   

        fmt.Printf("\n────────────────────────────────────────────────────┤\n\n")


    }

    fmt.Printf("\n")


}

Solution

  • As pointed out in the comments, you need a nested loop. Instead of this:

        fmt.Printf("    Part Name: %s \n", items.Part[0].PartName)
        fmt.Printf("    Part Link: %s \n", items.Part[0].PartLink)
    

    use this:

        for _, part := range items.Part {
            fmt.Printf("    Part Name: %s \n", part.PartName)
            fmt.Printf("    Part Link: %s \n", part.PartLink)
        }