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

proposal: encoding/json: add mechanism to mark fields as required #19858

Open
mrajashree opened this Issue Apr 5, 2017 · 12 comments

Comments

Projects
None yet
6 participants
@mrajashree

mrajashree commented Apr 5, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.7.5

While unmarshaling json into a struct, no error is thrown if the struct doesn't contain a particular field. For example if three fields are needed in the struct and the json input provides only two, no error is thrown and the third field is just empty.
this is my example:

type Example struct {
	Name string `json:"name"`
	Place string `json:"place"`
	Date string `json:"date"`
}

func main() {
	jsonStr := []byte(`{"name":"someName","place":"somePlace"}`)
	var ex *Example
	err := json.Unmarshal(jsonStr, &ex)
	if err != nil {
		fmt.Printf("ERROR : %v\n", err)
	}
	fmt.Printf("%#v",ex)
}

Output:
&main.Example{Name:"someName", Place:"somePlace", Date:""}

Expected:
Some error indicating required field not provided

I know I can check if the field is empty after unmarshaling, but is there a way to throw error during it?

@mrajashree

This comment has been minimized.

mrajashree commented Apr 5, 2017

Is it possible to add a required tag so not all fields need to be provided, but the necessary one would be?

@bradfitz

This comment has been minimized.

Member

bradfitz commented Apr 5, 2017

(Similar but opposite proposal is #15314, to reject unknown fields.)

@bradfitz bradfitz changed the title from Add Unmarshaling error if required field not provided to encoding/json: add mechanism to mark fields as required Apr 5, 2017

@bradfitz bradfitz changed the title from encoding/json: add mechanism to mark fields as required to proposal: encoding/json: add mechanism to mark fields as required Apr 5, 2017

@gopherbot gopherbot added this to the Proposal milestone Apr 5, 2017

@gopherbot gopherbot added the Proposal label Apr 5, 2017

@mrajashree

This comment has been minimized.

mrajashree commented Apr 5, 2017

@bradfitz oh, can I work on this?

@bradfitz

This comment has been minimized.

Member

bradfitz commented Apr 5, 2017

No, not yet. We're still in the "Discuss your design" phase right now. This hasn't been approved.

@mrajashree

This comment has been minimized.

mrajashree commented Apr 5, 2017

Ah lol! my bad

@mrajashree

This comment has been minimized.

mrajashree commented Jun 9, 2017

Any update on this?

@edsonmedina

This comment has been minimized.

edsonmedina commented Dec 13, 2017

+1

@mvdan

This comment has been minimized.

Member

mvdan commented Dec 13, 2017

@edsonmedina

This comment has been minimized.

edsonmedina commented Dec 13, 2017

@mvdan Fair enough. How does this work then? Who approves the proposals? Is there a way to vote or is it decided by committee?

@mvdan

This comment has been minimized.

Member

mvdan commented Dec 13, 2017

@edsonmedina see https://github.com/golang/proposal#readme. You can vote with reactions above (which I believe you already did), but otherwise "+1" comments do not add to the discussion. Of course, that doesn't apply to comments that do add information or bring up new points.

If what you're asking is how soon will the proposal reviewing team will get to this one, I don't have an answer. Bear in mind that there are nearly two hundred open proposals without decision yet, so I'm not sure that the team can give reliable estimates.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Dec 13, 2017

We do seem to have missed this one somehow. Proposal review has gone on vacation until the new year, but I'll make sure it gets reviewed then.

@bradfitz

This comment has been minimized.

Member

bradfitz commented Dec 13, 2017

I think @rsc had said that all JSON proposals are on hold until they can be processed together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment