Skip to content

Do not unconditionally do new() to fill embedded struct pointer #161

@dynaxis

Description

@dynaxis

In the case of encoding/json, if I set Inner of Test struct before unmarshalling, it is kept after unmarshalled. But in the case of easyjson, the generated implementation unconditionally do new(Inner)
and assign to the Test.Inner.

If there is no particular reason for doing it unconditionally, it would be good if easyjson works
the same way as encoding/json.

Thanks.

package main

import (
	"encoding/json"
	"fmt"
)

type Test struct {
	Dummy int
	*Inner
}

type Inner struct {
	Boo int
}

const data = `
{
	"Dummy": 7,
	"Boo": 3
}
`

func main() {
	t := &Test{}
	i := &Inner{}
	//t.Inner = i

	json.Unmarshal([]byte(data), t)
	fmt.Printf("Check: %v, %v, %v\n", t.Inner == i, t.Inner.Boo, t.Dummy)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions