Skip to content

Commit f169111

Browse files
authored
Add bin targets for filter_by_availability and transform_expand_generics (#2703)
* add bin targets for filter_by_availability and transform_expand_generics * add newline at the end of generics expansion * add comments to make bin less obscure
1 parent 246b1a6 commit f169111

File tree

5 files changed

+160
-7
lines changed

5 files changed

+160
-7
lines changed

compiler-rs/Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler-rs/clients_schema/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ anyhow = "1.0"
1414
indexmap = { version="1.9.3", features = ["serde"] }
1515

1616
arcstr = { version = "1.1.5", features = ["serde", "substr"] }
17+
clap = { version = "4.5.9", features = ["derive"] }
1718

1819
[dev-dependencies]
1920
serde_path_to_error = "0.1"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use std::path::{Path, PathBuf};
19+
20+
use clap::Parser;
21+
22+
use clients_schema::{Availabilities, Flavor, Visibility};
23+
24+
fn main() -> anyhow::Result<()> {
25+
let cli = Cli::parse();
26+
27+
cli.run()?;
28+
29+
Ok(())
30+
}
31+
32+
impl Cli {
33+
fn run(self) -> anyhow::Result<()> {
34+
let json = if self.schema == Path::new("-") {
35+
std::io::read_to_string(std::io::stdin())?
36+
} else {
37+
std::fs::read_to_string(self.schema)?
38+
};
39+
40+
let mut schema = clients_schema::IndexedModel::from_reader(json.as_bytes())?;
41+
42+
let filter: fn(&Option<Availabilities>) -> bool = match self.flavor {
43+
Flavor::Stack => |a| {
44+
Flavor::Stack.available(a)
45+
},
46+
Flavor::Serverless => |a| {
47+
Flavor::Serverless.visibility(a) == Some(Visibility::Public)
48+
}
49+
};
50+
51+
schema = clients_schema::transform::filter_availability(schema, filter)?;
52+
53+
let output: Box<dyn std::io::Write> = {
54+
if let Some(output) = self.output {
55+
if output == Path::new("-") {
56+
Box::new(std::io::stdout())
57+
} else {
58+
Box::new(std::fs::File::create(output)?)
59+
}
60+
} else {
61+
Box::new(std::io::stdout())
62+
}
63+
};
64+
let output = std::io::BufWriter::new(output);
65+
serde_json::to_writer_pretty(output, &schema).expect("TODO: panic message");
66+
67+
Ok(())
68+
}
69+
}
70+
71+
72+
#[derive(Debug, Parser)]
73+
#[command(author, version, about, long_about)]
74+
pub struct Cli {
75+
/// input schema file, eg: ../output/schema/schema-no-generics.json
76+
schema: PathBuf,
77+
/// filter flavor, eg: stack
78+
flavor: Flavor,
79+
/// default is stdout
80+
#[arg(long)]
81+
output: Option<PathBuf>,
82+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use std::path::{Path, PathBuf};
19+
20+
use clap::Parser;
21+
22+
use clients_schema::transform::ExpandConfig;
23+
24+
fn main() -> anyhow::Result<()> {
25+
let cli = Cli::parse();
26+
27+
cli.run()?;
28+
29+
Ok(())
30+
}
31+
32+
impl Cli {
33+
fn run(self) -> anyhow::Result<()> {
34+
let json = if self.schema == Path::new("-") {
35+
std::io::read_to_string(std::io::stdin())?
36+
} else {
37+
std::fs::read_to_string(self.schema)?
38+
};
39+
40+
let mut schema = clients_schema::IndexedModel::from_reader(json.as_bytes())?;
41+
schema = clients_schema::transform::expand_generics(schema, ExpandConfig::default())?;
42+
43+
let output: Box<dyn std::io::Write> = {
44+
if let Some(output) = self.output {
45+
if output == Path::new("-") {
46+
Box::new(std::io::stdout())
47+
} else {
48+
Box::new(std::fs::File::create(output)?)
49+
}
50+
} else {
51+
Box::new(std::io::stdout())
52+
}
53+
};
54+
let output = std::io::BufWriter::new(output);
55+
serde_json::to_writer_pretty(output, &schema).expect("TODO: panic message");
56+
57+
Ok(())
58+
}
59+
}
60+
61+
62+
#[derive(Debug, Parser)]
63+
#[command(author, version, about, long_about = None)]
64+
pub struct Cli {
65+
/// input schema file, eg: ../output/schema/schema-no-generics.json
66+
schema: PathBuf,
67+
/// default is stdout
68+
output: Option<PathBuf>,
69+
}

compiler-rs/clients_schema/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub struct Deprecation {
241241
}
242242

243243
/// An API flavor
244-
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
244+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, clap::ValueEnum)]
245245
#[serde(rename_all = "snake_case")]
246246
pub enum Flavor {
247247
Stack,

0 commit comments

Comments
 (0)