diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-01 12:01:46 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-01 12:01:46 +0300 |
| commit | 2e65c90a04a399215290df824ef90168922946e4 (patch) | |
| tree | fac137c3eb4b4faf3c35a0d5730ddd715e0b9aeb | |
| parent | f853a5dcaa18ce0ecaf0198fe0367917447df4bf (diff) | |
| download | nimpb-2e65c90a04a399215290df824ef90168922946e4.tar.gz nimpb-2e65c90a04a399215290df824ef90168922946e4.zip | |
Update README.md
| -rw-r--r-- | README.md | 55 |
1 files changed, 32 insertions, 23 deletions
@@ -1,6 +1,6 @@ # Protocol Buffers for Nim -A pure Nim library for dealing with protobuf serialization and deserialization. A `protoc` plugin is also included which can be used for generating Nim code, usable by the library, from `.proto` files. +A Nim library to serialize/deserialize Protocol Buffers and a `protoc` plugin for generating Nim code from `.proto` files. At the moment this is at a very rough state. Do not use for any kind of production use. Anything can change at any time. You've been warned. @@ -13,36 +13,45 @@ syntax = "proto3"; message Test1 { int32 a = 1; + + enum MyEnum { + Foo = 0; + Bar = 1; + } + + MyEnum e = 2; } ``` -The `protoc` plugin will basically generate the following code: +The `protoc` plugin will generate the following types (and procs for interacting with them): ```nim -const - Test1Desc = MessageDesc( - name: "Test1", - fields: @[ - FieldDesc( - name: "a", - number: 1, - ftype: FieldType.Int32, - label: FieldLabel.Optional, - typeName: "", - packed: false - ) - ] - ) - -generateMessageType(Test1Desc) -generateMessageProcs(Test1Desc) +type + Test1_MyEnum* {.pure.} = enum + Foo = 0 + Bar = 1 + + Test1* = ref Test1Obj + Test1Obj* = object of RootObj + a: int32 + e: MyEnum ``` -And you can use it like this: +And you can use the generated code like this: ```nim let message = newTest1() -setA(message, 150) -let pbso = newProtobufStream(newFileStream(stdout)) -writeTest1(pbso, message) +message.a = 150 +message.e = Test1_MyEnum.Bar + +let data = serialize(message) + +let message2 = newTest1(data) + +assert message2.a == 150 +assert message2.e == Test1_MyEnum.Bar ``` + +# Other libraries + +If you do not want to depend on the `protoc` compiler, check Peter Munch-Ellingsen's protobuf library. It handles parsing of the protobuf files in compile time which can make building simpler. |
