msgpack-d icon indicating copy to clipboard operation
msgpack-d copied to clipboard

allow option for deserializing with missing, extra, or renamed fields

Open timotheecour opened this issue 9 years ago • 1 comments

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 option bool allow_reordered_fields

timotheecour avatar Oct 14 '16 21:10 timotheecour

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...

repeatedly avatar Nov 04 '16 00:11 repeatedly