influxdb/generated_types/build.rs

81 lines
2.9 KiB
Rust
Raw Normal View History

//! Compiles Protocol Buffers into native Rust types.
2021-05-05 19:52:55 +00:00
use std::env;
use std::path::{Path, PathBuf};
type Error = Box<dyn std::error::Error>;
type Result<T, E = Error> = std::result::Result<T, E>;
fn main() -> Result<()> {
2021-02-22 10:48:11 +00:00
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("protos");
generate_grpc_types(&root)?;
Ok(())
}
/// Schema used with IOx specific gRPC requests
///
/// Creates:
///
2021-02-22 10:48:11 +00:00
/// - `com.github.influxdata.idpe.storage.read.rs`
/// - `influxdata.iox.catalog.v1.rs`
2021-02-22 10:48:11 +00:00
/// - `influxdata.iox.management.v1.rs`
/// - `influxdata.iox.write.v1.rs`
/// - `influxdata.platform.storage.rs`
fn generate_grpc_types(root: &Path) -> Result<()> {
2021-02-22 10:48:11 +00:00
let storage_path = root.join("influxdata/platform/storage");
let idpe_path = root.join("com/github/influxdata/idpe/storage/read");
let catalog_path = root.join("influxdata/iox/catalog/v1");
2021-02-22 10:48:11 +00:00
let management_path = root.join("influxdata/iox/management/v1");
let write_path = root.join("influxdata/iox/write/v1");
2021-02-22 10:48:11 +00:00
let proto_files = vec![
2021-02-22 10:48:11 +00:00
storage_path.join("test.proto"),
storage_path.join("predicate.proto"),
storage_path.join("storage_common.proto"),
storage_path.join("service.proto"),
2021-02-23 10:55:09 +00:00
storage_path.join("storage_common_idpe.proto"),
idpe_path.join("source.proto"),
catalog_path.join("catalog.proto"),
2021-02-22 10:48:11 +00:00
management_path.join("base_types.proto"),
management_path.join("database_rules.proto"),
management_path.join("chunk.proto"),
management_path.join("partition.proto"),
2021-02-22 10:48:11 +00:00
management_path.join("service.proto"),
2021-03-19 09:19:13 +00:00
management_path.join("shard.proto"),
management_path.join("jobs.proto"),
write_path.join("service.proto"),
root.join("grpc/health/v1/service.proto"),
root.join("google/longrunning/operations.proto"),
root.join("google/rpc/error_details.proto"),
root.join("google/rpc/status.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());
}
let mut config = prost_build::Config::new();
config
.compile_well_known_types()
.disable_comments(&[".google"])
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
// Tracking issue for proper jsonpb support in prost: https://github.com/danburkert/prost/issues/277
.type_attribute(
".",
"#[derive(serde::Serialize,serde::Deserialize)] #[serde(rename_all = \"camelCase\")]",
)
.extern_path(".google.protobuf", "::google_types::protobuf");
2021-05-05 19:52:55 +00:00
let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("proto_descriptor.bin");
tonic_build::configure()
.file_descriptor_set_path(&descriptor_path)
.format(true)
.compile_with_config(config, &proto_files, &[root.into()])?;
Ok(())
}