Permalink
Browse files

[HAL] Select links by secondary key like name, title, etc.

  • Loading branch information...
basti1302 committed Dec 5, 2013
1 parent 9eaa21b commit ae4aec4d74fcf32a4c0ea8f0000d72c85705b6ee
@@ -1823,11 +1823,12 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
log.debug('parsing hal')
var halResource = halfred.parse(doc)
+ var secondaryKey = parseSecondaryKey(key)
var index = parseIndex(key)
key = parseKey(key)
// try _links first
- var step = findLink(halResource, key, index)
+ var step = findLink(halResource, key, secondaryKey, index)
if (step) {
return step
}
@@ -1841,20 +1842,76 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
' in document:\n' + JSON.stringify(doc))
}
-function findLink(halResource, key, index) {
+function parseSecondaryKey(key) {
+ var match = key.match(/.*\[(.*):(.*)\]/)
+ if (match) {
+ return [match[1], match[2]]
+ }
+ return null
+}
+
+function parseIndex(key) {
+ var match = key.match(/.*\[(\d+)\]/)
+ if (match) {
+ return match[1]
+ }
+ return null
+}
+
+function parseKey(key) {
+ var match = key.match(/(.*)\[.*\]/)
+ if (match) {
+ return match[1]
+ }
+ return key
+}
+
+function findLink(halResource, key, secondaryKey, index) {
var linkArray = halResource.linkArray(key)
if (!linkArray || linkArray.length === 0) {
return null
}
- var step = findLinkByIndex(linkArray, key, index)
+ var step = findLinkBySecondaryKey(linkArray, key, secondaryKey)
+ if (!step) {
+ step = findLinkByIndex(linkArray, key, index)
+ }
if (!step) {
step = findLinkWithoutIndex(linkArray, key)
}
return step
}
+function findLinkBySecondaryKey(linkArray, key, secondaryKeyArray) {
+ if (secondaryKeyArray &&
+ secondaryKeyArray[0] != null &&
+ secondaryKeyArray[1] != null) {
+
+ // client selected a specific link by an explicit secondary key like 'name',
+ // so use it or fail
+ var secondaryKey = secondaryKeyArray[0]
+ var secondaryValue = secondaryKeyArray[1]
+ var i = 0
+ for (; i < linkArray.length; i++) {
+ var val = linkArray[i][secondaryKey]
+ /* jshint -W116 */
+ if (val != null && val == secondaryValue) {
+ if (!linkArray[i].href) {
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but this link had no href attribute.')
+ }
+ log.debug('found hal link: ' + linkArray[i].href)
+ return { uri: linkArray[i].href }
+ }
+ /* jshint +W116 */
+ }
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but there is no such link.')
+ }
+ return null
+}
+
function findLinkByIndex(linkArray, key, index) {
if (index) {
// client specified an explicit array index for this link, so use it or fail
@@ -1934,22 +1991,6 @@ function findEmbeddedWithoutIndex(resourceArray, key) {
return { doc: resourceArray[0].original() }
}
-function parseIndex(key) {
- var match = key.match(/.*\[(\d+)\]/)
- if (match) {
- return match[1]
- }
- return null
-}
-
-function parseKey(key) {
- var match = key.match(/(.*)\[\d+\]/)
- if (match) {
- return match[1]
- }
- return key
-}
-
JsonHalWalker.prototype.postProcessStep = function(nextStep) {
if (nextStep.uri) {
if (_s.endsWith(this.startUri, '/') &&

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1823,11 +1823,12 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
log.debug('parsing hal')
var halResource = halfred.parse(doc)
+ var secondaryKey = parseSecondaryKey(key)
var index = parseIndex(key)
key = parseKey(key)
// try _links first
- var step = findLink(halResource, key, index)
+ var step = findLink(halResource, key, secondaryKey, index)
if (step) {
return step
}
@@ -1841,20 +1842,76 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
' in document:\n' + JSON.stringify(doc))
}
-function findLink(halResource, key, index) {
+function parseSecondaryKey(key) {
+ var match = key.match(/.*\[(.*):(.*)\]/)
+ if (match) {
+ return [match[1], match[2]]
+ }
+ return null
+}
+
+function parseIndex(key) {
+ var match = key.match(/.*\[(\d+)\]/)
+ if (match) {
+ return match[1]
+ }
+ return null
+}
+
+function parseKey(key) {
+ var match = key.match(/(.*)\[.*\]/)
+ if (match) {
+ return match[1]
+ }
+ return key
+}
+
+function findLink(halResource, key, secondaryKey, index) {
var linkArray = halResource.linkArray(key)
if (!linkArray || linkArray.length === 0) {
return null
}
- var step = findLinkByIndex(linkArray, key, index)
+ var step = findLinkBySecondaryKey(linkArray, key, secondaryKey)
+ if (!step) {
+ step = findLinkByIndex(linkArray, key, index)
+ }
if (!step) {
step = findLinkWithoutIndex(linkArray, key)
}
return step
}
+function findLinkBySecondaryKey(linkArray, key, secondaryKeyArray) {
+ if (secondaryKeyArray &&
+ secondaryKeyArray[0] != null &&
+ secondaryKeyArray[1] != null) {
+
+ // client selected a specific link by an explicit secondary key like 'name',
+ // so use it or fail
+ var secondaryKey = secondaryKeyArray[0]
+ var secondaryValue = secondaryKeyArray[1]
+ var i = 0
+ for (; i < linkArray.length; i++) {
+ var val = linkArray[i][secondaryKey]
+ /* jshint -W116 */
+ if (val != null && val == secondaryValue) {
+ if (!linkArray[i].href) {
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but this link had no href attribute.')
+ }
+ log.debug('found hal link: ' + linkArray[i].href)
+ return { uri: linkArray[i].href }
+ }
+ /* jshint +W116 */
+ }
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but there is no such link.')
+ }
+ return null
+}
+
function findLinkByIndex(linkArray, key, index) {
if (index) {
// client specified an explicit array index for this link, so use it or fail
@@ -1934,22 +1991,6 @@ function findEmbeddedWithoutIndex(resourceArray, key) {
return { doc: resourceArray[0].original() }
}
-function parseIndex(key) {
- var match = key.match(/.*\[(\d+)\]/)
- if (match) {
- return match[1]
- }
- return null
-}
-
-function parseKey(key) {
- var match = key.match(/(.*)\[\d+\]/)
- if (match) {
- return match[1]
- }
- return key
-}
-
JsonHalWalker.prototype.postProcessStep = function(nextStep) {
if (nextStep.uri) {
if (_s.endsWith(this.startUri, '/') &&

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -15,11 +15,12 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
log.debug('parsing hal')
var halResource = halfred.parse(doc)
+ var secondaryKey = parseSecondaryKey(key)
var index = parseIndex(key)
key = parseKey(key)
// try _links first
- var step = findLink(halResource, key, index)
+ var step = findLink(halResource, key, secondaryKey, index)
if (step) {
return step
}
@@ -33,20 +34,76 @@ JsonHalWalker.prototype.findNextStep = function(doc, key) {
' in document:\n' + JSON.stringify(doc))
}
-function findLink(halResource, key, index) {
+function parseSecondaryKey(key) {
+ var match = key.match(/.*\[(.*):(.*)\]/)
+ if (match) {
+ return [match[1], match[2]]
+ }
+ return null
+}
+
+function parseIndex(key) {
+ var match = key.match(/.*\[(\d+)\]/)
+ if (match) {
+ return match[1]
+ }
+ return null
+}
+
+function parseKey(key) {
+ var match = key.match(/(.*)\[.*\]/)
+ if (match) {
+ return match[1]
+ }
+ return key
+}
+
+function findLink(halResource, key, secondaryKey, index) {
var linkArray = halResource.linkArray(key)
if (!linkArray || linkArray.length === 0) {
return null
}
- var step = findLinkByIndex(linkArray, key, index)
+ var step = findLinkBySecondaryKey(linkArray, key, secondaryKey)
+ if (!step) {
+ step = findLinkByIndex(linkArray, key, index)
+ }
if (!step) {
step = findLinkWithoutIndex(linkArray, key)
}
return step
}
+function findLinkBySecondaryKey(linkArray, key, secondaryKeyArray) {
+ if (secondaryKeyArray &&
+ secondaryKeyArray[0] != null &&
+ secondaryKeyArray[1] != null) {
+
+ // client selected a specific link by an explicit secondary key like 'name',
+ // so use it or fail
+ var secondaryKey = secondaryKeyArray[0]
+ var secondaryValue = secondaryKeyArray[1]
+ var i = 0
+ for (; i < linkArray.length; i++) {
+ var val = linkArray[i][secondaryKey]
+ /* jshint -W116 */
+ if (val != null && val == secondaryValue) {
+ if (!linkArray[i].href) {
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but this link had no href attribute.')
+ }
+ log.debug('found hal link: ' + linkArray[i].href)
+ return { uri: linkArray[i].href }
+ }
+ /* jshint +W116 */
+ }
+ throw new Error(key + '[' + secondaryKey + ':' + secondaryValue +
+ '] requested, but there is no such link.')
+ }
+ return null
+}
+
function findLinkByIndex(linkArray, key, index) {
if (index) {
// client specified an explicit array index for this link, so use it or fail
@@ -126,22 +183,6 @@ function findEmbeddedWithoutIndex(resourceArray, key) {
return { doc: resourceArray[0].original() }
}
-function parseIndex(key) {
- var match = key.match(/.*\[(\d+)\]/)
- if (match) {
- return match[1]
- }
- return null
-}
-
-function parseKey(key) {
- var match = key.match(/(.*)\[\d+\]/)
- if (match) {
- return match[1]
- }
- return key
-}
-
JsonHalWalker.prototype.postProcessStep = function(nextStep) {
if (nextStep.uri) {
if (_s.endsWith(this.startUri, '/') &&
View
@@ -53,7 +53,10 @@ module.exports = {
'title': 'Fred'
}, {
'href': '/admins/5',
- 'title': 'Kate'
+ 'title': 'Kate',
+ 'name': 'boss'
+ }, {
+ 'name': 'no-href'
}]
},
'currentlyProcessing': 14,
Oops, something went wrong.

0 comments on commit ae4aec4

Please sign in to comment.