Unused variable portion in a route? - playframework-2.1

I'm using Play! Framework 2.1 (Java version). I have a route that will look something like this:
http://www.mydomain.com/games/52/super-mario-bros
The "super-mario-bros" part is arbitrary and only added there for SEO purposes. Only the 52 is needed which corresponds to the ID to get for that game.
The issue is that I can't seem to figure out a way to have an arbitrary value in a route. I currently have, in my conf/routes file:
GET /games/:id/:title controllers.Games.viewGame(id: Long, title: String)
This works but now I have to add a second parameter into my "viewGame" method inside my Games controller, which never gets used. I would much rather have a route that looks something like this:
GET /games/:id/:arbitrary controllers.Games.viewGame(id: Long)
Is there any way to do this?

Related

How to prevent empty list errors in in clause in sql?

One common problem we have in our codebase is that people forget to check if a list is empty before using it in an in clause.
For example (in Scala with Anorm):
def exists(element: String, list: List[String]): Boolean =
SQL("select {element} in {list} as result")
.on('element -> element, 'list -> list)
.as(SqlParser.bool("result").single)
This code works perfectly well as long as list has at least one element.
If it has 0 elements, you get a syntax error, which is weird if you're used to other programming languages that would allow this empty list case.
So, my question is: what's the best way to prevent this error from happening?
Initially, we did this:
def exists(element: String, list: List[String]): Boolean =
if (list.nonEmpty) {
SQL("select {element} in {list} as result")
.on('element -> element, 'list -> list)
.as(SqlParser.bool("result").single)
} else {
false
}
This works perfectly well, and has the added advantage that it doesn't hit the database at all.
Unfortunately, we don't remember to do this every time, and it seems that 1-2 times a month we're fixing an issue related to this.
An alternate solution we came up with was to use a NonEmptyList class instead of a standard List. This class must have at least one element. This works excellent, but again, people have not been diligent with always using this class.
So I'm wondering if there's an approach I'm missing that prevent this type of error better?
It looks like you've already found a way to resolve this problem - you have an exists() function which handles an empty list cleanly. The problem is that people are writing their own exists() functions which don't do that.
You need to make sure that your function is accessible as a utility function, so that you can reuse it whenever you need to, rather than having to rewrite the function.
Your problem is an encapsulation problem: the Anorm API is like an open flame and people can burn themselves. If you rely just on people to take precautions, someone will get burnt.
The solution is to restrict the access to the Anorm API to a limited module/package/area of your code:
Anorm API will be private and accessible only from very few places, where it is going to be easy to perform the necessary controls. This part of the code will expose an API
Every other part of the code will need to go through that API, effectively using Anorm in the "safe" way

How to apply more than one function to a passed in live template variable?

I'm trying to build a Python Unit Test File Template in PyCharm. The overall result I want to achieve is:
A user creates a new file with my template, say "widget_builder.py"
Inside the template I want to create the class name by taking the file name "widget_builder" and turning it into "WidgetBuilderTests"
It looks like I need to use a Live Template to manipulate the file template variable $FILE_NAME$?
How can I create a Live Template that given a passed in variable (in this case $FILE_NAME$), applies both the underscoresToCamelCase and capitalize functions to it?
If I declare the Template text as:
$CLASS_NAME$
...and then edit variables, how can I reference a passed in variable of '$FILE_NAME$'?
I'd imagine it to look something like this, but I just can't get it to work:
I'm sure there must be a way to do this, but I just can't quite wrap my head round it.
Is this possible? Thanks!
EDIT
I've got a bit further. If I define the template as this:
If I then use it, this happens:
So the end result of $CLASS_NAME$ (WidgetBuilder) on the left is what I want, but I don't want $FILE_NAME$ (widget_builder) to be there once I hit return.
So your problem here is that $FILE_NAME$ is not a native variable in the live templates, merely an arbitrary name. What you actually want to be using is another function: fileNameWithoutExtension().
So your template would look something like:

Runtime method to get names of argument variables?

