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 #371 from illia-li/il/fix/marshal/timestamp
Fix `timestamp` marshal, unmarshall
- Loading branch information
Showing
8 changed files
with
414 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package timestamp | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
) | ||
|
||
func Marshal(value interface{}) ([]byte, error) { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil, nil | ||
case int64: | ||
return EncInt64(v) | ||
case *int64: | ||
return EncInt64R(v) | ||
case time.Time: | ||
return EncTime(v) | ||
case *time.Time: | ||
return EncTimeR(v) | ||
|
||
default: | ||
// Custom types (type MyTime int64) 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,65 @@ | ||
package timestamp | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
const ( | ||
maxValInt64 int64 = 86399999999999 | ||
minValInt64 int64 = 0 | ||
maxValDur time.Duration = 86399999999999 | ||
minValDur time.Duration = 0 | ||
) | ||
|
||
func EncInt64(v int64) ([]byte, error) { | ||
return encInt64(v), nil | ||
} | ||
|
||
func EncInt64R(v *int64) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return EncInt64(*v) | ||
} | ||
|
||
func EncTime(v time.Time) ([]byte, error) { | ||
if v.IsZero() { | ||
return make([]byte, 0), nil | ||
} | ||
ms := v.Unix()*1e3 + int64(v.Nanosecond())/1e6 | ||
return []byte{byte(ms >> 56), byte(ms >> 48), byte(ms >> 40), byte(ms >> 32), byte(ms >> 24), byte(ms >> 16), byte(ms >> 8), byte(ms)}, nil | ||
} | ||
|
||
func EncTimeR(v *time.Time) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return EncTime(*v) | ||
} | ||
|
||
func EncReflect(v reflect.Value) ([]byte, error) { | ||
switch v.Kind() { | ||
case reflect.Int64: | ||
return encInt64(v.Int()), nil | ||
case reflect.Struct: | ||
if v.Type().String() == "gocql.unsetColumn" { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("failed to marshal timestamp: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
default: | ||
return nil, fmt.Errorf("failed to marshal timestamp: 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 encInt64(v int64) []byte { | ||
return []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(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,36 @@ | ||
package timestamp | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
func Unmarshal(data []byte, value interface{}) error { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil | ||
|
||
case *int64: | ||
return DecInt64(data, v) | ||
case **int64: | ||
return DecInt64R(data, v) | ||
case *time.Time: | ||
return DecTime(data, v) | ||
case **time.Time: | ||
return DecTimeR(data, v) | ||
default: | ||
|
||
// Custom types (type MyTime int64) 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 timestamp: 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.