Skip to content

Commit cccb371

Browse files
authored
Merge pull request #48 from thedevsaddam/fix/attributes
Fix select attributes in First/Last/Nth methods
2 parents 7acca89 + 769e0c9 commit cccb371

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

jsonq.go

+6-18
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ func (j *JSONQ) prepare() *JSONQ {
350350
if len(j.queries) > 0 {
351351
j.processQuery()
352352
}
353+
if j.distinctProperty != "" {
354+
j.distinct()
355+
}
356+
if len(j.attributes) > 0 {
357+
j.jsonContent = j.only(j.attributes...)
358+
}
353359
j.queryIndex = 0
354360
return j
355361
}
@@ -545,27 +551,18 @@ func (j *JSONQ) Reset() *JSONQ {
545551
// Get return the result
546552
func (j *JSONQ) Get() interface{} {
547553
j.prepare()
548-
if j.distinctProperty != "" {
549-
j.distinct()
550-
}
551554
if j.offsetRecords != 0 {
552555
j.offset()
553556
}
554557
if j.limitRecords != 0 {
555558
j.limit()
556559
}
557-
if len(j.attributes) > 0 {
558-
return j.only(j.attributes...)
559-
}
560560
return j.jsonContent
561561
}
562562

563563
// First returns the first element of a list
564564
func (j *JSONQ) First() interface{} {
565565
j.prepare()
566-
if j.distinctProperty != "" {
567-
j.distinct()
568-
}
569566
if arr, ok := j.jsonContent.([]interface{}); ok {
570567
if len(arr) > 0 {
571568
return arr[0]
@@ -577,9 +574,6 @@ func (j *JSONQ) First() interface{} {
577574
// Last returns the last element of a list
578575
func (j *JSONQ) Last() interface{} {
579576
j.prepare()
580-
if j.distinctProperty != "" {
581-
j.distinct()
582-
}
583577
if arr, ok := j.jsonContent.([]interface{}); ok {
584578
if l := len(arr); l > 0 {
585579
return arr[l-1]
@@ -596,9 +590,6 @@ func (j *JSONQ) Nth(index int) interface{} {
596590
}
597591

598592
j.prepare()
599-
if j.distinctProperty != "" {
600-
j.distinct()
601-
}
602593
if arr, ok := j.jsonContent.([]interface{}); ok {
603594
alen := len(arr)
604595
if alen == 0 {
@@ -626,9 +617,6 @@ func (j *JSONQ) Find(path string) interface{} {
626617
// This could be a length of list/array/map
627618
func (j *JSONQ) Count() int {
628619
j.prepare()
629-
if j.distinctProperty != "" {
630-
j.distinct()
631-
}
632620
lnth := 0
633621
// list of items
634622
if list, ok := j.jsonContent.([]interface{}); ok {

0 commit comments

Comments
 (0)