Skip to content

Commit

Permalink
fix: codec default reader/writer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
redaLaanait committed Jan 12, 2024
1 parent b96aefc commit f2c19a2
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions codec_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@ func createDefaultDecoder(cfg *frozenConfig, field *Field, typ reflect2.Type) Va
defaultEncoder = &onePtrEncoder{defaultEncoder}
}
w := cfg.borrowWriter()
defer cfg.returnWriter(w)

defaultEncoder.Encode(reflect2.PtrOf(def), w)
if w.Error != nil {
return nil, w.Error
}
return w.Buffer(), nil
b := w.Buffer()
data := make([]byte, len(b))
copy(data, b)

return data, nil
}

b, err := field.encodeDefault(fn)
if err != nil {
return &errorDecoder{err: fmt.Errorf("decode default: %w", err)}
}
return &defaultDecoder{
defaultReader: cfg.borrowReader(b),
decoder: decoderOfType(cfg, field.Type(), typ),
data: b,
decoder: decoderOfType(cfg, field.Type(), typ),
}
}

type defaultDecoder struct {
defaultReader *Reader
decoder ValDecoder
data []byte
decoder ValDecoder
}

// Decode implements ValDecoder.
func (d *defaultDecoder) Decode(ptr unsafe.Pointer, _ *Reader) {
d.decoder.Decode(ptr, d.defaultReader)
func (d *defaultDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
rr := r.cfg.borrowReader(d.data)
defer r.cfg.returnReader(rr)

d.decoder.Decode(ptr, rr)
}

var _ ValDecoder = &defaultDecoder{}

0 comments on commit f2c19a2

Please sign in to comment.