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
duration
marshal, unmarshal functions
Changes: 1. Unmarshalling into `int64`,`custom int64`,`time.Duration`,`string` and `custom string`, was unsupported before, now is supported. 2. Marshalling are `custom string`, `custom int64` was unsupported before, now is supported. 3. Unmarshalling `broken data` does not return an error before, now returns an error.
- Loading branch information
Showing
8 changed files
with
955 additions
and
177 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,17 @@ | ||
package duration | ||
|
||
type Duration struct { | ||
Months int32 | ||
Days int32 | ||
Nanoseconds int64 | ||
} | ||
|
||
func (d Duration) Valid() bool { | ||
if d.Months >= 0 && d.Days >= 0 && d.Nanoseconds >= 0 { | ||
return true | ||
} | ||
if d.Months <= 0 && d.Days <= 0 && d.Nanoseconds <= 0 { | ||
return true | ||
} | ||
return false | ||
} |
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,38 @@ | ||
package duration | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
) | ||
|
||
func Marshal(value interface{}) ([]byte, error) { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil, nil | ||
case int64: | ||
return EncInt64(v) | ||
case time.Duration: | ||
return EncDur(v) | ||
case string: | ||
return EncString(v) | ||
case Duration: | ||
return EncDuration(v) | ||
|
||
case *int64: | ||
return EncInt64R(v) | ||
case *time.Duration: | ||
return EncDurR(v) | ||
case *string: | ||
return EncStringR(v) | ||
case *Duration: | ||
return EncDurationR(v) | ||
default: | ||
// Custom types (type MyDate uint32) 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)) | ||
} | ||
} |
Oops, something went wrong.