using NativeBuffering;
using System.Diagnostics;

var entity = new Entity
{
    Dictionary1 = new Dictionary<int, long> { { 1, 1 }, { 2, 2 }, { 3, 3 } },
    Dictionary2 = new Dictionary<int, string> { { 1, "foo" }, { 2, "bar" }, { 3, "baz" } },
    Dictionary3 = new Dictionary<string, long> { { "foo", 1 }, { "bar", 2 }, { "baz", 3 } },
    Dictionary4 = new Dictionary<string, string> { { "a", "foo" }, { "b", "bar" }, { "c", "baz" } }
};

var bytes = new byte[entity.CalculateSize()];
var context = new BufferedObjectWriteContext(bytes);
entity.Write(context);
unsafe
{
    fixed (void* _ = bytes)
    {
        var bufferedMessage = EntityBufferedMessage.Parse(new NativeBuffer(bytes));

        ref var value1 = ref bufferedMessage.Dictionary1.AsRef(1);
        Debug.Assert(value1 == 1);
        ref var value2 = ref bufferedMessage.Dictionary3.AsRef("baz");
        Debug.Assert(value2 == 3);

        var dictionary1 = bufferedMessage.Dictionary1;
        Debug.Assert(dictionary1[1] == 1);
        Debug.Assert(dictionary1[2] == 2);
        Debug.Assert(dictionary1[3] == 3);

        var dictionary2 = bufferedMessage.Dictionary2;
        Debug.Assert(dictionary2[1] == "foo");
        Debug.Assert(dictionary2[2] == "bar");
        Debug.Assert(dictionary2[3] == "baz");

        var dictionary3 = bufferedMessage.Dictionary3;
        Debug.Assert(dictionary3["foo"] == 1);
        Debug.Assert(dictionary3["bar"] == 2);
        Debug.Assert(dictionary3["baz"] == 3);

        var dictionary4 = bufferedMessage.Dictionary4;
        Debug.Assert(dictionary4["a"] == "foo");
        Debug.Assert(dictionary4["b"] == "bar");
        Debug.Assert(dictionary4["c"] == "baz");
    }
}