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

Unmarshal to map #150

Closed
n10v opened this issue Apr 1, 2017 · 7 comments
Closed

Unmarshal to map #150

n10v opened this issue Apr 1, 2017 · 7 comments
Labels
feature Issue asking for a new feature in go-toml.

Comments

@n10v
Copy link
Contributor

n10v commented Apr 1, 2017

It will be very useful for nicksnyder/go-i18n#66 to unmarshal TOML to maps, not only to structs.

@tro3
Copy link

tro3 commented Apr 1, 2017

It should be able to marshal/unmarshal a map of string to <type> now, where <type> is anything but interface{}. Or do you mean at the top level?

[Edit] Never mind - you must mean at the top level. That shouldn't be too difficult. I'll try to take a look at it the next couple of days.

@n10v
Copy link
Contributor Author

n10v commented Apr 1, 2017

I just try to execute this code:

var v map[string]map[string]interface{}
if err := toml.Unmarshal(tomlData, v); err != nil {
    panic(err)
}

and I become an error Only a pointer to struct can be unmarshaled from TOML. I looked into Unmarshal func and in https://github.com/pelletier/go-toml/blob/master/marshal.go#L216 I saw, what v should be obligatorily pointer to a struct.

@tro3
Copy link

tro3 commented Apr 3, 2017

A top-level of map[string]T, where T is a specific type, is not too bad to implement. But map[string]interface{} (or map[string]map[string]interface{} is not trivial. I won't have time to attack that for the foreseeable future.

@pelletier
Copy link
Owner

@BoGeM would the ToMap() method be enough in your case? Or do you actually have a more specific structure to map to?

@n10v
Copy link
Contributor Author

n10v commented Apr 6, 2017

Yes, it's enough for me, but we should add some extra code now to unmarshal TOML (like this). It will be more convenient to just pass our map to Unmarshal function (like this)

@pelletier
Copy link
Owner

Yes that would make sense. Marking it as a future improvement.

@pelletier
Copy link
Owner

Better late than never! Since #341 got merged, the following test passes:

	tomlData := `
[a]
b = 1
[c]
d = "e"
`

	expected := map[string]map[string]interface{}{
		"a": map[string]interface{}{
			"b": int64(1),
		},
		"c": map[string]interface{}{
			"d": "e",
		},
	}
	
	var v map[string]map[string]interface{}
	if err := Unmarshal([]byte(tomlData), &v); err != nil {
		panic(err)
	}
	if !reflect.DeepEqual(expected, v) {
		t.Errorf("Bad unmarshal: expected %v, got %v", expected, v)
	}

I it closes this issue. If the latest master version does not work for you feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Issue asking for a new feature in go-toml.
Projects
None yet
Development

No branches or pull requests

3 participants