1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
use crate::util::{ to_camel_case, AttributeArguments, MacroError, QuotableBorrowedStr, QuotableDirection, QuotableOption, TryFrom, }; use azure_functions_shared::codegen; use quote::{quote, ToTokens}; use std::borrow::Cow; use syn::spanned::Spanned; use syn::Lit; pub struct Table<'a>(pub Cow<'a, codegen::bindings::Table>); impl TryFrom<AttributeArguments> for Table<'_> { type Error = MacroError; fn try_from(args: AttributeArguments) -> Result<Self, Self::Error> { let mut name = None; let mut table_name = None; let mut partition_key = None; let mut row_key = None; let mut filter = None; let mut take = None; let mut connection = None; for (key, value) in args.list.iter() { let key_str = key.to_string(); match key_str.as_str() { "name" => match value { Lit::Str(s) => { name = Some(Cow::Owned(to_camel_case(&s.value()))); } _ => { return Err(( value.span(), "expected a literal string value for the 'name' argument", ) .into()); } }, "table_name" => match value { Lit::Str(s) => { table_name = Some(Cow::Owned(s.value())); } _ => { return Err(( value.span(), "expected a literal string value for the 'table_name' argument", ) .into()); } }, "partition_key" => match value { Lit::Str(s) => { partition_key = Some(Cow::Owned(s.value())); } _ => { return Err(( value.span(), "expected a literal string value for the 'partition_key' argument", ) .into()); } }, "row_key" => match value { Lit::Str(s) => { row_key = Some(Cow::Owned(s.value())); } _ => { return Err(( value.span(), "expected a literal string value for the 'row_key' argument", ) .into()); } }, "filter" => match value { Lit::Str(s) => { filter = Some(Cow::Owned(s.value())); } _ => { return Err(( value.span(), "expected a literal string value for the 'filter' argument", ) .into()); } }, "take" => match value { Lit::Int(i) => { take = Some(i.value()); } _ => { return Err(( value.span(), "expected a literal integer value for the 'take' argument", ) .into()); } }, "connection" => match value { Lit::Str(s) => { connection = Some(Cow::Owned(s.value())); } _ => { return Err(( value.span(), "expected a literal string value for the 'connection' argument", ) .into()); } }, _ => { return Err(( key.span(), format!("unsupported binding attribute argument '{}'", key_str).as_ref(), ) .into()); } }; } if table_name.is_none() { return Err(( args.span, "the 'table_name' argument is required for table bindings.", ) .into()); } Ok(Table(Cow::Owned(codegen::bindings::Table { name: name.expect("expected a name for a table binding"), table_name: table_name.expect("expected a table name for a table binding"), partition_key, row_key, filter, take, connection, direction: codegen::Direction::In, }))) } } impl ToTokens for Table<'_> { fn to_tokens(&self, tokens: &mut ::proc_macro2::TokenStream) { let name = QuotableBorrowedStr(&self.0.name); let table_name = QuotableBorrowedStr(&self.0.table_name); let partition_key = QuotableOption( self.0 .partition_key .as_ref() .map(|x| QuotableBorrowedStr(x)), ); let row_key = QuotableOption(self.0.row_key.as_ref().map(|x| QuotableBorrowedStr(x))); let filter = QuotableOption(self.0.filter.as_ref().map(|x| QuotableBorrowedStr(x))); let take = QuotableOption(self.0.take.as_ref()); let connection = QuotableOption(self.0.connection.as_ref().map(|x| QuotableBorrowedStr(x))); let direction = QuotableDirection(self.0.direction.clone()); quote!(::azure_functions::codegen::bindings::Table { name: #name, table_name: #table_name, partition_key: #partition_key, row_key: #row_key, filter: #filter, take: #take, connection: #connection, direction: #direction, }) .to_tokens(tokens) } }