From f990aab4cf74076572e8706f02402ecb5503c0b6 Mon Sep 17 00:00:00 2001 From: Reda Laanait Date: Sat, 7 Sep 2024 12:55:22 +0100 Subject: [PATCH] opt for solution 2 + cleanups --- codec_default.go | 27 --------------------------- codec_native.go | 27 ++++----------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/codec_default.go b/codec_default.go index 3155a3f..c42bdc3 100644 --- a/codec_default.go +++ b/codec_default.go @@ -2,7 +2,6 @@ package avro import ( "fmt" - "time" "unsafe" "github.com/modern-go/reflect2" @@ -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)) diff --git a/codec_native.go b/codec_native.go index ed097e0..675de08 100644 --- a/codec_native.go +++ b/codec_native.go @@ -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)} } @@ -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 } @@ -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 }