forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
272 additions
and
322 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,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...)) | ||
} |
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,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) | ||
} |
Oops, something went wrong.