sdomino/scribble

scribble.read() hides json.InvalidUnmarshalError

Closed this issue · 1 comments

Passing a receiving variable by value to scribble.Read() results in a silent failure to populate it. The underlying json.Unmarshall() handles correctly returns an error, but it does not propagate.

I believe this happens because scribble.read() "references" (puts ampersand in front of) the v variable). This means that json.Unmarshall() gets the pointer it expects and populates it, but in reality, it is a pointer to a local copy of v inside scribble.read().

I believe the fix is to remove the ampersand from the last line of scribble.read(). There is no need to include it, because the value must already be a pointer, and it should be passed as is to json.Unmarshall().

Basically, change:

func read(record string, v interface{}) error {

	b, err := ioutil.ReadFile(record + ".json")
	if err != nil {
		return err
	}

	// unmarshal data
	return json.Unmarshal(b, &v)
}

to

func read(record string, v interface{}) error {

	b, err := ioutil.ReadFile(record + ".json")
	if err != nil {
		return err
	}

	// unmarshal data
	return json.Unmarshal(b, v)
}

After applying this fix locally, all my unit tests of scribble still pass, but passing a v variable by value results in an error as expected and desired.

Thanks for your feedback! I actually haven't worked on this project in some time. I'd love some help if you had the time to submit a PR.