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

Support unmarshal into toml.Tree #347

Merged
merged 1 commit into from
Apr 3, 2020

Conversation

AllenX2018
Copy link
Contributor

Issue: #333

This pr add support of *toml.Tree marshaling/unmarshaling. With this, you can unmarshal parts of a TOML file into an *toml.Tree for later inspection and unmarshaling.

@A-UNDERSCORE-D
Copy link

Tested this with some more complex and nested elements (Which probably was covered by your test, but I wanted to try anyway) and it worked perfectly:

package main

import (
	"fmt"
	"go-toml"
)

type test struct {
	A string
	B int
	C float64
	D innerComplex
}

type innerComplex struct {
	Type    string
	Content *toml.Tree
}

type innerSimple struct {
	Stuff    string
	Things   float64
	someMap  map[string]int
	someList []string
}

func main() {
	const testStr = `
	A = "this is A"
	B = 10
	C = 3.14159
	[D]
	Type = "Something"
	[D.content]
	stuff = "thing"
	things = 3.14235
	someList = ["this", "is", "a", "test"]
	[D.content.somemap]
	life = 42
	st = 47
	leet = 1337
	`

	x := new(test)
	toml.Unmarshal([]byte(testStr), x)
	fmt.Printf("%#v\n", x)
	res, err := x.D.Content.ToTomlString()
	if err != nil {
		panic(err)
	}
	fmt.Printf("----\n%s\n", res)
}
&main.test{A:"this is A", B:10, C:3.14159, D:main.innerComplex{Type:"Something", Content:(*toml.Tree)(0xc000072960)}}
----
someList = ["this","is","a","test"]
stuff = "thing"
things = 3.14235

[somemap]
  leet = 1337
  life = 42
  st = 47

@pelletier pelletier changed the title fix issue #333 Support unmarshal into toml.Tree Apr 3, 2020
@pelletier
Copy link
Owner

Looking great! Patch is cleaner than I anticipated. Thank you for taking care of this!

@pelletier pelletier merged commit 8e8d2a6 into pelletier:master Apr 3, 2020
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.

3 participants