naoina / toml

TOML parser and encoder library for Golang
MIT License
294 stars 50 forks source link

Marshal doesn't handle multiple struct tag fields properly #33

Closed dhiltgen closed 7 years ago

dhiltgen commented 7 years ago

If you have multiple struct field tags, and the first one isn't "toml" then it's ignored and the default logic kicks in for naming the fields in the serialized output.

Example:


package main

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

type cfg struct {
        MySetting string `json:"JSONMySetting",toml:"toml_my_setting"` // Does not work
        //MySetting string `toml:"toml_my_setting",json:"JSONMySetting"` // Works
}

func main() {
        c1 := cfg{"foo"}

        data, err := toml.Marshal(&c1)
        if err != nil {
                panic(err)
        }
        fmt.Printf("Serialized: %s", string(data))

        var c2 cfg

        if err := toml.Unmarshal(data, &c2); err != nil {
                panic(err)
        }
        fmt.Printf("Unmarshaled: %#v\n", c2)
}

Expected output:

Serialized: toml_my_setting="foo"
Unmarshaled: main.cfg{MySetting:"foo"}

Actual output:

Serialized: my_setting="foo"
Unmarshaled: main.cfg{MySetting:"foo"}
dhiltgen commented 7 years ago

Nevermind, it's supposed to be a space, not a comma.