Re: Passing struct and struct[] into a template
On Wednesday, 22 July 2015 at 05:02:59 UTC, Timon Gehr wrote: template Uks(T){ T query(string q){ T result; static if(is(T==S[],S)){ ulong test=result.length; // append S newResult; result~=newResult; }else{ // assign } return result; } } void main(){ struct U_Struct{} auto res1=Uks!(U_Struct).query("email"); auto res2=Uks!(U_Struct[]).query("email"); } Oh that's phenomenal. Thanks! For reference, the is() function can accept template parameters. More here, under isExpression http://dlang.org/expression.html
Passing struct and struct[] into a template
Hi, I have a template function, and I want it to do something if the input variable is a list of structs, and something else if the input is a struct. 1) What's the best way to test this? I suppose I can call __traits(identifier, results) and look at the last two characters. 2) If `T` is a list of structs, how can I initialize a new struct so that I can append to `T`? This is sort of how it might be done in python. I haven't implemented question 2 yet since I'm not sure how atm: template Uks(T) { T query(string q) { T result; try { ulong test = result.length; // won't compile for non-list structs // append to a struct list T.subtype newResult; // is there a function like this? result ~= newResult; } catch { // assign the struct members directly } return result; } } I would like to call it like either of the following, depending on if I want 1 result or X results: auto res = Uks!(U_Struct).query("email"); auto res = Uks!(U_Struct[]).query("email"); I'd be happy to hear better design methods. Thanks
Re: oauth, Twitter, and std.net.curl
You two are phenomenal! Thanks!
oauth, Twitter, and std.net.curl
Hello, I've picked up a web design project, and I would love to use vibe.d/dlang. I need to use oauth to search on a number of web api's, such as searching for Twitter tweets. I tried using Adam's oauth (Thanks!). However, it doesn't appear support the validation needed for searching Twitter. https://github.com/adamdruppe/arsd/blob/master/oauth.d Now I'm looking at std.net.curl, and this is my thought: is HTTP.addRequestHeader(key, val) able to set the oauth authentication headers? It would look a bit odd, but if something like this works, then it wouldn't be difficult to code for each set of headers specifically: auto client = HTTP("https://api.twitter.com";); // not sure if https should be set here or somewhere else string bearerUrl = "https://api.twitter.com/oauth2/token";; client.addRequestHeader("Authorization", "Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw"); client.postData("grant_type=client_credentials"); client.perform(); (The above example is based on Step 2 of this page) https://dev.twitter.com/oauth/application-only Of course, the oauth headers look a bit different, but I imagine it being something like this: client.addRequestHeader("Authorization", "OAuth2 token: \"string\", tokensecret: \"string\", appkey, \"string\" ..."); \\ not sure if that's how to escape quotes in dlang Can I get some guidance on this? I would really like to use dlang, but I can't afford to be stuck on this for too long. I'd be happy to try and write a modification of Adam's code, but I lack experience, so it's a slow process for me. I'd also be happy to write up little tutorials/blogposts about how I accomplish various tasks. Thanks,