Skip to content

Commit

Permalink
fix: Fix and include last remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
redaLaanait committed Nov 25, 2023
1 parent 4ade51d commit 845d478
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 221 deletions.
12 changes: 8 additions & 4 deletions codec_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func createDefaultDecoder(cfg *frozenConfig, schema Schema, def any, typ reflect

switch schema.Type() {
case Null:
return &nullDefaultDecoder{}
return &nullDefaultDecoder{
typ: typ,
}

case Boolean:
return &boolDefaultDecoder{
Expand Down Expand Up @@ -150,10 +152,13 @@ func (d *boolDefaultDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
}

type nullDefaultDecoder struct {
typ reflect2.Type
}

func (d *nullDefaultDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
return
func (d *nullDefaultDecoder) Decode(ptr unsafe.Pointer, _ *Reader) {
if d.typ.IsNullable() {
d.typ.UnsafeSet(ptr, d.typ.UnsafeNew())
}
}

type intDefaultDecoder struct {
Expand Down Expand Up @@ -270,7 +275,6 @@ func (d *doubleDefaultDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
default:
r.ReportError("decode default", "unsupported type")
}

}

type stringDefaultDecoder struct {
Expand Down
102 changes: 48 additions & 54 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {
actual := schema.(*PrimitiveSchema).actual
converter := resolveConverter(schema.(*PrimitiveSchema).actual)

switch typ.Kind() {
case reflect.Bool:
Expand Down Expand Up @@ -60,9 +60,7 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {
if schema.Type() != Long {
break
}
return &longCodec[uint32]{
promoter: getCodecPromoter[uint32](actual),
}
return &longCodec[uint32]{convert: converter.toLong}

case reflect.Int64:
st := schema.Type()
Expand All @@ -73,12 +71,12 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {

case st == Long && lt == TimeMicros: // time.Duration
return &timeMicrosCodec{
promoter: getCodecPromoter[int64](actual),
convert: converter.toLong,
}

case st == Long:
return &longCodec[int64]{
promoter: getCodecPromoter[int64](actual),
convert: converter.toLong,
}

default:
Expand All @@ -90,23 +88,23 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {
break
}
return &float32Codec{
promoter: getCodecPromoter[float32](actual),
convert: converter.toFloat,
}

case reflect.Float64:
if schema.Type() != Double {
break
}
return &float64Codec{
promoter: getCodecPromoter[float64](actual),
convert: converter.toDouble,
}

case reflect.String:
if schema.Type() != String {
break
}
return &stringCodec{
promoter: getCodecPromoter[string](actual),
convert: converter.toString,
}

case reflect.Slice:
Expand All @@ -115,7 +113,7 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {
}
return &bytesCodec{
sliceType: typ.(*reflect2.UnsafeSliceType),
promoter: getCodecPromoter[[]byte](actual),
convert: converter.toBytes,
}

case reflect.Struct:
Expand All @@ -131,20 +129,19 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {

case Istpy1Time && st == Long && lt == TimestampMillis:
return &timestampMillisCodec{
promoter: getCodecPromoter[int64](actual),
convert: converter.toLong,
}

case Istpy1Time && st == Long && lt == TimestampMicros:
return &timestampMicrosCodec{
promoter: getCodecPromoter[int64](actual),
convert: converter.toLong,
}

case Istpy1Rat && st == Bytes && lt == Decimal:
dec := ls.(*DecimalLogicalSchema)

return &bytesDecimalCodec{
prec: dec.Precision(), scale: dec.Scale(),
promoter: getCodecPromoter[[]byte](actual),
convert: converter.toBytes,
}

default:
Expand All @@ -165,7 +162,7 @@ func createDecoderOfNative(schema Schema, typ reflect2.Type) ValDecoder {

return &bytesDecimalPtrCodec{
prec: dec.Precision(), scale: dec.Scale(),
promoter: getCodecPromoter[[]byte](actual),
convert: converter.toBytes,
}
}

Expand Down Expand Up @@ -369,13 +366,13 @@ type largeInt interface {
}

type longCodec[T largeInt] struct {
promoter *codecPromoter[T]
convert func(*Reader) int64
}

func (c *longCodec[T]) Decode(ptr unsafe.Pointer, r *Reader) {
var v T
if c.promoter != nil {
v = c.promoter.promote(r)
if c.convert != nil {
v = T(c.convert(r))
} else {
v = T(r.ReadLong())
}
Expand All @@ -387,13 +384,13 @@ func (*longCodec[T]) Encode(ptr unsafe.Pointer, w *Writer) {
}

type float32Codec struct {
promoter *codecPromoter[float32]
convert func(*Reader) float32
}

func (c *float32Codec) Decode(ptr unsafe.Pointer, r *Reader) {
var v float32
if c.promoter != nil {
v = c.promoter.promote(r)
if c.convert != nil {
v = c.convert(r)
} else {
v = r.ReadFloat()
}
Expand All @@ -412,13 +409,13 @@ func (*float32DoubleCodec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type float64Codec struct {
promoter *codecPromoter[float64]
convert func(*Reader) float64
}

func (c *float64Codec) Decode(ptr unsafe.Pointer, r *Reader) {
var v float64
if c.promoter != nil {
v = c.promoter.promote(r)
if c.convert != nil {
v = c.convert(r)
} else {
v = r.ReadDouble()
}
Expand All @@ -430,13 +427,13 @@ func (*float64Codec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type stringCodec struct {
promoter *codecPromoter[string]
convert func(*Reader) string
}

func (c *stringCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var v string
if c.promoter != nil {
v = c.promoter.promote(r)
if c.convert != nil {
v = c.convert(r)
} else {
v = r.ReadString()
}
Expand All @@ -449,17 +446,16 @@ func (*stringCodec) Encode(ptr unsafe.Pointer, w *Writer) {

type bytesCodec struct {
sliceType *reflect2.UnsafeSliceType
promoter *codecPromoter[[]byte]
convert func(*Reader) []byte
}

func (c *bytesCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var b []byte
if c.promoter != nil {
b = c.promoter.promote(r)
if c.convert != nil {
b = c.convert(r)
} else {
b = r.ReadBytes()
}
// b := r.ReadBytes()
c.sliceType.UnsafeSet(ptr, reflect2.PtrOf(b))
}

Expand All @@ -482,13 +478,13 @@ func (c *dateCodec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type timestampMillisCodec struct {
promoter *codecPromoter[int64]
convert func(*Reader) int64
}

func (c *timestampMillisCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var i int64
if c.promoter != nil {
i = c.promoter.promote(r)
if c.convert != nil {
i = c.convert(r)
} else {
i = r.ReadLong()
}
Expand All @@ -503,13 +499,13 @@ func (c *timestampMillisCodec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type timestampMicrosCodec struct {
promoter *codecPromoter[int64]
convert func(*Reader) int64
}

func (c *timestampMicrosCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var i int64
if c.promoter != nil {
i = c.promoter.promote(r)
if c.convert != nil {
i = c.convert(r)
} else {
i = r.ReadLong()
}
Expand All @@ -523,8 +519,7 @@ func (c *timestampMicrosCodec) Encode(ptr unsafe.Pointer, w *Writer) {
w.WriteLong(t.Unix()*1e6 + int64(t.Nanosecond()/1e3))
}

type timeMillisCodec struct {
}
type timeMillisCodec struct{}

func (c *timeMillisCodec) Decode(ptr unsafe.Pointer, r *Reader) {
i := r.ReadInt()
Expand All @@ -537,13 +532,13 @@ func (c *timeMillisCodec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type timeMicrosCodec struct {
promoter *codecPromoter[int64]
convert func(*Reader) int64
}

func (c *timeMicrosCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var i int64
if c.promoter != nil {
i = c.promoter.promote(r)
if c.convert != nil {
i = c.convert(r)
} else {
i = r.ReadLong()
}
Expand All @@ -558,19 +553,18 @@ func (c *timeMicrosCodec) Encode(ptr unsafe.Pointer, w *Writer) {
var one = big.NewInt(1)

type bytesDecimalCodec struct {
prec int
scale int
promoter *codecPromoter[[]byte]
prec int
scale int
convert func(*Reader) []byte
}

func (c *bytesDecimalCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var b []byte
if c.promoter != nil {
b = c.promoter.promote(r)
if c.convert != nil {
b = c.convert(r)
} else {
b = r.ReadBytes()
}
// b := r.ReadBytes()
if i := (&big.Int{}).SetBytes(b); len(b) > 0 && b[0]&0x80 > 0 {
i.Sub(i, new(big.Int).Lsh(one, uint(len(b))*8))
}
Expand Down Expand Up @@ -611,19 +605,19 @@ func (c *bytesDecimalCodec) Encode(ptr unsafe.Pointer, w *Writer) {
}

type bytesDecimalPtrCodec struct {
prec int
scale int
promoter *codecPromoter[[]byte]
prec int
scale int
convert func(*Reader) []byte
}

func (c *bytesDecimalPtrCodec) Decode(ptr unsafe.Pointer, r *Reader) {
var b []byte
if c.promoter != nil {
b = c.promoter.promote(r)
if c.convert != nil {
b = c.convert(r)
} else {
b = r.ReadBytes()
}
// b := r.ReadBytes()

if i := (&big.Int{}).SetBytes(b); len(b) > 0 && b[0]&0x80 > 0 {
i.Sub(i, new(big.Int).Lsh(one, uint(len(b))*8))
}
Expand Down
Loading

0 comments on commit 845d478

Please sign in to comment.