From ebee2b27ddb9d94c6ea19db6164b3571316a1d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Saint-F=C3=A9lix?= Date: Wed, 10 Jul 2024 16:41:04 +0200 Subject: [PATCH 1/3] add bin targets for filter_by_availability and transform_expand_generics --- compiler-rs/Cargo.lock | 13 +-- compiler-rs/clients_schema/Cargo.toml | 1 + .../src/bin/filter_by_availability.rs | 81 +++++++++++++++++++ .../src/bin/transform_expand_generics.rs | 67 +++++++++++++++ compiler-rs/clients_schema/src/lib.rs | 2 +- 5 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 compiler-rs/clients_schema/src/bin/filter_by_availability.rs create mode 100644 compiler-rs/clients_schema/src/bin/transform_expand_generics.rs 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..7657598f3b --- /dev/null +++ b/compiler-rs/clients_schema/src/bin/filter_by_availability.rs @@ -0,0 +1,81 @@ +// 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(); + + println!("{:?}", cli.flavor); + + 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 = None)] +pub struct Cli { + schema: PathBuf, + flavor: Flavor, + + 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..8db22bc77d --- /dev/null +++ b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs @@ -0,0 +1,67 @@ +// 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 { + schema: PathBuf, + output: Option, +} \ No newline at end of file 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, From 14e189bdedcf12de0b1ce9d4a9918ac93c009a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Saint-F=C3=A9lix?= Date: Wed, 10 Jul 2024 16:45:26 +0200 Subject: [PATCH 2/3] add newline at the end of generics expansion --- compiler-rs/clients_schema/src/bin/transform_expand_generics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs index 8db22bc77d..47c927b498 100644 --- a/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs +++ b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs @@ -64,4 +64,4 @@ impl Cli { pub struct Cli { schema: PathBuf, output: Option, -} \ No newline at end of file +} From cd4079b46399eb86858444e353a8d31bb2cc659f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Saint-F=C3=A9lix?= Date: Thu, 11 Jul 2024 14:49:30 +0200 Subject: [PATCH 3/3] add comments to make bin less obscure --- .../clients_schema/src/bin/filter_by_availability.rs | 9 +++++---- .../clients_schema/src/bin/transform_expand_generics.rs | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/compiler-rs/clients_schema/src/bin/filter_by_availability.rs b/compiler-rs/clients_schema/src/bin/filter_by_availability.rs index 7657598f3b..dfa3fa5eff 100644 --- a/compiler-rs/clients_schema/src/bin/filter_by_availability.rs +++ b/compiler-rs/clients_schema/src/bin/filter_by_availability.rs @@ -24,8 +24,6 @@ use clients_schema::{Availabilities, Flavor, Visibility}; fn main() -> anyhow::Result<()> { let cli = Cli::parse(); - println!("{:?}", cli.flavor); - cli.run()?; Ok(()) @@ -72,10 +70,13 @@ impl Cli { #[derive(Debug, Parser)] -#[command(author, version, about, long_about = None)] +#[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 index 47c927b498..145f96434c 100644 --- a/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs +++ b/compiler-rs/clients_schema/src/bin/transform_expand_generics.rs @@ -62,6 +62,8 @@ impl Cli { #[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, }