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 #379 from illia-li/il/fix/marshal/boolean
Fix `boolean`, marshal, unmarshall functions
- Loading branch information
Showing
8 changed files
with
331 additions
and
126 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,24 @@ | ||
package boolean | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func Marshal(value interface{}) ([]byte, error) { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil, nil | ||
case bool: | ||
return EncBool(v) | ||
case *bool: | ||
return EncBoolR(v) | ||
default: | ||
// Custom types (type MyBool bool) 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,45 @@ | ||
package boolean | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func EncBool(v bool) ([]byte, error) { | ||
return encBool(v), nil | ||
} | ||
|
||
func EncBoolR(v *bool) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return encBool(*v), nil | ||
} | ||
|
||
func EncReflect(v reflect.Value) ([]byte, error) { | ||
switch v.Kind() { | ||
case reflect.Bool: | ||
return encBool(v.Bool()), nil | ||
case reflect.Struct: | ||
if v.Type().String() == "gocql.unsetColumn" { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("failed to marshal boolean: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
default: | ||
return nil, fmt.Errorf("failed to marshal boolean: 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 encBool(v bool) []byte { | ||
if v { | ||
return []byte{1} | ||
} | ||
return []byte{0} | ||
} |
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 boolean | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func Unmarshal(data []byte, value interface{}) error { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil | ||
case *bool: | ||
return DecBool(data, v) | ||
case **bool: | ||
return DecBoolR(data, v) | ||
default: | ||
// Custom types (type MyBool bool) 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 boolean: unsupported value type (%T)(%[1]v)", v) | ||
} | ||
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,108 @@ | ||
package boolean | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
var errWrongDataLen = fmt.Errorf("failed to unmarshal boolean: the length of the data should be 0 or 1") | ||
|
||
func errNilReference(v interface{}) error { | ||
return fmt.Errorf("failed to unmarshal boolean: can not unmarshal into nil reference(%T)(%[1]v)", v) | ||
} | ||
|
||
func DecBool(p []byte, v *bool) error { | ||
if v == nil { | ||
return errNilReference(v) | ||
} | ||
switch len(p) { | ||
case 0: | ||
*v = false | ||
case 1: | ||
*v = decBool(p) | ||
default: | ||
return errWrongDataLen | ||
} | ||
return nil | ||
} | ||
|
||
func DecBoolR(p []byte, v **bool) error { | ||
if v == nil { | ||
return errNilReference(v) | ||
} | ||
switch len(p) { | ||
case 0: | ||
if p == nil { | ||
*v = nil | ||
} else { | ||
*v = new(bool) | ||
} | ||
case 1: | ||
val := decBool(p) | ||
*v = &val | ||
default: | ||
return errWrongDataLen | ||
} | ||
return nil | ||
} | ||
|
||
func DecReflect(p []byte, v reflect.Value) error { | ||
if v.IsNil() { | ||
return errNilReference(v) | ||
} | ||
|
||
switch v = v.Elem(); v.Kind() { | ||
case reflect.Bool: | ||
return decReflectBool(p, v) | ||
default: | ||
return fmt.Errorf("failed to unmarshal boolean: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
} | ||
} | ||
|
||
func DecReflectR(p []byte, v reflect.Value) error { | ||
if v.IsNil() { | ||
return errNilReference(v) | ||
} | ||
|
||
switch v.Type().Elem().Elem().Kind() { | ||
case reflect.Bool: | ||
return decReflectBoolR(p, v) | ||
default: | ||
return fmt.Errorf("failed to unmarshal boolean: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
} | ||
} | ||
|
||
func decReflectBool(p []byte, v reflect.Value) error { | ||
switch len(p) { | ||
case 0: | ||
v.SetBool(false) | ||
case 1: | ||
v.SetBool(decBool(p)) | ||
default: | ||
return errWrongDataLen | ||
} | ||
return nil | ||
} | ||
|
||
func decReflectBoolR(p []byte, v reflect.Value) error { | ||
switch len(p) { | ||
case 0: | ||
if p == nil { | ||
v.Elem().Set(reflect.Zero(v.Type().Elem())) | ||
} else { | ||
val := reflect.New(v.Type().Elem().Elem()) | ||
v.Elem().Set(val) | ||
} | ||
case 1: | ||
val := reflect.New(v.Type().Elem().Elem()) | ||
val.Elem().SetBool(decBool(p)) | ||
v.Elem().Set(val) | ||
default: | ||
return errWrongDataLen | ||
} | ||
return nil | ||
} | ||
|
||
func decBool(p []byte) bool { | ||
return p[0] != 0 | ||
} |
Oops, something went wrong.