Inside an Objective-C method, it is possible to get the selector of the method with the keyword _cmd. Does such a thing exist for the names of arguments?
For example, if I have a method declared as such:
- (void)methodWithAnArgument:(id)foo {
...
}
Is there some sort of construct that would allow me to get access to some sort of string-like representation of the variable name? That is, not the value of foo, but something that actually reflects the variable name "foo" in a local variable inside the method.
This information doesn't appear to be stored in NSInvocation or any of its related classes (NSMethodSignature, etc), so I'm not optimistic this can be done using Apple's frameworks or the runtime. I suspect it might be possible with some sort of compile-time macro, but I'm unfamiliar with C macros so I wouldn't know where to begin.
Edit to contain more information about what I'm actually trying to do.
I'm building a tool to help make working with third-party URL schemes easier. There are two sides to how I want my API to look:
As a consumer of a URL scheme, I can call a method like [twitterHandler showUserWithScreenName:#"someTwitterHandle"];
As a creator of an app with a URL scheme, I can define my URLs in a plist dictionary, whose key-value pairs look something like #"showUserWithScreenName": #"twitter://user?screenName={screenName}".
What I'm working on now is finding the best way to glue these together. The current fully-functioning implementation of showUserWithScreenName: looks something like this:
- (void)showUserWithScreenName:(NSString *)screenName {
[self performCommand:NSStringFromSelector(_cmd) withArguments:#{#"screenName": screenName}];
}
Where performCommand:withArguments: is a method that (besides some other logic) looks up the command key in the plist (in this case "showUserWithScreenName:") and evaluates the value as a template using the passed dictionary as the values to bind.
The problem I'm trying to solve: there are dozens of methods like this that look exactly the same, but just swap out the dictionary definition to contain the correct template params. In every case, the desired dictionary key is the name of the parameter. I'm trying to find a way to minimize my boilerplate.
In practice, I assume I'm going to accept that there will be some boilerplate needed, but I can probably make it ever-so-slightly cleaner thanks to NSDictionaryOfVariableBindings (thanks #CodaFi — I wasn't familiar with that macro!). For the sake of argument, I'm curious if it would be possible to completely metaprogram this using something like forwardInvocation:, which as far as I can tell would require some way to access parameter names.
You can use componentsSeparatedByString: with a : after you get the string from NSStringFromSelector(_cmd) and use your #selector's argument names to put the arguments in the correct order.
You can also take a look at this post, which is describing the method naming conventions in Objective C

Proper error propagation in clojure

I'm currently working on my first major project in clojure and have run into a question regarding coding style and the most "clojure-esque" way of doing something. Basically I have a function I'm writing which takes in a data structure and a template that the function will try to massage the data structure into. The template structure will look something like this:
{
:key1 (:string (:opt :param))
:key2 (:int (:opt :param))
:key3 (:obj (:tpl :template-structure))
:key4 (:list (:tpl :template-structure))
}
Each key is an atom that will be searched for in the given data structure, and it's value will be attempted to be matched to the type given in the template structure. So it would look for :key1 and check that it's a string, for instance. The return value would be a map that has :key1 pointing to the value from the given data structure (the function could potentially change the value depending on the options given).
In the case of :obj it takes in another template structure, and recursively calls itself on that value and the template structure, and places the result from that in the return. However, if there's an error I want that error returned directly.
Similarly for lists I want it to basically do a map of the function again, except in the case of an error which I want returned directly.
My question is what is the best way to handle these errors? Some simple exception handling would be the easiest way, but I feel that it's not the most functional way. I could try and babysit the errors all the way up the chain with tons of if statements, but that also doesn't seem very sporting. Is there something simple I'm missing or is this just an ugly problem?
You might be interested in schematic, which does pretty similar stuff. You can see how it's used in the tests, and the implementation.
Basically I defined an error function, which returns nil for correctly-formatted data, or a string describing the error. Doing it with exceptions instead would make the plumbing easier, but would make it harder to get the detailed error messages like "[person: [first_name: expected string, got integer]]".

Find references to string/symbol/method

This relates to the Dolphin variant of Smalltalk.
I'm digging around in the image to try and figure this out but haven't
find the correct method invocation yet and I'm hoping someone might be
able to help shortcut this process. What I'm trying to do is to find
all methods (either within the entire system or, preferably, just
within a single class) which refer to a given string, symbol, or
method. I've found the #references family of methods in
SmalltalkSystem but have not had luck figuring out how to get them to
give back something resembling what I want.
The programmatic way, here we go
SmalltalkSystem current browseContainingText: 'Dolphin'.
I don't have Dolphin at hand, but the following code should work in all Smalltalk with the refactoring engine (this includes Dolphin):
result := BrowserEnvironment new matches: 'Dolphin'.
Then you can iterate over the results like this:
result classesAndSelectorsDo: [ :class :selector | ... ].