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.
- Loading branch information
Showing
5 changed files
with
312 additions
and
71 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,28 @@ | ||
package ascii | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func Marshal(value interface{}) ([]byte, error) { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil, nil | ||
case string: | ||
return EncString(v) | ||
case *string: | ||
return EncStringR(v) | ||
case []byte: | ||
return EncBytes(v) | ||
case *[]byte: | ||
return EncBytesR(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.ValueOf(value) | ||
if rv.Kind() != reflect.Ptr { | ||
return EncReflect(rv) | ||
} | ||
return EncReflectR(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,56 @@ | ||
package ascii | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func EncString(v string) ([]byte, error) { | ||
return encString(v), nil | ||
} | ||
|
||
func EncStringR(v *string) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return encString(*v), nil | ||
} | ||
|
||
func EncBytes(v []byte) ([]byte, error) { | ||
return v, nil | ||
} | ||
|
||
func EncBytesR(v *[]byte) ([]byte, error) { | ||
if v == nil { | ||
return nil, nil | ||
} | ||
return *v, nil | ||
} | ||
|
||
func EncReflect(v reflect.Value) ([]byte, error) { | ||
switch v.Kind() { | ||
case reflect.String: | ||
return encString(v.String()), nil | ||
case reflect.Slice: | ||
if v.Type().Elem().Kind() != reflect.Uint8 { | ||
return nil, fmt.Errorf("failed to marshal ascii: unsupported value type (%T)(%[1]v)", v.Interface()) | ||
} | ||
return EncBytes(v.Bytes()) | ||
default: | ||
return nil, fmt.Errorf("failed to marshal ascii: 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 encString(v string) []byte { | ||
if v == "" { | ||
return make([]byte, 0) | ||
} | ||
return []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,35 @@ | ||
package ascii | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
func Unmarshal(data []byte, value interface{}) error { | ||
switch v := value.(type) { | ||
case nil: | ||
return nil | ||
case *string: | ||
return DecString(data, v) | ||
case **string: | ||
return DecStringR(data, v) | ||
case *[]byte: | ||
return DecBytes(data, v) | ||
case **[]byte: | ||
return DecBytesR(data, v) | ||
case *interface{}: | ||
return DecInterface(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 ascii: unsupported value type (%T)(%[1]v)", v) | ||
} | ||
if rt.Elem().Kind() != reflect.Ptr { | ||
return DecReflect(data, rv) | ||
} | ||
return DecReflectR(data, rv) | ||
} | ||
} |
Oops, something went wrong.