-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathoutputnode.go
846 lines (753 loc) · 19.9 KB
/
outputnode.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package query
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/task"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/types/facets"
"github.com/dgraph-io/dgraph/x"
)
// ToJson converts the list of subgraph into a JSON response by calling toFastJSON.
func ToJson(l *Latency, sgl []*SubGraph) ([]byte, error) {
sgr := &SubGraph{}
for _, sg := range sgl {
if sg.Params.Alias == "var" || sg.Params.Alias == "shortest" {
continue
}
if sg.Params.GetUid {
sgr.Params.GetUid = true
}
sgr.Children = append(sgr.Children, sg)
}
return sgr.toFastJSON(l)
}
// outputNode is the generic output / writer for preTraverse.
type outputNode interface {
AddValue(attr string, v types.Val)
AddListValue(attr string, v types.Val, list bool)
AddMapChild(attr string, node outputNode, isRoot bool)
AddListChild(attr string, child outputNode)
New(attr string) outputNode
SetUID(uid uint64, attr string)
SetFlatten(flatten bool)
IsEmpty() bool
addCountAtRoot(*SubGraph)
addGroupby(*SubGraph, *groupResults, string)
addAggregations(*SubGraph) error
}
func makeScalarNode(attr string, isChild bool, val []byte, list bool) *fastJsonNode {
return &fastJsonNode{
attr: attr,
isChild: isChild,
scalarVal: val,
list: list,
}
}
type fastJsonNode struct {
attr string
order int // relative ordering (for sorted results)
isChild bool
scalarVal []byte
attrs []*fastJsonNode
list bool
flatten bool
}
func (fj *fastJsonNode) AddValue(attr string, v types.Val) {
fj.AddListValue(attr, v, false)
}
func (fj *fastJsonNode) AddListValue(attr string, v types.Val, list bool) {
if bs, err := valToBytes(v); err == nil {
fj.attrs = append(fj.attrs, makeScalarNode(attr, false, bs, list))
}
}
func (fj *fastJsonNode) AddMapChild(attr string, val outputNode, isRoot bool) {
var childNode *fastJsonNode
for _, c := range fj.attrs {
if c.attr == attr {
childNode = c
break
}
}
if childNode != nil {
val.(*fastJsonNode).isChild = true
val.(*fastJsonNode).attr = attr
childNode.attrs = append(childNode.attrs, val.(*fastJsonNode).attrs...)
} else {
val.(*fastJsonNode).isChild = false
val.(*fastJsonNode).attr = attr
fj.attrs = append(fj.attrs, val.(*fastJsonNode))
}
}
func (fj *fastJsonNode) AddListChild(attr string, child outputNode) {
child.(*fastJsonNode).attr = attr
child.(*fastJsonNode).isChild = true
fj.attrs = append(fj.attrs, child.(*fastJsonNode))
}
func (fj *fastJsonNode) New(attr string) outputNode {
return &fastJsonNode{attr: attr, isChild: false}
}
func (fj *fastJsonNode) SetUID(uid uint64, attr string) {
// if we're in debug mode, uid may be added second time, skip this
if attr == "uid" {
for _, a := range fj.attrs {
if a.attr == attr {
return
}
}
}
fj.attrs = append(fj.attrs, makeScalarNode(attr, false, []byte(fmt.Sprintf("\"%#x\"", uid)),
false))
}
func (fj *fastJsonNode) IsEmpty() bool {
return len(fj.attrs) == 0
}
func (fj *fastJsonNode) SetFlatten(flatten bool) {
fj.flatten = flatten
}
func valToBytes(v types.Val) ([]byte, error) {
switch v.Tid {
case types.StringID, types.DefaultID:
return json.Marshal(v.Value)
case types.BinaryID:
return []byte(fmt.Sprintf("%q", v.Value)), nil
case types.IntID:
return []byte(fmt.Sprintf("%d", v.Value)), nil
case types.FloatID:
return []byte(fmt.Sprintf("%f", v.Value)), nil
case types.BoolID:
if v.Value.(bool) {
return []byte("true"), nil
}
return []byte("false"), nil
case types.DateTimeID:
// Return empty string instead of zero-time value string - issue#3166
t := v.Value.(time.Time)
if t.IsZero() {
return []byte(`""`), nil
}
return t.MarshalJSON()
case types.GeoID:
return geojson.Marshal(v.Value.(geom.T))
case types.UidID:
return []byte(fmt.Sprintf("\"%#x\"", v.Value)), nil
case types.PasswordID:
return []byte(fmt.Sprintf("%q", v.Value.(string))), nil
default:
return nil, errors.New("Unsupported types.Val.Tid")
}
}
type nodeSlice []*fastJsonNode
func (n nodeSlice) Len() int {
return len(n)
}
func (n nodeSlice) Less(i, j int) bool {
cmp := strings.Compare(n[i].attr, n[j].attr)
if cmp == 0 {
return n[i].order < n[j].order
}
return cmp < 0
}
func (n nodeSlice) Swap(i, j int) {
n[i], n[j] = n[j], n[i]
}
func (fj *fastJsonNode) writeKey(out *bytes.Buffer) {
out.WriteRune('"')
out.WriteString(fj.attr)
out.WriteRune('"')
out.WriteRune(':')
}
func (fj *fastJsonNode) encode(out *bytes.Buffer) {
// set relative ordering
for i, a := range fj.attrs {
a.order = i
}
i := 0
if i < len(fj.attrs) {
out.WriteRune('{')
cur := fj.attrs[i]
i++
cnt := 1
last := false
inArray := false
for {
var next *fastJsonNode
if i < len(fj.attrs) {
next = fj.attrs[i]
i++
} else {
last = true
}
if !last {
if cur.attr == next.attr {
if cnt == 1 {
cur.writeKey(out)
out.WriteRune('[')
inArray = true
}
cur.encode(out)
cnt++
} else {
if cnt == 1 {
cur.writeKey(out)
if cur.isChild || cur.list {
out.WriteRune('[')
inArray = true
}
}
cur.encode(out)
if cnt != 1 || (cur.isChild || cur.list) {
out.WriteRune(']')
inArray = false
}
cnt = 1
}
out.WriteRune(',')
cur = next
} else {
if cnt == 1 {
cur.writeKey(out)
}
if (cur.isChild || cur.list) && !inArray {
out.WriteRune('[')
}
cur.encode(out)
if cnt != 1 || (cur.isChild || cur.list) {
out.WriteRune(']')
}
break
}
}
out.WriteRune('}')
} else {
out.Write(fj.scalarVal)
}
}
func merge(parent [][]*fastJsonNode, child [][]*fastJsonNode) ([][]*fastJsonNode, error) {
if len(parent) == 0 {
return child, nil
}
// Here we merge two slices of maps.
mergedList := make([][]*fastJsonNode, 0, len(parent)*len(child))
cnt := 0
for _, pa := range parent {
for _, ca := range child {
cnt += len(pa) + len(ca)
if cnt > x.Config.NormalizeNodeLimit {
return nil, errors.Errorf(
"Couldn't evaluate @normalize directive - too many results")
}
list := make([]*fastJsonNode, 0, len(pa)+len(ca))
list = append(list, pa...)
list = append(list, ca...)
mergedList = append(mergedList, list)
}
}
return mergedList, nil
}
func (fj *fastJsonNode) normalize() ([][]*fastJsonNode, error) {
cnt := 0
for _, a := range fj.attrs {
if a.isChild {
cnt++
}
}
if cnt == 0 {
// Recursion base case
// There are no children, we can just return slice with fj.attrs map.
return [][]*fastJsonNode{fj.attrs}, nil
}
parentSlice := make([][]*fastJsonNode, 0, 5)
// If the parents has attrs, lets add them to the slice so that it can be
// merged with children later.
attrs := make([]*fastJsonNode, 0, len(fj.attrs)-cnt)
for _, a := range fj.attrs {
if !a.isChild {
attrs = append(attrs, a)
}
}
parentSlice = append(parentSlice, attrs)
for ci := 0; ci < len(fj.attrs); {
childNode := fj.attrs[ci]
if !childNode.isChild {
ci++
continue
}
childSlice := make([][]*fastJsonNode, 0, 5)
for ci < len(fj.attrs) && childNode.attr == fj.attrs[ci].attr {
normalized, err := fj.attrs[ci].normalize()
if err != nil {
return nil, err
}
childSlice = append(childSlice, normalized...)
ci++
}
// Merging with parent.
var err error
parentSlice, err = merge(parentSlice, childSlice)
if err != nil {
return nil, err
}
}
for i, slice := range parentSlice {
sort.Sort(nodeSlice(slice))
first := -1
last := 0
for i := range slice {
if slice[i].attr == "uid" {
if first == -1 {
first = i
}
last = i
}
}
if first != -1 && first != last {
if first == 0 {
parentSlice[i] = slice[last:]
} else {
parentSlice[i] = append(slice[:first], slice[last:]...)
}
}
}
return parentSlice, nil
}
func (fj *fastJsonNode) addGroupby(sg *SubGraph, res *groupResults, fname string) {
// Don't add empty groupby
if len(res.group) == 0 {
return
}
g := fj.New(fname)
for _, grp := range res.group {
uc := g.New("@groupby")
for _, it := range grp.keys {
uc.AddValue(it.attr, it.key)
}
for _, it := range grp.aggregates {
uc.AddValue(it.attr, it.key)
}
g.AddListChild("@groupby", uc)
}
fj.AddListChild(fname, g)
}
func (fj *fastJsonNode) addCountAtRoot(sg *SubGraph) {
c := types.ValueForType(types.IntID)
c.Value = int64(len(sg.DestUIDs.Uids))
n1 := fj.New(sg.Params.Alias)
field := sg.Params.UidCountAlias
if field == "" {
field = "count"
}
n1.AddValue(field, c)
fj.AddListChild(sg.Params.Alias, n1)
}
func (fj *fastJsonNode) addAggregations(sg *SubGraph) error {
for _, child := range sg.Children {
aggVal, ok := child.Params.UidToVal[0]
if !ok {
if len(child.Params.NeedsVar) == 0 {
return errors.Errorf("Only aggregated variables allowed within empty block.")
}
// the aggregation didn't happen, most likely was called with unset vars.
// See: query.go:fillVars
aggVal = types.Val{Tid: types.FloatID, Value: float64(0)}
}
if child.Params.Normalize && child.Params.Alias == "" {
continue
}
fieldName := aggWithVarFieldName(child)
n1 := fj.New(fieldName)
n1.AddValue(fieldName, aggVal)
fj.AddListChild(sg.Params.Alias, n1)
}
if fj.IsEmpty() {
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
}
return nil
}
func processNodeUids(fj *fastJsonNode, sg *SubGraph) error {
var seedNode *fastJsonNode
if sg.Params.IsEmpty {
return fj.addAggregations(sg)
}
if sg.uidMatrix == nil {
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
return nil
}
hasChild := false
if sg.Params.UidCount && !(sg.Params.UidCountAlias == "" && sg.Params.Normalize) {
hasChild = true
fj.addCountAtRoot(sg)
}
if sg.Params.IsGroupBy {
if len(sg.GroupbyRes) == 0 {
return errors.Errorf("Expected GroupbyRes to have length > 0.")
}
fj.addGroupby(sg, sg.GroupbyRes[0], sg.Params.Alias)
return nil
}
lenList := len(sg.uidMatrix[0].Uids)
for i := 0; i < lenList; i++ {
uid := sg.uidMatrix[0].Uids[i]
if algo.IndexOf(sg.DestUIDs, uid) < 0 {
// This UID was filtered. So Ignore it.
continue
}
n1 := seedNode.New(sg.Params.Alias)
if err := sg.preTraverse(uid, n1); err != nil {
if err.Error() == "_INV_" {
continue
}
return err
}
if n1.IsEmpty() {
continue
}
hasChild = true
err := createAttrs(n1)
if err != nil {
return err
}
fj.AddListChild(sg.Params.Alias, n1)
}
if !hasChild {
// So that we return an empty key if the root didn't have any children.
fj.AddListChild(sg.Params.Alias, &fastJsonNode{})
}
return nil
}
// Extensions represents the extra information appended to query results.
type Extensions struct {
Latency *api.Latency `json:"server_latency,omitempty"`
Txn *api.TxnContext `json:"txn,omitempty"`
}
func (sg *SubGraph) toFastJSON(l *Latency) ([]byte, error) {
encodingStart := time.Now()
defer func() {
l.Json = time.Since(encodingStart)
}()
var seedNode *fastJsonNode
var err error
n := seedNode.New("_root_")
for _, sg := range sg.Children {
err = processNodeUids(n.(*fastJsonNode), sg)
if err != nil {
return nil, err
}
}
// According to GraphQL spec response should only contain data, errors and extensions as top
// level keys. Hence we send server_latency under extensions key.
// https://facebook.github.io/graphql/#sec-Response-Format
var bufw bytes.Buffer
if len(n.(*fastJsonNode).attrs) == 0 {
bufw.WriteString(`{}`)
} else {
n.(*fastJsonNode).encode(&bufw)
}
return bufw.Bytes(), nil
}
func (sg *SubGraph) fieldName() string {
fieldName := sg.Attr
if sg.Params.Alias != "" {
fieldName = sg.Params.Alias
}
return fieldName
}
func addCount(pc *SubGraph, count uint64, dst outputNode) {
if pc.Params.Normalize && pc.Params.Alias == "" {
return
}
c := types.ValueForType(types.IntID)
c.Value = int64(count)
fieldName := pc.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("count(%s)", pc.Attr)
}
dst.AddValue(fieldName, c)
}
func aggWithVarFieldName(pc *SubGraph) string {
if pc.Params.Alias != "" {
return pc.Params.Alias
}
fieldName := fmt.Sprintf("val(%v)", pc.Params.Var)
if len(pc.Params.NeedsVar) > 0 {
fieldName = fmt.Sprintf("val(%v)", pc.Params.NeedsVar[0].Name)
if pc.SrcFunc != nil {
fieldName = fmt.Sprintf("%s(%v)", pc.SrcFunc.Name, fieldName)
}
}
return fieldName
}
func addInternalNode(pc *SubGraph, uid uint64, dst outputNode) error {
sv, ok := pc.Params.UidToVal[uid]
if !ok || sv.Value == nil {
return nil
}
fieldName := aggWithVarFieldName(pc)
dst.AddValue(fieldName, sv)
return nil
}
func addCheckPwd(pc *SubGraph, vals []*pb.TaskValue, dst outputNode) {
c := types.ValueForType(types.BoolID)
if len(vals) == 0 {
c.Value = false
} else {
c.Value = task.ToBool(vals[0])
}
fieldName := pc.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("checkpwd(%s)", pc.Attr)
}
dst.AddValue(fieldName, c)
}
func alreadySeen(parentIds []uint64, uid uint64) bool {
for _, id := range parentIds {
if id == uid {
return true
}
}
return false
}
func facetName(fieldName string, f *api.Facet) string {
if f.Alias != "" {
return f.Alias
}
return fieldName + x.FacetDelimeter + f.Key
}
// This method gets the values and children for a subprotos.
func (sg *SubGraph) preTraverse(uid uint64, dst outputNode) error {
if sg.Params.IgnoreReflex {
if alreadySeen(sg.Params.ParentIds, uid) {
// A node can't have itself as the child at any level.
return nil
}
// Push myself to stack before sending this to children.
sg.Params.ParentIds = append(sg.Params.ParentIds, uid)
}
dst.SetFlatten(sg.Params.Normalize)
var invalidUids map[uint64]bool
// We go through all predicate children of the subprotos.
for _, pc := range sg.Children {
if pc.Params.IgnoreResult {
continue
}
if pc.IsInternal() {
if pc.Params.Expand != "" {
continue
}
if pc.Params.Normalize && pc.Params.Alias == "" {
continue
}
if err := addInternalNode(pc, uid, dst); err != nil {
return err
}
continue
}
if len(pc.uidMatrix) == 0 {
// Can happen in recurse query.
continue
}
if len(pc.facetsMatrix) > 0 && len(pc.facetsMatrix) != len(pc.uidMatrix) {
return errors.Errorf("Length of facetsMatrix and uidMatrix mismatch: %d vs %d",
len(pc.facetsMatrix), len(pc.uidMatrix))
}
idx := algo.IndexOf(pc.SrcUIDs, uid)
if idx < 0 {
continue
}
if pc.Params.IsGroupBy {
if len(pc.GroupbyRes) <= idx {
return errors.Errorf("Unexpected length while adding Groupby. Idx: [%v], len: [%v]",
idx, len(pc.GroupbyRes))
}
dst.addGroupby(pc, pc.GroupbyRes[idx], pc.fieldName())
continue
}
fieldName := pc.fieldName()
if len(pc.counts) > 0 {
addCount(pc, uint64(pc.counts[idx]), dst)
} else if pc.SrcFunc != nil && pc.SrcFunc.Name == "checkpwd" {
addCheckPwd(pc, pc.valueMatrix[idx].Values, dst)
} else if idx < len(pc.uidMatrix) && len(pc.uidMatrix[idx].Uids) > 0 {
var fcsList []*pb.Facets
if pc.Params.Facet != nil {
fcsList = pc.facetsMatrix[idx].FacetsList
}
if sg.Params.IgnoreReflex {
pc.Params.ParentIds = sg.Params.ParentIds
}
// We create as many predicate entity children as the length of uids for
// this predicate.
ul := pc.uidMatrix[idx]
for childIdx, childUID := range ul.Uids {
if fieldName == "" || (invalidUids != nil && invalidUids[childUID]) {
continue
}
uc := dst.New(fieldName)
if rerr := pc.preTraverse(childUID, uc); rerr != nil {
if rerr.Error() == "_INV_" {
if invalidUids == nil {
invalidUids = make(map[uint64]bool)
}
invalidUids[childUID] = true
continue // next UID.
}
// Some other error.
glog.Errorf("Error while traversal: %v", rerr)
return rerr
}
if pc.Params.Facet != nil && len(fcsList) > childIdx {
fs := fcsList[childIdx]
for _, f := range fs.Facets {
fVal, err := facets.ValFor(f)
if err != nil {
return err
}
uc.AddValue(facetName(fieldName, f), fVal)
}
}
if !uc.IsEmpty() {
if sg.Params.GetUid {
uc.SetUID(childUID, "uid")
}
if pc.List {
dst.AddListChild(fieldName, uc)
} else {
dst.AddMapChild(fieldName, uc, false)
}
}
}
if pc.Params.UidCount && !(pc.Params.UidCountAlias == "" && pc.Params.Normalize) {
uc := dst.New(fieldName)
c := types.ValueForType(types.IntID)
c.Value = int64(len(ul.Uids))
alias := pc.Params.UidCountAlias
if alias == "" {
alias = "count"
}
uc.AddValue(alias, c)
dst.AddListChild(fieldName, uc)
}
} else {
if pc.Params.Alias == "" && len(pc.Params.Langs) > 0 {
fieldName += "@"
fieldName += strings.Join(pc.Params.Langs, ":")
}
if pc.Attr == "uid" {
dst.SetUID(uid, pc.fieldName())
continue
}
if len(pc.facetsMatrix) > idx && len(pc.facetsMatrix[idx].FacetsList) > 0 {
// in case of Value we have only one Facets
for _, f := range pc.facetsMatrix[idx].FacetsList[0].Facets {
fVal, err := facets.ValFor(f)
if err != nil {
return err
}
dst.AddValue(facetName(fieldName, f), fVal)
}
}
if len(pc.valueMatrix) <= idx {
continue
}
for i, tv := range pc.valueMatrix[idx].Values {
// if conversion not possible, we ignore it in the result.
sv, convErr := convertWithBestEffort(tv, pc.Attr)
if convErr != nil {
return convErr
}
if pc.Params.ExpandAll && len(pc.LangTags[idx].Lang) != 0 {
if i >= len(pc.LangTags[idx].Lang) {
return errors.Errorf(
"pb.error: all lang tags should be either present or absent")
}
fieldNameWithTag := fieldName
lang := pc.LangTags[idx].Lang[i]
if lang != "" {
fieldNameWithTag += "@" + lang
}
encodeAsList := pc.List && len(lang) == 0
dst.AddListValue(fieldNameWithTag, sv, encodeAsList)
continue
}
encodeAsList := pc.List && len(pc.Params.Langs) == 0
if !pc.Params.Normalize {
dst.AddListValue(fieldName, sv, encodeAsList)
continue
}
// If the query had the normalize directive, then we only add nodes
// with an Alias.
if pc.Params.Alias != "" {
dst.AddListValue(fieldName, sv, encodeAsList)
}
}
}
}
if sg.Params.IgnoreReflex && len(sg.Params.ParentIds) > 0 {
// Lets pop the stack.
sg.Params.ParentIds = (sg.Params.ParentIds)[:len(sg.Params.ParentIds)-1]
}
// Only for shortest path query we wan't to return uid always if there is
// nothing else at that level.
if (sg.Params.GetUid && !dst.IsEmpty()) || sg.Params.Shortest {
dst.SetUID(uid, "uid")
}
if sg.pathMeta != nil {
totalWeight := types.Val{
Tid: types.FloatID,
Value: sg.pathMeta.weight,
}
dst.AddValue("_weight_", totalWeight)
}
return nil
}
func createAttrs(oNode outputNode) error {
node := oNode.(*fastJsonNode)
err := flattenResult(node)
if err != nil {
return err
}
return nil
}
func flattenResult(node *fastJsonNode) error {
if node.flatten {
attrList, err := node.normalize()
if err != nil {
return err
}
var allAttrs []*fastJsonNode
for _, attrs := range attrList {
allAttrs = append(allAttrs, attrs...)
}
node.attrs = allAttrs
} else {
for _, child := range node.attrs {
flattenResult(child)
}
}
return nil
}