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

Decoding into embedded struct #38

Open
vmihailenco opened this issue Aug 1, 2017 · 1 comment
Open

Decoding into embedded struct #38

vmihailenco opened this issue Aug 1, 2017 · 1 comment

Comments

@vmihailenco
Copy link

vmihailenco commented Aug 1, 2017

At some point this library was changed so it can't decode into embedded structs any more

package main

import (
	"fmt"

	"github.com/naoina/toml"
)

type DBConfig struct {
	User string `toml:"user"`
}

type DBClusterConfig struct {
	DBConfig
}

func main() {
	doc := []byte(`
		user = "test_user"
	`)

	config := new(DBClusterConfig)
	if err := toml.Unmarshal(doc, &config); err != nil {
		panic(err)
	}
	fmt.Println("user", config.User)
}

Old revisions (such as 7a3d4a6) work just fine.

@fjl
Copy link
Collaborator

fjl commented Aug 4, 2017

I'm thinking about how to do this. The best way would probably be to use the same rules as "encoding/json", but they're complicated:

// Anonymous struct fields are usually marshaled as if their inner exported fields
// were fields in the outer struct, subject to the usual Go visibility rules amended
// as described in the next paragraph.
// An anonymous struct field with a name given in its JSON tag is treated as
// having that name, rather than being anonymous.
// An anonymous struct field of interface type is treated the same as having
// that type as its name, rather than being anonymous.
//
// The Go visibility rules for struct fields are amended for JSON when
// deciding which field to marshal or unmarshal. If there are
// multiple fields at the same level, and that level is the least
// nested (and would therefore be the nesting level selected by the
// usual Go rules), the following extra rules apply:
//
// 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
// even if there are multiple untagged fields that would otherwise conflict.
//
// 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
//
// 3) Otherwise there are multiple fields, and all are ignored; no error occurs.

timob added a commit to timob/toml that referenced this issue Dec 4, 2017
glinton pushed a commit to influxdata/toml that referenced this issue Mar 1, 2019
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

2 participants