BurntSushi / toml

TOML parser for Golang with reflection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issues populating an Array of Tables

BrandonC98 opened this issue · comments

I can't seem to get values back from an array of tables

Code:

package main

import (
	"fmt"
	"os"

	"github.com/BurntSushi/toml"
)

type (
	Node struct {
		Key	string
		Arr	[]SubNode
	}

	SubNode	struct {
		Name	string
	}
)

func main() {
	t := `
		key="Val"

		[[SubNode]]
		name="first"

		[[SubNode]]
		name="second"
	`

	var node Node
	_, err := toml.Decode(t, &node)
	if err != nil {
		os.Exit(1)
	}
	
	fmt.Println("node key: " + node.Key)
	fmt.Println("arr: " + node.Arr[0].Name)
}

output:

node key: Val
panic: runtime error: index out of range [0] with length 0

it's able to pull the value from the root level table's field but doesn't seem to register there being any values in the array of tables. Is there something I'm missing?

The struct field name doesn't match with what's in the TOML file; it doesn't know that [[SubNode]] is supposed to go in to that Arr field. You'll have to use:

type Node struct {
	Key string
	Arr []SubNode `toml:"SubNode"`
}

Or:

type Node struct {
	Key     string
	SubNode []SubNode
}