mapstructure icon indicating copy to clipboard operation
mapstructure copied to clipboard

Allowed hook to skip the decode

Open wenerme opened this issue 5 years ago • 4 comments

If input is zero, add and option to skip the outVal.Set(reflect.Zero(outVal.Type()))

https://github.com/mitchellh/mapstructure/blob/95075d6e1a88d608e5258a054d7f0de9401f255e/mapstructure.go#L403-L411

If this is acceptable, I can make a pr

wenerme avatar Dec 17 '20 17:12 wenerme

I don't understand the motivation here, if you can show a failed test case that would help.

mitchellh avatar Dec 17 '20 17:12 mitchellh

With patch can do this

func TestPatch(t *testing.T) {
	a := &Data{
		Name: "a",
		Age:  10,
	}
	path := &Data{
		Name: "neo",
	}
	throwError(mapstructure.Decode(path, a))
	assert.Equal(t, a.Age, 10)
}

type Data struct {
	Name string
	Age  int
}

wenerme avatar Dec 17 '20 17:12 wenerme

Hope allowed DecodeHook to return a skip, like

type DecodeHookFuncValueSkip func(from reflect.Value, to reflect.Value) (interface{}, bool, error)

If skipped, should abort current decode.

wenerme avatar Dec 30 '20 14:12 wenerme

Add a special error can keep everything unchang, and allowed to skip the rest decode.

var ErrSkip = errors.New("skip")

wenerme avatar Jan 05 '21 12:01 wenerme