Abstract the data structure out of the path handling logic.

This commit is contained in:
Jordan Bancino 2022-09-30 12:48:45 -04:00
parent 90166882b0
commit 271cdd8ff0
3 changed files with 18 additions and 7 deletions

View file

@ -34,9 +34,9 @@ ROUTE(RouteMatrix)
HashMap *response = NULL; HashMap *response = NULL;
char *pathPart = MATRIX_PATH_POP(path); char *pathPart = MATRIX_PATH_POP(path);
(void) args; (void) args;
if (!MATRIX_PATH_EQUALS(pathPart, "client") || ArraySize(path) != 1) if (!MATRIX_PATH_EQUALS(pathPart, "client") || MATRIX_PATH_PARTS(path) != 1)
{ {
free(pathPart); free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);
@ -48,11 +48,11 @@ ROUTE(RouteMatrix)
if (MATRIX_PATH_EQUALS(pathPart, "versions")) if (MATRIX_PATH_EQUALS(pathPart, "versions"))
{ {
Array *versions = ArrayCreate(); Array *versions = ArrayCreate();
free(pathPart); free(pathPart);
ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4"))); ArrayAdd(versions, JsonValueString(UtilStringDuplicate("v1.4")));
response = HashMapCreate(); response = HashMapCreate();
HashMapSet(response, "versions", JsonValueArray(versions)); HashMapSet(response, "versions", JsonValueArray(versions));

View file

@ -34,7 +34,7 @@ ROUTE(RouteWellKnown)
HashMap *response = NULL; HashMap *response = NULL;
char *pathPart = MATRIX_PATH_POP(path); char *pathPart = MATRIX_PATH_POP(path);
if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || ArraySize(path) != 1) if (!MATRIX_PATH_EQUALS(pathPart, "matrix") || MATRIX_PATH_PARTS(path) != 1)
{ {
free(pathPart); free(pathPart);
HttpResponseStatus(context, HTTP_NOT_FOUND); HttpResponseStatus(context, HTTP_NOT_FOUND);

View file

@ -30,8 +30,19 @@
#include <TelodendriaConfig.h> #include <TelodendriaConfig.h>
#define MATRIX_PATH_POP(arr) ArrayDelete(arr, 0) /*
* Abstract away the underlying data structure of the path so that
* routes don't have to care what it is.
*
* This will be helpful, for instance, if we decide to switch to a
* queue (which can be easily done with the current implementation if
* we just add a function that computes how many elements are in a
* queue.) An array isn't the most efficient data structure for this
* purpose; a queue would be much better. This allows us to change that
* down the road without having to rewrite all the routes.
*/
#define MATRIX_PATH_POP(path) ArrayDelete(path, 0)
#define MATRIX_PATH_PARTS(path) ArraySize(path)
#define MATRIX_PATH_EQUALS(pathPart, str) ((pathPart != NULL) && (strcmp(pathPart, str) == 0)) #define MATRIX_PATH_EQUALS(pathPart, str) ((pathPart != NULL) && (strcmp(pathPart, str) == 0))
typedef enum MatrixError typedef enum MatrixError