Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support env var expansion in path attributes #489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions graphql_query_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2018"
proc-macro = true

[dependencies]
shellexpand = "^3.1"
syn = { version = "^1.0", features = ["extra-traits"] }
proc-macro2 = { version = "^1.0", features = [] }
graphql_client_codegen = { path = "../graphql_client_codegen/", version = "0.14.0" }
15 changes: 10 additions & 5 deletions graphql_query_derive/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ pub fn ident_exists(ast: &syn::DeriveInput, ident: &str) -> Result<(), syn::Erro
))
}

/// Extract an configuration parameter specified in the `graphql` attribute.
pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, syn::Error> {
/// Extract a literal string configuration parameter specified in the `graphql` attribute.
pub fn extract_attr_literal(ast: &syn::DeriveInput, attr: &str) -> Result<syn::LitStr, syn::Error> {
let attributes = &ast.attrs;
let graphql_path = path_to_match();
let attribute = attributes
.iter()
.find(|attr| attr.path == graphql_path)
.ok_or_else(|| syn::Error::new_spanned(ast, "The graphql attribute is missing"))?;
if let syn::Meta::List(items) = &attribute.parse_meta().expect("Attribute is well formatted") {
for item in items.nested.iter() {
if let syn::Meta::List(items) = attribute.parse_meta().expect("Attribute is well formatted") {
for item in items.nested.into_iter() {
if let syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) = item {
let syn::MetaNameValue { path, lit, .. } = name_value;
if let Some(ident) = path.get_ident() {
if ident == attr {
if let syn::Lit::Str(lit) = lit {
return Ok(lit.value());
return Ok(lit);
}
}
}
Expand All @@ -66,6 +66,11 @@ pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, syn::E
))
}

/// Extract an configuration parameter specified in the `graphql` attribute.
pub fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, syn::Error> {
extract_attr_literal(ast, attr).map(|lit| lit.value())
}

/// Extract a list of configuration parameter values specified in the `graphql` attribute.
pub fn extract_attr_list(ast: &syn::DeriveInput, attr: &str) -> Result<Vec<String>, syn::Error> {
let attributes = &ast.attrs;
Expand Down
12 changes: 10 additions & 2 deletions graphql_query_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ fn build_query_and_schema_path(input: &syn::DeriveInput) -> Result<(PathBuf, Pat
)
})?;

let query_path = attributes::extract_attr(input, "query_path")?;
fn extract_and_expand_attr(input: &syn::DeriveInput, attr: &str) -> Result<String, syn::Error> {
let literal = attributes::extract_attr_literal(input, attr)?;

shellexpand::env(&literal.value())
.map(std::borrow::Cow::into_owned)
.map_err(|err| syn::Error::new_spanned(literal, err))
}

let query_path = extract_and_expand_attr(input, "query_path")?;
let query_path = format!("{}/{}", cargo_manifest_dir, query_path);
let query_path = Path::new(&query_path).to_path_buf();
let schema_path = attributes::extract_attr(input, "schema_path")?;
let schema_path = extract_and_expand_attr(input, "schema_path")?;
let schema_path = Path::new(&cargo_manifest_dir).join(schema_path);
Ok((query_path, schema_path))
}
Expand Down