Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dhiltgen opened this issue Mar 17, 2017 · 1 comment
Closed

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

dhiltgen opened this issue Mar 17, 2017 · 1 comment

Comments

@dhiltgen
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant