Added deserialization for map type, nil map, and empty map

gql_cataclysm
noah metz 2023-09-05 01:02:41 -06:00
parent 47107dec1c
commit b766aadef9
2 changed files with 57 additions and 3 deletions

@ -708,11 +708,65 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) {
reflect_type := reflect.MapOf(key_type, elem_type) reflect_type := reflect.MapOf(key_type, elem_type)
return reflect_type, nil, value, nil return reflect_type, nil, value, nil
} else if len(value.Data) < 8 { } else if len(value.Data) < 8 {
return nil, nil, value, fmt.Errorf("Not enough data to deserialize map")
} else { } else {
var map_size_bytes []byte
var err error
map_size_bytes, value, err = value.PopData(8)
if err != nil {
return nil, nil, value, err
}
map_size := binary.BigEndian.Uint64(map_size_bytes)
if map_size == 0xFFFFFFFFFFFFFFFF {
var key_type, elem_type reflect.Type
var err error
tmp_value := SerializedValue{
value.TypeStack,
nil,
}
key_type, _, tmp_value, err = DeserializeValue(ctx, tmp_value)
if err != nil {
return nil, nil, value, err
}
elem_type, _, tmp_value, err = DeserializeValue(ctx, tmp_value)
if err != nil {
return nil, nil, value, err
}
new_value := SerializedValue{
tmp_value.TypeStack,
value.Data,
}
reflect_type := reflect.MapOf(key_type, elem_type)
reflect_value := reflect.New(reflect_type).Elem()
return reflect_type, &reflect_value, new_value, nil
} else if map_size == 0x00 {
var key_type, elem_type reflect.Type
var err error
tmp_value := SerializedValue{
value.TypeStack,
nil,
}
key_type, _, tmp_value, err = DeserializeValue(ctx, tmp_value)
if err != nil {
return nil, nil, value, err
}
elem_type, _, tmp_value, err = DeserializeValue(ctx, tmp_value)
if err != nil {
return nil, nil, value, err
}
new_value := SerializedValue{
tmp_value.TypeStack,
value.Data,
}
reflect_type := reflect.MapOf(key_type, elem_type)
reflect_value := reflect.MakeMap(reflect_type)
return reflect_type, &reflect_value, new_value, nil
} else {
return nil, nil, value, fmt.Errorf("deserialize map with elements unimplemented")
}
} }
return nil, nil, value, fmt.Errorf("deserialize map unimplemented")
}) })
if err != nil { if err != nil {
return nil, err return nil, err

@ -27,7 +27,7 @@ func TestSerializeBasic(t *testing.T) {
testSerializeSliceSlice[[][]int](t, ctx, [][]int{{123, 456, 789, 101112}, {3253, 2341, 735, 212}, {123, 51}, nil}) testSerializeSliceSlice[[][]int](t, ctx, [][]int{{123, 456, 789, 101112}, {3253, 2341, 735, 212}, {123, 51}, nil})
testSerializeSliceSlice[[][]string](t, ctx, [][]string{{"123", "456", "789", "101112"}, {"3253", "2341", "735", "212"}, {"123", "51"}, nil}) testSerializeSliceSlice[[][]string](t, ctx, [][]string{{"123", "456", "789", "101112"}, {"3253", "2341", "735", "212"}, {"123", "51"}, nil})
testSerialize(t, ctx, []map[int8]map[*int8]string{}) testSerialize(t, ctx, map[int8]map[*int8]string{})
testSerializeMap(t, ctx, map[int8]int32{ testSerializeMap(t, ctx, map[int8]int32{
0: 1234, 0: 1234,