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.
Merge pull request #367 from illia-li/il/fix/marshal/decimal
Fix `decimal` marshal, unmarshall functions
- Loading branch information
Showing
10 changed files
with
1,044 additions
and
82 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
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) | ||
} | ||
} |
Oops, something went wrong.