https://github.com/egmkang/protobuf_message_factory
https://github.com/egmkang/protobuf_message_factory
factory-pattern protobuf reflection rust-lang
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/egmkang/protobuf_message_factory
- Owner: egmkang
- License: mit
- Created: 2019-09-08T06:12:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-10T15:16:31.000Z (almost 7 years ago)
- Last Synced: 2025-07-10T21:27:43.865Z (11 months ago)
- Topics: factory-pattern, protobuf, reflection, rust-lang
- Language: Rust
- Size: 6.84 KB
- Stars: 1
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protobuf_message_factory
this repo provide you a way to generate a message factory to create a message instance by message name.
```cpp
//use can do this in cpp
google::protobuf::Descriptor* desc =
google::protobuf::DescriptorPool::generated_pool()
->FindMessageTypeByName("mypkg.MyType");
google::protobuf::Message* message =
google::protobuf::MessageFactory::generated_factory()
->GetPrototype(desc)->New();
```
```rust
extern crate proto;
use proto::factory::*;
//now you can do this in rust
let desc = get_descriptor(&"mypkg.MyType".to_string()).unwrap();
let message = desc.new_instance();
```
API Docs: [https://docs.rs/protobuf_message_factory](https://docs.rs/protobuf_message_factory)
### Usage
## Step 1
create a project to generate `proto`
```sh
$ cargo new proto
```
Add this to Cargo.toml:
```
[dependencies]
protobuf = "2.8.0"
[build-dependencies]
protoc-rust = "2.8.0"
protobuf_message_factory = "0.1.3"
```
## Step 2
add `.proto` file into `src/`
## Step 3
add codes to build.rs
```rust
extern crate protobuf_message_factory;
use protobuf_message_factory::*;
...
fn main() {
let proto_path = "src/";
let proto_files = get_protos_info(proto_path);
let proto_messages = get_proto_list(&proto_files);
//!!! this is importent. !!!
protoc_rust::run(protoc_rust::Args {
out_dir: proto_path,
input: &protos,
includes: &[proto_path],
customize: Customize {
..Default::default()
},
}).expect("protoc");
//now generate factory codes
generate_factory_file(proto_path, &proto_files);
}
```
## Step 4
add `proto` deps into your project's toml
```
[dependencies]
proto = {version="^0", path="proto_path"}
```
step 1 create a proj named `proto`, replace `proto_path` into yours
### License
MIT