msgpack icon indicating copy to clipboard operation
msgpack copied to clipboard

Add nil aware encoder

Open vmihailenco opened this issue 3 years ago • 2 comments

vmihailenco avatar Mar 16 '21 08:03 vmihailenco

@vmihailenco could you expand on what you're thinking here?

tmm1 avatar May 12 '21 20:05 tmm1

@tmm1 I am looking into implementing a function decorator that does common nil checks (ideally during encoder initialization). See nilAwareDecoder and this commit that introduced that change.

Should be something like

func nilAwareEncoder(typ reflect.Type, fn encoderFunc) encoderFunc {
	if nilable(typ.Kind()) {
		return func(d *Decoder, v reflect.Value) error {
			if v.IsNil() {
				return e.EncodeNil()
			}
			return fn(d, v)
		}
	}
	return fn
}

vmihailenco avatar May 13 '21 06:05 vmihailenco