@@ -4296,6 +4296,60 @@ func TestUnmarshalEmbedNonString(t *testing.T) {
42964296 assert .Equal (t , d .Foo , nil )
42974297}
42984298
4299+ // Regression test for https://github.com/pelletier/go-toml/issues/1078: an
4300+ // embedded struct whose toml tag only sets options (e.g. `,inline`) has no
4301+ // explicit name and must be flattened, just like an untagged embedded struct
4302+ // and just like the encoder does. Before the fix, the fields stayed at their
4303+ // zero values.
4304+ func TestUnmarshalEmbeddedInline (t * testing.T ) {
4305+ type Base struct {
4306+ Region string
4307+ Name string
4308+ Port int
4309+ }
4310+ type Config struct {
4311+ Base `toml:",inline"`
4312+ PID uint32
4313+ }
4314+
4315+ doc := `
4316+ Region = "us"
4317+ Name = "srv1"
4318+ Port = 100
4319+ PID = 42
4320+ `
4321+ var c Config
4322+ err := toml .Unmarshal ([]byte (doc ), & c )
4323+ assert .NoError (t , err )
4324+ assert .Equal (t , Config {
4325+ Base : Base {Region : "us" , Name : "srv1" , Port : 100 },
4326+ PID : 42 ,
4327+ }, c )
4328+ }
4329+
4330+ // An embedded struct given an explicit tag name keeps acting as a regular
4331+ // named field: its contents come from a sub-table, not from promoted keys.
4332+ func TestUnmarshalEmbeddedNamed (t * testing.T ) {
4333+ type Base struct {
4334+ Region string
4335+ }
4336+ type Config struct {
4337+ Base `toml:"base"`
4338+ PID uint32
4339+ }
4340+
4341+ var c Config
4342+ err := toml .Unmarshal ([]byte ("PID = 42\n [base]\n Region = 'us'\n " ), & c )
4343+ assert .NoError (t , err )
4344+ assert .Equal (t , Config {Base : Base {Region : "us" }, PID : 42 }, c )
4345+
4346+ // Promoted keys must NOT populate a named embedded struct.
4347+ var c2 Config
4348+ err = toml .Unmarshal ([]byte ("Region = 'us'\n PID = 42\n " ), & c2 )
4349+ assert .NoError (t , err )
4350+ assert .Equal (t , Config {PID : 42 }, c2 )
4351+ }
4352+
42994353func TestUnmarshal_Nil (t * testing.T ) {
43004354 type Foo struct {
43014355 Foo * Foo `toml:"foo,omitempty"`
0 commit comments