Implement v2 Protocol. Adding expectedVersion for optimistic concurrency and metadata.

This commit is contained in:
Nicolas Dextraze
2016-08-26 22:10:15 -07:00
parent d819fb7de6
commit 5549c9a135
13 changed files with 648 additions and 422 deletions

View File

@ -4,6 +4,7 @@ import (
"reflect"
"encoding/json"
"errors"
"fmt"
)
type JsonSerializer struct {
@ -27,6 +28,9 @@ func (me *JsonSerializer) RegisterType(t interface{}) {
}
func (me *JsonSerializer) Serialize(obj interface{}) ([]byte, string, error) {
if obj == nil {
return []byte(""), "", nil
}
type_ := reflect.TypeOf(obj)
if (type_.Kind() == reflect.Interface || type_.Kind() == reflect.Ptr) {
return nil, "", errors.New("Trying to serialize a Ptr type.")
@ -40,9 +44,12 @@ func (me *JsonSerializer) Serialize(obj interface{}) ([]byte, string, error) {
}
func (me *JsonSerializer) Deserialize(serialized []byte, typeId string) (interface{}, error) {
if (typeId == "") {
return nil, nil
}
type_ := me.types[typeId]
if type_ == nil {
return nil, errors.New("type not registered in serializer")
return nil, errors.New(fmt.Sprintf("type %q not registered in serializer", typeId))
}
objPtr := reflect.New(type_).Interface()
err := json.Unmarshal(serialized, objPtr)

View File

@ -12,6 +12,10 @@ func NewPassthruSerializer() *PassthruSerializer {
}
func (me PassthruSerializer) Serialize(input interface{}) (output []byte, typeId string, err error) {
if input == nil {
return nil, "", nil
}
content, ok := input.([]byte)
if !ok {
err = errors.New("input should be []byte")
@ -30,6 +34,10 @@ func (me PassthruSerializer) Serialize(input interface{}) (output []byte, typeId
}
func (me PassthruSerializer) Deserialize(input []byte, typeId string) (interface{}, error) {
if (typeId == "") {
return nil, nil
}
output := []byte(typeId)
output = append(output, ' ')
output = append(output, input...)