Updated code for ProtoBuf.jl 1.0.0
# important: .proto files are required in runtime!
const _proj_path = normpath(joinpath(@__FILE__, "..", ".."))
const _proto_path = "some_path"
const ENV_ = copy(ENV)
ENV_["JULIA"] = joinpath(Sys.BINDIR, Base.julia_exename())
function protobuf_from_text(
input::String,
proto_type::Type{T},
proto_file = "my.proto"
) where {T}
proto_type_name = string(Base.typename(proto_type).wrapper)
args = `--proto_path=$_proto_path --encode=namespace.$proto_type_name $proto_file`
protoc_jll.protoc() do protoc_path
inbuf = PipeBuffer()
write(inbuf, input)
outbuf = PipeBuffer()
run(pipeline(setenv(`$protoc_path $args`, ENV_); stdin = inbuf, stdout = outbuf))
d = ProtoDecoder(outbuf)
return decode(d, proto_type)::T
end
end
function protobuf_to_text(input::T, proto_file = "my.proto") where {T}
proto_type = typeof(input)
proto_type_name = string(Base.typename(proto_type).wrapper)
args = `--proto_path=$_proto_path --decode=namespace.$proto_type_name $proto_file`
protoc_jll.protoc() do protoc_path
inbuf = PipeBuffer()
e = ProtoEncoder(inbuf)
encode(e, input)
outbuf = PipeBuffer()
run(pipeline(setenv(`$protoc_path $args`, ENV_); stdin = inbuf, stdout = outbuf))
return read(outbuf, String)
end
end