blob: 2a0aed30ad32a8415d3ed102e7081a17172b3d7b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# Protocol Buffers for Nim
A Nim library to serialize/deserialize Protocol Buffers.
For generating Nim code usable with nimpb, you should use [nimpb_build](https://github.com/oswjk/nimpb-build).
**NOTE** 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.
# Example
Given the following file:
```
syntax = "proto3";
message Test1 {
int32 a = 1;
enum MyEnum {
Foo = 0;
Bar = 1;
}
MyEnum e = 2;
}
```
You can use [nimpb_build](https://github.com/oswjk/nimpb-build) to generate code like this (procs not included in the example):
```nim
type
Test1_MyEnum* {.pure.} = enum
Foo = 0
Bar = 1
Test1* = ref Test1Obj
Test1Obj* = object of RootObj
a: int32
e: MyEnum
```
And you can use the generated code like this:
```nim
let message = newTest1()
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](https://github.com/PMunch/protobuf-nim). It handles parsing of the protobuf files in compile time which can make building simpler.
|