antocuni / capnpy

Other
45 stars 26 forks source link

Fix nullable default & shortrepr #24

Closed colinfang closed 6 years ago

colinfang commented 6 years ago
struct Foo {

    bar :group $Py.nullable {
        isNull @0 :Int8;
        value @1 :Float64;
    }
}

generate

@staticmethod
def __new(bar=None):
    builder = _SegmentBuilder()
    pos = builder.allocate(16)
    if bar is None:
        bar_is_null = 1
        bar_value = 0
    else:
        bar_is_null = 0
        bar_value = bar
    builder.write_int8(pos + 0, bar_is_null)
    builder.write_float64(pos + 8, bar_value)
    return builder.as_string()

def __init__(self, bar=None):
    _buf = Foo.__new(bar)
    self._init_from_buffer(_buf, 0, 2, 0)

def shortrepr(self):
    parts = []
    parts.append("bar = %s" % (_float64_repr(self.bar) if self.bar is not None else None))
    return "(%s)" % ", ".join(parts)

So that

In [1]: Foo().shortrepr()
Out[1]: '(bar = None)'
In [2]: Foo(3).shortrepr()
Out[2]: '(bar = 3.0)'
colinfang commented 6 years ago

23