Skip to content

Commit

Permalink
Add bin targets for filter_by_availability and transform_expand_gener…
Browse files Browse the repository at this point in the history
…ics (#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
  • Loading branch information
Anaethelion authored Jul 11, 2024
1 parent 246b1a6 commit f169111
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 7 deletions.
13 changes: 7 additions & 6 deletions compiler-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiler-rs/clients_schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
82 changes: 82 additions & 0 deletions compiler-rs/clients_schema/src/bin/filter_by_availability.rs
Original file line number Diff line number Diff line change
@@ -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<Availabilities>) -> 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<dyn std::io::Write> = {
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<PathBuf>,
}
69 changes: 69 additions & 0 deletions compiler-rs/clients_schema/src/bin/transform_expand_generics.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::io::Write> = {
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<PathBuf>,
}
2 changes: 1 addition & 1 deletion compiler-rs/clients_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f169111

Please sign in to comment.