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

Overwrite existing slices when unmarshaling #105

Closed
wants to merge 1 commit into from
Closed

Overwrite existing slices when unmarshaling #105

wants to merge 1 commit into from

Conversation

SamWhited
Copy link
Contributor

This brings the behavior inline with that of the standard library (eg.
encoding/json). This does change the behavior when the second slice is smaller (eg. before a new slice with 1 element would have overwritten just the first element of the existing slice, now you end up with a new slice). I think this is what most users will expect though (especially since it's what encoding/json and the like do).

Fixes #49
Fixes #99

Eg. the following now works (which would have produced a panic before):

package main

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

func main() {
    blob := []byte(`
    test = ["aaa", "bbb"]

    [[table]]
    a = "test"
    `)

    type log struct {
        A string
        B string
    }

    type config struct {
        Table []log `toml:"table"`
        Test  []string
    }

    c := new(config)

    err := toml.Unmarshal(blob, &c)
    if err != nil {
        panic(err)
    }

    blob2 := []byte(`
    test = ["ccc"]

    [[table]]
    a = "test2"

    [[table]]
    a = "test3"
    `)

    err = toml.Unmarshal(blob2, &c)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%+v\n", c)
}

Output:

&{Table:[{A:test2 B:} {A:test3 B:}] Test:[ccc]}

This brings the behavior inline with that of the standard library (eg.
encoding/json).

Fixes #49
@cespare
Copy link
Collaborator

cespare commented Feb 23, 2016

This was taken care of in #84.

@cespare cespare closed this Feb 23, 2016
@SamWhited SamWhited deleted the array-decoding-fix branch February 23, 2016 05:18
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

Successfully merging this pull request may close these issues.

2 participants