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

Encode: fix embedded struct with explicit field name #773

Merged
merged 1 commit into from May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions unmarshaler.go
Expand Up @@ -1167,11 +1167,6 @@ func forEachField(t reflect.Type, path []int, do func(name string, path []int))
fieldPath := append(path, i)
fieldPath = fieldPath[:len(fieldPath):len(fieldPath)]

if f.Anonymous {
forEachField(f.Type, fieldPath, do)
continue
}

name := f.Tag.Get("toml")
if name == "-" {
continue
Expand All @@ -1180,6 +1175,12 @@ func forEachField(t reflect.Type, path []int, do func(name string, path []int))
if i := strings.IndexByte(name, ','); i >= 0 {
name = name[:i]
}

if f.Anonymous && name == "" {
forEachField(f.Type, fieldPath, do)
continue
}

if name == "" {
name = f.Name
}
Expand Down
21 changes: 20 additions & 1 deletion unmarshaler_test.go
Expand Up @@ -554,7 +554,7 @@ wibble = 'wobble'
[foo]

[foo.bar]
huey = 'dewey'
huey = 'dewey'
`,
gen: func() test {
m := map[string]interface{}{}
Expand Down Expand Up @@ -2380,6 +2380,25 @@ func TestIssue714(t *testing.T) {
require.Error(t, err)
}

func TestIssue772(t *testing.T) {
type FileHandling struct {
FilePattern string `toml:"pattern"`
}

type Config struct {
FileHandling `toml:"filehandling"`
}

var defaultConfigFile = []byte(`
[filehandling]
pattern = "reach-masterdev-"`)

config := Config{}
err := toml.Unmarshal(defaultConfigFile, &config)
require.NoError(t, err)
require.Equal(t, "reach-masterdev-", config.FileHandling.FilePattern)
}

func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
desc string
Expand Down