vmihailenco/msgpack

Add nil aware encoder

vmihailenco opened this issue · 2 comments

Add nil aware encoder
tmm1 commented

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

@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
}