msgpack-d
msgpack-d copied to clipboard
allow option for deserializing with missing, extra, or renamed fields
happy to discuss design of this
this is one of the best features of protocol buffers, allowing one to be forward and backward compatible with schema changes.
Eg:
struct Dataset{int age; int height;}
Dataset(13).pack.writeToFile("dataset.mpck");
// later on, Dataset is updated but we still want to use the saved data
struct Dataset{int age; string address = "bar";}
auto dataset="dataset.mpck".read.unpack!Dataset;
// currently, error: incompatible type
- NOTE: error is actually uninformative, it should also show which fields are wrong
// desired behavior:
struct MsgpackOption{
bool allow_missing_fields=false;
bool allow_added_fields=false;
}
MsgpackOption opt={allow_missing_fields:true, allow_added_fields:true};
auto dataset="dataset.mpck".read.unpack!Dataset(opt);
assert(dataset==Dataset(13, "bar"));
- NOTE: should also work with reordered fields (eg:
struct Dataset{int height;int age; }, possibly modulo an optionbool allow_reordered_fields
Allowing missing / extra fields seems good for me. msgpack doesn't have a schema unlike protocol buffers, so it seems to work with only tail fields. I will try to implement it.
For reorder support, user should provide serialized data index to actual struct field index. For such case, msgpack seems to not fit...