mercredi 17 août 2016

how to deal with duplicated named parameters /:uuid/:uuid on an HTTP router

When creating an HTTP router, is common to see this pattern:

/get/:user         --> /get/username
/get/:book/:page/  --> /get/book/3

The handlers for the request can call a method the one will return the matching name, for example:

get(":user") and that return "username"
get(":book") and that return "book"
..

In most cases a key/value map is used for storing this pairs, but what could be a best practice to deal with named parameters that have different values but share the same name, for example a hash or an UUID, example:

/get/:uuid/:uuid/  --> get/36146E6-D7EB-421C-9F7D-5F676A481A00/E122DA77-171E-4FAB-91CB-C724BF25B772

One "practice" I have found is to save the key and an array with the values in the order of appearance, something like:

:uuid = [uuid0, uuid1]

that way, the getting of the params can be consistent:

get(":uuid")  return array of UUIDS instead of only the first uuid

Probably a best practice could be to avoid using duplicated params names, and that could solve all this, but there are cases when the same regex/method is tight to the same named parameter and is desired to avoid duplication code just with a different name, I mean something like

:uuid1 = uuidgen()
:uuid2 = uuidgen()

So would like to know what could be a best approach for dealing with this cases.

Aucun commentaire:

Enregistrer un commentaire