Skip to content

Commit

Permalink
💥 Replace all the varint types with the built-in int and ZigZagInt
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed May 10, 2023
1 parent fdbe51a commit a5e1c8b
Show file tree
Hide file tree
Showing 18 changed files with 395 additions and 401 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -43,19 +43,19 @@ And here's the same via `pure-protobuf`:
from dataclasses import dataclass
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class SearchRequest(BaseMessage):
query: Annotated[str, Field(1)] = ""
page_number: Annotated[uint, Field(2)] = 0
result_per_page: Annotated[uint, Field(3)] = 0
page_number: Annotated[int, Field(2)] = 0
result_per_page: Annotated[int, Field(3)] = 0


request = SearchRequest(query="hello", page_number=uint(1), result_per_page=uint(10))
request = SearchRequest(query="hello", page_number=1, result_per_page=10)
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
assert SearchRequest.read_from(BytesIO(buffer)) == request
Expand All @@ -66,19 +66,19 @@ assert SearchRequest.read_from(BytesIO(buffer)) == request
```python title="pydantic_example.py"
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from pydantic import BaseModel
from typing_extensions import Annotated


class SearchRequest(BaseMessage, BaseModel):
query: Annotated[str, Field(1)] = ""
page_number: Annotated[uint, Field(2)] = 0
result_per_page: Annotated[uint, Field(3)] = 0
page_number: Annotated[int, Field(2)] = 0
result_per_page: Annotated[int, Field(3)] = 0


request = SearchRequest(query="hello", page_number=uint(1), result_per_page=uint(10))
request = SearchRequest(query="hello", page_number=1, result_per_page=10)
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
assert SearchRequest.read_from(BytesIO(buffer)) == request
Expand Down
73 changes: 36 additions & 37 deletions docs/annotating_fields.md

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions docs/base_message.md
Expand Up @@ -6,57 +6,53 @@

```python title="test_write_to.py"
from dataclasses import dataclass
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class Message(BaseMessage):
a: Annotated[uint, Field(1)] = uint(0)
a: Annotated[int, Field(1)] = int(0)


io = BytesIO()
Message(a=uint(150)).write_to(io)
assert io.getvalue() == b"\x08\x96\x01"
assert Message(a=150).dumps() == b"\x08\x96\x01"
```

### Serialization into a [byte string](https://docs.python.org/3/library/stdtypes.html#bytes)

```python title="test_bytes.py"
from dataclasses import dataclass

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class Message(BaseMessage):
a: Annotated[uint, Field(1)] = uint(0)
a: Annotated[int, Field(1)] = 0


message = Message(a=uint(150))
message = Message(a=150)
assert bytes(message) == b"\x08\x96\x01"
```

### Deserialization from a [file object](https://docs.python.org/3/glossary.html#term-file-object)

```python title="test_read_from.py"
from dataclasses import dataclass
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class Message(BaseMessage):
a: Annotated[uint, Field(1)] = uint(0)
a: Annotated[int, Field(1)] = 0


assert Message.read_from(BytesIO(b"\x08\x96\x01")) == Message(a=uint(150))
assert Message.loads(b"\x08\x96\x01") == Message(a=150)
```
28 changes: 14 additions & 14 deletions docs/index.md
Expand Up @@ -19,22 +19,22 @@
from dataclasses import dataclass
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


@dataclass
class SearchRequest(BaseMessage):
query: Annotated[str, Field(1)] = ""
page_number: Annotated[uint, Field(2)] = 0
result_per_page: Annotated[uint, Field(3)] = 0
page_number: Annotated[int, Field(2)] = 0
result_per_page: Annotated[int, Field(3)] = 0


request = SearchRequest(
query="hello",
page_number=uint(1),
result_per_page=uint(10),
page_number=1,
result_per_page=10,
)
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
Expand All @@ -46,22 +46,22 @@
```python title="pydantic_example.py"
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from pydantic import BaseModel
from typing_extensions import Annotated


class SearchRequest(BaseMessage, BaseModel):
query: Annotated[str, Field(1)] = ""
page_number: Annotated[uint, Field(2)] = 0
result_per_page: Annotated[uint, Field(3)] = 0
page_number: Annotated[int, Field(2)] = 0
result_per_page: Annotated[int, Field(3)] = 0


request = SearchRequest(
query="hello",
page_number=uint(1),
result_per_page=uint(10),
page_number=1,
result_per_page=10,
)
buffer = bytes(request)
assert buffer == b"\x0A\x05hello\x10\x01\x18\x0A"
Expand All @@ -75,17 +75,17 @@
```python title="test_vanilla.py"
from io import BytesIO

from pure_protobuf.annotations import Field, uint
from pure_protobuf.annotations import Field
from pure_protobuf.message import BaseMessage
from typing_extensions import Annotated


class Message(BaseMessage):
a: Annotated[uint, Field(1)] = uint(0)
a: Annotated[int, Field(1)] = 0

def __init__(self, a: uint) -> None:
def __init__(self, a: int) -> None:
self.a = a


assert Message.read_from(BytesIO(b"\x08\x96\x01")).a == uint(150)
assert Message.read_from(BytesIO(b"\x08\x96\x01")).a == 150
```
5 changes: 5 additions & 0 deletions docs/migration.md
Expand Up @@ -12,6 +12,11 @@ The decorator has been removed. You should inherit your message classes from `#!

Replace annotations like `#!python foo: int = field(1)` with `#!python foo: Annotated[int, Field(1)]`.

## Integers

- Use the built-in `int` for variable-length integers, which use two's compliment negative representation.
- For ZigZag-encoded integers the new annotation `ZigZagInt` is introduced.

## Well-known types

`#!python typing.Any`, `#!python datetime.datetime`, and `#!python datetime.timedelta` are no longer mapped into the `.proto` types. Use `#!python pure_protobuf.well_known.Any_`, `#!python pure_protobuf.well_known.Timestamp`, and `#!python pure_protobuf.well_known.Duration` explicitly.
Expand Down

0 comments on commit a5e1c8b

Please sign in to comment.