BurntSushi / toml

TOML parser for Golang with reflection.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

confused about using `BURNTSUSHI_TOML_110` with `os.Setenv()`

notnmeyer opened this issue · comments

I'm testing out the TOML 1.1 support that's available behind the flag in v1.3.0. the notes mention it can be enabled with either os.Setenv() or setting it explicitly on the command line.

the latter works, but the former doesn't and I'm questioning whether or not I'm misunderstanding it or doing something wrong.

here's a repro case,

package main

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

type Config struct {
        blah map[string]string `toml:"blah"`
}

func main() {
        os.Setenv("BURNTSUSHI_TOML_110", "somevalue")

        tomlData := `blah = {
                foo = "bar",
        }`

        var conf Config
        _, err := toml.Decode(tomlData, &conf)
        if err != nil {
                panic(err)
        }
}

running this directly seem to be validating as TOML 1.0,

➜ go run main.go
panic: toml: line 1 (last key "blah"): newlines not allowed within inline tables

goroutine 1 [running]:
main.main()
        /var/folders/qm/fvys1tr11b7130r0zbbbzpx00000gn/T/tmp.sjsn2xa7/main.go:23 +0xd0
exit status 2

but this works,

➜ BURNTSUSHI_TOML_110=somevalue go run main.go; echo $status
0

am I misunderstanding what the release notes are suggesting about using os.Setenv()?

The variable that sets this is a package-level variable: https://github.com/BurntSushi/toml/blob/60801d0/parse.go#L14

So this will get run as soon as the package is imported, and your os.Setenv("BURNTSUSHI_TOML_110", "somevalue") happens after that.

This is something I changed after I wrote the release note comment for this. It's kind of obvious that it won't work, but I did both things some weeks apart and it didn't really register 😅

I moved it back in the parse; sorry for the confusion.

I tagged v1.3.1 with a fix for this.

appreciate the explanation and fix :) cheers!