2021-03-18 17:43:58 +00:00
|
|
|
//! Compiles Protocol Buffers into native Rust types.
|
2020-06-05 20:22:27 +00:00
|
|
|
|
2021-05-05 19:52:55 +00:00
|
|
|
use std::env;
|
2021-03-18 17:43:58 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-06-05 20:22:27 +00:00
|
|
|
|
|
|
|
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");
|
2020-06-05 20:22:27 +00:00
|
|
|
|
|
|
|
generate_grpc_types(&root)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-11 20:20:53 +00:00
|
|
|
/// Schema used with IOx specific gRPC requests
|
2020-06-05 20:22:27 +00:00
|
|
|
///
|
2021-04-29 16:07:49 +00:00
|
|
|
/// Creates:
|
|
|
|
///
|
2021-02-22 10:48:11 +00:00
|
|
|
/// - `com.github.influxdata.idpe.storage.read.rs`
|
2021-04-29 16:07:49 +00:00
|
|
|
/// - `influxdata.iox.catalog.v1.rs`
|
2021-02-22 10:48:11 +00:00
|
|
|
/// - `influxdata.iox.management.v1.rs`
|
2021-04-29 16:07:49 +00:00
|
|
|
/// - `influxdata.iox.write.v1.rs`
|
|
|
|
/// - `influxdata.platform.storage.rs`
|
2020-06-05 20:22:27 +00:00
|
|
|
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");
|
2021-04-29 16:07:49 +00:00
|
|
|
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");
|
2021-03-08 17:35:19 +00:00
|
|
|
let write_path = root.join("influxdata/iox/write/v1");
|
2021-02-22 10:48:11 +00:00
|
|
|
|
2020-11-11 20:20:53 +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"),
|
2021-04-29 16:07:49 +00:00
|
|
|
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"),
|
2021-03-12 13:56:14 +00:00
|
|
|
management_path.join("chunk.proto"),
|
2021-03-11 19:33:04 +00:00
|
|
|
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"),
|
2021-03-12 15:01:27 +00:00
|
|
|
management_path.join("jobs.proto"),
|
2021-03-08 17:35:19 +00:00
|
|
|
write_path.join("service.proto"),
|
2021-03-10 17:34:07 +00:00
|
|
|
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"),
|
2020-11-11 20:20:53 +00:00
|
|
|
];
|
2020-06-05 20:22:27 +00:00
|
|
|
|
2020-11-11 20:20:53 +00:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:14:53 +00:00
|
|
|
let mut config = prost_build::Config::new();
|
|
|
|
|
|
|
|
config
|
|
|
|
.compile_well_known_types()
|
2021-03-10 17:34:07 +00:00
|
|
|
.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\")]",
|
|
|
|
)
|
2021-03-10 17:34:07 +00:00
|
|
|
.extern_path(".google.protobuf", "::google_types::protobuf");
|
2021-02-12 16:14:53 +00:00
|
|
|
|
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()])?;
|
2020-06-05 20:22:27 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|