diff --git a/compiler-rs/Cargo.lock b/compiler-rs/Cargo.lock index 7244c2f184..855f5ffc31 100644 --- a/compiler-rs/Cargo.lock +++ b/compiler-rs/Cargo.lock @@ -86,9 +86,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.4" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive", @@ -96,9 +96,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", @@ -108,9 +108,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085" dependencies = [ "heck", "proc-macro2", @@ -130,6 +130,7 @@ version = "0.1.0" dependencies = [ "anyhow", "arcstr", + "clap", "derive_more", "indexmap", "once_cell", diff --git a/compiler-rs/clients_schema/Cargo.toml b/compiler-rs/clients_schema/Cargo.toml index d8253445bd..9a30b5ff86 100644 --- a/compiler-rs/clients_schema/Cargo.toml +++ b/compiler-rs/clients_schema/Cargo.toml @@ -14,6 +14,7 @@ anyhow = "1.0" indexmap = { version="1.9.3", features = ["serde"] } arcstr = { version = "1.1.5", features = ["serde", "substr"] } +clap = { version = "4.5.9", features = ["derive"] } [dev-dependencies] serde_path_to_error = "0.1" diff --git a/compiler-rs/clients_schema/src/bin/filter_by_availability.rs b/compiler-rs/clients_schema/src/bin/filter_by_availability.rs new file mode 100644 index 0000000000..dfa3fa5eff --- /dev/null +++ b/compiler-rs/clients_schema/src/bin/filter_by_availability.rs @@ -0,0 +1,82 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::path::{Path, PathBuf}; + +use clap::Parser; + +use clients_schema::{Availabilities, Flavor, Visibility}; + +fn main() -> anyhow::Result<()> { + let cli = Cli::parse(); + + cli.run()?; + + Ok(()) +} + +impl Cli { + fn run(self) -> anyhow::Result<()> { + let json = if self.schema == Path::new("-") { + std::io::read_to_string(std::io::stdin())? + } else { + std::fs::read_to_string(self.schema)? + }; + + let mut schema = clients_schema::IndexedModel::from_reader(json.as_bytes())?; + + let filter: fn(&Option) -> bool = match self.flavor { + Flavor::Stack => |a| { + Flavor::Stack.available(a) + }, + Flavor::Serverless => |a| { + Flavor::Serverless.visibility(a) == Some(Visibility::Public) + } + }; + + schema = clients_schema::transform::filter_availability(schema, filter)?; + + let output: Box = { + if let Some(output) = self.output { + if output == Path::new("-") { + Box::new(std::io::stdout()) + } else { + Box::new(std::fs::File::create(output)?) + } + } else { + Box::new(std::io::stdout()) + } + }; + let output = std::io::BufWriter::new(output); + serde_json::to_writer_pretty(output, &schema).expect("TODO: panic message"); + + Ok(()) + } +} + + +#[derive(Debug, Parser)] +#[command(author, version, about, long_about)] +pub struct Cli { + /// input schema file, eg: ../output/schema/schema-no-generics.json + schema: PathBuf, + /// filter flavor, eg: stack + flavor: Flavor, + /// default is stdout + #[arg(long)] + output: Option, +} \ No newline at end of file diff --git a/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs new file mode 100644 index 0000000000..145f96434c --- /dev/null +++ b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs @@ -0,0 +1,69 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::path::{Path, PathBuf}; + +use clap::Parser; + +use clients_schema::transform::ExpandConfig; + +fn main() -> anyhow::Result<()> { + let cli = Cli::parse(); + + cli.run()?; + + Ok(()) +} + +impl Cli { + fn run(self) -> anyhow::Result<()> { + let json = if self.schema == Path::new("-") { + std::io::read_to_string(std::io::stdin())? + } else { + std::fs::read_to_string(self.schema)? + }; + + let mut schema = clients_schema::IndexedModel::from_reader(json.as_bytes())?; + schema = clients_schema::transform::expand_generics(schema, ExpandConfig::default())?; + + let output: Box = { + if let Some(output) = self.output { + if output == Path::new("-") { + Box::new(std::io::stdout()) + } else { + Box::new(std::fs::File::create(output)?) + } + } else { + Box::new(std::io::stdout()) + } + }; + let output = std::io::BufWriter::new(output); + serde_json::to_writer_pretty(output, &schema).expect("TODO: panic message"); + + Ok(()) + } +} + + +#[derive(Debug, Parser)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + /// input schema file, eg: ../output/schema/schema-no-generics.json + schema: PathBuf, + /// default is stdout + output: Option, +} diff --git a/compiler-rs/clients_schema/src/lib.rs b/compiler-rs/clients_schema/src/lib.rs index caba0d491f..0227760c65 100644 --- a/compiler-rs/clients_schema/src/lib.rs +++ b/compiler-rs/clients_schema/src/lib.rs @@ -241,7 +241,7 @@ pub struct Deprecation { } /// An API flavor -#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, clap::ValueEnum)] #[serde(rename_all = "snake_case")] pub enum Flavor { Stack,