Commit 4997896b authored by Ruslan Kuprieiev's avatar Ruslan Kuprieiev Committed by Pavel Emelyanov

pycriu: images: merge payload field into entry and add extra field to entry if needed

Before:
{
    "magic" : "FOO",
    "entries" : [
        {
            "payload" : {
                "foo" : "bar",
                "bar" : "foo"
            },
            "extra" : "abc"
        },
        {
            "payload" : {
            "foo" : "bar",
            "bar" : "foo"
            },
            "extra" : "abc"
        }
    ]
}

After:
{
    "magic" : "FOO",
    "entries" : [
        {
            "foo" : "bar",
            "bar" : "foo"
            "extra" : "abc"
        },
        {
            "foo" : "bar",
            "bar" : "foo"
            "extra" : "abc"
        }
    ]
}

We don't have any fields named "extra" in our pb msgs and it is
not likely that we will ever have one, so there is no reason to
worry about that.
Signed-off-by: 's avatarRuslan Kuprieiev <kupruser@gmail.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 912f1a5a
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
# Entry, in its turn, could be described as: # Entry, in its turn, could be described as:
# #
# { # {
# 'payload' : pb_msg, # pb_msg,
# 'extra' : extra_msg # 'extra' : extra_msg
# } # }
# #
...@@ -77,7 +77,7 @@ class entry_handler: ...@@ -77,7 +77,7 @@ class entry_handler:
break break
size, = struct.unpack('i', buf) size, = struct.unpack('i', buf)
pb.ParseFromString(f.read(size)) pb.ParseFromString(f.read(size))
entry['payload'] = pb2dict.pb2dict(pb) entry = pb2dict.pb2dict(pb)
# Read extra # Read extra
if self.extra_handler: if self.extra_handler:
...@@ -101,17 +101,19 @@ class entry_handler: ...@@ -101,17 +101,19 @@ class entry_handler:
in binary format to. in binary format to.
""" """
for entry in entries: for entry in entries:
extra = entry.pop('extra', None)
# Write payload # Write payload
pb = self.payload() pb = self.payload()
pb2dict.dict2pb(entry['payload'], pb) pb2dict.dict2pb(entry, pb)
pb_str = pb.SerializeToString() pb_str = pb.SerializeToString()
size = len(pb_str) size = len(pb_str)
f.write(struct.pack('i', size)) f.write(struct.pack('i', size))
f.write(pb_str) f.write(pb_str)
# Write extra # Write extra
if self.extra_handler: if self.extra_handler and extra:
self.extra_handler.dump(entry['extra'], f, pb) self.extra_handler.dump(extra, f, pb)
def dumps(self, entries): def dumps(self, entries):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment