From b766aadef97dd615a91f70a1d672fb567cd0a8f7 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 5 Sep 2023 01:02:41 -0600 Subject: [PATCH] Added deserialization for map type, nil map, and empty map --- context.go | 58 +++++++++++++++++++++++++++++++++++++++++++++-- serialize_test.go | 2 +- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 95e43be..77d8f44 100644 --- a/context.go +++ b/context.go @@ -708,11 +708,65 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) { reflect_type := reflect.MapOf(key_type, elem_type) return reflect_type, nil, value, nil } else if len(value.Data) < 8 { - + return nil, nil, value, fmt.Errorf("Not enough data to deserialize map") } 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 { return nil, err diff --git a/serialize_test.go b/serialize_test.go index 6aee6f3..8291763 100644 --- a/serialize_test.go +++ b/serialize_test.go @@ -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[[][]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{ 0: 1234,