forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
192 additions
and
4 deletions.
There are no files selected for viewing
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
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,42 @@ | ||
extern crate proc_macro; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, AttributeArgs, ItemFn, Lit, Meta}; | ||
#[proc_macro_attribute] | ||
pub fn generate_fn_with_params(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
// Parse the input tokens into a syntax tree | ||
let args = parse_macro_input!(attr as AttributeArgs); | ||
let input_fn = parse_macro_input!(item as ItemFn); | ||
|
||
// Extract parameters from the attribute | ||
let mut param1 = String::new(); | ||
let mut param2 = String::new(); | ||
|
||
if let Some(syn::NestedMeta::Meta(Meta::NameValue(meta))) = args.get(0) { | ||
if let Lit::Str(lit_str) = &meta.lit { | ||
param1 = lit_str.value(); | ||
} | ||
} | ||
|
||
if let Some(syn::NestedMeta::Meta(Meta::NameValue(meta))) = args.get(1) { | ||
if let Lit::Str(lit_str) = &meta.lit { | ||
param2 = lit_str.value(); | ||
} | ||
} | ||
|
||
// Get the original function's name | ||
let fn_name = &input_fn.sig.ident; | ||
|
||
// Generate the new function that prints the two parameters | ||
let expanded = quote! { | ||
#input_fn | ||
|
||
pub fn #fn_name() { | ||
println!("Param1: {}", #param1); | ||
println!("Param2: {}", #param2); | ||
} | ||
}; | ||
|
||
// Return the generated code as a TokenStream | ||
TokenStream::from(expanded) | ||
} |
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
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
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,43 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF 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. | ||
|
||
[package] | ||
name = "datafusion-macros" | ||
description = "Proc macros for DataFusion query engine" | ||
keywords = ["datafusion", "query", "sql"] | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = { workspace = true } | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
name = "datafusion_macros" | ||
path = "src/lib.rs" | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1.0.37" | ||
syn = "2.0.79" | ||
proc-macro2 = "1.0" | ||
datafusion-pre-macros = { workspace = true } | ||
|
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,47 @@ | ||
extern crate proc_macro; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, DeriveInput, Lit, LitStr, Meta, MetaNameValue}; | ||
use datafusion_pre_macros::DocumentationTest; | ||
|
||
#[proc_macro_attribute] | ||
pub fn udf_doc(args: TokenStream, input: TokenStream) -> TokenStream { | ||
let mut description: Option<LitStr> = None; | ||
let mut example: Option<LitStr> = None; | ||
|
||
let parser = syn::meta::parser(|meta| { | ||
if meta.path.is_ident("description") { | ||
description = Some(meta.value()?.parse()?); | ||
Ok(()) | ||
} else if meta.path.is_ident("example") { | ||
example = Some(meta.value()?.parse()?); | ||
Ok(()) | ||
} else { | ||
Err(meta.error("unsupported property")) | ||
} | ||
}); | ||
parse_macro_input!(args with parser); | ||
eprintln!("description={description:?} example={example:?}"); | ||
|
||
// Parse the input struct | ||
let input = parse_macro_input!(input as DeriveInput); | ||
let name = input.clone().ident; | ||
|
||
//eprintln!("input={input:?}"); | ||
|
||
|
||
let expanded = quote! { | ||
#input | ||
|
||
use datafusion_pre_macros::DocumentationTest; | ||
|
||
impl #name { | ||
fn documentation_test(&self) -> Option<DocumentationTest> { | ||
Some(DocumentationTest { description: #description.to_string(), syntax_example: #example.to_string() }) | ||
} | ||
} | ||
}; | ||
|
||
// Return the generated code | ||
TokenStream::from(expanded) | ||
} |
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,35 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF 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. | ||
|
||
[package] | ||
name = "datafusion-pre-macros" | ||
description = "Proc pre macros for DataFusion query engine" | ||
keywords = ["datafusion", "query", "sql"] | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
authors = { workspace = true } | ||
rust-version = { workspace = true } | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
name = "datafusion_pre_macros" | ||
path = "src/lib.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,7 @@ | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct DocumentationTest { | ||
/// the description for the UDF | ||
pub description: String, | ||
/// a brief example of the syntax. For example "ascii(str)" | ||
pub syntax_example: String, | ||
} |