|
|
@ -43,7 +43,9 @@ func NewPolicyType(name string) PolicyType {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewSerializedType(name string) SerializedType {
|
|
|
|
func NewSerializedType(name string) SerializedType {
|
|
|
|
return SerializedType(Hash(SerializedTypeBase, name))
|
|
|
|
val := SerializedType(Hash(SerializedTypeBase, name))
|
|
|
|
|
|
|
|
println(fmt.Sprintf("TYPE: %s: %d", name, val))
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
@ -89,6 +91,7 @@ var (
|
|
|
|
ExtensionType = NewSerializedType("extension")
|
|
|
|
ExtensionType = NewSerializedType("extension")
|
|
|
|
|
|
|
|
|
|
|
|
StringType = NewSerializedType("string")
|
|
|
|
StringType = NewSerializedType("string")
|
|
|
|
|
|
|
|
Uint8Type = NewSerializedType("uint8")
|
|
|
|
NodeKeyType = NewSerializedType("node_key")
|
|
|
|
NodeKeyType = NewSerializedType("node_key")
|
|
|
|
|
|
|
|
|
|
|
|
NodeNotFoundError = errors.New("Node not found in DB")
|
|
|
|
NodeNotFoundError = errors.New("Node not found in DB")
|
|
|
@ -112,18 +115,6 @@ type TypeInfo struct {
|
|
|
|
Deserialize TypeDeserialize
|
|
|
|
Deserialize TypeDeserialize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Int int
|
|
|
|
|
|
|
|
func (i Int) MarshalBinary() ([]byte, error) {
|
|
|
|
|
|
|
|
ret := make([]byte, 8)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(ret, uint64(i))
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type String string
|
|
|
|
|
|
|
|
func (str String) MarshalBinary() ([]byte, error) {
|
|
|
|
|
|
|
|
return []byte(str), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A Context stores all the data to run a graphvent process
|
|
|
|
// A Context stores all the data to run a graphvent process
|
|
|
|
type Context struct {
|
|
|
|
type Context struct {
|
|
|
|
// DB is the database connection used to load and write nodes
|
|
|
|
// DB is the database connection used to load and write nodes
|
|
|
@ -328,124 +319,194 @@ type SerializedValue struct {
|
|
|
|
Data []byte
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (field SerializedValue) MarshalBinary() ([]byte, error) {
|
|
|
|
func SerializeValue(ctx *Context, value reflect.Value) (SerializedValue, error) {
|
|
|
|
data := []byte{}
|
|
|
|
val, err := serializeValue(ctx, value.Type(), &value)
|
|
|
|
for _, t := range(field.TypeStack) {
|
|
|
|
ctx.Log.Logf("serialize", "SERIALIZED_VALUE(%+v): %+v - %s", value, val, err)
|
|
|
|
t_ser := make([]byte, 8)
|
|
|
|
return val, err
|
|
|
|
binary.BigEndian.PutUint64(t_ser, uint64(t))
|
|
|
|
|
|
|
|
data = append(data, t_ser...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
data = append(data, field.Data...)
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func RecurseTypes(ctx *Context, t reflect.Type) ([]uint64, []reflect.Kind, error) {
|
|
|
|
func serializeValue(ctx *Context, t reflect.Type, value *reflect.Value) (SerializedValue, error) {
|
|
|
|
var ctx_type uint64 = 0x00
|
|
|
|
var ctx_type uint64 = 0x00
|
|
|
|
ctype, exists := ctx.TypeReflects[t]
|
|
|
|
ctype, exists := ctx.TypeReflects[t]
|
|
|
|
|
|
|
|
ctx.Log.Logf("serialize", "TYPE_REFLECTS: %+v", ctx.TypeReflects)
|
|
|
|
if exists == true {
|
|
|
|
if exists == true {
|
|
|
|
|
|
|
|
type_info := ctx.Types[ctype]
|
|
|
|
ctx_type = uint64(ctype)
|
|
|
|
ctx_type = uint64(ctype)
|
|
|
|
|
|
|
|
val_ser, err := type_info.Serialize(ctx, value.Interface())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return SerializedValue{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
|
|
|
|
[]uint64{ctx_type},
|
|
|
|
|
|
|
|
val_ser,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var new_types []uint64
|
|
|
|
|
|
|
|
var new_kinds []reflect.Kind
|
|
|
|
|
|
|
|
kind := t.Kind()
|
|
|
|
kind := t.Kind()
|
|
|
|
switch kind {
|
|
|
|
switch kind {
|
|
|
|
case reflect.Array:
|
|
|
|
case reflect.Map:
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
ctx_type = uint64(ArrayType)
|
|
|
|
ctx_type = uint64(MapType)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if value == nil {
|
|
|
|
|
|
|
|
data = nil
|
|
|
|
|
|
|
|
} else if value.IsZero() {
|
|
|
|
|
|
|
|
data = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
|
|
|
|
|
|
|
|
} else if value.Len() == 0 {
|
|
|
|
|
|
|
|
data = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
map_iter := value.MapRange()
|
|
|
|
|
|
|
|
key_data := []byte{}
|
|
|
|
|
|
|
|
val_data := []byte{}
|
|
|
|
|
|
|
|
var key_types []uint64 = nil
|
|
|
|
|
|
|
|
var val_types []uint64 = nil
|
|
|
|
|
|
|
|
map_len := 0
|
|
|
|
|
|
|
|
for map_iter.Next() {
|
|
|
|
|
|
|
|
map_len += 1
|
|
|
|
|
|
|
|
key_value := map_iter.Key()
|
|
|
|
|
|
|
|
val_value := map_iter.Value()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
key, err := serializeValue(ctx, t.Key(), &key_value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem_types, elem_kinds, err := RecurseTypes(ctx, t.Elem())
|
|
|
|
val, err := serializeValue(ctx, t.Elem(), &val_value)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_types = append(new_types, ctx_type)
|
|
|
|
|
|
|
|
new_types = append(new_types, elem_types...)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_kinds = append(new_kinds, reflect.Array)
|
|
|
|
if key_types == nil {
|
|
|
|
new_kinds = append(new_kinds, elem_kinds...)
|
|
|
|
key_types = key.TypeStack
|
|
|
|
case reflect.Map:
|
|
|
|
val_types = val.TypeStack
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
}
|
|
|
|
ctx_type = uint64(MapType)
|
|
|
|
|
|
|
|
|
|
|
|
key_data = append(key_data, key.Data...)
|
|
|
|
|
|
|
|
val_data = append(val_data, val.Data...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key_types, key_kinds, err := RecurseTypes(ctx, t.Key())
|
|
|
|
|
|
|
|
|
|
|
|
type_stack := []uint64{ctx_type}
|
|
|
|
|
|
|
|
type_stack = append(type_stack, key_types...)
|
|
|
|
|
|
|
|
type_stack = append(type_stack, val_types...)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data := make([]byte, 8)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(data, uint64(map_len))
|
|
|
|
|
|
|
|
data = append(data, key_data...)
|
|
|
|
|
|
|
|
data = append(data, val_data...)
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
|
|
|
|
type_stack,
|
|
|
|
|
|
|
|
data,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := serializeValue(ctx, t.Key(), nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem_types, elem_kinds, err := RecurseTypes(ctx, t.Elem())
|
|
|
|
elem, err := serializeValue(ctx, t.Elem(), nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_types = append(new_types, ctx_type)
|
|
|
|
type_stack := []uint64{ctx_type}
|
|
|
|
new_types = append(new_types, key_types...)
|
|
|
|
type_stack = append(type_stack, key.TypeStack...)
|
|
|
|
new_types = append(new_types, elem_types...)
|
|
|
|
type_stack = append(type_stack, elem.TypeStack...)
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
new_kinds = append(new_kinds, reflect.Map)
|
|
|
|
type_stack,
|
|
|
|
new_kinds = append(new_kinds, key_kinds...)
|
|
|
|
data,
|
|
|
|
new_kinds = append(new_kinds, elem_kinds...)
|
|
|
|
}, nil
|
|
|
|
case reflect.Slice:
|
|
|
|
case reflect.Slice:
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
ctx_type = uint64(SliceType)
|
|
|
|
ctx_type = uint64(SliceType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem_types, elem_kinds, err := RecurseTypes(ctx, t.Elem())
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if value == nil {
|
|
|
|
|
|
|
|
data = nil
|
|
|
|
|
|
|
|
} else if value.IsZero() {
|
|
|
|
|
|
|
|
data = []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
|
|
|
|
|
|
|
|
} else if value.Len() == 0 {
|
|
|
|
|
|
|
|
data = []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
data := make([]byte, 8)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(data, uint64(value.Len()))
|
|
|
|
|
|
|
|
var elem SerializedValue
|
|
|
|
|
|
|
|
for i := 0; i < value.Len(); i += 1 {
|
|
|
|
|
|
|
|
val := value.Index(i)
|
|
|
|
|
|
|
|
element, err := serializeValue(ctx, t.Elem(), &val)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_types = append(new_types, ctx_type)
|
|
|
|
if i == 0 {
|
|
|
|
new_types = append(new_types, elem_types...)
|
|
|
|
elem = element
|
|
|
|
|
|
|
|
}
|
|
|
|
new_kinds = append(new_kinds, reflect.Slice)
|
|
|
|
data = append(data, elem.Data...)
|
|
|
|
new_kinds = append(new_kinds, elem_kinds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
|
|
|
|
append([]uint64{ctx_type}, elem.TypeStack...),
|
|
|
|
|
|
|
|
data,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elem, err := serializeValue(ctx, t.Elem(), nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return SerializedValue{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
|
|
|
|
append([]uint64{ctx_type}, elem.TypeStack...),
|
|
|
|
|
|
|
|
data,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
case reflect.Pointer:
|
|
|
|
case reflect.Pointer:
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
ctx_type = uint64(PointerType)
|
|
|
|
ctx_type = uint64(PointerType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem_types, elem_kinds, err := RecurseTypes(ctx, t.Elem())
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
var elem_value *reflect.Value = nil
|
|
|
|
|
|
|
|
if value == nil {
|
|
|
|
|
|
|
|
data = nil
|
|
|
|
|
|
|
|
} else if value.IsZero() {
|
|
|
|
|
|
|
|
data = []byte{0x01}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
data = []byte{0x00}
|
|
|
|
|
|
|
|
ev := value.Elem()
|
|
|
|
|
|
|
|
elem_value = &ev
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
elem, err := serializeValue(ctx, t.Elem(), elem_value)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
return SerializedValue{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_types = append(new_types, ctx_type)
|
|
|
|
if elem.Data != nil {
|
|
|
|
new_types = append(new_types, elem_types...)
|
|
|
|
data = append(data, elem.Data...)
|
|
|
|
|
|
|
|
}
|
|
|
|
new_kinds = append(new_kinds, reflect.Pointer)
|
|
|
|
return SerializedValue{
|
|
|
|
new_kinds = append(new_kinds, elem_kinds...)
|
|
|
|
append([]uint64{uint64(ctx_type)}, elem.TypeStack...),
|
|
|
|
|
|
|
|
data,
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
case reflect.String:
|
|
|
|
case reflect.String:
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
ctx_type = uint64(StringType)
|
|
|
|
ctx_type = uint64(StringType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_types = append(new_types, ctx_type)
|
|
|
|
if value == nil {
|
|
|
|
new_kinds = append(new_kinds, reflect.String)
|
|
|
|
return SerializedValue{
|
|
|
|
default:
|
|
|
|
[]uint64{ctx_type},
|
|
|
|
return nil, nil, fmt.Errorf("unhandled kind: %+v - %+v", kind, t)
|
|
|
|
nil,
|
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
return new_types, new_kinds, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func serializeValue(ctx *Context, kind_stack []reflect.Kind, value reflect.Value) ([]byte, error) {
|
|
|
|
|
|
|
|
kind := kind_stack[len(kind_stack) - 1]
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unhandled kind: %+v", kind)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func SerializeValue(ctx *Context, value reflect.Value) (SerializedValue, error) {
|
|
|
|
|
|
|
|
if value.IsValid() == false {
|
|
|
|
|
|
|
|
return SerializedValue{}, fmt.Errorf("Cannot serialize invalid value: %+v", value)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type_stack, kind_stack, err := RecurseTypes(ctx, value.Type())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return SerializedValue{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bytes, err := serializeValue(ctx, kind_stack, value)
|
|
|
|
data := make([]byte, 8)
|
|
|
|
if err != nil {
|
|
|
|
str := value.String()
|
|
|
|
return SerializedValue{}, err
|
|
|
|
binary.BigEndian.PutUint64(data, uint64(len(str)))
|
|
|
|
|
|
|
|
return SerializedValue{
|
|
|
|
|
|
|
|
[]uint64{uint64(ctx_type)},
|
|
|
|
|
|
|
|
append(data, []byte(str)...),
|
|
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case reflect.Uint8:
|
|
|
|
|
|
|
|
if ctx_type == 0x00 {
|
|
|
|
|
|
|
|
ctx_type = uint64(Uint8Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SerializedValue{
|
|
|
|
return SerializedValue{
|
|
|
|
type_stack,
|
|
|
|
[]uint64{uint64(ctx_type)},
|
|
|
|
bytes,
|
|
|
|
[]byte{uint8(value.Uint())},
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return SerializedValue{}, fmt.Errorf("unhandled kind: %+v - %+v", kind, t)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -532,13 +593,16 @@ func SerializeExtension(ctx *Context, ext Extension, ctx_type ExtType) (Serializ
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (value SerializedValue) MarshalBinary() ([]byte, error) {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("SerializedValue.MarshalBinary Undefined")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func ParseSerializedValue(ctx *Context, data []byte) (SerializedValue, []byte, error) {
|
|
|
|
|
|
|
|
return SerializedValue{}, nil, fmt.Errorf("ParseSerializedValue Undefined")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DeserializeValue(ctx *Context, value SerializedValue) (interface{}, error) {
|
|
|
|
func DeserializeValue(ctx *Context, value SerializedValue) (interface{}, error) {
|
|
|
|
// TODO: do the opposite of SerializeValue.
|
|
|
|
return nil, fmt.Errorf("DeserializeValue Undefined")
|
|
|
|
// 1) Check the type to handle special types(array, list, map, pointer)
|
|
|
|
|
|
|
|
// 2) Check if the type is registered in the context, handle if so
|
|
|
|
|
|
|
|
// 3) Check if the type is a default type, handle if so
|
|
|
|
|
|
|
|
// 4) Return error if we don't know how to deserialize the type
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Undefined")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new Context with the base library content added
|
|
|
|
// Create a new Context with the base library content added
|
|
|
@ -559,6 +623,21 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = ctx.RegisterType(reflect.TypeOf(SerializedValue{}), NewSerializedType("SerializedValue"),
|
|
|
|
|
|
|
|
func(ctx *Context, val interface{}) ([]byte, error) {
|
|
|
|
|
|
|
|
value := val.(SerializedValue)
|
|
|
|
|
|
|
|
return value.MarshalBinary()
|
|
|
|
|
|
|
|
}, func(ctx *Context, data []byte) (interface{}, error) {
|
|
|
|
|
|
|
|
value, data, err := ParseSerializedValue(ctx, data)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if data != nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("%+v remaining after parse", data)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
err = ctx.RegisterExtension(reflect.TypeOf((*LockableExt)(nil)), LockableExtType, nil)
|
|
|
|
err = ctx.RegisterExtension(reflect.TypeOf((*LockableExt)(nil)), LockableExtType, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|