Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for schema evolution #329

Merged
merged 28 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b9d7442
feat: add support for schema evolution poc
redaLaanait Nov 13, 2023
d68f799
Merge branch 'main' of https://github.com/hamba/avro into feat/schema…
redaLaanait Nov 13, 2023
4ade51d
fix: type promotion POC
redaLaanait Nov 17, 2023
845d478
fix: Fix and include last remarks
redaLaanait Nov 25, 2023
908868d
reduce default decoder verbosity
redaLaanait Nov 25, 2023
3f02ab4
add tests for schema resolution
redaLaanait Nov 30, 2023
2498c29
attempt replacing readNext by native decoders
redaLaanait Nov 30, 2023
9d6c104
fix(schema compatibility): support named schema aliases
redaLaanait Dec 11, 2023
9b51d57
fix(default decoder): try different implementation
redaLaanait Dec 11, 2023
eaef2ad
fix(defaut decoder): cache field encoded default and borrow reader
redaLaanait Dec 12, 2023
b4224dc
fix: resolver unable to resolve nullDefault type
redaLaanait Dec 12, 2023
0b74a61
clean up generic decode and improve test coverage
redaLaanait Dec 15, 2023
0fc43f2
fix decoder cachekey to consider primitives with promotion and fields…
redaLaanait Dec 15, 2023
62b27ce
fix: codec generic
redaLaanait Dec 15, 2023
7db00da
cleanups and fixes
redaLaanait Dec 16, 2023
04e2ddc
Merge branch 'main' of https://github.com/hamba/avro into feat/schema…
redaLaanait Dec 21, 2023
51224e6
fix: fix resolve record
redaLaanait Dec 21, 2023
ade5d38
improve record cache fingerprint
redaLaanait Dec 21, 2023
5adccb8
Merge branch 'main' of https://github.com/hamba/avro into feat/schema…
redaLaanait Dec 21, 2023
e364790
fix(default encoder): better handling of nullDefault
redaLaanait Jan 9, 2024
bc0e276
fix: record cache fingerprint
redaLaanait Jan 9, 2024
2ec1b60
rename FieldDrain by FieldIgnore
redaLaanait Jan 9, 2024
17926ac
fix: record cache fingerprint
redaLaanait Jan 10, 2024
1dc0276
fix: record cache fingerprint
redaLaanait Jan 10, 2024
85f8d7c
clean up
redaLaanait Jan 10, 2024
b96aefc
clean up
redaLaanait Jan 10, 2024
f2c19a2
fix: codec default reader/writer usage
redaLaanait Jan 12, 2024
35a64d9
fix: bytes to string converter
redaLaanait Jan 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type ValEncoder interface {

// ReadVal parses Avro value and stores the result in the value pointed to by obj.
func (r *Reader) ReadVal(schema Schema, obj any) {
decoder := r.cfg.getDecoderFromCache(schema.Fingerprint(), reflect2.RTypeOf(obj))
key := cacheFingerprintOf(schema)
decoder := r.cfg.getDecoderFromCache(key, reflect2.RTypeOf(obj))
if decoder == nil {
typ := reflect2.TypeOf(obj)
if typ.Kind() != reflect.Ptr {
Expand Down Expand Up @@ -65,14 +66,15 @@ func (w *Writer) WriteVal(schema Schema, val any) {

func (c *frozenConfig) DecoderOf(schema Schema, typ reflect2.Type) ValDecoder {
rtype := typ.RType()
decoder := c.getDecoderFromCache(schema.Fingerprint(), rtype)
key := cacheFingerprintOf(schema)
decoder := c.getDecoderFromCache(key, rtype)
if decoder != nil {
return decoder
}

ptrType := typ.(*reflect2.UnsafePtrType)
decoder = decoderOfType(c, schema, ptrType.Elem())
c.addDecoderToCache(schema.Fingerprint(), rtype, decoder)
c.addDecoderToCache(key, rtype, decoder)
return decoder
}

Expand Down
57 changes: 57 additions & 0 deletions codec_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package avro

import (
"fmt"
"unsafe"

"github.com/modern-go/reflect2"
)

func createDefaultDecoder(cfg *frozenConfig, field *Field, typ reflect2.Type) ValDecoder {
fn := func(def any) ([]byte, error) {
defaultType := reflect2.TypeOf(def)
if defaultType == nil {
defaultType = reflect2.TypeOf((*null)(nil))
}
defaultEncoder := encoderOfType(cfg, field.Type(), defaultType)
if defaultType.LikePtr() {
defaultEncoder = &onePtrEncoder{defaultEncoder}
}
w := cfg.borrowWriter()
nrwiersma marked this conversation as resolved.
Show resolved Hide resolved
defer cfg.returnWriter(w)

defaultEncoder.Encode(reflect2.PtrOf(def), w)
if w.Error != nil {
return nil, w.Error
}
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{
data: b,
decoder: decoderOfType(cfg, field.Type(), typ),
}
nrwiersma marked this conversation as resolved.
Show resolved Hide resolved
}

type defaultDecoder struct {
data []byte
decoder ValDecoder
}

// Decode implements ValDecoder.
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{}
Loading
Loading