Skip to content

Commit

Permalink
protobuf: message fields moved to get_fields method
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 authored and jpochyla committed Sep 19, 2018
1 parent 3ae4106 commit 6d98a97
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ class UnicodeType:

class MessageType:
WIRE_TYPE = 2
FIELDS = {}

@classmethod
def get_fields(cls):
return {}

def __init__(self, **kwargs):
for kw in kwargs:
Expand Down Expand Up @@ -149,7 +152,7 @@ async def awrite(self, buf):


async def load_message(reader, msg_type):
fields = msg_type.FIELDS
fields = msg_type.get_fields()
msg = msg_type()

while True:
Expand Down Expand Up @@ -204,8 +207,8 @@ async def load_message(reader, msg_type):
setattr(msg, fname, fvalue)

# fill missing fields
for tag in msg.FIELDS:
field = msg.FIELDS[tag]
for tag in fields:
field = fields[tag]
if not hasattr(msg, field[0]):
setattr(msg, field[0], None)

Expand All @@ -215,7 +218,7 @@ async def load_message(reader, msg_type):
async def dump_message(writer, msg):
repvalue = [0]
mtype = msg.__class__
fields = mtype.FIELDS
fields = mtype.get_fields()

for ftag in fields:
fname, ftype, fflags = fields[ftag]
Expand Down

0 comments on commit 6d98a97

Please sign in to comment.