I'm writing an algorithm that will first take a config file of various endpoints and their associated method like the following:
/guest guestEndpoint
/guest/lists listEndpoint
/guest/friends guestFriendsEndpoint
/guest/X/friends guetFriendsEndpoint
/guest/X/friends/X guestFriendsEndpoint
/X/guest guestEndpoint
/X/lists listEndpoint
/options optionsEndpoint
X
here represents a wildcard, so any string would match with this.
The algorithm would take this as input and build a tree with each node representing one token between the /
's. Each leaf would be a valid endpoint.
Then when the user passes in something like guest/abc/friends
it would traverse the tree starting from the root, then look for a guest
node attached to the root, if it exists go to node guest
, if guest
here guest would have a wildcard
node, so if abc
did not match with any of guest
's nodes but there was a wildcard
node present it would go to the wildcard
. Then it would look from wildcard
to see if it had a friends
node, if so go there. Then if friends
is a leaf node it would return the corresponding method.
Does this algorithm makes sense? I'm wondering what the runtime of the lookup would be. I'm thinking it would be O(n) where n is the number of tokens in the param that was passed in.
Here is an image of the graph I would build based on the input above. Each diamond represents an endpoint method.
Thanks for any help!