Skip to content

Commit

Permalink
breaking dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Sep 16, 2024
1 parent 1b9a9f3 commit d6c8249
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 322 deletions.
39 changes: 36 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/gocql/gocql/marshal/erro"
"github.com/gocql/gocql/marshal/types/tinyint"
"math"
"math/big"
"math/bits"
Expand Down Expand Up @@ -95,6 +97,10 @@ func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
panic("protocol version not set")
}

if _, ok := value.(unsetColumn); ok {
return nil, nil
}

if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr {
if valueRef.IsNil() {
return nil, nil
Expand All @@ -115,7 +121,7 @@ func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
case TypeBoolean:
return marshalBool(info, value)
case TypeTinyInt:
return marshalTinyInt(info, value)
return mutateMarshal(info, value, tinyint.Marshal)
case TypeSmallInt:
return marshalSmallInt(info, value)
case TypeInt:
Expand Down Expand Up @@ -225,7 +231,7 @@ func Unmarshal(info TypeInfo, data []byte, value interface{}) error {
case TypeSmallInt:
return unmarshalSmallInt(info, data, value)
case TypeTinyInt:
return unmarshalTinyInt(info, data, value)
return mutateUnmarshal(info, data, value, tinyint.Unmarshal)
case TypeFloat:
return unmarshalFloat(info, data, value)
case TypeDouble:
Expand Down Expand Up @@ -716,7 +722,7 @@ func unmarshalSmallInt(info TypeInfo, data []byte, value interface{}) error {
}

func unmarshalTinyInt(info TypeInfo, data []byte, value interface{}) error {
return unmarshalIntlike(info, int64(decTiny(data)), data, value)
return tinyint.Unmarshal(data, value)
}

func unmarshalVarint(info TypeInfo, data []byte, value interface{}) error {
Expand Down Expand Up @@ -2731,3 +2737,30 @@ func (m UnmarshalError) Error() string {
func unmarshalErrorf(format string, args ...interface{}) UnmarshalError {
return UnmarshalError(fmt.Sprintf(format, args...))
}

func mutateMarshal(info TypeInfo, value interface{}, f func(interface{}) ([]byte, error)) ([]byte, error) {
switch v := value.(type) {
case nil, unsetColumn:
return nil, nil
case Marshaler:
return v.MarshalCQL(info)
default:
data, err := f(value)
if err != nil {
return nil, MarshalError(err.(erro.Error))
}
return data, nil
}
}

func mutateUnmarshal(info TypeInfo, data []byte, value interface{}, f func([]byte, interface{}) error) error {
switch v := value.(type) {
case Unmarshaler:
return v.UnmarshalCQL(info, data)
default:
if err := f(data, value); err != nil {
return UnmarshalError(err.(erro.Error))
}
}
return nil
}
19 changes: 19 additions & 0 deletions marshal/erro/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package erro

import "fmt"

var Default = Newf("gocql error:")

type Error string

func (m Error) Error() string {
return string(m)
}

func (m Error) Addf(format string, args ...interface{}) Error {
return m + " " + Error(fmt.Sprintf(format, args...))
}

func Newf(format string, args ...interface{}) Error {
return Error(fmt.Sprintf(format, args...))
}
88 changes: 88 additions & 0 deletions marshal/types/tinyint/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tinyint

import (
"math"
"reflect"
"strconv"

"github.com/gocql/gocql/marshal/erro"
)

func Marshal(value interface{}) ([]byte, error) {
switch v := value.(type) {
case int8:
return []byte{byte(v)}, nil
case uint8:
return []byte{byte(v)}, nil
case int16:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case uint16:
if v > math.MaxUint8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case int:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case int32:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case int64:
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case uint:
if v > math.MaxUint8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case uint32:
if v > math.MaxUint8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case uint64:
if v > math.MaxUint8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case string:
n, err := strconv.ParseInt(v, 10, 8)
if err != nil {
return nil, erro.Newf("marshal tinyint: can not marshal %#v %s", value, err)
}
return []byte{byte(n)}, nil
}

if value == nil {
return nil, nil
}

switch rv := reflect.ValueOf(value); rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
if v > math.MaxInt8 || v < math.MinInt8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
v := rv.Uint()
if v > math.MaxUint8 {
return nil, erro.Newf("marshal tinyint: value %#v out of range", v)
}
return []byte{byte(v)}, nil
case reflect.Ptr:
if rv.IsNil() {
return nil, nil
}
}
return nil, erro.Newf("marshal tinyint: can not marshal %#v", value)
}
Loading

0 comments on commit d6c8249

Please sign in to comment.