Skip to content

Commit

Permalink
IPC Prep (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored May 13, 2019
1 parent f56072d commit 933e657
Show file tree
Hide file tree
Showing 29 changed files with 1,909 additions and 487 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ type name, or scheam full name in the case of a named schema (enum, fixed or rec
* ***T:** This is allowed in a "nullable" union. A nullable union is defined as a two schema union,
with the first being `null` (ie. `["null", "string"]`), in this case a `*T` is allowed,
with `T` matching the conversion table above.
* **avro.UnionType:** A `struct` in implementing `avro.UnionType` can be provided, allowing for
strong type encoding. An example can be found in the [godoc](https://godoc.org/github.com/hamba/avro).
* **interface{}:** An `interface` can be provided and the type or name resolved. Primitive types
are pre-registered, but named types, maps and slices will need to be registered with the `Register` function. In the
case of arrays and maps the enclosed schema type or name is postfix to the type
with a `:` separator, e.g `"map:string"`. If any type cannot be resolved the map type above is used unless
`Config.UnionResolutionError` is set to `true` in which case an error is returned.

## Benchmark

Expand Down
9 changes: 0 additions & 9 deletions avro.go

This file was deleted.

2 changes: 1 addition & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func BenchmarkSuperheroGenericDecode(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m := map[string]interface{}{}
var m interface{}
_ = avro.Unmarshal(schema, data, &m)
}
}
Expand Down
12 changes: 9 additions & 3 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/modern-go/reflect2"
)

type null struct{}

// ValDecoder represents an internal value decoder.
//
// You should never use ValDecoder directly.
Expand Down Expand Up @@ -72,8 +74,8 @@ func (c *frozenConfig) DecoderOf(schema Schema, typ reflect2.Type) ValDecoder {
}

func decoderOfType(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecoder {
// Handle eface case
if typ.Kind() == reflect.Interface {
// Handle eface case when it isnt a union
if typ.Kind() == reflect.Interface && schema.Type() != Union {
if _, ok := typ.(*reflect2.UnsafeIFaceType); !ok {
return &efaceDecoder{schema: schema}
}
Expand Down Expand Up @@ -111,6 +113,10 @@ func decoderOfType(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValDecod
}

func (c *frozenConfig) EncoderOf(schema Schema, typ reflect2.Type) ValEncoder {
if typ == nil {
typ = reflect2.TypeOf((*null)(nil))
}

rtype := typ.RType()
encoder := c.getEncoderFromCache(schema.Fingerprint(), rtype)
if encoder != nil {
Expand Down Expand Up @@ -139,7 +145,7 @@ func encoderOfType(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEncod
}

switch schema.Type() {
case String, Bytes, Int, Long, Float, Double, Boolean:
case String, Bytes, Int, Long, Float, Double, Boolean, Null:
return createEncoderOfNative(schema, typ)

case Record:
Expand Down
8 changes: 8 additions & 0 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,17 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
return &bytesCodec{sliceType: typ.(*reflect2.UnsafeSliceType)}
}

if schema.Type() == Null {
return &nullCodec{}
}

return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s", typ.String(), schema.Type())}
}

type nullCodec struct{}

func (*nullCodec) Encode(ptr unsafe.Pointer, w *Writer) {}

type boolCodec struct{}

func (*boolCodec) Decode(ptr unsafe.Pointer, r *Reader) {
Expand Down
14 changes: 7 additions & 7 deletions codec_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ func encoderOfStruct(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEnc
sf := structDesc.Fields.Get(field.Name())

if sf == nil {
if field.Default() == nil {
if field.Type().Type() == Null {
// We write nothing in a Null case, just skip it
continue
}

if !field.HasDefault() {
// In all other cases, this is a required field
return &errorEncoder{err: fmt.Errorf("avro: record %s is missing required field %s", rec.FullName(), field.Name())}
}

if field.Default() == nil {
// We write nothing in a Null case, just skip it
continue
}

defaultType := reflect2.TypeOf(field.Default())
fields = append(fields, &structFieldEncoder{
defaultPtr: reflect2.PtrOf(field.Default()),
Expand Down Expand Up @@ -234,7 +234,7 @@ func encoderOfRecord(cfg *frozenConfig, schema Schema, typ reflect2.Type) ValEnc
for i, field := range rec.Fields() {
fields[i] = mapEncoderField{
name: field.Name(),
hasDef: field.Default() != nil || field.Type().Type() == Null,
hasDef: field.HasDefault(),
def: field.Default(),
encoder: encoderOfType(cfg, field.Type(), mapType.Elem()),
}
Expand Down
2 changes: 1 addition & 1 deletion codec_skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ type unionSkipDecoder struct {
}

func (d *unionSkipDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
resSchema := getUnionSchema(d.schema, r)
_, resSchema := getUnionSchema(d.schema, r)
if resSchema == nil {
return
}
Expand Down
Loading

0 comments on commit 933e657

Please sign in to comment.