influxdb/google_types/build.rs

32 lines
1019 B
Rust
Raw Normal View History

//! Compiles Protocol Buffers and FlatBuffers schema definitions into
//! native Rust types.
use std::path::PathBuf;
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
fn main() -> Result<()> {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
let proto_files = vec![root.join("google/protobuf/types.proto")];
// Tell cargo to recompile if any of these proto files are changed
for proto_file in &proto_files {
println!("cargo:rerun-if-changed={}", proto_file.display());
}
prost_build::Config::new()
.compile_well_known_types()
.disable_comments(&["."])
feat: Derive serde for protos Rationale --------- Our CLI needs to be able to accept configuration as JSON and render configuration as JSON. Protobufs technically have an official JSON encoding rule called 'jsonpb` but prost doesn't offer native supprot for it. `prost` allows us to specify arbitrary derive metadata to be added to generated code. We emit the `serde` derive directives in the two packages that generate prost code (`generated_types` and `google_types`). We use the `serde(rename_all = "camelCase")` to approximate `jsonpb`. We instruct `prost` to use `bytes::Bytes` for some types, hence we must turn on the `serde` feature on the `bytes` dependency. We also use json to serialize the output of the `database get` command, to showcase the feature and get rid of a TODO. In a subsequent PR I'll teach `database create` (and the yet to be done `database update`) to accept an option JSON configuration body so we can configure partitioning, lifecycle, sharding etc rules etc. Caveats ------- This is not technically `jsonpb`. Main issues: 1. default values not omitted 2. no special rendering of special types like `google.protobuf.Any` Future work ----------- Figure out if we can get fully compliant `jsonpb`, or at least a decent approximation. Effect ------ ```console $ cargo run -- database get foobar_weather { "name": "foobar_weather", "partitionTemplate": { "parts": [ { "part": { "time": "%Y-%m-%d %H:00:00" } } ] }, "lifecycleRules": { "mutableLingerSeconds": 0, "mutableMinimumAgeSeconds": 0, "mutableSizeThreshold": 0, "bufferSizeSoft": 0, "bufferSizeHard": 0, "sortOrder": { "order": 2, "sort": { "createdAtTime": {} } }, "dropNonPersisted": false, "immutable": false }, "walBufferConfig": null, "shardConfig": { "specificTargets": null, "hashRing": null, "ignoreErrors": false } } ```
2021-03-30 02:29:47 +00:00
// approximates jsonpb. This is still not enough to deal with the special cases like Any.
.type_attribute(
".google",
"#[derive(serde::Serialize,serde::Deserialize)] #[serde(rename_all = \"camelCase\")]",
)
.bytes(&[".google"])
.compile_protos(&proto_files, &[root])?;
Ok(())
}