-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bin targets for filter_by_availability and transform_expand_gener…
…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
1 parent
246b1a6
commit f169111
Showing
5 changed files
with
160 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
compiler-rs/clients_schema/src/bin/filter_by_availability.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
compiler-rs/clients_schema/src/bin/transform_expand_generics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters