Skip to content

Commit 2d10c3e

Browse files
authored
Merge pull request #52 from gwicke/meta_nodes
Add meta namespace for annotations; v0.5.5
2 parents eb79f70 + f6f3ca3 commit 2d10c3e

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

lib/node.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,29 @@ class Node {
4444
this._parent = null;
4545
}
4646

47+
/**
48+
* Register a child with a node.
49+
*
50+
* @param {string|object} key: One of
51+
* - A string representing a fixed path segment.
52+
* - A meta key of the format { type: 'meta', name: 'someName' }. This
53+
* is used to attach annotations in the tree that won't interfere with
54+
* normal routing lookups.
55+
* - A named path segment with a fixed value. This will be bound to a
56+
* parameter of the given name. Structure:
57+
* { name: 'someParamName', pattern: 'fixed_path_value' }
58+
* - A raw wildcard path segment matching one or more arbitrary path
59+
* segments: { name: 'someParamName', modifier: '+' }
60+
* - Another wildcard match consuming a single path segment:
61+
* { name: 'someParamName' }
62+
* @param {Node} child, the child node to register.
63+
* @return undefined
64+
*/
4765
setChild(key, child) {
4866
if (key.constructor === String) {
4967
this._children[_keyPrefix + key] = child;
68+
} else if (key.type === 'meta') {
69+
this._children[`meta_${key.name}`] = child;
5070
} else if (key.name && key.pattern
5171
&& key.modifier !== '+'
5272
&& key.pattern.constructor === String) {
@@ -63,6 +83,16 @@ class Node {
6383
}
6484
}
6585

86+
/**
87+
* Look up a child in a node.
88+
*
89+
* @param {string|object} segment, one of
90+
* - A string,
91+
* - Pattern match objects as described in @setChild.
92+
* @param {object} params, an accumulator object used to build up
93+
* parameters encountered during the lookup process.
94+
* @return {null|Node}
95+
*/
6696
getChild(segment, params) {
6797
if (segment.constructor === String) {
6898
// Fast path
@@ -94,6 +124,8 @@ class Node {
94124

95125
// Fall-back cases for internal use during tree construction. These cases
96126
// are never used for actual routing.
127+
} else if (segment.type === 'meta') {
128+
return this._children[`meta_${segment.name}`];
97129
} else if (segment.pattern) {
98130
// Unwrap the pattern
99131
return this.getChild(segment.pattern, params);

lib/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Router {
121121
if (node || prevNode && path[path.length - 1] === '') {
122122
if (path[path.length - 1] === '') {
123123
// Pass in a listing
124-
params._ls = prevNode.keys();
124+
params._ls = prevNode.keys().filter((key) => !/^meta_/.test(key));
125125
}
126126
return {
127127
params,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-router",
3-
"version": "0.5.4",
3+
"version": "0.5.5",
44
"description": "An efficient swagger 2 based router with support for multiple APIs. For use in RESTBase.",
55
"main": "index.js",
66
"scripts": {

test/features/node.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
// mocha defines to avoid JSHint breakage
3+
/* global describe, it, before, beforeEach, after, afterEach */
4+
5+
const deepEqual = require('assert').deepEqual;
6+
const Node = require('../../lib/node');
7+
8+
9+
describe('meta', () => {
10+
const n = new Node();
11+
const testKey = { type: 'meta', name: 'apiRoot' };
12+
const testValue = { foo: 'bar' };
13+
n.setChild(testKey, new Node(testValue));
14+
deepEqual(n.getChild(testKey).value, testValue);
15+
deepEqual(n.getChild(''), null);
16+
});
17+

0 commit comments

Comments
 (0)