This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathneo4j.go
811 lines (801 loc) · 25.8 KB
/
neo4j.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/**
Neo4j REST client library written in GO
I took two methods from the Golang HTML package: EscapeString & escape. I appreciate it! Both of these are Copyright 2010 The Go Authors. All rights reserved.
Copyright (c) 2011, dave meehan
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
**/
package neo4j
import (
"net/http"
"log"
"errors"
"encoding/json"
"strings"
"bytes"
"strconv"
)
// general neo4j config
type Neo4j struct {
Method string // which http method
StatusCode int // last http status code received
URL string
Username string
Password string
}
type Error struct {
List map[int]error
Code int
}
// used when storing data returned from neo4j
type NeoTemplate struct {
ID uint64
Relationships string
RelationshipsOut string
RelationshipsIn string
RelationshipsAll string
RelationshipsCreate string
Data map[string]interface{}
Traverse string
Property string
Properties string
Self string
Extensions map[string]interface{}
Start string // relationships & traverse // returns both obj & string
End string // relationships & traverse // returns both obj & string
Type string // relationships & traverse
Indexed string // index related
Length string // traverse framework
Nodes []interface{} // traverse framework
TRelationships []interface{} // traverse framework
}
// what chars to escape of course
const escapedChars = `&'<>"*[]:% `
func NewNeo4j(u string, user string, passwd string) (*Neo4j, error) {
n := new(Neo4j)
if len(u) < 1 {
u = "http://127.0.0.1:7474/db/data"
}
if len(user) > 0 {
n.Username = user
}
if len(passwd) > 0 {
n.Password = passwd
}
n.URL = u
_, err := n.send(u, "") // just a test to see if the connection is valid
return n, err
}
/*
GetProperty(node id uint, name string) returns string of property value and any error raised as error
*/
func (this *Neo4j) GetProperty(id uint64, name string) (string, error) {
if len(name) < 1 {
return "", errors.New("Property name must be at least 1 character.")
}
node, err := this.GetNode(id) // find properties for node
if err != nil {
return "", err
}
this.Method = "get"
body, err := this.send(node.Properties+"/"+name, "")
if err != nil {
return "", err
}
errorList := map[int]error{
404: errors.New("Node or Property not found."),
204: errors.New("No properties found."),
}
return body, this.NewError(errorList)
}
/*
GetProperties(node id uint) returns a NeoTemplate struct and any errors raised as error
*/
func (this *Neo4j) GetProperties(id uint64) (tmp *NeoTemplate, err error) {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return tmp, err
}
this.Method = "get"
body, err := this.send(node.Properties, "")
if err != nil {
return tmp, err
}
// pack json string into variable "data" so the json unmarshaler knows where to put it on struct type NeoTemplate
jsonData, err := this.pack("data", body)
if err != nil {
return tmp, err
}
//convert json -> string and unmarshal -> NeoTemplate
template, err := this.unmarshal(string(jsonData))
if err != nil {
return tmp, err
}
errorList := map[int]error{
404: errors.New("Node or Property not found."),
204: errors.New("No properties found."),
}
return template[0], this.NewError(errorList)
}
/*
SetProperty(node id uint, data map[string]string, replace bool) returns any error raised as error
typically replace should be false unless you wish to drop any other properties *not* specified in the data you sent to SetProperty
*/
func (this *Neo4j) SetProperty(id uint64, data map[string]string, replace bool) error {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return err
}
this.Method = "put"
s, err := json.Marshal(data)
if err != nil {
return err
}
if replace { // drop all properties on the node if they aren't specified in "data" ?
_, err := this.send(node.Properties, string(s))
if err != nil {
return err
}
} else {
for k, v := range data {
k = strings.TrimSpace(k) // strip leading & trailing whitespace from key
_, err := this.send(node.Properties+"/"+k, strconv.Quote(v)) // wrap value in double quotes as neo4j expects
if err != nil {
return err
}
}
}
if err != nil {
return err
}
errorList := map[int]error{
404: errors.New("Node not found."),
400: errors.New("Invalid data sent."),
}
return this.NewError(errorList)
}
/*
CreateProperty(node id uint, data map[string]string, replace bool) returns any errors raised as error
typically replace should be false unless you wish to drop any other properties *not* specified in the data you sent to CreateProperty
*/
func (this *Neo4j) CreateProperty(id uint64, data map[string]string, replace bool) error {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return err
}
s, err := json.Marshal(data)
if err != nil {
return err
}
this.Method = "put"
if replace { // when replacing and dropping *ALL* values on node(not just new ones) we can simply pass in the entire json data set and neo4j will remove the old properties
_, err := this.send(node.Properties, string(s))
if err != nil {
return err
}
} else { // if we are keeping the other properties on the node we must pass in new properties 1 at a time
for k, v := range data {
k = strings.TrimSpace(k) // strip leading & trailing whitespace from key
_, err := this.send(node.Properties+"/"+k, strconv.Quote(v)) // wrap value in double quotes as neo4j expects
if err != nil {
return err
}
}
}
errorList := map[int]error{
404: errors.New("Node or Property not found."),
400: errors.New("Invalid data sent."),
}
return this.NewError(errorList)
}
/*
DelProperty(node id uint, s string) returns any errors raised as error
pass in the id of the node and string as the the name/key of the property to delete
could be extended to also delete relationship properties as well
*/
func (this *Neo4j) DelProperty(id uint64, s string) error {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return err
}
this.Method = "delete"
_, err = this.send(node.Properties+"/"+string(s), "")
if err != nil {
return err
}
errorList := map[int]error{
404: errors.New("Node or Property not found."),
}
return this.NewError(errorList)
}
/*
DelNode(node id uint) returns any errors raised as error
*/
func (this *Neo4j) DelNode(id uint64) error {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return err
}
this.Method = "delete"
_, err = this.send(node.Self, "")
if err != nil {
return err
}
errorList := map[int]error{
404: errors.New("Node not found."),
409: errors.New("Unable to delete node. May still have relationships."),
}
return this.NewError(errorList)
}
/*
CreateNode(data map[string]string) returns a NeoTemplate struct and any errors raised as error
*/
func (this *Neo4j) CreateNode(data map[string]string) (tmp *NeoTemplate, err error) {
s, err := json.Marshal(data)
if err != nil {
return tmp, errors.New("Unable to Marshal Json data")
}
this.Method = "post"
url := this.URL + "/node"
body, err := this.send(url, string(s))
if err != nil {
return tmp, err
}
template, err := this.unmarshal(body) // json.Unmarshal wrapper with some type assertions etc
if err != nil {
return tmp, err
}
errorList := map[int]error{
400: errors.New("Invalid data sent."),
}
return template[0], this.NewError(errorList)
}
/*
GetNode(id uint) returns a NeoTemplate struct and any errors raised as error
*/
func (this *Neo4j) GetNode(id uint64) (tmp *NeoTemplate, err error) {
if id < 1 {
return tmp, errors.New("Invalid node id specified.")
}
this.Method = "get"
url := this.URL + "/node/"
body, err := this.send(url+strconv.FormatUint(uint64(id), 10), "") // convert uint -> string and send http request
if err != nil {
return tmp, err
}
template, err := this.unmarshal(body) // json.Unmarshal wrapper with some type assertions etc
if err != nil {
return tmp, err
}
errorList := map[int]error{
404: errors.New("Node not found."),
}
return template[0], this.NewError(errorList)
}
/*
GetRelationshipsOnNode(node id uint, name string, direction string) returns an array of NeoTemplate structs containing relationship data and any errors raised as error
*/
func (this *Neo4j) GetRelationshipsOnNode(id uint64, name string, direction string) (map[int]*NeoTemplate, error) {
node, err := this.GetNode(id) // find properties for node
if err != nil {
return nil, err
}
this.Method = "get"
direction = strings.ToLower(direction)
url := ""
switch direction {
case "in":
url = node.RelationshipsIn
case "out":
url = node.RelationshipsOut
case "all":
fallthrough
default:
url = node.RelationshipsAll
}
body, err := this.send(url+"/"+name, "")
if err != nil {
return nil, err
}
template, err := this.unmarshal(body)
if err != nil {
return nil, err
}
errorList := map[int]error{
404: errors.New("Node not found."),
}
return template, this.NewError(errorList)
}
/*
SetRelationship(relationship id uint, data map[string]string) returns any errors raised as error
id is the relationship id
*/
func (this *Neo4j) SetRelationship(id uint64, data map[string]string) error {
this.Method = "put"
url := this.URL + "/relationship/"
s, err := json.Marshal(data)
if err != nil {
return errors.New("Unable to Marshal Json data")
}
_, err = this.send(url+strconv.FormatUint(uint64(id), 10)+"/properties", string(s))
if err != nil {
return err
}
errorList := map[int]error{
404: errors.New("Relationship not found."),
400: errors.New("Invalid data sent."),
}
return this.NewError(errorList)
}
/*
DelRelationship(relationship id uint) returns any errors raised as error
you can pass in more than 1 id
*/
func (this *Neo4j) DelRelationship(id ...uint64) error {
this.Method = "delete"
url := this.URL + "/relationship/"
for _, i := range id {
// delete each relationship for every id passed in
_, err := this.send(url+strconv.FormatUint(uint64(i), 10), "")
if err != nil {
return err
}
}
errorList := map[int]error{
404: errors.New("Relationship not found."),
}
return this.NewError(errorList)
}
/*
CreateRelationship(src node id uint, dst node id uint, data map[string]string, relationship type string) returns any errors raised as error
*/
func (this *Neo4j) CreateRelationship(src uint64, dst uint64, data map[string]string, rType string) error {
dstNode, err := this.GetNode(dst) // find properties for destination node so we can tie it into the relationship
if err != nil {
return err
}
srcNode, err := this.GetNode(src) // find properties for src node..
if err != nil {
return err
}
j := map[string]interface{}{} // empty map: keys are always strings in json, values vary
j["to"] = dstNode.Self
j["type"] = rType // type of relationship
j["data"] = map[string]string{} // empty array
j["data"] = data // add data to relationship
s, err := json.Marshal(j)
if err != nil {
return errors.New("Unable to Marshal Json data")
}
this.Method = "post"
_, err = this.send(srcNode.RelationshipsCreate, string(s)) // srcNode.RelationshipsCreate actually contains the full URL
if err != nil {
return err
}
errorList := map[int]error{
404: errors.New("Node or 'to' node not found."),
400: errors.New("Invalid data sent."),
}
return this.NewError(errorList)
}
/*
SearchIdx(key string, value string, query string, category string, index type string) returns array of NeoTemplate structs and any errors raised as error
Lucene query lang: http://lucene.apache.org/java/3_1_0/queryparsersyntax.html
example query: the_key:the_* AND the_other_key:[1 TO 100]
if you specifiy a query, it will not search by key/value and vice versa
*/
func (this *Neo4j) SearchIdx(key string, value string, query string, cat string, idxType string) (map[int]*NeoTemplate, error) {
url := this.URL + "/index/"
if strings.ToLower(idxType) == "relationship" {
url += "relationship"
} else {
url += "node"
}
url += "/" + cat
if len(query) > 0 { // query set, ignore key/value pair
url += "?query=" + this.EscapeString(query)
} else { // search key, val
url += "/" + strings.TrimSpace(key) + "/" + this.EscapeString(value)
}
this.Method = "get"
body, err := this.send(url, "")
if err != nil {
return nil, err
}
template, err := this.unmarshal(body)
if err != nil {
return nil, err
}
errorList := map[int]error{
400: errors.New("Invalid data sent."),
}
return template, this.NewError(errorList)
}
/*
CreateIdx(node id uint, key string, value string, category string, index type string) returns any errors raised as error
*/
func (this *Neo4j) CreateIdx(id uint64, key string, value string, cat string, idxType string) error {
template, err := this.GetNode(id)
if err != nil {
return err
}
if len(cat) < 1 {
idxType = "idx_nodes" // default, generic, index type
}
self := template.Self
url := this.URL + "/index/"
if strings.ToLower(idxType) == "relationship" {
url += "relationship"
} else {
url += "node"
}
url += "/" + cat + "/" + key + "/" + value + "/"
this.Method = "post"
_, err = this.send(url, strconv.Quote(self)) // add double quotes around the node url as neo4j expects
errorList := map[int]error{
400: errors.New("Invalid data sent."),
}
return this.NewError(errorList)
}
/*
Traverse(node id uint, return type string, order string, uniqueness string, relationships map[string]string, depth int, prune map[string]string, filter map[string]string) returns array of NeoTemplate structs and any errors raised as error
*/
func (this *Neo4j) Traverse(id uint64, returnType string, order string, uniqueness string, relationships map[string]string, depth int, prune map[string]string, filter map[string]string) (map[int]*NeoTemplate, error) {
node, err := this.GetNode(id) // find properties for destination node
if err != nil {
return nil, err
}
j := map[string]interface{}{} // empty map: keys are always strings in json, values vary
j["order"] = order
j["max depth"] = depth
j["uniqueness"] = uniqueness
if relationships != nil {
j["relationships"] = map[string]string{} // empty array
j["relationships"] = relationships // like: { "type": "KNOWS", "direction": "all" }
}
if prune != nil {
j["prune evaluator"] = map[string]string{} // empty array
j["prune evaluator"] = prune // like: { "language": "javascript", "body": "position.endNode().getProperty('date')>1234567;" }
}
if filter != nil {
j["return filter"] = map[string]string{} // empty array
j["return filter"] = filter // like: { "language": "builtin","name": "all" }
}
s, err := json.Marshal(j)
if err != nil {
return nil, errors.New("Unable to Marshal Json data")
}
this.Method = "post"
returnType = strings.ToLower(returnType)
switch returnType { // really just a list of allowed values and anything else is replaced with "node"
case "relationship":
case "path":
case "fullpath":
case "node":
default:
returnType = "node"
}
url := strings.Replace(node.Traverse, "{returnType}", returnType, 1) // neo4j returns the traverse URL with the literal "{returnType}" at the end
body, err := this.send(url, string(s))
if err != nil {
return nil, err
}
template, err := this.unmarshal(body)
if err != nil {
return nil, err
}
errorList := map[int]error{
404: errors.New("Node not found."),
}
return template, this.NewError(errorList)
}
/*
TraversePath(src node id uint, dst node id uint, relationships map[string]string, depth uint, algorithm string, paths bool) returns array of NeoTemplate structs and any errors raised as error
*/
func (this *Neo4j) TraversePath(src uint64, dst uint64, relationships map[string]string, depth uint, algo string, paths bool) (map[int]*NeoTemplate, error) {
dstNode, err := this.GetNode(dst) // find properties for destination node
if err != nil {
return nil, err
}
srcNode, err := this.GetNode(src) // find properties for src node..
if err != nil {
return nil, err
}
j := map[string]interface{}{} // empty map: keys are always strings in json, values vary
j["to"] = dstNode.Self
j["max depth"] = depth
j["algorithm"] = algo
j["relationships"] = map[string]string{} // empty array
j["relationships"] = relationships // specify relationships like type: "KNOWS" direction: "all"
s, err := json.Marshal(j)
if err != nil {
return nil, errors.New("Unable to Marshal Json data")
}
this.Method = "post"
url := srcNode.Self
if paths {
url += "/paths"
} else {
url += "/path"
}
body, err := this.send(url, string(s))
if err != nil {
return nil, err
}
template, err := this.unmarshal(body)
if err != nil {
return nil, err
}
errorList := map[int]error{
404: errors.New("No path found using current algorithm and parameters"),
}
return template, this.NewError(errorList)
}
/* shamelessly taken from golang html pkg */
func (this *Neo4j) EscapeString(s string) string {
if strings.IndexAny(s, escapedChars) == -1 {
return s
}
buf := bytes.NewBuffer(nil)
this.escape(buf, s)
return buf.String()
}
/* shamelessly taken from golang html pkg with a few minor updates */
func (this *Neo4j) escape(buf *bytes.Buffer, s string) {
i := strings.IndexAny(s, escapedChars)
for i != -1 {
buf.WriteString(s[0:i])
var esc string
switch s[i] {
case '%':
esc = "%25"
case '&':
esc = "&"
case '\'':
esc = "'"
case '<':
esc = "<"
case '>':
esc = ">"
case '"':
esc = """
case ' ':
esc = "%20"
case '*':
esc = "%2A"
case ':':
esc = "%3A"
case '[':
esc = "%5B"
case ']':
esc = "%5D"
default:
panic("unrecognized escape character")
}
s = s[i+1:]
buf.WriteString(esc)
i = strings.IndexAny(s, escapedChars)
}
buf.WriteString(s)
}
// packs string literal into json object structure around variable "varName"
// data string should already be in json format
func (this *Neo4j) pack(name string, data string) ([]byte, error) {
buf := new(bytes.Buffer)
err := json.Compact(buf, []byte("{ \""+name+"\": "+data+" } ")) // pkg data into new json string then compact() it onto our empty buffer
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (this *Neo4j) send(url string, data string) (string, error) {
var (
resp *http.Response // http response
buf bytes.Buffer // contains http response body
err error
)
if len(url) < 1 {
url = this.URL + "node" // default path
}
client := new(http.Client)
switch strings.ToLower(this.Method) { // which http method
case "delete":
req, e := http.NewRequest("DELETE", url, nil)
if e != nil {
err = e
break
}
this.setAuth(*req)
resp, err = client.Do(req)
case "post":
body := strings.NewReader(data)
req, e := http.NewRequest("POST", url, body)
if e != nil {
err = e
break
}
req.Header.Set("Content-Type", "application/json")
this.setAuth(*req)
resp, err = client.Do(req)
case "put":
body := strings.NewReader(data)
req, e := http.NewRequest("PUT", url, body)
if e != nil {
err = e
break
}
req.Header.Set("Content-Type", "application/json")
this.setAuth(*req)
resp, err = client.Do(req)
case "get":
fallthrough
default:
req, e := http.NewRequest("GET", url, nil)
if e != nil {
err = e
break
}
this.setAuth(*req)
resp, err = client.Do(req)
}
if err != nil {
return "", err
}
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return "", err
}
this.StatusCode = resp.StatusCode // the calling method should do more inspection with chkStatusCode() method and determine if the operation was successful or not.
return buf.String(), nil
}
// sets Basic HTTP Auth
func (this *Neo4j) setAuth(req http.Request) {
if len(this.Username) > 0 || len(this.Password) > 0 {
req.SetBasicAuth(this.Username, this.Password)
}
}
// this function unmarshals the individual node of data(or relationship etc).
// called internally to build the dataset of records returned from neo4j
func (this *Neo4j) unmarshalNode(template map[string]interface{}) (*NeoTemplate, error) {
var (
data interface{} // stores data from type assertion
assert bool // did the type assertion raise an err?
)
node := new(NeoTemplate)
for k, v := range template { // loop result data
data, assert = v.(map[string]interface{}) // type assertion
if assert {
switch vv := data.(type) { // switch on variable type so data/extensions are extracted properly
case map[string]interface{}:
switch k {
case "data":
node.Data = vv
case "extensions":
node.Extensions = vv
}
default:
log.Printf("*Notice: Unknown type in JSON stream: %T from key: %v\n", vv, k)
}
} else { // to my knowledge neo4j is only going to pass strings and arrays so if map assertion failed above try an array instead
data, assert = v.([]interface{}) // normal array?
if assert {
switch vv := data.(type) {
case []interface{}:
switch k {
case "nodes":
node.Nodes = vv
case "relationships":
node.TRelationships = vv
}
}
} else { // if nothing else, it must be a string
data, assert = v.(string)
if assert {
// copy string vars into node structure switch on key name
switch k {
case "self":
node.Self, _ = data.(string) // cast it to a string with type assertion
// "self" provides easy access to the ID property of the node(relationship, index,etc), we'll take advantage and axe it off right now
selfSlice := strings.Split(string(node.Self), "/") // slice string "Self" on each '/' char, -1 gets all instances
id, atouiErr := strconv.ParseUint(selfSlice[len(selfSlice)-1], 10, 0) // and pull off the last part which is the ID then string -> uint
if atouiErr != nil {
return nil, atouiErr
}
node.ID = id
case "traverse":
node.Traverse, _ = data.(string)
case "property":
node.Property, _ = data.(string)
case "properties":
node.Properties, _ = data.(string)
case "outgoing_relationships":
node.RelationshipsOut, _ = data.(string)
case "incoming_relationships":
node.RelationshipsIn, _ = data.(string)
case "all_relationships":
node.RelationshipsAll, _ = data.(string)
case "create_relationship":
node.RelationshipsCreate, _ = data.(string)
case "start": // relationships use this
node.Start, _ = data.(string)
case "end": // relationships use this
node.End, _ = data.(string)
case "type": // relationships use this
node.Type, _ = data.(string)
case "length":
node.Length, _ = data.(string)
case "indexed": // indices use this
node.Indexed, _ = data.(string)
}
}
}
}
}
return node, nil
}
/*
json.Unmarshal wrapper
extracts json data into new interface and returns populated array of interfaces and any errors raised
*/
func (this *Neo4j) unmarshal(s string) (dataSet map[int]*NeoTemplate, err error) {
var (
templateNode map[string]interface{} // blank interface for json.Unmarshal; used for node lvl data
templateSet []map[string]interface{} // array of blank interfaces for json.Unmarshal
)
dataSet = make(map[int]*NeoTemplate) // make it ready for elements
err = json.Unmarshal([]byte(s), &templateNode) // unmarshal json data into blank interface. the json pkg will populate with the proper data types
if err != nil { // fails on multiple results
err = json.Unmarshal([]byte(s), &templateSet) // if unable to unmarshal into single template, try an array of templates instead. If that fails, raise an error
if err != nil {
return nil, err
}
for _, v := range templateSet {
data, err := this.unmarshalNode(v) // append NeoTemplate into the data set
if err != nil {
return nil, err
}
dataSet[len(dataSet)] = data // new array element containing data
}
} else {
template, err := this.unmarshalNode(templateNode)
if err != nil {
return nil, err
}
dataSet[0] = template // just a single result
}
return
}
func (this *Neo4j) NewError(errorList map[int]error) error {
if errorList != nil {
errorList[500] = errors.New("Fatal Error 500.") // everything can return a 500 error
}
err := &Error{errorList, this.StatusCode}
return err.check()
}
// checks the status code of the http response and returns an appropriate error(or not).
func (this *Error) check() error {
if this.List != nil {
if this.List[this.Code] != nil {
return this.List[this.Code]
}
}
return nil // if error exists it was not defined in Error.List
}