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

Unexpected marshalling result for deeply-embedded structs #430

Open
falau opened this issue Oct 30, 2024 · 1 comment
Open

Unexpected marshalling result for deeply-embedded structs #430

falau opened this issue Oct 30, 2024 · 1 comment

Comments

@falau
Copy link

falau commented Oct 30, 2024

For structs layers below, the output doesn't seem to be correct:

Example code:

package main

import (
	"fmt"
	"os"

	"github.com/BurntSushi/toml"
)

type (
	StructA struct{ StructB }
	StructB struct{ StructC }
	StructC struct{ StructD }
	StructD struct {
		FieldD1 string
		FieldD2 string
		FieldD3 string
	}
)

func main() {
	err := toml.NewEncoder(os.Stdout).Encode(StructA{
		StructB: StructB{
			StructC: StructC{
				StructD: StructD{
					FieldD1: "V1",
					FieldD2: "V2",
					FieldD3: "V3",
				},
			},
		},
	})
	fmt.Println("err:", err)
}

Outputs:

FieldD3 = "V3"
FieldD3 = "V3"
FieldD3 = "V3"

vs json.Marshal:

{
    "FieldD1":"V1",
    "FieldD2":"V2",
    "FieldD3":"V3"
}

This behaviour seems like only happens in this specific case. If I embed StructD into StructC, then the result is correct. I didn't dig into .Encode, nor tried any other composition of structs, so perhaps there are more strange cases like this.

@arp242
Copy link
Collaborator

arp242 commented Oct 30, 2024

It works if I comment out this if is32Bit { line so it always runs: bafff94

I put that in because tests on 32bit systems were failing; never quite understood why though; someone sent a patch and that fixes it, so 🤷

Added a test and CI passes: bafff94

Need to sit down and understand what's going on here.

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