Skip to content

Commit

Permalink
parse deprecated note from config
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Dec 26, 2024
1 parent 26a2bfe commit d669f61
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
47 changes: 47 additions & 0 deletions dev/Cargo.lock

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

5 changes: 4 additions & 1 deletion dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ version = "0.0.1"
[dependencies]
anyhow = "1.0.95"
clap = { version = "4.5.23", features = ["derive"] }
enquote = "1.1.0"
env_logger = "0.11.6"
itertools = "0.13.0"
log = "0.4.22"
syn = { version = "2.0.91", features = ["visit","full","extra-traits"] }
proc-macro2 = { version = "1.0.91", features = ["span-locations"] }
syn = { version = "2.0.91", features = ['parsing', 'full', 'derive', 'visit', 'extra-traits'] }

[dev-dependencies]
pretty_assertions = "1.4.1"
3 changes: 2 additions & 1 deletion dev/src/generate/binding_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

use crate::generate::parser::Services;
use anyhow::Result;
use std::path::PathBuf;

pub fn generate(services: &Services) -> Result<()> {
pub fn generate(_project_root: PathBuf, services: &Services) -> Result<()> {
println!("{:?}", services);

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion dev/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use std::path::PathBuf;
pub fn run(language: &str) -> Result<()> {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let services_path = manifest_dir.join("../core/src/services").canonicalize()?;
let project_root = manifest_dir.join("..").canonicalize()?;
let services = parser::parse(&services_path.to_string_lossy())?;

match language {
"python" | "py" => binding_python::generate(&services),
"python" | "py" => binding_python::generate(project_root, &services),
_ => Err(anyhow::anyhow!("Unsupported language: {}", language)),
}
}
69 changes: 62 additions & 7 deletions dev/src/generate/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@

use anyhow::Result;
use anyhow::{anyhow, Context};
use itertools::Itertools;
use log::debug;
use std::collections::hash_map;
use std::collections::HashMap;
use std::fs;
use std::fs::read_dir;
use std::str::FromStr;
use std::{fs, vec};
use syn::{Field, GenericArgument, Item, ItemStruct, PathArguments, Type, TypePath};

#[derive(Debug, Clone)]
pub struct Services(HashMap<String, Service>);

impl IntoIterator for Services {
type Item = (String, Service);
type IntoIter = hash_map::IntoIter<String, Service>;
type IntoIter = vec::IntoIter<(String, Service)>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
self.0.into_iter().sorted_by_key(|x| x.0.clone())
}
}

Expand All @@ -53,6 +53,8 @@ pub struct Config {
pub value: ConfigType,
/// If given config is optional or not.
pub optional: bool,
/// if this field is deprecated, a deprecated message will be provided.
pub deprecated: Option<String>,
/// The comments for this config.
///
/// All white spaces and extra new lines will be trimmed.
Expand Down Expand Up @@ -181,7 +183,7 @@ impl ServiceParser {

let mut config = Vec::with_capacity(config_struct.fields.len());
for field in config_struct.fields {
let field = Self::parse_field(field)?;
let field = self.parse_field(field)?;
config.push(field);
}

Expand All @@ -190,12 +192,14 @@ impl ServiceParser {
}

/// TODO: Add comment parse support.
fn parse_field(field: Field) -> Result<Config> {
fn parse_field(&self, field: Field) -> Result<Config> {
let name = field
.ident
.clone()
.ok_or_else(|| anyhow!("field name is missing for {:?}", &field))?;

let deprecated = self.deprecated_note(&field);

let (cfg_type, optional) = match &field.ty {
Type::Path(TypePath { path, .. }) => {
let segment = path
Expand Down Expand Up @@ -226,6 +230,7 @@ impl ServiceParser {
};

let typ = type_name.as_str().parse()?;
let optional = optional || typ == ConfigType::Bool;

(typ, optional)
}
Expand All @@ -236,9 +241,44 @@ impl ServiceParser {
name: name.to_string(),
value: cfg_type,
optional,
deprecated,
comments: "".to_string(),
})
}

fn deprecated_note(&self, field: &Field) -> Option<String> {
for attr in &field.attrs {
if !attr.path().is_ident("deprecated") {
continue;
}

let meta_list = match &attr.meta {
syn::Meta::List(meta_list) => meta_list,
_ => continue,
};

let tokens = Vec::from_iter(meta_list.tokens.clone().into_iter());
for (index, token) in tokens.iter().enumerate() {
let ident = match token {
proc_macro2::TokenTree::Ident(ident) => ident,
_ => {
continue;
}
};

if ident == "note" {
return tokens
.get(index + 2)
.expect("deprecated attribute missing note")
.span()
.source_text()
.map(|s| enquote::unquote(s.as_str()).expect("should unquote string"));
}
}
}

return None;
}
}

#[cfg(test)]
Expand All @@ -256,6 +296,7 @@ mod tests {
name: "root".to_string(),
value: ConfigType::String,
optional: true,
deprecated: None,
comments: "".to_string(),
},
),
Expand All @@ -265,6 +306,7 @@ mod tests {
name: "root".to_string(),
value: ConfigType::String,
optional: false,
deprecated: None,
comments: "".to_string(),
},
),
Expand Down Expand Up @@ -495,12 +537,25 @@ impl Debug for S3Config {

let service = parser.parse().unwrap();
assert_eq!(service.config.len(), 26);
assert_eq!(
service.config[21],
Config {
name: "batch_max_operations".to_string(),
value: ConfigType::Usize,
optional: true,
deprecated: Some(
"Please use `delete_max_size` instead of `batch_max_operations`".into()
),
comments: "".to_string(),
},
);
assert_eq!(
service.config[25],
Config {
name: "disable_write_with_if_match".to_string(),
value: ConfigType::Bool,
optional: false,
optional: true,
deprecated: None,
comments: "".to_string(),
},
);
Expand Down

0 comments on commit d669f61

Please sign in to comment.