Permalink
Browse files

account for text node situations

  • Loading branch information...
1 parent d07f433 commit f857d17eb18133e5ef224ae4d6bd1146262fc823 Steve Conover committed May 30, 2011
Showing with 33 additions and 20 deletions.
  1. +29 −17 lib/splunk-socket/search/job.js
  2. +4 −3 spec/search/job_spec.js
@@ -6,8 +6,8 @@ function extend(toExtend, other) {
}
function dictToJsObject(dictElement) {
- var obj = {}
- var children = dictElement.childNodes()
+ var obj = {},
+ children = dictElement.childNodes()
for (var i=0; i<children.length; i++) {
var child = children[i]
if (child.name()=='key') {
@@ -18,14 +18,33 @@ function dictToJsObject(dictElement) {
return obj
}
+function childElements(parent) {
+ var elements = []
+ var children = parent.childNodes()
+ for (var i=0; i<children.length; i++) {
+ if (children[i].name() != 'text') elements.push(children[i])
+ }
+ return elements
+}
+
+function firstElementChild(parent) {
+ var elements = childElements(parent)
+ if (elements.length>=1) {
+ return elements[0]
+ } else {
+ return parent.child(0)
+ }
+}
+
function valueOf(element) {
- if (element.child(0) === null) {
+ var firstChild = firstElementChild(element)
+ if (firstChild === null) {
return null
- } else if (element.child(0).name()=='dict') {
- return dictToJsObject(element.child(0))
- } else if (element.child(0).name()=='list') {
- var items = []
- var list = element.child(0).childNodes()
+ } else if (firstChild.name()=='dict') {
+ return dictToJsObject(firstChild)
+ } else if (firstChild.name()=='list') {
+ var items = [],
+ list = childElements(firstChild)
for (var i=0; i<list.length; i++) items.push(valueOf(list[i]))
return items
} else {
@@ -50,11 +69,7 @@ module.exports = function(apiClient, requestInfo) {
//see http://splunk-base.splunk.com/answers/24551/structured-fields-and-values-in-json-api-results
}
for (var k in requestInfo) {
- if (requestInfo[k].push) {
- searchOptions[k] = requestInfo[k].join(',')
- } else {
- searchOptions[k] = requestInfo[k]
- }
+ searchOptions[k] = requestInfo[k].push ? requestInfo[k].join(',') : requestInfo[k]
}
searchOptions.search = 'search ' + requestInfo.search
apiClient.post(
@@ -73,10 +88,7 @@ module.exports = function(apiClient, requestInfo) {
this.getJobStatus = function(statusCallback) {
apiClient.get(
'/services/search/jobs/' + this._jobId,
- function(responseBodyXml) {
- var status = parseStatus(responseBodyXml)
- statusCallback(status)
- })
+ function(responseBodyXml) { statusCallback(parseStatus(responseBodyXml)) })
}
this.checkWhetherWeHaveAllResults = function(resultsOffset, callback) {
@@ -123,21 +123,22 @@ describe('splunk search job', function(){
})
})
- it('parses sub dicts and lists (integration)', function(){
+ it('parses sub dicts and lists (integration, with text elements)', function(){
var status = Job.parseStatus(
'<foo xmlns:s="http://dev.splunk.com/ns/rest">' +
'<s:dict>' +
'<s:key name="messages">' +
+ ' ' +
'<s:dict>' +
'<s:key name="fatal">' +
'<s:list>' +
'<s:item>Something fatal</s:item>' +
'</s:list>' +
'</s:key>' +
'<s:key name="error">' +
- '<s:list>' +
+ '<s:list> ' +
'<s:item>Some error</s:item>' +
- '</s:list>' +
+ ' </s:list>' +
'</s:key>' +
'</s:dict>' +
'</s:key>' +

0 comments on commit f857d17

Please sign in to comment.