pygob
pygob copied to clipboard
`interface{}` values
I tried to decode a MapType which had interface
keys and values.
I was able to decode it with these additions.
type.py
:
class GoInterface(GoType):
typeid = INTERFACE
zero = None # Seems to work.
def __init__(self, loader):
self._loader = loader
def decode(self, buf):
typename, buf = GoString.decode(buf)
# A nil value has an empty typename. What does that mean?
typeid, buf = GoInt.decode(buf)
segment, buf = self._loader._read_segment(buf)
zero, segment = GoUint.decode(segment) #What's this about?
assert zero == 0, 'illegal delta for singleton: %s' % zero
value, segment = self._loader.decode_value(typeid, segment)
assert segment == b'', 'trailing data in segment: %s' % list(segment)
return value, buf
loader.py
:
from .types import (INTERFACE, GoInterface)
class Loader:
def __init__(self):
# ...
interface_type = GoInterface(self)
self.types[INTERFACE] = interface_type
# ...
However, I don't know if this code only works for my own case (being new to Go), and I don't know if I made the right choices, such as inheriting from GoType. I am also uncomfortable with discarding the typename information.