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

cue: decode empty list as empty slice instead of slice-typed nil #2471

Closed
wants to merge 1 commit into from
Closed

cue: decode empty list as empty slice instead of slice-typed nil #2471

wants to merge 1 commit into from

Conversation

bozaro
Copy link
Contributor

@bozaro bozaro commented Jul 4, 2023

An empty slice and a slice-typed nil have the same behaviour in most cases, but sometimes there are differences.
For example, when marshaling in json.Marshal.
From my point of view, it is more correct to return an empty slice for an empty list instead of slice-typed nil.

https://go.dev/play/p/PfVuusup6UO

Source:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var a, b []interface{}
	a = []interface{}{}
	ja, _ := json.Marshal(a)
	jb, _ := json.Marshal(b)
	fmt.Println(string(ja))
	fmt.Println(string(jb))
}

Output:

[]
null

@bozaro bozaro requested a review from cueckoo as a code owner July 4, 2023 07:46
@mvdan
Copy link
Member

mvdan commented Jul 4, 2023

Is there a github issue that this PR is fixing? I would suggest filing an issue first if not, because this is a subtle change in behavior that we need to agree is a good idea first.

@bozaro
Copy link
Contributor Author

bozaro commented Jul 4, 2023

Is there a github issue that this PR is fixing?

I don't find related github issue, but at least two of my colleagues mentioned this problem before I encountered it.

because this is a subtle change in behavior that we need to agree is a good idea first.

I will be very surprised if the test case (current behaviour):

	{
		value: `[]`,
		dst:   new(interface{}),
		want:  ([]interface{})(nil),
	}, {
		value: `[]`,
		dst:   new([]interface{}),
		want:  []interface{}{},
	}, {
		value: `[]`,
		dst:   new([]int),
		want:  []int{},
	}

will be more preferable than (new behaviour):

	{
		value: `[]`,
		dst:   new(interface{}),
		want:  []interface{}{},
	}, {
		value: `[]`,
		dst:   new([]interface{}),
		want:  []interface{}{},
	}, {
		value: `[]`,
		dst:   new([]int),
		want:  []int{},
	}

@bozaro
Copy link
Contributor Author

bozaro commented Jul 4, 2023

That is, now an empty list turns into a slice-typed nil only on decode to interface{} type.
In other cases we got empty slice.

@mvdan
Copy link
Member

mvdan commented Jul 4, 2023

I see. I think I agree with that, for the sake of consistency. Most other decoders in Go, like encoding/json, also do that:

To unmarshal a JSON array into a slice, Unmarshal resets the slice length to zero and then appends each element to the slice. As a special case, to unmarshal an empty JSON array into a slice, Unmarshal replaces the slice with a new empty slice.

I'll check with Marcel to see if he disagrees or if there's a factor I'm missing.

For the future, I would still recommend filing an issue first and following the template. Discussions like this one get lost or forgotten if they happen as part of a patch.

@mvdan mvdan self-assigned this Jul 4, 2023
@myitcv
Copy link
Member

myitcv commented Jul 4, 2023

@bozaro thank you for raising this PR.

If I can just, however, emphasise @mvdan's request:

recommend filing an issue first and following the template

The issue template for the CUE project is, like the Go project, carefully designed to help with the triaging process to make it easier for everyone (including project maintainers) to understand the nature of a bug or feature request. For a bug report, the questions What did you do?, What did you expect to see?, and What did you see instead? help to illicit as many of the facts as possible in a standard form. That standard form is again designed to make exchanges from all sides as efficient and effective as possible, something that is especially important for an open source project. These motivations and others are also covered in the contribution docs.

Thanks again for contributing to the CUE project.

@bozaro
Copy link
Contributor Author

bozaro commented Aug 1, 2023

Is there a chance to get this change in 0.6.0?

@mvdan
Copy link
Member

mvdan commented Aug 1, 2023

We had to prioritise regressions and severe bugs like panics for v0.6.0, so I'm afraid not. We'll jump to v0.7 as soon as the final release is out, hopefully tomorrow, so I'll certainly look at this with Marcel soon. We would have of course liked to include more fixes into v0.6.0, but the more we try to include, the longer the release will take to be ready :)

Copy link
Member

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

Looked at this with @mpvl and we agree it's a good change :) Importing to Gerrit.

@cueckoo cueckoo closed this in 66ebe0f Aug 18, 2023
cueckoo pushed a commit that referenced this pull request Aug 20, 2024
These tests should not change with an upcoming
CL that to support pkg.Schema.

Issue #2471

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: Ic941ea65432dfdd23743692c1e1efefc8de9da95
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199623
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants