Skip to content

Commit

Permalink
opt for solution 2 + cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
redaLaanait committed Sep 7, 2024
1 parent cb3d6f5 commit f990aab
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
27 changes: 0 additions & 27 deletions codec_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package avro

import (
"fmt"
"time"
"unsafe"

"github.com/modern-go/reflect2"
Expand All @@ -11,32 +10,6 @@ import (
func createDefaultDecoder(d *decoderContext, field *Field, typ reflect2.Type) ValDecoder {
cfg := d.cfg
fn := func(def any) ([]byte, error) {
// Solution 1:

// def's Go type is decided by JSON decode.
// Force conversion of some Go types to ensure compatibility with AVRO codec.
switch schema := field.Type().Type(); schema {
case Long:
schema := field.Type().(*PrimitiveSchema)
if schema.Logical() == nil {
break
}
switch schema.Logical().Type() {
case TimestampMillis:
d, ok := def.(int64)
if !ok {
break
}
def = time.UnixMilli(d)
case TimestampMicros:
d, ok := def.(int64)
if !ok {
break
}
def = time.UnixMicro(d)
}
}

defaultType := reflect2.TypeOf(def)
if defaultType == nil {
defaultType = reflect2.TypeOf((*null)(nil))
Expand Down
27 changes: 4 additions & 23 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ func createDecoderOfNative(schema *PrimitiveSchema, typ reflect2.Type) ValDecode
convert: createLongConverter(schema.encodedType),
}

// Solution 2:
case st == Long:
timestampLogicalType := (lt == TimestampMillis || lt == TimestampMicros)
if timestampLogicalType && typ.Type1() == timeDurationType {
isTimestamp := (lt == TimestampMillis || lt == TimestampMicros)
if isTimestamp && typ.Type1() == timeDurationType {
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.Type1().String(), schema.Type(), lt)}
}
Expand All @@ -95,16 +94,6 @@ func createDecoderOfNative(schema *PrimitiveSchema, typ reflect2.Type) ValDecode
}
return &longCodec[int64]{}

// case st == Long && lt == "":
// if resolved {
// return &longConvCodec[int64]{convert: createLongConverter(schema.encodedType)}
// }
// return &longCodec[int64]{}

// case lt != "":
// return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
// typ.String(), schema.Type(), lt)}

default:
break
}
Expand Down Expand Up @@ -257,22 +246,14 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
case st == Long && lt == TimeMicros: // time.Duration
return &timeMicrosCodec{}

// Solution 2:
case st == Long:
timestampLogicalType := (lt == TimestampMillis || lt == TimestampMicros)
if timestampLogicalType && typ.Type1() == timeDurationType {
isTimestamp := (lt == TimestampMillis || lt == TimestampMicros)
if isTimestamp && typ.Type1() == timeDurationType {
return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.Type1().String(), schema.Type(), lt)}
}
return &longCodec[int64]{}

// case st == Long && lt == "":
// return &longCodec[int64]{}

// case lt != "":
// return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
// typ.String(), schema.Type(), lt)}

default:
break
}
Expand Down

0 comments on commit f990aab

Please sign in to comment.