forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix
decimal
marshal, unmarshal functions
- Loading branch information
Showing
6 changed files
with
619 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package decimal | ||
|
||
import ( | ||
"gopkg.in/inf.v0" | ||
"reflect" | ||
) | ||
|
||
func Marshal(value interface{}) ([]byte, error) { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil, nil | ||
case inf.Dec: | ||
return EncInfDec(v) | ||
case *inf.Dec: | ||
return EncInfDecR(v) | ||
case string: | ||
return EncString(v) | ||
case *string: | ||
return EncStringR(v) | ||
default: | ||
// Custom types (type MyString string) can be serialized only via `reflect` package. | ||
// Later, when generic-based serialization is introduced we can do that via generics. | ||
rv := reflect.TypeOf(value) | ||
if rv.Kind() != reflect.Ptr { | ||
return EncReflect(reflect.ValueOf(v)) | ||
} | ||
return EncReflectR(reflect.ValueOf(v)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package decimal | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/inf.v0" | ||
"math/big" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gocql/gocql/serialization/varint" | ||
) | ||
|
||
func EncInfDec(v inf.Dec) ([]byte, error) { | ||
sign := v.Sign() | ||
if sign == 0 { | ||
return []byte{0, 0, 0, 0, 0}, nil | ||
} | ||
return append(encScale(v.Scale()), varint.EncBigIntRS(v.UnscaledBig())...), nil | ||
} | ||
|
||
func EncInfDecR(v *inf.Dec) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return encInfDecR(v), nil | ||
} | ||
|
||
// EncString encodes decimal string which should contains `scale` and `unscaled` strings separated by `;`. | ||
func EncString(v string) ([]byte, error) { | ||
if v == "" { | ||
return nil, nil | ||
} | ||
vs := strings.Split(v, ";") | ||
if len(vs) != 2 { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid decimal string %s", v) | ||
} | ||
scale, err := strconv.ParseInt(vs[0], 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid decimal scale string %s", vs[0]) | ||
} | ||
unscaleData, err := encUnscaledString(vs[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(encScale64(scale), unscaleData...), nil | ||
} | ||
|
||
func EncStringR(v *string) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return EncString(*v) | ||
} | ||
|
||
func EncReflect(v reflect.Value) ([]byte, error) { | ||
switch v.Type().Kind() { | ||
case reflect.String: | ||
return encReflectString(v) | ||
case reflect.Struct: | ||
if v.Type().String() == "gocql.unsetColumn" { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("failed to marshal decimal: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
default: | ||
return nil, fmt.Errorf("failed to marshal decimal: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
} | ||
} | ||
|
||
func EncReflectR(v reflect.Value) ([]byte, error) { | ||
if v.IsNil() { | ||
return nil, nil | ||
} | ||
return EncReflect(v.Elem()) | ||
} | ||
|
||
func encReflectString(v reflect.Value) ([]byte, error) { | ||
val := v.String() | ||
if val == "" { | ||
return nil, nil | ||
} | ||
vs := strings.Split(val, ";") | ||
if len(vs) != 2 { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid decimal string (%T)(%[1]v)", v.Interface()) | ||
} | ||
scale, err := strconv.ParseInt(vs[0], 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid decimal scale string (%T)(%s)", v.Interface(), vs[0]) | ||
} | ||
unscaledData, err := encUnscaledString(vs[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(encScale64(scale), unscaledData...), nil | ||
} | ||
|
||
func encInfDecR(v *inf.Dec) []byte { | ||
sign := v.Sign() | ||
if sign == 0 { | ||
return []byte{0, 0, 0, 0, 0} | ||
} | ||
return append(encScale(v.Scale()), varint.EncBigIntRS(v.UnscaledBig())...) | ||
} | ||
|
||
func encScale(v inf.Scale) []byte { | ||
return []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)} | ||
} | ||
|
||
func encScale64(v int64) []byte { | ||
return []byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)} | ||
} | ||
|
||
func encUnscaledString(v string) ([]byte, error) { | ||
switch { | ||
case len(v) == 0: | ||
return nil, nil | ||
case len(v) <= 18: | ||
n, err := strconv.ParseInt(v, 10, 64) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid unscaled string %s, %s", v, err) | ||
} | ||
return varint.EncInt64Ext(n), nil | ||
case len(v) <= 20: | ||
n, err := strconv.ParseInt(v, 10, 64) | ||
if err == nil { | ||
return varint.EncInt64Ext(n), nil | ||
} | ||
|
||
t, ok := new(big.Int).SetString(v, 10) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid unscaled string %s", v) | ||
} | ||
return varint.EncBigIntRS(t), nil | ||
default: | ||
t, ok := new(big.Int).SetString(v, 10) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to marshal decimal: invalid unscaled string %s", v) | ||
} | ||
return varint.EncBigIntRS(t), nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package decimal | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/inf.v0" | ||
"reflect" | ||
) | ||
|
||
func Unmarshal(data []byte, value interface{}) error { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil | ||
case *inf.Dec: | ||
return DecInfDec(data, v) | ||
case **inf.Dec: | ||
return DecInfDecR(data, v) | ||
case *string: | ||
return DecString(data, v) | ||
case **string: | ||
return DecStringR(data, v) | ||
default: | ||
// Custom types (type MyString string) can be deserialized only via `reflect` package. | ||
// Later, when generic-based serialization is introduced we can do that via generics. | ||
rv := reflect.ValueOf(value) | ||
rt := rv.Type() | ||
if rt.Kind() != reflect.Ptr { | ||
return fmt.Errorf("failed to unmarshal decimal: unsupported value type (%T)(%#[1]v)", value) | ||
} | ||
if rt.Elem().Kind() != reflect.Ptr { | ||
return DecReflect(data, rv) | ||
} | ||
return DecReflectR(data, rv) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package decimal | ||
|
||
import ( | ||
"gopkg.in/inf.v0" | ||
) | ||
|
||
const ( | ||
neg8 = int64(-1) << 8 | ||
neg16 = int64(-1) << 16 | ||
neg24 = int64(-1) << 24 | ||
neg32 = int64(-1) << 32 | ||
neg40 = int64(-1) << 40 | ||
neg48 = int64(-1) << 48 | ||
neg56 = int64(-1) << 56 | ||
neg32Int = int(-1) << 32 | ||
) | ||
|
||
func decScale(p []byte) inf.Scale { | ||
return inf.Scale(p[0])<<24 | inf.Scale(p[1])<<16 | inf.Scale(p[2])<<8 | inf.Scale(p[3]) | ||
} | ||
|
||
func decScaleInt64(p []byte) int64 { | ||
if p[0] > 127 { | ||
return neg32 | int64(p[0])<<24 | int64(p[1])<<16 | int64(p[2])<<8 | int64(p[3]) | ||
} | ||
return int64(p[0])<<24 | int64(p[1])<<16 | int64(p[2])<<8 | int64(p[3]) | ||
} | ||
|
||
func dec1toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg8 | int64(p[4]) | ||
} | ||
return int64(p[4]) | ||
} | ||
|
||
func dec2toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg16 | int64(p[4])<<8 | int64(p[5]) | ||
} | ||
return int64(p[4])<<8 | int64(p[5]) | ||
} | ||
|
||
func dec3toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg24 | int64(p[4])<<16 | int64(p[5])<<8 | int64(p[6]) | ||
} | ||
return int64(p[4])<<16 | int64(p[5])<<8 | int64(p[6]) | ||
} | ||
|
||
func dec4toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg32 | int64(p[4])<<24 | int64(p[5])<<16 | int64(p[6])<<8 | int64(p[7]) | ||
} | ||
return int64(p[4])<<24 | int64(p[5])<<16 | int64(p[6])<<8 | int64(p[7]) | ||
} | ||
|
||
func dec5toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg40 | int64(p[4])<<32 | int64(p[5])<<24 | int64(p[6])<<16 | int64(p[7])<<8 | int64(p[8]) | ||
} | ||
return int64(p[4])<<32 | int64(p[5])<<24 | int64(p[6])<<16 | int64(p[7])<<8 | int64(p[8]) | ||
} | ||
|
||
func dec6toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg48 | int64(p[4])<<40 | int64(p[5])<<32 | int64(p[6])<<24 | int64(p[7])<<16 | int64(p[8])<<8 | int64(p[9]) | ||
} | ||
return int64(p[4])<<40 | int64(p[5])<<32 | int64(p[6])<<24 | int64(p[7])<<16 | int64(p[8])<<8 | int64(p[9]) | ||
} | ||
|
||
func dec7toInt64(p []byte) int64 { | ||
if p[4] > 127 { | ||
return neg56 | int64(p[4])<<48 | int64(p[5])<<40 | int64(p[6])<<32 | int64(p[7])<<24 | int64(p[8])<<16 | int64(p[9])<<8 | int64(p[10]) | ||
} | ||
return int64(p[4])<<48 | int64(p[5])<<40 | int64(p[6])<<32 | int64(p[7])<<24 | int64(p[8])<<16 | int64(p[9])<<8 | int64(p[10]) | ||
} | ||
|
||
func dec8toInt64(p []byte) int64 { | ||
return int64(p[4])<<56 | int64(p[5])<<48 | int64(p[6])<<40 | int64(p[7])<<32 | int64(p[8])<<24 | int64(p[9])<<16 | int64(p[10])<<8 | int64(p[11]) | ||
} |
Oops, something went wrong.