nim-protobuf-serialization/README.md

95 lines
3.9 KiB
Markdown
Raw Permalink Normal View History

2020-03-10 14:53:52 +00:00
# nim-protobuf-serialization
[![License: Apache](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
![Github action](https://github.com/status-im/nim-protobuf-serialization/workflows/CI/badge.svg)
2020-03-10 14:53:52 +00:00
Protobuf implementation compatible with the [nim-serialization](https://github.com/status-im/nim-serialization) framework.
2020-03-10 14:53:52 +00:00
## Usage
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
Messages in protobuf are serialized according to a schema found in `.proto` files. The library requires that types are annotated with schema information - this can be done either directly in Nim or, for some `proto3` files, generated using the `import_proto3` macro.
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
Both Protobuf 2 and Protobuf 3 semantics are supported. When declaring an object, add either the `proto2` or `proto3` pragma to declare which to use, as seen in the `syntax` element in protobuf.
When using Protobuf 3, a `import_proto3` macro is available. Taking in a file path, it can directly parse a Protobuf 3 spec file and generate the matching Nim types, same as if they had been written manually.
### Annotating objects
The protobuf schema can be declared using annotations similar to what is found in a typical `.proto` file - see [types](./protobuf_serialization/types.nim) for available annotations:
2020-08-03 09:18:31 +00:00
**my_protocol.proto3**:
```proto3
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
syntax = "proto3";
2020-08-03 09:18:31 +00:00
message ExampleMsg {
int32 a = 1;
float b = 2;
}
```
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
**Annotated Nim code**
2020-08-03 09:18:31 +00:00
```nim
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
type ExampleMsg {.proto3.} = object
a {.fieldNumber: 1, pint.}: int32
b {.fieldNumber: 2.}: float32
```
**Importing proto file**:
```nim
import protobuf_serialization/proto_parser
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
# This generates the same definition as above using a compile-time macro / parser
2020-08-03 09:18:31 +00:00
import_proto3 "my_protocol.proto3"
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
```
2020-08-03 09:18:31 +00:00
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
**Encoding and decoding**
2020-08-03 09:18:31 +00:00
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
```nim
2020-08-03 09:18:31 +00:00
let x = ExampleMsg(a: 10, b: 20.0)
let encoded = Protobuf.encode(x)
2020-08-03 09:18:31 +00:00
...
let decoded = Protobuf.decode(encoded, ExampleMsg)
2020-08-03 09:18:31 +00:00
```
Both Protobuf 2 and Protobuf 3 objects have the following properties:
- Every field requires the `fieldNumber` pragma, which takes in an integer of what field number to encode that field with.
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
- Every int/uint must have its bits explicitly specified.
2021-01-14 11:03:50 +00:00
- int/uint fields require their encoding to be specified. `pint` is valid for both, and uses VarInt encoding, which only uses the amount of bytes it needs. `fixed` is also valid for both, and uses the full amount of bytes the number uses, instead of stripping unused bytes. This has performance advantages for large numbers. Finally, `sint` uses zig-zagged VarInt encoding, which is recommended for numbers which are frequently negative, and is only valid for ints.
Protobuf 2 has the additional properties:
- A `required` pragma is enabled, matching the syntax of Protobuf 2's required keyword.
- Every primitive value, such as a number or string, must have the `required` pragma or be a `PBOption`. `PBOption`s are a generic type instantiated with the default value for that field. They serve as regular Options, except when they're none, they still return a value when get is called (the default value). `PBOption`s can be constructed using `pbSome(PBOption[T], value)`.
2020-08-03 09:18:31 +00:00
Here is an example demonstrating how the various pragmas can be combined:
```nim
type
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
X {.proto3.} = object
a {.fieldNumber: 1, pint.}: int32
b {.fieldNumber: 2.}: float32
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
Y {.proto2.} = object
a {.fieldNumber: 1.}: seq[string]
Cleanup / rewrite (#36) This is a cleanup / rewrite of the implementation increasing interoperability with other implementations and fixing several bugs / security issues * remove special handling of nim:isms (`ptr`, `ref`, `Option`, `HashSet` etc) (#4) * these deviate from "standard" protobuf behavior making the library less interoperable with other langagues * supporting "custom" types should be done as part of an extension framework instead (that can support any type / collection instead of a few hand-picked special cases) * don't allow encoding scalars in the core encoder (#31) * `codec` can be used to encode simple scalars * switch to `libp2p/minprotobuf`-like encoding base, fixing several bugs, crashes and inaccuracies (#30, #32, #33) * move parsing support to separate import * the parser is a heavy dependency * allow unknown fields * unknown fields should be given an extension point allowing the user to detect / handle them - standard behavior for protobuf is to ignore them * work around several faststreams bugs (#22) * remove machine-word-dependent length prefix options (#35) * actually, remove varint length prefix too for now (due to faststreams bugs) * update version * verify that strings are valid utf-8 on parse * fix warnings * truncate values like C++ version * allow unpacked fields in proto3 * protobuf2/3 -> proto2/3 * update docs There's lots left to do here in terms of tests and features: * Almost all tests are roundtrip tests - meaning they check that writing and reading have the same bugs (vs outputting conforming protobuf) * There are very few invalid-input tests * There's a beginning of an extension mechanism, but it needs more work * It's generally inefficient to copy data to a protobuf object and then write it to a stream - the stream writers should probably be made more general to handle this case better (either via callbacks or some other "builder-like" mechanism - projects currently using `minprotobuf` will likely see a performance regression using this library * `required` semantics are still off - a set/not-set flag is needed for every field in proto2 * possibly, when annotated with proto2, we should simply rewrite all members to become `PBOption` (as well as rename the field)
2023-01-10 08:07:24 +00:00
b {.fieldNumber: 2, pint.}: PBOption[int32(2)]
c {.fieldNumber: 3, required, sint.}: int32
```
2020-03-10 14:53:52 +00:00
## License
Licensed and distributed under either of
* MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
or
* Apache License, Version 2.0, ([LICENSE-APACHEv2](LICENSE-APACHEv2) or http://www.apache.org/licenses/LICENSE-2.0)
at your option. These files may not be copied, modified, or distributed except according to those terms.