Skip to content

Conversation

tonistiigi
Copy link
Member

@tonistiigi tonistiigi commented May 13, 2025

fixes #3175

While lsBuilder has a field called Current that gets lost because the embedded struct implements custom MarshalJSON method.

This was the best workaround hack I could come up with. Didn't know about this behavior, but here is the sample case https://go.dev/play/p/XH8oRG_wGEU

While lsBuilder has a field called Current that gets lost
because the embedded struct implements custom MarshalJSON method.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
@tonistiigi tonistiigi requested a review from crazy-max May 13, 2025 05:34
@tonistiigi tonistiigi added this to the v0.24.0 milestone May 13, 2025
Comment on lines +216 to +226
// can't marshal c.Builder directly because Builder type has custom MarshalJSON
dt, err := json.Marshal(c.Builder.Builder)
if err != nil {
return nil, err
}
var m map[string]any
if err := json.Unmarshal(dt, &m); err != nil {
return nil, err
}
m["Current"] = c.Builder.Current
return json.Marshal(m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting, this happens when marshaling structs that embed pointers. What do you think of this:

func (c *lsContext) MarshalJSON() ([]byte, error) {
	type Alias builder.Builder
	return json.Marshal(&struct {
		*Alias
		Current bool
	}{
		Alias:   (*Alias)(c.Builder.Builder),
		Current: c.Builder.Current,
	})
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work though. If you the original Builder.MarshalJSON does not get called anymore. https://go.dev/play/p/eJgDvB1mVAh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed

@tonistiigi tonistiigi merged commit 4feb05b into docker:master May 13, 2025
139 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

buildx ls --format=json missing "selected" field
2 participants