oskaritimperi / nimpb

Protocol Buffers for Nim
MIT License
37 stars 6 forks source link

text format serialization and deserialization #8

Open timotheecour opened 6 years ago

timotheecour commented 6 years ago

text format is very useful for debugging, or for cross language, human readable,, type safe, configuration files:

https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.text_format

example of text format

https://github.com/BVLC/caffe/blob/master/models/bvlc_reference_caffenet/solver.prototxt

usage

let message = newTest1()
message.a = 150

## serialize
let data = protoToText(message)
echo data # a : 150
echo message # would use `$` as alias to serializeText

## deserialize
let message2 = Test1.deserializeText(data)
assert message2 == message

## serialize with option
let data = serializeText(message, oneline = false)

WORKAROUND

same as https://github.com/msoucy/dproto/issues/71#issuecomment-365172146 : shell out to protoc eg:

## serializeText
serialize message to /tmp/z01.pb
protoc --decode=Test1 --proto_path=tests/t29_nimpb/ tests/t29_nimpb/test1.proto < /tmp/z01.pb > /tmp/z01.txt
read /tmp/z01.txt

## deserializeText
TODO

workaround is not great, as not as efficient, and requires passing the proto file; the proper solution should involve using reflection

oskaritimperi commented 6 years ago

This could be a nice addition. Need to take a look at some point.